Commit b59ba593 authored by Weilun Shi's avatar Weilun Shi Committed by Commit Bot

[NTP] Update NTP to respect the HideWebStoreIcon policy and enable NTP MD...

[NTP] Update NTP to respect the HideWebStoreIcon policy and enable NTP MD feature for HiddenWebStoreIcon test

Now, Changing the HideWebStoreIcon policy will affect the
prepopulated_list when a new profile opens or reopen the browser. The
policy doesn't affect current profile on the NTP. This is because the
browser will cache the prepopulated list so that it can fetch the tiles
quickly. Also, force to enable New Tab Page Material Design flag for the
HiddenWebStoreIcon test so that the PolicyTest can now get the new
md-tile element instead of the old mv-tile.

Bug: 871166, 855603
Change-Id: I77a632daa73d655d9d1df053186782917405a15b
Reviewed-on: https://chromium-review.googlesource.com/1164077Reviewed-by: default avatarScott Violet <sky@chromium.org>
Commit-Queue: Weilun Shi <sweilun@chromium.org>
Cr-Commit-Position: refs/heads/master@{#581378}
parent df2d9a7b
...@@ -21,6 +21,7 @@ ...@@ -21,6 +21,7 @@
#include "chrome/browser/history/history_utils.h" #include "chrome/browser/history/history_utils.h"
#include "chrome/browser/profiles/profile.h" #include "chrome/browser/profiles/profile.h"
#include "chrome/common/chrome_features.h" #include "chrome/common/chrome_features.h"
#include "chrome/common/pref_names.h"
#include "chrome/grit/chromium_strings.h" #include "chrome/grit/chromium_strings.h"
#include "chrome/grit/generated_resources.h" #include "chrome/grit/generated_resources.h"
#include "chrome/grit/locale_settings.h" #include "chrome/grit/locale_settings.h"
...@@ -32,6 +33,7 @@ ...@@ -32,6 +33,7 @@
#include "components/keyed_service/content/browser_context_dependency_manager.h" #include "components/keyed_service/content/browser_context_dependency_manager.h"
#include "components/ntp_tiles/constants.h" #include "components/ntp_tiles/constants.h"
#include "components/pref_registry/pref_registry_syncable.h" #include "components/pref_registry/pref_registry_syncable.h"
#include "components/prefs/pref_service.h"
#include "content/public/browser/browser_thread.h" #include "content/public/browser/browser_thread.h"
#include "ui/base/l10n/l10n_util.h" #include "ui/base/l10n/l10n_util.h"
#include "url/gurl.h" #include "url/gurl.h"
...@@ -66,12 +68,16 @@ const RawPrepopulatedPage kRawPrepopulatedPages[] = { ...@@ -66,12 +68,16 @@ const RawPrepopulatedPage kRawPrepopulatedPages[] = {
#endif #endif
void InitializePrepopulatedPageList( void InitializePrepopulatedPageList(
PrefService* prefs,
history::PrepopulatedPageList* prepopulated_pages) { history::PrepopulatedPageList* prepopulated_pages) {
#if !defined(OS_ANDROID) #if !defined(OS_ANDROID)
DCHECK(prepopulated_pages); DCHECK(prepopulated_pages);
bool hide_web_store_icon = prefs->GetBoolean(prefs::kHideWebStoreIcon);
prepopulated_pages->reserve(arraysize(kRawPrepopulatedPages)); prepopulated_pages->reserve(arraysize(kRawPrepopulatedPages));
for (size_t i = 0; i < arraysize(kRawPrepopulatedPages); ++i) { for (size_t i = 0; i < arraysize(kRawPrepopulatedPages); ++i) {
const RawPrepopulatedPage& page = kRawPrepopulatedPages[i]; const RawPrepopulatedPage& page = kRawPrepopulatedPages[i];
if (hide_web_store_icon && page.url_id == IDS_WEBSTORE_URL)
continue;
prepopulated_pages->push_back(history::PrepopulatedPage( prepopulated_pages->push_back(history::PrepopulatedPage(
GURL(l10n_util::GetStringUTF8(page.url_id)), GURL(l10n_util::GetStringUTF8(page.url_id)),
l10n_util::GetStringUTF16(page.title_id), l10n_util::GetStringUTF16(page.title_id),
...@@ -116,7 +122,6 @@ scoped_refptr<history::TopSites> TopSitesFactory::BuildTopSites( ...@@ -116,7 +122,6 @@ scoped_refptr<history::TopSites> TopSitesFactory::BuildTopSites(
history::HistoryService* history_service = history::HistoryService* history_service =
HistoryServiceFactory::GetForProfile(profile, HistoryServiceFactory::GetForProfile(profile,
ServiceAccessType::EXPLICIT_ACCESS); ServiceAccessType::EXPLICIT_ACCESS);
scoped_refptr<history::TopSitesImpl> top_sites(new history::TopSitesImpl( scoped_refptr<history::TopSitesImpl> top_sites(new history::TopSitesImpl(
profile->GetPrefs(), history_service, profile->GetPrefs(), history_service,
CreateTopSitesProvider(profile, history_service), prepopulated_page_list, CreateTopSitesProvider(profile, history_service), prepopulated_page_list,
...@@ -142,7 +147,8 @@ TopSitesFactory::~TopSitesFactory() { ...@@ -142,7 +147,8 @@ TopSitesFactory::~TopSitesFactory() {
scoped_refptr<RefcountedKeyedService> TopSitesFactory::BuildServiceInstanceFor( scoped_refptr<RefcountedKeyedService> TopSitesFactory::BuildServiceInstanceFor(
content::BrowserContext* context) const { content::BrowserContext* context) const {
history::PrepopulatedPageList prepopulated_pages; history::PrepopulatedPageList prepopulated_pages;
InitializePrepopulatedPageList(&prepopulated_pages); InitializePrepopulatedPageList(
Profile::FromBrowserContext(context)->GetPrefs(), &prepopulated_pages);
return BuildTopSites(context, prepopulated_pages); return BuildTopSites(context, prepopulated_pages);
} }
......
...@@ -84,6 +84,7 @@ ...@@ -84,6 +84,7 @@
#include "chrome/browser/profiles/profile.h" #include "chrome/browser/profiles/profile.h"
#include "chrome/browser/resource_coordinator/tab_load_tracker_test_support.h" #include "chrome/browser/resource_coordinator/tab_load_tracker_test_support.h"
#include "chrome/browser/safe_browsing/chrome_password_protection_service.h" #include "chrome/browser/safe_browsing/chrome_password_protection_service.h"
#include "chrome/browser/search/ntp_features.h"
#include "chrome/browser/search/search.h" #include "chrome/browser/search/search.h"
#include "chrome/browser/search_engines/template_url_service_factory.h" #include "chrome/browser/search_engines/template_url_service_factory.h"
#include "chrome/browser/ssl/ssl_blocking_page.h" #include "chrome/browser/ssl/ssl_blocking_page.h"
...@@ -98,6 +99,8 @@ ...@@ -98,6 +99,8 @@
#include "chrome/browser/ui/browser_window.h" #include "chrome/browser/ui/browser_window.h"
#include "chrome/browser/ui/location_bar/location_bar.h" #include "chrome/browser/ui/location_bar/location_bar.h"
#include "chrome/browser/ui/permission_bubble/mock_permission_prompt_factory.h" #include "chrome/browser/ui/permission_bubble/mock_permission_prompt_factory.h"
#include "chrome/browser/ui/search/instant_test_utils.h"
#include "chrome/browser/ui/search/local_ntp_test_utils.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h" #include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/browser/ui/toolbar/component_toolbar_actions_factory.h" #include "chrome/browser/ui/toolbar/component_toolbar_actions_factory.h"
#include "chrome/browser/ui/toolbar/media_router_action_controller.h" #include "chrome/browser/ui/toolbar/media_router_action_controller.h"
...@@ -114,7 +117,9 @@ ...@@ -114,7 +117,9 @@
#include "chrome/common/pref_names.h" #include "chrome/common/pref_names.h"
#include "chrome/common/url_constants.h" #include "chrome/common/url_constants.h"
#include "chrome/common/web_application_info.h" #include "chrome/common/web_application_info.h"
#include "chrome/common/webui_url_constants.h"
#include "chrome/grit/generated_resources.h" #include "chrome/grit/generated_resources.h"
#include "chrome/grit/locale_settings.h"
#include "chrome/test/base/in_process_browser_test.h" #include "chrome/test/base/in_process_browser_test.h"
#include "chrome/test/base/search_test_utils.h" #include "chrome/test/base/search_test_utils.h"
#include "chrome/test/base/testing_profile.h" #include "chrome/test/base/testing_profile.h"
...@@ -131,6 +136,7 @@ ...@@ -131,6 +136,7 @@
#include "components/infobars/core/infobar.h" #include "components/infobars/core/infobar.h"
#include "components/language/core/browser/pref_names.h" #include "components/language/core/browser/pref_names.h"
#include "components/network_time/network_time_tracker.h" #include "components/network_time/network_time_tracker.h"
#include "components/ntp_tiles/constants.h"
#include "components/omnibox/browser/autocomplete_controller.h" #include "components/omnibox/browser/autocomplete_controller.h"
#include "components/omnibox/browser/omnibox_edit_model.h" #include "components/omnibox/browser/omnibox_edit_model.h"
#include "components/omnibox/browser/omnibox_view.h" #include "components/omnibox/browser/omnibox_view.h"
...@@ -354,6 +360,14 @@ void GetTestDataDirectory(base::FilePath* test_data_directory) { ...@@ -354,6 +360,14 @@ void GetTestDataDirectory(base::FilePath* test_data_directory) {
base::PathService::Get(chrome::DIR_TEST_DATA, test_data_directory)); base::PathService::Get(chrome::DIR_TEST_DATA, test_data_directory));
} }
content::RenderFrameHost* GetMostVisitedIframe(content::WebContents* tab) {
for (content::RenderFrameHost* frame : tab->GetAllFrames()) {
if (frame->GetFrameName() == "mv-single")
return frame;
}
return nullptr;
}
// Filters requests to the hosts in |urls| and redirects them to the test data // Filters requests to the hosts in |urls| and redirects them to the test data
// dir through URLRequestMockHTTPJobs. // dir through URLRequestMockHTTPJobs.
void RedirectHostsToTestData(const char* const urls[], size_t size) { void RedirectHostsToTestData(const char* const urls[], size_t size) {
...@@ -599,6 +613,16 @@ bool ContainsVisibleElement(content::WebContents* contents, ...@@ -599,6 +613,16 @@ bool ContainsVisibleElement(content::WebContents* contents,
return result; return result;
} }
bool ContainsWebstoreTile(content::RenderFrameHost* iframe) {
int num_webstore_tiles = 0;
EXPECT_TRUE(instant_test_utils::GetIntFromJS(
iframe,
"document.querySelectorAll(\".md-tile[href='" +
l10n_util::GetStringUTF8(IDS_WEBSTORE_URL) + "']\").length",
&num_webstore_tiles));
return num_webstore_tiles == 1;
}
#if defined(OS_CHROMEOS) #if defined(OS_CHROMEOS)
class TestAudioObserver : public chromeos::CrasAudioHandler::AudioObserver { class TestAudioObserver : public chromeos::CrasAudioHandler::AudioObserver {
public: public:
...@@ -2038,39 +2062,6 @@ IN_PROC_BROWSER_TEST_F(PolicyTest, DeveloperToolsDisabledExtensionsDevMode) { ...@@ -2038,39 +2062,6 @@ IN_PROC_BROWSER_TEST_F(PolicyTest, DeveloperToolsDisabledExtensionsDevMode) {
EXPECT_TRUE(is_toggle_dev_mode_checkbox_disabled); EXPECT_TRUE(is_toggle_dev_mode_checkbox_disabled);
} }
// TODO(samarth): remove along with rest of NTP4 code.
IN_PROC_BROWSER_TEST_F(PolicyTest, DISABLED_WebStoreIconHidden) {
// Verifies that the web store icons can be hidden from the new tab page.
// Open new tab page and look for the web store icons.
ui_test_utils::NavigateToURL(browser(), GURL(chrome::kChromeUINewTabURL));
content::WebContents* contents =
browser()->tab_strip_model()->GetActiveWebContents();
#if !defined(OS_CHROMEOS)
// Look for web store's app ID in the apps page.
EXPECT_TRUE(ContainsVisibleElement(contents,
"ahfgeienlihckogmohjhadlkjgocpleb"));
#endif
// The next NTP has no footer.
if (ContainsVisibleElement(contents, "footer"))
EXPECT_TRUE(ContainsVisibleElement(contents, "chrome-web-store-link"));
// Turn off the web store icons.
PolicyMap policies;
policies.Set(key::kHideWebStoreIcon, POLICY_LEVEL_MANDATORY,
POLICY_SCOPE_USER, POLICY_SOURCE_CLOUD,
base::WrapUnique(new base::Value(true)), nullptr);
UpdateProviderPolicy(policies);
// The web store icons should now be hidden.
ui_test_utils::NavigateToURL(browser(), GURL(chrome::kChromeUINewTabURL));
EXPECT_FALSE(ContainsVisibleElement(contents,
"ahfgeienlihckogmohjhadlkjgocpleb"));
EXPECT_FALSE(ContainsVisibleElement(contents, "chrome-web-store-link"));
}
IN_PROC_BROWSER_TEST_F(PolicyTest, DownloadDirectory) { IN_PROC_BROWSER_TEST_F(PolicyTest, DownloadDirectory) {
// Verifies that the download directory can be forced by policy. // Verifies that the download directory can be forced by policy.
...@@ -4063,6 +4054,127 @@ IN_PROC_BROWSER_TEST_F(PolicyStatisticsCollectorTest, Startup) { ...@@ -4063,6 +4054,127 @@ IN_PROC_BROWSER_TEST_F(PolicyStatisticsCollectorTest, Startup) {
EXPECT_GT(samples->GetCount(82), 0); EXPECT_GT(samples->GetCount(82), 0);
} }
// Similar to PolicyTest, but force to enable the new tab material design flag
// before the browser start.
class PolicyWebStoreIconTest : public PolicyTest {
public:
PolicyWebStoreIconTest() {}
~PolicyWebStoreIconTest() override {}
void SetUpInProcessBrowserTestFixture() override {
PolicyTest::SetUpInProcessBrowserTestFixture();
}
void SetUpCommandLine(base::CommandLine* command_line) override {
PolicyTest::SetUpCommandLine(command_line);
// Force to enable the new tab page material design flag
scoped_feature_list.InitAndEnableFeature(ntp_tiles::kNtpIcons);
}
private:
DISALLOW_COPY_AND_ASSIGN(PolicyWebStoreIconTest);
base::test::ScopedFeatureList scoped_feature_list;
};
IN_PROC_BROWSER_TEST_F(PolicyWebStoreIconTest, AppsWebStoreIconHidden) {
// Verifies that the web store icon can be hidden from the chrome://apps
// page. A policy change takes immediate effect on the apps page for the
// current profile. Browser restart is not required.
// Open new tab page and look for the web store icons.
ui_test_utils::NavigateToURL(browser(), GURL(chrome::kChromeUIAppsURL));
content::WebContents* contents =
browser()->tab_strip_model()->GetActiveWebContents();
#if !defined(OS_CHROMEOS)
// Look for web store's app ID in the apps page.
EXPECT_TRUE(
ContainsVisibleElement(contents, "ahfgeienlihckogmohjhadlkjgocpleb"));
#endif
// The next NTP has no footer.
if (ContainsVisibleElement(contents, "footer"))
EXPECT_TRUE(ContainsVisibleElement(contents, "chrome-web-store-link"));
// Turn off the web store icons.
PolicyMap policies;
policies.Set(key::kHideWebStoreIcon, POLICY_LEVEL_MANDATORY,
POLICY_SCOPE_USER, POLICY_SOURCE_CLOUD,
base::WrapUnique(new base::Value(true)), nullptr);
UpdateProviderPolicy(policies);
// The web store icons should now be hidden.
ui_test_utils::NavigateToURL(browser(), GURL(chrome::kChromeUIAppsURL));
EXPECT_FALSE(
ContainsVisibleElement(contents, "ahfgeienlihckogmohjhadlkjgocpleb"));
EXPECT_FALSE(ContainsVisibleElement(contents, "chrome-web-store-link"));
}
IN_PROC_BROWSER_TEST_F(PolicyWebStoreIconTest, NTPWebStoreIconShown) {
// This test is to verify that the web store icons is shown when no policy
// applies. See WebStoreIconPolicyTest.NTPWebStoreIconHidden for verification
// when a policy is in effect.
// Force to enable the new tab page material design flag
base::test::ScopedFeatureList scoped_feature_list;
scoped_feature_list.InitAndEnableFeature(ntp_tiles::kNtpIcons);
// Open new tab page and look for the web store icons.
content::WebContents* active_tab =
local_ntp_test_utils::OpenNewTab(browser(), GURL("about:blank"));
local_ntp_test_utils::NavigateToNTPAndWaitUntilLoaded(browser());
content::RenderFrameHost* iframe = GetMostVisitedIframe(active_tab);
// Look though all the tiles and see whether there is a webstore icon.
// Make sure that there is one web store icon.
EXPECT_TRUE(ContainsWebstoreTile(iframe));
}
// Similar to PolicyWebStoreIconShownTest, but applies the HideWebStoreIcon
// policy before the browser is started. This is required because the list that
// includes the WebStoreIcon on the NTP is initialized at browser start.
class PolicyWebStoreIconHiddenTest : public PolicyTest {
public:
PolicyWebStoreIconHiddenTest() {}
~PolicyWebStoreIconHiddenTest() override {}
void SetUpInProcessBrowserTestFixture() override {
PolicyTest::SetUpInProcessBrowserTestFixture();
PolicyMap policies;
policies.Set(key::kHideWebStoreIcon, POLICY_LEVEL_MANDATORY,
POLICY_SCOPE_USER, POLICY_SOURCE_CLOUD,
std::make_unique<base::Value>(true), nullptr);
provider_.UpdateChromePolicy(policies);
}
void SetUpCommandLine(base::CommandLine* command_line) override {
PolicyTest::SetUpCommandLine(command_line);
// Force to enable the new tab page material design flag
scoped_feature_list.InitAndEnableFeature(ntp_tiles::kNtpIcons);
}
private:
DISALLOW_COPY_AND_ASSIGN(PolicyWebStoreIconHiddenTest);
base::test::ScopedFeatureList scoped_feature_list;
};
IN_PROC_BROWSER_TEST_F(PolicyWebStoreIconHiddenTest, NTPWebStoreIconHidden) {
// Verifies that the web store icon can be hidden from the new tab page. Check
// to see NTPWebStoreIconShown for behavior when the policy is not applied.
// Open new tab page and look for the web store icon
content::WebContents* active_tab =
local_ntp_test_utils::OpenNewTab(browser(), GURL("about:blank"));
local_ntp_test_utils::NavigateToNTPAndWaitUntilLoaded(browser());
content::RenderFrameHost* iframe = GetMostVisitedIframe(active_tab);
// Applying the policy before the browser started, the web store icon should
// now be hidden.
EXPECT_FALSE(ContainsWebstoreTile(iframe));
}
class MediaStreamDevicesControllerBrowserTest class MediaStreamDevicesControllerBrowserTest
: public PolicyTest, : public PolicyTest,
public testing::WithParamInterface<bool> { public testing::WithParamInterface<bool> {
......
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