Commit 44c4ca24 authored by Weilun Shi's avatar Weilun Shi Committed by Commit Bot

[NTP] Automated test for custom background attribution

Adding automated test for set and reset custom background attribution.
The attribution should be removed when we cleared the custom background
and when a theme was applied.

Bug: 857256
Change-Id: Iccabe90925bc7e7d3da8ca0fa03705264493cf7b
Reviewed-on: https://chromium-review.googlesource.com/1208469
Commit-Queue: Weilun Shi <sweilun@chromium.org>
Reviewed-by: default avatarKristi Park <kristipark@chromium.org>
Cr-Commit-Position: refs/heads/master@{#589324}
parent b39baca0
......@@ -5,11 +5,15 @@
#include <string>
#include "base/test/scoped_feature_list.h"
#include "chrome/browser/chrome_notification_types.h"
#include "chrome/browser/extensions/extension_browsertest.h"
#include "chrome/browser/search/instant_service.h"
#include "chrome/browser/search/instant_service_factory.h"
#include "chrome/browser/search/instant_service_observer.h"
#include "chrome/browser/search/ntp_features.h"
#include "chrome/browser/search/search.h"
#include "chrome/browser/themes/theme_service.h"
#include "chrome/browser/themes/theme_service_factory.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_commands.h"
#include "chrome/browser/ui/search/instant_test_utils.h"
......@@ -19,6 +23,8 @@
#include "chrome/common/url_constants.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "content/public/browser/web_contents.h"
#include "content/public/test/test_utils.h"
#include "extensions/browser/extension_registry.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace {
......@@ -55,11 +61,34 @@ class TestThemeInfoObserver : public InstantServiceObserver {
run_loop.Run();
}
void WaitForThemeApplied(bool theme_installed) {
DCHECK(!quit_closure_);
theme_installed_ = theme_installed;
if (!theme_info_.using_default_theme == theme_installed) {
return;
}
base::RunLoop run_loop;
quit_closure_ = run_loop.QuitClosure();
run_loop.Run();
}
// Switch the exit condition for ThemeInfoChanged.
void SwitchCheck() {
wait_for_custom_background_or_theme_ =
!wait_for_custom_background_or_theme_;
}
bool IsUsingDefaultTheme() { return theme_info_.using_default_theme; }
private:
void ThemeInfoChanged(const ThemeBackgroundInfo& theme_info) override {
theme_info_ = theme_info;
if (quit_closure_ &&
if (quit_closure_) {
// Exit when the custom background was applied successfully.
if (wait_for_custom_background_or_theme_ &&
theme_info_.custom_background_url == expected_background_url_ &&
theme_info_.custom_background_attribution_line_1 ==
expected_attribution_1_ &&
......@@ -70,6 +99,13 @@ class TestThemeInfoObserver : public InstantServiceObserver {
std::move(quit_closure_).Run();
quit_closure_.Reset();
}
// Exit when the theme was applied successfully.
else if (!wait_for_custom_background_or_theme_ &&
!theme_info_.using_default_theme == theme_installed_) {
std::move(quit_closure_).Run();
quit_closure_.Reset();
}
}
}
void MostVisitedItemsChanged(const std::vector<InstantMostVisitedItem>&,
......@@ -79,6 +115,11 @@ class TestThemeInfoObserver : public InstantServiceObserver {
ThemeBackgroundInfo theme_info_;
bool theme_installed_;
// When wait_for_custom_background_or_theme_ is true, we wait for the custom
// background to get applied. When wait_for_custom_background_or_theme_ is
// false, we wait for a theme gets applied
bool wait_for_custom_background_or_theme_ = true;
std::string expected_background_url_;
std::string expected_attribution_1_;
std::string expected_attribution_2_;
......@@ -130,7 +171,48 @@ IN_PROC_BROWSER_TEST_F(LocalNTPCustomBackgroundsTest,
observer.WaitForThemeInfoUpdated("", "", "", "");
}
IN_PROC_BROWSER_TEST_F(LocalNTPCustomBackgroundsTest, SetandReset) {
IN_PROC_BROWSER_TEST_F(LocalNTPCustomBackgroundsTest, AttributionSetAndReset) {
content::WebContents* active_tab =
local_ntp_test_utils::OpenNewTab(browser(), GURL("about:blank"));
TestThemeInfoObserver observer(
InstantServiceFactory::GetForProfile(browser()->profile()));
local_ntp_test_utils::NavigateToNTPAndWaitUntilLoaded(browser());
// Set a custom background attribution via the EmbeddedSearch API.
EXPECT_TRUE(content::ExecuteScript(active_tab,
"window.chrome.embeddedSearch.newTabPage."
"setBackgroundURLWithAttributions('https:/"
"/www.test.com/', 'attr1', 'attr2', "
"'https://www.attribution.com/')"));
observer.WaitForThemeInfoUpdated("https://www.test.com/", "attr1", "attr2",
"https://www.attribution.com/");
// Check that the custom background element has the correct attribution
// applied.
bool result = false;
EXPECT_TRUE(instant_test_utils::GetBoolFromJS(
active_tab,
"document.querySelector('.attr1').innerText === 'attr1' && "
"document.querySelector('.attr2').innerText === 'attr2'",
&result));
EXPECT_TRUE(result);
// Reset custom background via the EmbeddedSearch API.
EXPECT_TRUE(content::ExecuteScript(active_tab,
"window.chrome.embeddedSearch.newTabPage."
"setBackgroundURL('')"));
observer.WaitForThemeInfoUpdated("", "", "", "");
// Check that the custom background attribution was cleared.
EXPECT_TRUE(instant_test_utils::GetBoolFromJS(
active_tab, "$('custom-bg-attr').hasChildNodes()", &result));
EXPECT_FALSE(result);
}
IN_PROC_BROWSER_TEST_F(LocalNTPCustomBackgroundsTest,
BackgroundImageSetandReset) {
content::WebContents* active_tab =
local_ntp_test_utils::OpenNewTab(browser(), GURL("about:blank"));
......@@ -148,7 +230,7 @@ IN_PROC_BROWSER_TEST_F(LocalNTPCustomBackgroundsTest, SetandReset) {
observer.WaitForThemeInfoUpdated("chrome-search://local-ntp/background1.jpg",
"", "", "");
// Check that the custom background element has the correct image with
// Check that the custom background element has the correct attribution with
// the scrim applied.
bool result = false;
EXPECT_TRUE(instant_test_utils::GetBoolFromJS(
......@@ -167,7 +249,96 @@ IN_PROC_BROWSER_TEST_F(LocalNTPCustomBackgroundsTest, SetandReset) {
// Check that the custom background was cleared.
EXPECT_TRUE(instant_test_utils::GetBoolFromJS(
active_tab, "$('custom-bg').backgroundImage == ''", &result));
active_tab, "$('custom-bg').backgroundImage === undefined", &result));
EXPECT_TRUE(result);
}
class LocalNTPCustomBackgroundsThemeTest
: public extensions::ExtensionBrowserTest {
public:
LocalNTPCustomBackgroundsThemeTest() {
feature_list_.InitWithFeatures(
{features::kUseGoogleLocalNtp, features::kNtpBackgrounds}, {});
}
protected:
void InstallThemeAndVerify(const std::string& theme_dir,
const std::string& theme_name) {
bool had_previous_theme =
!!ThemeServiceFactory::GetThemeForProfile(profile());
const base::FilePath theme_path = test_data_dir_.AppendASCII(theme_dir);
// Themes install asynchronously so we must check the number of enabled
// extensions after theme install completes.
size_t num_before = extensions::ExtensionRegistry::Get(profile())
->enabled_extensions()
.size();
content::WindowedNotificationObserver theme_change_observer(
chrome::NOTIFICATION_BROWSER_THEME_CHANGED,
content::Source<ThemeService>(
ThemeServiceFactory::GetForProfile(profile())));
ASSERT_TRUE(InstallExtensionWithUIAutoConfirm(
theme_path, 1, extensions::ExtensionBrowserTest::browser()));
theme_change_observer.Wait();
size_t num_after = extensions::ExtensionRegistry::Get(profile())
->enabled_extensions()
.size();
// If a theme was already installed, we're just swapping one for another, so
// no change in extension count.
int expected_change = had_previous_theme ? 0 : 1;
EXPECT_EQ(num_before + expected_change, num_after);
const extensions::Extension* new_theme =
ThemeServiceFactory::GetThemeForProfile(profile());
ASSERT_NE(nullptr, new_theme);
ASSERT_EQ(new_theme->name(), theme_name);
}
private:
base::test::ScopedFeatureList feature_list_;
};
IN_PROC_BROWSER_TEST_F(LocalNTPCustomBackgroundsThemeTest,
RemoveAttributeAfterThemeApplied) {
content::WebContents* active_tab =
local_ntp_test_utils::OpenNewTab(browser(), GURL("about:blank"));
TestThemeInfoObserver observer(
InstantServiceFactory::GetForProfile(profile()));
local_ntp_test_utils::NavigateToNTPAndWaitUntilLoaded(browser());
// Set a custom background attribution via the EmbeddedSearch API.
EXPECT_TRUE(content::ExecuteScript(active_tab,
"window.chrome.embeddedSearch.newTabPage."
"setBackgroundURLWithAttributions('https:/"
"/www.test.com/', 'attr1', 'attr2', "
"'https://www.attribution.com/')"));
observer.WaitForThemeInfoUpdated("https://www.test.com/", "attr1", "attr2",
"https://www.attribution.com/");
// Check that the custom background element has the correct attribution
// applied.
bool result = false;
EXPECT_TRUE(instant_test_utils::GetBoolFromJS(
active_tab,
"document.querySelector('.attr1').innerText === 'attr1' && "
"document.querySelector('.attr2').innerText === 'attr2'",
&result));
EXPECT_TRUE(result);
// Apply a custom background still count as using default theme
EXPECT_TRUE(observer.IsUsingDefaultTheme());
// Switch to waiting for the theme to get applied.
observer.SwitchCheck();
ASSERT_NO_FATAL_FAILURE(InstallThemeAndVerify("theme", "camo theme"));
observer.WaitForThemeApplied(true);
EXPECT_FALSE(observer.IsUsingDefaultTheme());
// Check that the custom background attribution is cleared after
// a theme was applied.
EXPECT_TRUE(instant_test_utils::GetBoolFromJS(
active_tab, "$('custom-bg-attr').hasChildNodes()", &result));
EXPECT_FALSE(result);
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment