diff options
-rw-r--r-- | base/command_line.cc | 1 | ||||
-rw-r--r-- | chrome/browser/ui/startup/startup_browser_creator_browsertest.cc | 383 | ||||
-rw-r--r-- | chrome/browser/ui/startup/startup_browser_creator_impl.cc | 60 |
3 files changed, 411 insertions, 33 deletions
diff --git a/base/command_line.cc b/base/command_line.cc index b743d51..7d137f26 100644 --- a/base/command_line.cc +++ b/base/command_line.cc @@ -220,6 +220,7 @@ void CommandLine::InitFromArgv(int argc, void CommandLine::InitFromArgv(const StringVector& argv) { argv_ = StringVector(1); + switches_.clear(); begin_args_ = 1; SetProgram(argv.empty() ? FilePath() : FilePath(argv[0])); AppendSwitchesAndArguments(*this, argv); diff --git a/chrome/browser/ui/startup/startup_browser_creator_browsertest.cc b/chrome/browser/ui/startup/startup_browser_creator_browsertest.cc index fe0300e..1af06b1 100644 --- a/chrome/browser/ui/startup/startup_browser_creator_browsertest.cc +++ b/chrome/browser/ui/startup/startup_browser_creator_browsertest.cc @@ -2,9 +2,13 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. +#include <algorithm> +#include <string> + #include "base/command_line.h" #include "base/files/file_path.h" #include "base/prefs/pref_service.h" +#include "base/strings/utf_string_conversions.h" #include "chrome/browser/browser_process.h" #include "chrome/browser/extensions/extension_browsertest.h" #include "chrome/browser/extensions/extension_service.h" @@ -39,7 +43,22 @@ #include "chrome/browser/managed_mode/managed_mode_navigation_observer.h" #include "chrome/browser/managed_mode/managed_user_service.h" #include "chrome/browser/managed_mode/managed_user_service_factory.h" -#endif +#endif // defined(ENABLE_MANAGED_USERS) + +#if defined(ENABLE_CONFIGURATION_POLICY) && !defined(OS_CHROMEOS) +#include "base/run_loop.h" +#include "base/values.h" +#include "chrome/browser/policy/browser_policy_connector.h" +#include "chrome/browser/policy/mock_configuration_policy_provider.h" +#include "chrome/browser/policy/policy_map.h" +#include "chrome/browser/policy/policy_types.h" +#include "policy/policy_constants.h" +#include "testing/gmock/include/gmock/gmock.h" + +using testing::_; +using testing::AnyNumber; +using testing::Return; +#endif // defined(ENABLE_CONFIGURATION_POLICY) && !defined(OS_CHROMEOS) using extensions::Extension; @@ -969,6 +988,364 @@ IN_PROC_BROWSER_TEST_F(ManagedModeBrowserCreatorTest, EXPECT_EQ(1, tab_strip->count()); } -#endif // ENABLE_MANAGED_USERS +#endif // defined(ENABLE_MANAGED_USERS) + +#endif // !defined(OS_CHROMEOS) + +// These tests are not applicable to Chrome OS as neither master_preferences nor +// the sync promo exist there. +#if !defined(OS_CHROMEOS) + +// On a branded Linux build, policy is required to suppress the first-run +// dialog. +#if !defined(OS_LINUX) || !defined(GOOGLE_CHROME_BUILD) || \ + defined(ENABLE_CONFIGURATION_POLICY) + +class StartupBrowserCreatorFirstRunTest : public InProcessBrowserTest { + protected: + virtual void SetUpInProcessBrowserTestFixture() OVERRIDE; + +#if defined(ENABLE_CONFIGURATION_POLICY) + policy::MockConfigurationPolicyProvider provider_; + policy::PolicyMap policy_map_; +#endif // defined(ENABLE_CONFIGURATION_POLICY) +}; + +void StartupBrowserCreatorFirstRunTest::SetUpInProcessBrowserTestFixture() { + // Remove the --no-first-run flag from the command line. + CommandLine* command_line = CommandLine::ForCurrentProcess(); + CommandLine::StringVector argv = command_line->argv(); + const std::string first_run_flag = std::string("--") + switches::kNoFirstRun; +#if defined(OS_WIN) + argv.erase(std::find(argv.begin(), argv.end(), ASCIIToWide(first_run_flag))); +#else + argv.erase(std::find(argv.begin(), argv.end(), first_run_flag)); +#endif + command_line->InitFromArgv(argv); + +#if defined(ENABLE_CONFIGURATION_POLICY) +#if defined(OS_LINUX) && defined(GOOGLE_CHROME_BUILD) + // Set a policy that prevents the first-run dialog from being shown. + policy_map_.Set(policy::key::kMetricsReportingEnabled, + policy::POLICY_LEVEL_MANDATORY, policy::POLICY_SCOPE_USER, + base::Value::CreateBooleanValue(false)); + provider_.UpdateChromePolicy(policy_map_); +#endif // defined(OS_LINUX) && defined(GOOGLE_CHROME_BUILD) + + EXPECT_CALL(provider_, IsInitializationComplete(_)) + .WillRepeatedly(Return(true)); + EXPECT_CALL(provider_, RegisterPolicyDomain(_)).Times(AnyNumber()); + policy::BrowserPolicyConnector::SetPolicyProviderForTesting(&provider_); +#endif // defined(ENABLE_CONFIGURATION_POLICY) +} + +IN_PROC_BROWSER_TEST_F(StartupBrowserCreatorFirstRunTest, SyncPromoForbidden) { + // Consistently enable the welcome page on all platforms. + first_run::SetShouldShowWelcomePage(); + + // Simulate the following master_preferences: + // { + // "sync_promo": { + // "show_on_first_run_allowed": false + // } + // } + StartupBrowserCreator browser_creator; + browser()->profile()->GetPrefs()->SetBoolean( + prefs::kSyncPromoShowOnFirstRunAllowed, false); + + // Do a process-startup browser launch. + CommandLine dummy(CommandLine::NO_PROGRAM); + StartupBrowserCreatorImpl launch(base::FilePath(), dummy, &browser_creator, + chrome::startup::IS_FIRST_RUN); + ASSERT_TRUE(launch.Launch(browser()->profile(), std::vector<GURL>(), true, + browser()->host_desktop_type())); + + // This should have created a new browser window. + Browser* new_browser = FindOneOtherBrowser(browser()); + ASSERT_TRUE(new_browser); + + // Verify that the NTP and the welcome page are shown. + TabStripModel* tab_strip = new_browser->tab_strip_model(); + ASSERT_EQ(2, tab_strip->count()); + EXPECT_EQ(GURL(chrome::kChromeUINewTabURL), + tab_strip->GetWebContentsAt(0)->GetURL()); + EXPECT_EQ(internals::GetWelcomePageURL(), + tab_strip->GetWebContentsAt(1)->GetURL()); +} + +IN_PROC_BROWSER_TEST_F(StartupBrowserCreatorFirstRunTest, SyncPromoAllowed) { + // Consistently enable the welcome page on all platforms. + first_run::SetShouldShowWelcomePage(); + + // Simulate the following master_preferences: + // { + // "sync_promo": { + // "show_on_first_run_allowed": true + // } + // } + StartupBrowserCreator browser_creator; + browser()->profile()->GetPrefs()->SetBoolean( + prefs::kSyncPromoShowOnFirstRunAllowed, true); + + // Do a process-startup browser launch. + CommandLine dummy(CommandLine::NO_PROGRAM); + StartupBrowserCreatorImpl launch(base::FilePath(), dummy, &browser_creator, + chrome::startup::IS_FIRST_RUN); + ASSERT_TRUE(launch.Launch(browser()->profile(), std::vector<GURL>(), true, + browser()->host_desktop_type())); + + // This should have created a new browser window. + Browser* new_browser = FindOneOtherBrowser(browser()); + ASSERT_TRUE(new_browser); + + // Verify that the sync promo and the welcome page are shown. + TabStripModel* tab_strip = new_browser->tab_strip_model(); + ASSERT_EQ(2, tab_strip->count()); + EXPECT_EQ("accounts.google.com", + tab_strip->GetWebContentsAt(0)->GetURL().host()); + EXPECT_EQ(internals::GetWelcomePageURL(), + tab_strip->GetWebContentsAt(1)->GetURL()); +} + +IN_PROC_BROWSER_TEST_F(StartupBrowserCreatorFirstRunTest, + FirstRunTabsPromoAllowed) { + // Simulate the following master_preferences: + // { + // "first_run_tabs" : [ + // "files/title1.html" + // ], + // "sync_promo": { + // "show_on_first_run_allowed": true + // } + // } + StartupBrowserCreator browser_creator; + browser_creator.AddFirstRunTab(test_server()->GetURL("files/title1.html")); + browser()->profile()->GetPrefs()->SetBoolean( + prefs::kSyncPromoShowOnFirstRunAllowed, true); + + // Do a process-startup browser launch. + CommandLine dummy(CommandLine::NO_PROGRAM); + StartupBrowserCreatorImpl launch(base::FilePath(), dummy, &browser_creator, + chrome::startup::IS_FIRST_RUN); + ASSERT_TRUE(launch.Launch(browser()->profile(), std::vector<GURL>(), true, + browser()->host_desktop_type())); + + // This should have created a new browser window. + Browser* new_browser = FindOneOtherBrowser(browser()); + ASSERT_TRUE(new_browser); + + // Verify that the first-run tab is shown and the sync promo has been added. + TabStripModel* tab_strip = new_browser->tab_strip_model(); + ASSERT_EQ(2, tab_strip->count()); + EXPECT_EQ("accounts.google.com", + tab_strip->GetWebContentsAt(0)->GetURL().host()); + EXPECT_EQ("title1.html", + tab_strip->GetWebContentsAt(1)->GetURL().ExtractFileName()); +} + +IN_PROC_BROWSER_TEST_F(StartupBrowserCreatorFirstRunTest, + FirstRunTabsContainSyncPromo) { + // Simulate the following master_preferences: + // { + // "first_run_tabs" : [ + // "files/title1.html", + // "chrome://signin/?source=0&next_page=chrome%3A%2F%2Fnewtab%2F" + // ], + // "sync_promo": { + // "show_on_first_run_allowed": true + // } + // } + StartupBrowserCreator browser_creator; + browser_creator.AddFirstRunTab(test_server()->GetURL("files/title1.html")); + browser_creator.AddFirstRunTab(SyncPromoUI::GetSyncPromoURL( + SyncPromoUI::SOURCE_START_PAGE, + false)); + browser()->profile()->GetPrefs()->SetBoolean( + prefs::kSyncPromoShowOnFirstRunAllowed, true); + + // Do a process-startup browser launch. + CommandLine dummy(CommandLine::NO_PROGRAM); + StartupBrowserCreatorImpl launch(base::FilePath(), dummy, &browser_creator, + chrome::startup::IS_FIRST_RUN); + ASSERT_TRUE(launch.Launch(browser()->profile(), std::vector<GURL>(), true, + browser()->host_desktop_type())); + + // This should have created a new browser window. + Browser* new_browser = FindOneOtherBrowser(browser()); + ASSERT_TRUE(new_browser); + + // Verify that the first-run tabs are shown and no sync promo has been added + // as the first-run tabs contain it already. + TabStripModel* tab_strip = new_browser->tab_strip_model(); + ASSERT_EQ(2, tab_strip->count()); + EXPECT_EQ("title1.html", + tab_strip->GetWebContentsAt(0)->GetURL().ExtractFileName()); + EXPECT_EQ("accounts.google.com", + tab_strip->GetWebContentsAt(1)->GetURL().host()); +} + +IN_PROC_BROWSER_TEST_F(StartupBrowserCreatorFirstRunTest, + FirstRunTabsContainNTPSyncPromoAllowed) { + // Simulate the following master_preferences: + // { + // "first_run_tabs" : [ + // "new_tab_page", + // "files/title1.html" + // ], + // "sync_promo": { + // "show_on_first_run_allowed": true + // } + // } + StartupBrowserCreator browser_creator; + browser_creator.AddFirstRunTab(GURL("new_tab_page")); + browser_creator.AddFirstRunTab(test_server()->GetURL("files/title1.html")); + browser()->profile()->GetPrefs()->SetBoolean( + prefs::kSyncPromoShowOnFirstRunAllowed, true); + + // Do a process-startup browser launch. + CommandLine dummy(CommandLine::NO_PROGRAM); + StartupBrowserCreatorImpl launch(base::FilePath(), dummy, &browser_creator, + chrome::startup::IS_FIRST_RUN); + ASSERT_TRUE(launch.Launch(browser()->profile(), std::vector<GURL>(), true, + browser()->host_desktop_type())); + + // This should have created a new browser window. + Browser* new_browser = FindOneOtherBrowser(browser()); + ASSERT_TRUE(new_browser); + + // Verify that the first-run tabs are shown but the NTP that they contain has + // been replaced by the sync promo. + TabStripModel* tab_strip = new_browser->tab_strip_model(); + ASSERT_EQ(2, tab_strip->count()); + EXPECT_EQ("accounts.google.com", + tab_strip->GetWebContentsAt(0)->GetURL().host()); + EXPECT_EQ("title1.html", + tab_strip->GetWebContentsAt(1)->GetURL().ExtractFileName()); +} + +IN_PROC_BROWSER_TEST_F(StartupBrowserCreatorFirstRunTest, + FirstRunTabsContainNTPSyncPromoForbidden) { + // Simulate the following master_preferences: + // { + // "first_run_tabs" : [ + // "new_tab_page", + // "files/title1.html" + // ], + // "sync_promo": { + // "show_on_first_run_allowed": false + // } + // } + StartupBrowserCreator browser_creator; + browser_creator.AddFirstRunTab(GURL("new_tab_page")); + browser_creator.AddFirstRunTab(test_server()->GetURL("files/title1.html")); + browser()->profile()->GetPrefs()->SetBoolean( + prefs::kSyncPromoShowOnFirstRunAllowed, false); + + // Do a process-startup browser launch. + CommandLine dummy(CommandLine::NO_PROGRAM); + StartupBrowserCreatorImpl launch(base::FilePath(), dummy, &browser_creator, + chrome::startup::IS_FIRST_RUN); + ASSERT_TRUE(launch.Launch(browser()->profile(), std::vector<GURL>(), true, + browser()->host_desktop_type())); + + // This should have created a new browser window. + Browser* new_browser = FindOneOtherBrowser(browser()); + ASSERT_TRUE(new_browser); -#endif // !OS_CHROMEOS + // Verify that the first-run tabs are shown, the NTP that they contain has not + // not been replaced by the sync promo and no sync promo has been added. + TabStripModel* tab_strip = new_browser->tab_strip_model(); + ASSERT_EQ(2, tab_strip->count()); + EXPECT_EQ(GURL(chrome::kChromeUINewTabURL), + tab_strip->GetWebContentsAt(0)->GetURL()); + EXPECT_EQ("title1.html", + tab_strip->GetWebContentsAt(1)->GetURL().ExtractFileName()); +} + +IN_PROC_BROWSER_TEST_F(StartupBrowserCreatorFirstRunTest, + FirstRunTabsSyncPromoForbidden) { + // Simulate the following master_preferences: + // { + // "first_run_tabs" : [ + // "files/title1.html" + // ], + // "sync_promo": { + // "show_on_first_run_allowed": false + // } + // } + StartupBrowserCreator browser_creator; + browser_creator.AddFirstRunTab(test_server()->GetURL("files/title1.html")); + browser()->profile()->GetPrefs()->SetBoolean( + prefs::kSyncPromoShowOnFirstRunAllowed, false); + + // Do a process-startup browser launch. + CommandLine dummy(CommandLine::NO_PROGRAM); + StartupBrowserCreatorImpl launch(base::FilePath(), dummy, &browser_creator, + chrome::startup::IS_FIRST_RUN); + ASSERT_TRUE(launch.Launch(browser()->profile(), std::vector<GURL>(), true, + browser()->host_desktop_type())); + + // This should have created a new browser window. + Browser* new_browser = FindOneOtherBrowser(browser()); + ASSERT_TRUE(new_browser); + + // Verify that the first-run tab is shown and no sync promo has been added. + TabStripModel* tab_strip = new_browser->tab_strip_model(); + ASSERT_EQ(1, tab_strip->count()); + EXPECT_EQ("title1.html", + tab_strip->GetWebContentsAt(0)->GetURL().ExtractFileName()); +} + +#if defined(ENABLE_CONFIGURATION_POLICY) +IN_PROC_BROWSER_TEST_F(StartupBrowserCreatorFirstRunTest, + RestoreOnStartupURLsPolicySpecified) { + // Simulate the following master_preferences: + // { + // "sync_promo": { + // "show_on_first_run_allowed": true + // } + // } + StartupBrowserCreator browser_creator; + browser()->profile()->GetPrefs()->SetBoolean( + prefs::kSyncPromoShowOnFirstRunAllowed, true); + + // Set the following user policies: + // * RestoreOnStartup = RestoreOnStartupIsURLs + // * RestoreOnStartupURLs = [ "files/title1.html" ] + policy_map_.Set(policy::key::kRestoreOnStartup, + policy::POLICY_LEVEL_MANDATORY, policy::POLICY_SCOPE_USER, + base::Value::CreateIntegerValue( + SessionStartupPref::kPrefValueURLs)); + base::ListValue startup_urls; + startup_urls.Append(base::Value::CreateStringValue( + test_server()->GetURL("files/title1.html").spec())); + policy_map_.Set(policy::key::kRestoreOnStartupURLs, + policy::POLICY_LEVEL_MANDATORY, policy::POLICY_SCOPE_USER, + startup_urls.DeepCopy()); + provider_.UpdateChromePolicy(policy_map_); + base::RunLoop().RunUntilIdle(); + + // Do a process-startup browser launch. + CommandLine dummy(CommandLine::NO_PROGRAM); + StartupBrowserCreatorImpl launch(base::FilePath(), dummy, &browser_creator, + chrome::startup::IS_FIRST_RUN); + ASSERT_TRUE(launch.Launch(browser()->profile(), std::vector<GURL>(), true, + browser()->host_desktop_type())); + + // This should have created a new browser window. + Browser* new_browser = FindOneOtherBrowser(browser()); + ASSERT_TRUE(new_browser); + + // Verify that the URL specified through policy is shown and no sync promo has + // been added. + TabStripModel* tab_strip = new_browser->tab_strip_model(); + ASSERT_EQ(1, tab_strip->count()); + EXPECT_EQ("title1.html", + tab_strip->GetWebContentsAt(0)->GetURL().ExtractFileName()); +} +#endif // defined(ENABLE_CONFIGURATION_POLICY) + +#endif // !defined(OS_LINUX) || !defined(GOOGLE_CHROME_BUILD) || + // defined(ENABLE_CONFIGURATION_POLICY) + +#endif // !defined(OS_CHROMEOS) diff --git a/chrome/browser/ui/startup/startup_browser_creator_impl.cc b/chrome/browser/ui/startup/startup_browser_creator_impl.cc index 14314c4..2f29df6 100644 --- a/chrome/browser/ui/startup/startup_browser_creator_impl.cc +++ b/chrome/browser/ui/startup/startup_browser_creator_impl.cc @@ -272,30 +272,6 @@ bool IsNewTabURL(Profile* profile, const GURL& url) { (url.is_empty() && profile->GetHomePage() == ntp_url); } -void AddSyncPromoTab(Profile* profile, StartupTabs* tabs) { - SyncPromoUI::DidShowSyncPromoAtStartup(profile); - - StartupTab sync_promo_tab; - sync_promo_tab.url = SyncPromoUI::GetSyncPromoURL( - SyncPromoUI::SOURCE_START_PAGE, false); - sync_promo_tab.is_pinned = false; - - // No need to add if the sync promo is already in the startup list. - for (StartupTabs::const_iterator it = tabs->begin(); it != tabs->end(); - ++it) { - if (it->url == sync_promo_tab.url) - return; - } - - tabs->insert(tabs->begin(), sync_promo_tab); - - // If the next URL is the NTP then remove it, effectively replacing the NTP - // with the sync promo. This behavior is desired because completing or - // skipping the sync promo causes a redirect to the NTP. - if (tabs->size() > 1 && IsNewTabURL(profile, tabs->at(1).url)) - tabs->erase(tabs->begin() + 1); -} - class WebContentsCloseObserver : public content::NotificationObserver { public: WebContentsCloseObserver() : contents_(NULL) {} @@ -763,11 +739,6 @@ Browser* StartupBrowserCreatorImpl::ProcessSpecifiedURLs( NOTREACHED() << "SessionStartupPref has deprecated type HOMEPAGE"; } - if (pref.type != SessionStartupPref::LAST && - SyncPromoUI::ShouldShowSyncPromoAtStartup(profile_, is_first_run_)) { - AddSyncPromoTab(profile_, &tabs); - } - if (tabs.empty()) return NULL; @@ -930,9 +901,11 @@ void StartupBrowserCreatorImpl::AddInfoBarsIfNecessary( } } - void StartupBrowserCreatorImpl::AddStartupURLs( std::vector<GURL>* startup_urls) const { + // TODO(atwilson): Simplify the logic that decides which tabs to open on + // start-up and make it more consistent. http://crbug.com/248883 + // If we have urls specified by the first run master preferences use them // and nothing else. if (browser_creator_ && startup_urls->empty()) { @@ -962,6 +935,33 @@ void StartupBrowserCreatorImpl::AddStartupURLs( if (first_run::ShouldShowWelcomePage()) startup_urls->push_back(internals::GetWelcomePageURL()); } + + if (SyncPromoUI::ShouldShowSyncPromoAtStartup(profile_, is_first_run_)) { + SyncPromoUI::DidShowSyncPromoAtStartup(profile_); + + const GURL sync_promo_url = SyncPromoUI::GetSyncPromoURL( + SyncPromoUI::SOURCE_START_PAGE, false); + + // No need to add if the sync promo is already in the startup list. + bool add_promo = true; + for (std::vector<GURL>::const_iterator it = startup_urls->begin(); + it != startup_urls->end(); ++it) { + if (*it == sync_promo_url) { + add_promo = false; + break; + } + } + + if (add_promo) { + // If the first URL is the NTP, replace it with the sync promo. This + // behavior is desired because completing or skipping the sync promo + // causes a redirect to the NTP. + if (!startup_urls->empty() && IsNewTabURL(profile_, startup_urls->at(0))) + startup_urls->at(0) = sync_promo_url; + else + startup_urls->insert(startup_urls->begin(), sync_promo_url); + } + } } #if !defined(OS_WIN) |