1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
8 #include "base/command_line.h"
9 #include "base/files/file_path.h"
10 #include "base/prefs/pref_service.h"
11 #include "base/strings/utf_string_conversions.h"
12 #include "chrome/browser/browser_process.h"
13 #include "chrome/browser/extensions/extension_browsertest.h"
14 #include "chrome/browser/extensions/extension_service.h"
15 #include "chrome/browser/extensions/extension_system.h"
16 #include "chrome/browser/extensions/launch_util.h"
17 #include "chrome/browser/first_run/first_run.h"
18 #include "chrome/browser/infobars/infobar_service.h"
19 #include "chrome/browser/managed_mode/managed_mode_navigation_observer.h"
20 #include "chrome/browser/managed_mode/managed_user_service.h"
21 #include "chrome/browser/managed_mode/managed_user_service_factory.h"
22 #include "chrome/browser/prefs/session_startup_pref.h"
23 #include "chrome/browser/profiles/profile.h"
24 #include "chrome/browser/profiles/profile_impl.h"
25 #include "chrome/browser/profiles/profile_manager.h"
26 #include "chrome/browser/sessions/session_restore.h"
27 #include "chrome/browser/signin/signin_promo.h"
28 #include "chrome/browser/ui/browser.h"
29 #include "chrome/browser/ui/browser_finder.h"
30 #include "chrome/browser/ui/browser_iterator.h"
31 #include "chrome/browser/ui/browser_list.h"
32 #include "chrome/browser/ui/browser_list_observer.h"
33 #include "chrome/browser/ui/browser_window.h"
34 #include "chrome/browser/ui/host_desktop.h"
35 #include "chrome/browser/ui/startup/startup_browser_creator.h"
36 #include "chrome/browser/ui/startup/startup_browser_creator_impl.h"
37 #include "chrome/browser/ui/tabs/tab_strip_model.h"
38 #include "chrome/common/chrome_switches.h"
39 #include "chrome/common/extensions/extension_constants.h"
40 #include "chrome/common/pref_names.h"
41 #include "chrome/common/url_constants.h"
42 #include "chrome/test/base/in_process_browser_test.h"
43 #include "chrome/test/base/test_switches.h"
44 #include "chrome/test/base/ui_test_utils.h"
45 #include "content/public/browser/web_contents.h"
46 #include "testing/gtest/include/gtest/gtest.h"
49 #if defined(ENABLE_CONFIGURATION_POLICY) && !defined(OS_CHROMEOS)
50 #include "base/callback.h"
51 #include "base/run_loop.h"
52 #include "base/values.h"
53 #include "chrome/browser/policy/browser_policy_connector.h"
54 #include "components/policy/core/common/external_data_fetcher.h"
55 #include "components/policy/core/common/mock_configuration_policy_provider.h"
56 #include "components/policy/core/common/policy_map.h"
57 #include "components/policy/core/common/policy_types.h"
58 #include "policy/policy_constants.h"
59 #include "testing/gmock/include/gmock/gmock.h"
62 using testing::Return
;
63 #endif // defined(ENABLE_CONFIGURATION_POLICY) && !defined(OS_CHROMEOS)
65 using extensions::Extension
;
69 // Check that there are two browsers. Find the one that is not |browser|.
70 Browser
* FindOneOtherBrowser(Browser
* browser
) {
71 // There should only be one other browser.
72 EXPECT_EQ(2u, chrome::GetBrowserCount(browser
->profile(),
73 browser
->host_desktop_type()));
75 // Find the new browser.
76 Browser
* other_browser
= NULL
;
77 for (chrome::BrowserIterator it
; !it
.done() && !other_browser
; it
.Next()) {
86 class StartupBrowserCreatorTest
: public ExtensionBrowserTest
{
88 virtual bool SetUpUserDataDirectory() OVERRIDE
{
89 return ExtensionBrowserTest::SetUpUserDataDirectory();
92 virtual void SetUpCommandLine(CommandLine
* command_line
) OVERRIDE
{
93 ExtensionBrowserTest::SetUpCommandLine(command_line
);
94 command_line
->AppendSwitch(switches::kEnablePanels
);
95 command_line
->AppendSwitchASCII(switches::kHomePage
,
96 content::kAboutBlankURL
);
97 #if defined(OS_CHROMEOS)
98 // TODO(nkostylev): Investigate if we can remove this switch.
99 command_line
->AppendSwitch(switches::kCreateBrowserOnStartupForTests
);
103 // Helper functions return void so that we can ASSERT*().
104 // Use ASSERT_NO_FATAL_FAILURE around calls to these functions to stop the
105 // test if an assert fails.
106 void LoadApp(const std::string
& app_name
,
107 const Extension
** out_app_extension
) {
108 ASSERT_TRUE(LoadExtension(test_data_dir_
.AppendASCII(app_name
.c_str())));
110 ExtensionService
* service
= extensions::ExtensionSystem::Get(
111 browser()->profile())->extension_service();
112 *out_app_extension
= service
->GetExtensionById(
113 last_loaded_extension_id(), false);
114 ASSERT_TRUE(*out_app_extension
);
116 // Code that opens a new browser assumes we start with exactly one.
117 ASSERT_EQ(1u, chrome::GetBrowserCount(browser()->profile(),
118 browser()->host_desktop_type()));
121 void SetAppLaunchPref(const std::string
& app_id
,
122 extensions::LaunchType launch_type
) {
123 ExtensionService
* service
= extensions::ExtensionSystem::Get(
124 browser()->profile())->extension_service();
125 extensions::SetLaunchType(service
, app_id
, launch_type
);
128 Browser
* FindOneOtherBrowserForProfile(Profile
* profile
,
129 Browser
* not_this_browser
) {
130 for (chrome::BrowserIterator it
; !it
.done(); it
.Next()) {
131 if (*it
!= not_this_browser
&& it
->profile() == profile
)
138 class OpenURLsPopupObserver
: public chrome::BrowserListObserver
{
140 OpenURLsPopupObserver() : added_browser_(NULL
) { }
142 virtual void OnBrowserAdded(Browser
* browser
) OVERRIDE
{
143 added_browser_
= browser
;
146 virtual void OnBrowserRemoved(Browser
* browser
) OVERRIDE
{ }
148 Browser
* added_browser_
;
151 // Test that when there is a popup as the active browser any requests to
152 // StartupBrowserCreatorImpl::OpenURLsInBrowser don't crash because there's no
153 // explicit profile given.
154 IN_PROC_BROWSER_TEST_F(StartupBrowserCreatorTest
, OpenURLsPopup
) {
155 std::vector
<GURL
> urls
;
156 urls
.push_back(GURL("http://localhost"));
158 // Note that in our testing we do not ever query the BrowserList for the "last
159 // active" browser. That's because the browsers are set as "active" by
160 // platform UI toolkit messages, and those messages are not sent during unit
163 OpenURLsPopupObserver observer
;
164 BrowserList::AddObserver(&observer
);
166 Browser
* popup
= new Browser(
167 Browser::CreateParams(Browser::TYPE_POPUP
, browser()->profile(),
168 browser()->host_desktop_type()));
169 ASSERT_TRUE(popup
->is_type_popup());
170 ASSERT_EQ(popup
, observer
.added_browser_
);
172 CommandLine
dummy(CommandLine::NO_PROGRAM
);
173 chrome::startup::IsFirstRun first_run
= first_run::IsChromeFirstRun() ?
174 chrome::startup::IS_FIRST_RUN
: chrome::startup::IS_NOT_FIRST_RUN
;
175 StartupBrowserCreatorImpl
launch(base::FilePath(), dummy
, first_run
);
176 // This should create a new window, but re-use the profile from |popup|. If
177 // it used a NULL or invalid profile, it would crash.
178 launch
.OpenURLsInBrowser(popup
, false, urls
, chrome::GetActiveDesktop());
179 ASSERT_NE(popup
, observer
.added_browser_
);
180 BrowserList::RemoveObserver(&observer
);
183 // We don't do non-process-startup browser launches on ChromeOS.
184 // Session restore for process-startup browser launches is tested
185 // in session_restore_uitest.
186 #if !defined(OS_CHROMEOS)
187 // Verify that startup URLs are honored when the process already exists but has
188 // no tabbed browser windows (eg. as if the process is running only due to a
189 // background application.
190 IN_PROC_BROWSER_TEST_F(StartupBrowserCreatorTest
,
191 StartupURLsOnNewWindowWithNoTabbedBrowsers
) {
192 // Use a couple same-site HTTP URLs.
193 ASSERT_TRUE(test_server()->Start());
194 std::vector
<GURL
> urls
;
195 urls
.push_back(test_server()->GetURL("files/title1.html"));
196 urls
.push_back(test_server()->GetURL("files/title2.html"));
198 // Set the startup preference to open these URLs.
199 SessionStartupPref
pref(SessionStartupPref::URLS
);
201 SessionStartupPref::SetStartupPref(browser()->profile(), pref
);
203 // Close the browser.
204 browser()->window()->Close();
206 // Do a simple non-process-startup browser launch.
207 CommandLine
dummy(CommandLine::NO_PROGRAM
);
208 chrome::startup::IsFirstRun first_run
= first_run::IsChromeFirstRun() ?
209 chrome::startup::IS_FIRST_RUN
: chrome::startup::IS_NOT_FIRST_RUN
;
210 StartupBrowserCreatorImpl
launch(base::FilePath(), dummy
, first_run
);
211 ASSERT_TRUE(launch
.Launch(browser()->profile(), std::vector
<GURL
>(), false,
212 browser()->host_desktop_type()));
214 // This should have created a new browser window. |browser()| is still
215 // around at this point, even though we've closed its window.
216 Browser
* new_browser
= FindOneOtherBrowser(browser());
217 ASSERT_TRUE(new_browser
);
219 // The new browser should have one tab for each URL.
220 TabStripModel
* tab_strip
= new_browser
->tab_strip_model();
221 ASSERT_EQ(static_cast<int>(urls
.size()), tab_strip
->count());
222 for (size_t i
=0; i
< urls
.size(); i
++) {
223 EXPECT_EQ(urls
[i
], tab_strip
->GetWebContentsAt(i
)->GetURL());
226 // The two tabs, despite having the same site, should be in different
228 EXPECT_NE(tab_strip
->GetWebContentsAt(0)->GetSiteInstance(),
229 tab_strip
->GetWebContentsAt(1)->GetSiteInstance());
232 // Verify that startup URLs aren't used when the process already exists
233 // and has other tabbed browser windows. This is the common case of starting a
235 IN_PROC_BROWSER_TEST_F(StartupBrowserCreatorTest
,
236 StartupURLsOnNewWindow
) {
237 // Use a couple arbitrary URLs.
238 std::vector
<GURL
> urls
;
239 urls
.push_back(ui_test_utils::GetTestUrl(
240 base::FilePath(base::FilePath::kCurrentDirectory
),
241 base::FilePath(FILE_PATH_LITERAL("title1.html"))));
242 urls
.push_back(ui_test_utils::GetTestUrl(
243 base::FilePath(base::FilePath::kCurrentDirectory
),
244 base::FilePath(FILE_PATH_LITERAL("title2.html"))));
246 // Set the startup preference to open these URLs.
247 SessionStartupPref
pref(SessionStartupPref::URLS
);
249 SessionStartupPref::SetStartupPref(browser()->profile(), pref
);
251 // Do a simple non-process-startup browser launch.
252 CommandLine
dummy(CommandLine::NO_PROGRAM
);
253 chrome::startup::IsFirstRun first_run
= first_run::IsChromeFirstRun() ?
254 chrome::startup::IS_FIRST_RUN
: chrome::startup::IS_NOT_FIRST_RUN
;
255 StartupBrowserCreatorImpl
launch(base::FilePath(), dummy
, first_run
);
256 ASSERT_TRUE(launch
.Launch(browser()->profile(), std::vector
<GURL
>(), false,
257 browser()->host_desktop_type()));
259 // This should have created a new browser window.
260 Browser
* new_browser
= FindOneOtherBrowser(browser());
261 ASSERT_TRUE(new_browser
);
263 // The new browser should have exactly one tab (not the startup URLs).
264 ASSERT_EQ(1, new_browser
->tab_strip_model()->count());
267 // App shortcuts are not implemented on mac os.
268 #if !defined(OS_MACOSX)
269 IN_PROC_BROWSER_TEST_F(StartupBrowserCreatorTest
, OpenAppShortcutNoPref
) {
270 // Load an app with launch.container = 'tab'.
271 const Extension
* extension_app
= NULL
;
272 ASSERT_NO_FATAL_FAILURE(LoadApp("app_with_tab_container", &extension_app
));
274 // Add --app-id=<extension->id()> to the command line.
275 CommandLine
command_line(CommandLine::NO_PROGRAM
);
276 command_line
.AppendSwitchASCII(switches::kAppId
, extension_app
->id());
278 chrome::startup::IsFirstRun first_run
= first_run::IsChromeFirstRun() ?
279 chrome::startup::IS_FIRST_RUN
: chrome::startup::IS_NOT_FIRST_RUN
;
280 StartupBrowserCreatorImpl
launch(base::FilePath(), command_line
, first_run
);
281 ASSERT_TRUE(launch
.Launch(browser()->profile(), std::vector
<GURL
>(), false,
282 browser()->host_desktop_type()));
284 // No pref was set, so the app should have opened in a window.
285 // The launch should have created a new browser.
286 Browser
* new_browser
= FindOneOtherBrowser(browser());
287 ASSERT_TRUE(new_browser
);
289 // Expect an app window.
290 EXPECT_TRUE(new_browser
->is_app());
292 // The browser's app_name should include the app's ID.
294 new_browser
->app_name_
.find(extension_app
->id()),
295 std::string::npos
) << new_browser
->app_name_
;
298 IN_PROC_BROWSER_TEST_F(StartupBrowserCreatorTest
, OpenAppShortcutWindowPref
) {
299 const Extension
* extension_app
= NULL
;
300 ASSERT_NO_FATAL_FAILURE(LoadApp("app_with_tab_container", &extension_app
));
302 // Set a pref indicating that the user wants to open this app in a window.
303 SetAppLaunchPref(extension_app
->id(), extensions::LAUNCH_TYPE_WINDOW
);
305 CommandLine
command_line(CommandLine::NO_PROGRAM
);
306 command_line
.AppendSwitchASCII(switches::kAppId
, extension_app
->id());
307 chrome::startup::IsFirstRun first_run
= first_run::IsChromeFirstRun() ?
308 chrome::startup::IS_FIRST_RUN
: chrome::startup::IS_NOT_FIRST_RUN
;
309 StartupBrowserCreatorImpl
launch(base::FilePath(), command_line
, first_run
);
310 ASSERT_TRUE(launch
.Launch(browser()->profile(), std::vector
<GURL
>(), false,
311 browser()->host_desktop_type()));
313 // Pref was set to open in a window, so the app should have opened in a
314 // window. The launch should have created a new browser. Find the new
316 Browser
* new_browser
= FindOneOtherBrowser(browser());
317 ASSERT_TRUE(new_browser
);
319 // Expect an app window.
320 EXPECT_TRUE(new_browser
->is_app());
322 // The browser's app_name should include the app's ID.
324 new_browser
->app_name_
.find(extension_app
->id()),
325 std::string::npos
) << new_browser
->app_name_
;
328 IN_PROC_BROWSER_TEST_F(StartupBrowserCreatorTest
, OpenAppShortcutTabPref
) {
329 // Load an app with launch.container = 'tab'.
330 const Extension
* extension_app
= NULL
;
331 ASSERT_NO_FATAL_FAILURE(LoadApp("app_with_tab_container", &extension_app
));
333 // Set a pref indicating that the user wants to open this app in a window.
334 SetAppLaunchPref(extension_app
->id(), extensions::LAUNCH_TYPE_REGULAR
);
336 CommandLine
command_line(CommandLine::NO_PROGRAM
);
337 command_line
.AppendSwitchASCII(switches::kAppId
, extension_app
->id());
338 chrome::startup::IsFirstRun first_run
= first_run::IsChromeFirstRun() ?
339 chrome::startup::IS_FIRST_RUN
: chrome::startup::IS_NOT_FIRST_RUN
;
340 StartupBrowserCreatorImpl
launch(base::FilePath(), command_line
, first_run
);
341 ASSERT_TRUE(launch
.Launch(browser()->profile(), std::vector
<GURL
>(), false,
342 browser()->host_desktop_type()));
344 // When an app shortcut is open and the pref indicates a tab should
345 // open, the tab is open in a new browser window. Expect a new window.
346 ASSERT_EQ(2u, chrome::GetBrowserCount(browser()->profile(),
347 browser()->host_desktop_type()));
349 Browser
* new_browser
= FindOneOtherBrowser(browser());
350 ASSERT_TRUE(new_browser
);
352 // The tab should be in a tabbed window.
353 EXPECT_TRUE(new_browser
->is_type_tabbed());
355 // The browser's app_name should not include the app's ID: It is in a
358 new_browser
->app_name_
.find(extension_app
->id()),
359 std::string::npos
) << new_browser
->app_name_
;
362 #endif // !defined(OS_MACOSX)
364 #endif // !defined(OS_CHROMEOS)
366 IN_PROC_BROWSER_TEST_F(StartupBrowserCreatorTest
,
367 ReadingWasRestartedAfterRestart
) {
368 // Tests that StartupBrowserCreator::WasRestarted reads and resets the
369 // preference kWasRestarted correctly.
370 StartupBrowserCreator::was_restarted_read_
= false;
371 PrefService
* pref_service
= g_browser_process
->local_state();
372 pref_service
->SetBoolean(prefs::kWasRestarted
, true);
373 EXPECT_TRUE(StartupBrowserCreator::WasRestarted());
374 EXPECT_FALSE(pref_service
->GetBoolean(prefs::kWasRestarted
));
375 EXPECT_TRUE(StartupBrowserCreator::WasRestarted());
378 IN_PROC_BROWSER_TEST_F(StartupBrowserCreatorTest
,
379 ReadingWasRestartedAfterNormalStart
) {
380 // Tests that StartupBrowserCreator::WasRestarted reads and resets the
381 // preference kWasRestarted correctly.
382 StartupBrowserCreator::was_restarted_read_
= false;
383 PrefService
* pref_service
= g_browser_process
->local_state();
384 pref_service
->SetBoolean(prefs::kWasRestarted
, false);
385 EXPECT_FALSE(StartupBrowserCreator::WasRestarted());
386 EXPECT_FALSE(pref_service
->GetBoolean(prefs::kWasRestarted
));
387 EXPECT_FALSE(StartupBrowserCreator::WasRestarted());
390 // Fails on official builds. See http://crbug.com/313856
391 #if defined(GOOGLE_CHROME_BUILD)
392 #define MAYBE_AddFirstRunTab DISABLED_AddFirstRunTab
394 #define MAYBE_AddFirstRunTab AddFirstRunTab
396 IN_PROC_BROWSER_TEST_F(StartupBrowserCreatorTest
, MAYBE_AddFirstRunTab
) {
397 StartupBrowserCreator browser_creator
;
398 browser_creator
.AddFirstRunTab(test_server()->GetURL("files/title1.html"));
399 browser_creator
.AddFirstRunTab(test_server()->GetURL("files/title2.html"));
401 // Do a simple non-process-startup browser launch.
402 CommandLine
dummy(CommandLine::NO_PROGRAM
);
403 StartupBrowserCreatorImpl
launch(base::FilePath(), dummy
, &browser_creator
,
404 chrome::startup::IS_FIRST_RUN
);
405 ASSERT_TRUE(launch
.Launch(browser()->profile(), std::vector
<GURL
>(), false,
406 browser()->host_desktop_type()));
408 // This should have created a new browser window.
409 Browser
* new_browser
= FindOneOtherBrowser(browser());
410 ASSERT_TRUE(new_browser
);
412 TabStripModel
* tab_strip
= new_browser
->tab_strip_model();
413 EXPECT_EQ(2, tab_strip
->count());
415 EXPECT_EQ("title1.html",
416 tab_strip
->GetWebContentsAt(0)->GetURL().ExtractFileName());
417 EXPECT_EQ("title2.html",
418 tab_strip
->GetWebContentsAt(1)->GetURL().ExtractFileName());
421 // Test hard-coded special first run tabs (defined in
422 // StartupBrowserCreatorImpl::AddStartupURLs()).
423 // Fails on official builds. See http://crbug.com/313856
424 #if defined(GOOGLE_CHROME_BUILD)
425 #define MAYBE_AddCustomFirstRunTab DISABLED_AddCustomFirstRunTab
427 #define MAYBE_AddCustomFirstRunTab AddCustomFirstRunTab
429 IN_PROC_BROWSER_TEST_F(StartupBrowserCreatorTest
, MAYBE_AddCustomFirstRunTab
) {
430 StartupBrowserCreator browser_creator
;
431 browser_creator
.AddFirstRunTab(test_server()->GetURL("files/title1.html"));
432 browser_creator
.AddFirstRunTab(GURL("http://new_tab_page"));
433 browser_creator
.AddFirstRunTab(test_server()->GetURL("files/title2.html"));
434 browser_creator
.AddFirstRunTab(GURL("http://welcome_page"));
436 // Do a simple non-process-startup browser launch.
437 CommandLine
dummy(CommandLine::NO_PROGRAM
);
438 StartupBrowserCreatorImpl
launch(base::FilePath(), dummy
, &browser_creator
,
439 chrome::startup::IS_FIRST_RUN
);
440 ASSERT_TRUE(launch
.Launch(browser()->profile(), std::vector
<GURL
>(), false,
441 browser()->host_desktop_type()));
443 // This should have created a new browser window.
444 Browser
* new_browser
= FindOneOtherBrowser(browser());
445 ASSERT_TRUE(new_browser
);
447 TabStripModel
* tab_strip
= new_browser
->tab_strip_model();
448 EXPECT_EQ(4, tab_strip
->count());
450 EXPECT_EQ("title1.html",
451 tab_strip
->GetWebContentsAt(0)->GetURL().ExtractFileName());
452 EXPECT_EQ(GURL(chrome::kChromeUINewTabURL
),
453 tab_strip
->GetWebContentsAt(1)->GetURL());
454 EXPECT_EQ("title2.html",
455 tab_strip
->GetWebContentsAt(2)->GetURL().ExtractFileName());
456 EXPECT_EQ(internals::GetWelcomePageURL(),
457 tab_strip
->GetWebContentsAt(3)->GetURL());
460 IN_PROC_BROWSER_TEST_F(StartupBrowserCreatorTest
, SyncPromoNoWelcomePage
) {
461 // Do a simple non-process-startup browser launch.
462 CommandLine
dummy(CommandLine::NO_PROGRAM
);
463 StartupBrowserCreatorImpl
launch(base::FilePath(), dummy
,
464 chrome::startup::IS_FIRST_RUN
);
465 ASSERT_TRUE(launch
.Launch(browser()->profile(), std::vector
<GURL
>(), false,
466 browser()->host_desktop_type()));
468 // This should have created a new browser window.
469 Browser
* new_browser
= FindOneOtherBrowser(browser());
470 ASSERT_TRUE(new_browser
);
472 TabStripModel
* tab_strip
= new_browser
->tab_strip_model();
473 EXPECT_EQ(1, tab_strip
->count());
475 if (signin::ShouldShowPromoAtStartup(browser()->profile(), true)) {
476 EXPECT_EQ(signin::GetPromoURL(signin::SOURCE_START_PAGE
, false),
477 tab_strip
->GetWebContentsAt(0)->GetURL());
479 EXPECT_EQ(GURL(chrome::kChromeUINewTabURL
),
480 tab_strip
->GetWebContentsAt(0)->GetURL());
484 IN_PROC_BROWSER_TEST_F(StartupBrowserCreatorTest
, SyncPromoWithWelcomePage
) {
485 first_run::SetShouldShowWelcomePage();
487 // Do a simple non-process-startup browser launch.
488 CommandLine
dummy(CommandLine::NO_PROGRAM
);
489 StartupBrowserCreatorImpl
launch(base::FilePath(), dummy
,
490 chrome::startup::IS_FIRST_RUN
);
491 ASSERT_TRUE(launch
.Launch(browser()->profile(), std::vector
<GURL
>(), false,
492 browser()->host_desktop_type()));
494 // This should have created a new browser window.
495 Browser
* new_browser
= FindOneOtherBrowser(browser());
496 ASSERT_TRUE(new_browser
);
498 TabStripModel
* tab_strip
= new_browser
->tab_strip_model();
499 EXPECT_EQ(2, tab_strip
->count());
501 if (signin::ShouldShowPromoAtStartup(browser()->profile(), true)) {
502 EXPECT_EQ(signin::GetPromoURL(signin::SOURCE_START_PAGE
, false),
503 tab_strip
->GetWebContentsAt(0)->GetURL());
505 EXPECT_EQ(GURL(chrome::kChromeUINewTabURL
),
506 tab_strip
->GetWebContentsAt(0)->GetURL());
508 EXPECT_EQ(internals::GetWelcomePageURL(),
509 tab_strip
->GetWebContentsAt(1)->GetURL());
512 IN_PROC_BROWSER_TEST_F(StartupBrowserCreatorTest
, SyncPromoWithFirstRunTabs
) {
513 StartupBrowserCreator browser_creator
;
514 browser_creator
.AddFirstRunTab(test_server()->GetURL("files/title1.html"));
516 // The welcome page should not be shown, even if
517 // first_run::ShouldShowWelcomePage() says so, when there are already
518 // more than 2 first run tabs.
519 first_run::SetShouldShowWelcomePage();
521 // Do a simple non-process-startup browser launch.
522 CommandLine
dummy(CommandLine::NO_PROGRAM
);
523 StartupBrowserCreatorImpl
launch(base::FilePath(), dummy
, &browser_creator
,
524 chrome::startup::IS_FIRST_RUN
);
525 ASSERT_TRUE(launch
.Launch(browser()->profile(), std::vector
<GURL
>(), false,
526 browser()->host_desktop_type()));
528 // This should have created a new browser window.
529 Browser
* new_browser
= FindOneOtherBrowser(browser());
530 ASSERT_TRUE(new_browser
);
532 TabStripModel
* tab_strip
= new_browser
->tab_strip_model();
533 if (signin::ShouldShowPromoAtStartup(browser()->profile(), true)) {
534 EXPECT_EQ(2, tab_strip
->count());
535 EXPECT_EQ(signin::GetPromoURL(signin::SOURCE_START_PAGE
, false),
536 tab_strip
->GetWebContentsAt(0)->GetURL());
537 EXPECT_EQ("title1.html",
538 tab_strip
->GetWebContentsAt(1)->GetURL().ExtractFileName());
540 EXPECT_EQ(1, tab_strip
->count());
541 EXPECT_EQ("title1.html",
542 tab_strip
->GetWebContentsAt(0)->GetURL().ExtractFileName());
546 // The welcome page should still be shown if there are more than 2 first run
547 // tabs, but the welcome page was explcitly added to the first run tabs.
548 IN_PROC_BROWSER_TEST_F(StartupBrowserCreatorTest
,
549 SyncPromoWithFirstRunTabsIncludingWelcomePage
) {
550 StartupBrowserCreator browser_creator
;
551 browser_creator
.AddFirstRunTab(test_server()->GetURL("files/title1.html"));
552 browser_creator
.AddFirstRunTab(GURL("http://welcome_page"));
554 // Do a simple non-process-startup browser launch.
555 CommandLine
dummy(CommandLine::NO_PROGRAM
);
556 StartupBrowserCreatorImpl
launch(base::FilePath(), dummy
, &browser_creator
,
557 chrome::startup::IS_FIRST_RUN
);
558 ASSERT_TRUE(launch
.Launch(browser()->profile(), std::vector
<GURL
>(), false,
559 browser()->host_desktop_type()));
561 // This should have created a new browser window.
562 Browser
* new_browser
= FindOneOtherBrowser(browser());
563 ASSERT_TRUE(new_browser
);
565 TabStripModel
* tab_strip
= new_browser
->tab_strip_model();
566 if (signin::ShouldShowPromoAtStartup(browser()->profile(), true)) {
567 EXPECT_EQ(3, tab_strip
->count());
568 EXPECT_EQ(signin::GetPromoURL(signin::SOURCE_START_PAGE
, false),
569 tab_strip
->GetWebContentsAt(0)->GetURL());
570 EXPECT_EQ("title1.html",
571 tab_strip
->GetWebContentsAt(1)->GetURL().ExtractFileName());
572 EXPECT_EQ(internals::GetWelcomePageURL(),
573 tab_strip
->GetWebContentsAt(2)->GetURL());
575 EXPECT_EQ(2, tab_strip
->count());
576 EXPECT_EQ("title1.html",
577 tab_strip
->GetWebContentsAt(0)->GetURL().ExtractFileName());
578 EXPECT_EQ(internals::GetWelcomePageURL(),
579 tab_strip
->GetWebContentsAt(1)->GetURL());
583 #if !defined(OS_CHROMEOS)
584 IN_PROC_BROWSER_TEST_F(StartupBrowserCreatorTest
, StartupURLsForTwoProfiles
) {
585 #if defined(OS_WIN) && defined(USE_ASH)
586 // Disable this test in Metro+Ash for now (http://crbug.com/262796).
587 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kAshBrowserTests
))
591 Profile
* default_profile
= browser()->profile();
593 ProfileManager
* profile_manager
= g_browser_process
->profile_manager();
594 // Create another profile.
595 base::FilePath dest_path
= profile_manager
->user_data_dir();
596 dest_path
= dest_path
.Append(FILE_PATH_LITERAL("New Profile 1"));
598 Profile
* other_profile
= profile_manager
->GetProfile(dest_path
);
599 ASSERT_TRUE(other_profile
);
601 // Use a couple arbitrary URLs.
602 std::vector
<GURL
> urls1
;
603 urls1
.push_back(ui_test_utils::GetTestUrl(
604 base::FilePath(base::FilePath::kCurrentDirectory
),
605 base::FilePath(FILE_PATH_LITERAL("title1.html"))));
606 std::vector
<GURL
> urls2
;
607 urls2
.push_back(ui_test_utils::GetTestUrl(
608 base::FilePath(base::FilePath::kCurrentDirectory
),
609 base::FilePath(FILE_PATH_LITERAL("title2.html"))));
611 // Set different startup preferences for the 2 profiles.
612 SessionStartupPref
pref1(SessionStartupPref::URLS
);
614 SessionStartupPref::SetStartupPref(default_profile
, pref1
);
615 SessionStartupPref
pref2(SessionStartupPref::URLS
);
617 SessionStartupPref::SetStartupPref(other_profile
, pref2
);
619 // Close the browser.
620 browser()->window()->Close();
622 // Do a simple non-process-startup browser launch.
623 CommandLine
dummy(CommandLine::NO_PROGRAM
);
626 StartupBrowserCreator browser_creator
;
627 std::vector
<Profile
*> last_opened_profiles
;
628 last_opened_profiles
.push_back(default_profile
);
629 last_opened_profiles
.push_back(other_profile
);
630 browser_creator
.Start(dummy
, profile_manager
->user_data_dir(),
631 default_profile
, last_opened_profiles
, &return_code
);
633 // urls1 were opened in a browser for default_profile, and urls2 were opened
634 // in a browser for other_profile.
635 Browser
* new_browser
= NULL
;
636 // |browser()| is still around at this point, even though we've closed its
637 // window. Thus the browser count for default_profile is 2.
638 ASSERT_EQ(2u, chrome::GetBrowserCount(default_profile
,
639 browser()->host_desktop_type()));
640 new_browser
= FindOneOtherBrowserForProfile(default_profile
, browser());
641 ASSERT_TRUE(new_browser
);
642 TabStripModel
* tab_strip
= new_browser
->tab_strip_model();
643 ASSERT_EQ(1, tab_strip
->count());
644 EXPECT_EQ(urls1
[0], tab_strip
->GetWebContentsAt(0)->GetURL());
646 ASSERT_EQ(1u, chrome::GetBrowserCount(other_profile
,
647 browser()->host_desktop_type()));
648 new_browser
= FindOneOtherBrowserForProfile(other_profile
, NULL
);
649 ASSERT_TRUE(new_browser
);
650 tab_strip
= new_browser
->tab_strip_model();
651 ASSERT_EQ(1, tab_strip
->count());
652 EXPECT_EQ(urls2
[0], tab_strip
->GetWebContentsAt(0)->GetURL());
655 IN_PROC_BROWSER_TEST_F(StartupBrowserCreatorTest
, PRE_UpdateWithTwoProfiles
) {
656 // Simulate a browser restart by creating the profiles in the PRE_ part.
657 ProfileManager
* profile_manager
= g_browser_process
->profile_manager();
659 // Create two profiles.
660 base::FilePath dest_path
= profile_manager
->user_data_dir();
662 Profile
* profile1
= profile_manager
->GetProfile(
663 dest_path
.Append(FILE_PATH_LITERAL("New Profile 1")));
664 ASSERT_TRUE(profile1
);
666 Profile
* profile2
= profile_manager
->GetProfile(
667 dest_path
.Append(FILE_PATH_LITERAL("New Profile 2")));
668 ASSERT_TRUE(profile2
);
670 // Use a couple arbitrary URLs.
671 std::vector
<GURL
> urls1
;
672 urls1
.push_back(ui_test_utils::GetTestUrl(
673 base::FilePath(base::FilePath::kCurrentDirectory
),
674 base::FilePath(FILE_PATH_LITERAL("title1.html"))));
675 std::vector
<GURL
> urls2
;
676 urls2
.push_back(ui_test_utils::GetTestUrl(
677 base::FilePath(base::FilePath::kCurrentDirectory
),
678 base::FilePath(FILE_PATH_LITERAL("title2.html"))));
680 // Set different startup preferences for the 2 profiles.
681 SessionStartupPref
pref1(SessionStartupPref::URLS
);
683 SessionStartupPref::SetStartupPref(profile1
, pref1
);
684 SessionStartupPref
pref2(SessionStartupPref::URLS
);
686 SessionStartupPref::SetStartupPref(profile2
, pref2
);
688 profile1
->GetPrefs()->CommitPendingWrite();
689 profile2
->GetPrefs()->CommitPendingWrite();
692 IN_PROC_BROWSER_TEST_F(StartupBrowserCreatorTest
, UpdateWithTwoProfiles
) {
693 #if defined(OS_WIN) && defined(USE_ASH)
694 // Disable this test in Metro+Ash for now (http://crbug.com/262796).
695 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kAshBrowserTests
))
699 // Make StartupBrowserCreator::WasRestarted() return true.
700 StartupBrowserCreator::was_restarted_read_
= false;
701 PrefService
* pref_service
= g_browser_process
->local_state();
702 pref_service
->SetBoolean(prefs::kWasRestarted
, true);
704 ProfileManager
* profile_manager
= g_browser_process
->profile_manager();
706 // Open the two profiles.
707 base::FilePath dest_path
= profile_manager
->user_data_dir();
709 Profile
* profile1
= profile_manager
->GetProfile(
710 dest_path
.Append(FILE_PATH_LITERAL("New Profile 1")));
711 ASSERT_TRUE(profile1
);
713 Profile
* profile2
= profile_manager
->GetProfile(
714 dest_path
.Append(FILE_PATH_LITERAL("New Profile 2")));
715 ASSERT_TRUE(profile2
);
717 // Simulate a launch after a browser update.
718 CommandLine
dummy(CommandLine::NO_PROGRAM
);
720 StartupBrowserCreator browser_creator
;
721 std::vector
<Profile
*> last_opened_profiles
;
722 last_opened_profiles
.push_back(profile1
);
723 last_opened_profiles
.push_back(profile2
);
724 browser_creator
.Start(dummy
, profile_manager
->user_data_dir(), profile1
,
725 last_opened_profiles
, &return_code
);
727 while (SessionRestore::IsRestoring(profile1
) ||
728 SessionRestore::IsRestoring(profile2
))
729 base::MessageLoop::current()->RunUntilIdle();
731 // The startup URLs are ignored, and instead the last open sessions are
733 EXPECT_TRUE(profile1
->restored_last_session());
734 EXPECT_TRUE(profile2
->restored_last_session());
736 Browser
* new_browser
= NULL
;
737 ASSERT_EQ(1u, chrome::GetBrowserCount(profile1
,
738 browser()->host_desktop_type()));
739 new_browser
= FindOneOtherBrowserForProfile(profile1
, NULL
);
740 ASSERT_TRUE(new_browser
);
741 TabStripModel
* tab_strip
= new_browser
->tab_strip_model();
742 ASSERT_EQ(1, tab_strip
->count());
743 EXPECT_EQ(GURL(content::kAboutBlankURL
),
744 tab_strip
->GetWebContentsAt(0)->GetURL());
746 ASSERT_EQ(1u, chrome::GetBrowserCount(profile2
,
747 browser()->host_desktop_type()));
748 new_browser
= FindOneOtherBrowserForProfile(profile2
, NULL
);
749 ASSERT_TRUE(new_browser
);
750 tab_strip
= new_browser
->tab_strip_model();
751 ASSERT_EQ(1, tab_strip
->count());
752 EXPECT_EQ(GURL(content::kAboutBlankURL
),
753 tab_strip
->GetWebContentsAt(0)->GetURL());
756 IN_PROC_BROWSER_TEST_F(StartupBrowserCreatorTest
,
757 ProfilesWithoutPagesNotLaunched
) {
758 #if defined(OS_WIN) && defined(USE_ASH)
759 // Disable this test in Metro+Ash for now (http://crbug.com/262796).
760 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kAshBrowserTests
))
764 Profile
* default_profile
= browser()->profile();
766 ProfileManager
* profile_manager
= g_browser_process
->profile_manager();
768 // Create 4 more profiles.
769 base::FilePath dest_path1
= profile_manager
->user_data_dir().Append(
770 FILE_PATH_LITERAL("New Profile 1"));
771 base::FilePath dest_path2
= profile_manager
->user_data_dir().Append(
772 FILE_PATH_LITERAL("New Profile 2"));
773 base::FilePath dest_path3
= profile_manager
->user_data_dir().Append(
774 FILE_PATH_LITERAL("New Profile 3"));
775 base::FilePath dest_path4
= profile_manager
->user_data_dir().Append(
776 FILE_PATH_LITERAL("New Profile 4"));
778 Profile
* profile_home1
= profile_manager
->GetProfile(dest_path1
);
779 ASSERT_TRUE(profile_home1
);
780 Profile
* profile_home2
= profile_manager
->GetProfile(dest_path2
);
781 ASSERT_TRUE(profile_home2
);
782 Profile
* profile_last
= profile_manager
->GetProfile(dest_path3
);
783 ASSERT_TRUE(profile_last
);
784 Profile
* profile_urls
= profile_manager
->GetProfile(dest_path4
);
785 ASSERT_TRUE(profile_urls
);
787 // Set the profiles to open urls, open last visited pages or display the home
789 SessionStartupPref
pref_home(SessionStartupPref::DEFAULT
);
790 SessionStartupPref::SetStartupPref(profile_home1
, pref_home
);
791 SessionStartupPref::SetStartupPref(profile_home2
, pref_home
);
793 SessionStartupPref
pref_last(SessionStartupPref::LAST
);
794 SessionStartupPref::SetStartupPref(profile_last
, pref_last
);
796 std::vector
<GURL
> urls
;
797 urls
.push_back(ui_test_utils::GetTestUrl(
798 base::FilePath(base::FilePath::kCurrentDirectory
),
799 base::FilePath(FILE_PATH_LITERAL("title1.html"))));
801 SessionStartupPref
pref_urls(SessionStartupPref::URLS
);
802 pref_urls
.urls
= urls
;
803 SessionStartupPref::SetStartupPref(profile_urls
, pref_urls
);
805 // Close the browser.
806 chrome::HostDesktopType original_desktop_type
=
807 browser()->host_desktop_type();
808 browser()->window()->Close();
810 // Do a simple non-process-startup browser launch.
811 CommandLine
dummy(CommandLine::NO_PROGRAM
);
814 StartupBrowserCreator browser_creator
;
815 std::vector
<Profile
*> last_opened_profiles
;
816 last_opened_profiles
.push_back(profile_home1
);
817 last_opened_profiles
.push_back(profile_home2
);
818 last_opened_profiles
.push_back(profile_last
);
819 last_opened_profiles
.push_back(profile_urls
);
820 browser_creator
.Start(dummy
, profile_manager
->user_data_dir(), profile_home1
,
821 last_opened_profiles
, &return_code
);
823 while (SessionRestore::IsRestoring(default_profile
) ||
824 SessionRestore::IsRestoring(profile_home1
) ||
825 SessionRestore::IsRestoring(profile_home2
) ||
826 SessionRestore::IsRestoring(profile_last
) ||
827 SessionRestore::IsRestoring(profile_urls
))
828 base::MessageLoop::current()->RunUntilIdle();
830 Browser
* new_browser
= NULL
;
831 // The last open profile (the profile_home1 in this case) will always be
832 // launched, even if it will open just the home page.
833 ASSERT_EQ(1u, chrome::GetBrowserCount(profile_home1
, original_desktop_type
));
834 new_browser
= FindOneOtherBrowserForProfile(profile_home1
, NULL
);
835 ASSERT_TRUE(new_browser
);
836 TabStripModel
* tab_strip
= new_browser
->tab_strip_model();
837 ASSERT_EQ(1, tab_strip
->count());
838 EXPECT_EQ(GURL(chrome::kChromeUINewTabURL
),
839 tab_strip
->GetWebContentsAt(0)->GetURL());
841 // profile_urls opened the urls.
842 ASSERT_EQ(1u, chrome::GetBrowserCount(profile_urls
, original_desktop_type
));
843 new_browser
= FindOneOtherBrowserForProfile(profile_urls
, NULL
);
844 ASSERT_TRUE(new_browser
);
845 tab_strip
= new_browser
->tab_strip_model();
846 ASSERT_EQ(1, tab_strip
->count());
847 EXPECT_EQ(urls
[0], tab_strip
->GetWebContentsAt(0)->GetURL());
849 // profile_last opened the last open pages.
850 ASSERT_EQ(1u, chrome::GetBrowserCount(profile_last
, original_desktop_type
));
851 new_browser
= FindOneOtherBrowserForProfile(profile_last
, NULL
);
852 ASSERT_TRUE(new_browser
);
853 tab_strip
= new_browser
->tab_strip_model();
854 ASSERT_EQ(1, tab_strip
->count());
855 EXPECT_EQ(GURL(content::kAboutBlankURL
),
856 tab_strip
->GetWebContentsAt(0)->GetURL());
858 // profile_home2 was not launched since it would've only opened the home page.
859 ASSERT_EQ(0u, chrome::GetBrowserCount(profile_home2
, original_desktop_type
));
862 IN_PROC_BROWSER_TEST_F(StartupBrowserCreatorTest
, ProfilesLaunchedAfterCrash
) {
863 #if defined(OS_WIN) && defined(USE_ASH)
864 // Disable this test in Metro+Ash for now (http://crbug.com/262796).
865 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kAshBrowserTests
))
869 // After an unclean exit, all profiles will be launched. However, they won't
870 // open any pages automatically.
872 ProfileManager
* profile_manager
= g_browser_process
->profile_manager();
874 // Create 3 profiles.
875 base::FilePath dest_path1
= profile_manager
->user_data_dir().Append(
876 FILE_PATH_LITERAL("New Profile 1"));
877 base::FilePath dest_path2
= profile_manager
->user_data_dir().Append(
878 FILE_PATH_LITERAL("New Profile 2"));
879 base::FilePath dest_path3
= profile_manager
->user_data_dir().Append(
880 FILE_PATH_LITERAL("New Profile 3"));
882 Profile
* profile_home
= profile_manager
->GetProfile(dest_path1
);
883 ASSERT_TRUE(profile_home
);
884 Profile
* profile_last
= profile_manager
->GetProfile(dest_path2
);
885 ASSERT_TRUE(profile_last
);
886 Profile
* profile_urls
= profile_manager
->GetProfile(dest_path3
);
887 ASSERT_TRUE(profile_urls
);
889 // Set the profiles to open the home page, last visited pages or URLs.
890 SessionStartupPref
pref_home(SessionStartupPref::DEFAULT
);
891 SessionStartupPref::SetStartupPref(profile_home
, pref_home
);
893 SessionStartupPref
pref_last(SessionStartupPref::LAST
);
894 SessionStartupPref::SetStartupPref(profile_last
, pref_last
);
896 std::vector
<GURL
> urls
;
897 urls
.push_back(ui_test_utils::GetTestUrl(
898 base::FilePath(base::FilePath::kCurrentDirectory
),
899 base::FilePath(FILE_PATH_LITERAL("title1.html"))));
901 SessionStartupPref
pref_urls(SessionStartupPref::URLS
);
902 pref_urls
.urls
= urls
;
903 SessionStartupPref::SetStartupPref(profile_urls
, pref_urls
);
905 // Simulate a launch after an unclear exit.
906 browser()->window()->Close();
907 static_cast<ProfileImpl
*>(profile_home
)->last_session_exit_type_
=
908 Profile::EXIT_CRASHED
;
909 static_cast<ProfileImpl
*>(profile_last
)->last_session_exit_type_
=
910 Profile::EXIT_CRASHED
;
911 static_cast<ProfileImpl
*>(profile_urls
)->last_session_exit_type_
=
912 Profile::EXIT_CRASHED
;
914 CommandLine
dummy(CommandLine::NO_PROGRAM
);
915 dummy
.AppendSwitchASCII(switches::kTestType
, "browser");
917 StartupBrowserCreator browser_creator
;
918 std::vector
<Profile
*> last_opened_profiles
;
919 last_opened_profiles
.push_back(profile_home
);
920 last_opened_profiles
.push_back(profile_last
);
921 last_opened_profiles
.push_back(profile_urls
);
922 browser_creator
.Start(dummy
, profile_manager
->user_data_dir(), profile_home
,
923 last_opened_profiles
, &return_code
);
925 // No profiles are getting restored, since they all display the crash info
927 EXPECT_FALSE(SessionRestore::IsRestoring(profile_home
));
928 EXPECT_FALSE(SessionRestore::IsRestoring(profile_last
));
929 EXPECT_FALSE(SessionRestore::IsRestoring(profile_urls
));
931 // The profile which normally opens the home page displays the new tab page.
932 Browser
* new_browser
= NULL
;
933 ASSERT_EQ(1u, chrome::GetBrowserCount(profile_home
,
934 browser()->host_desktop_type()));
935 new_browser
= FindOneOtherBrowserForProfile(profile_home
, NULL
);
936 ASSERT_TRUE(new_browser
);
937 TabStripModel
* tab_strip
= new_browser
->tab_strip_model();
938 ASSERT_EQ(1, tab_strip
->count());
939 content::WebContents
* web_contents
= tab_strip
->GetWebContentsAt(0);
940 EXPECT_EQ(GURL(chrome::kChromeUINewTabURL
), web_contents
->GetURL());
942 InfoBarService::FromWebContents(web_contents
)->infobar_count());
944 // The profile which normally opens last open pages displays the new tab page.
945 ASSERT_EQ(1u, chrome::GetBrowserCount(profile_last
,
946 browser()->host_desktop_type()));
947 new_browser
= FindOneOtherBrowserForProfile(profile_last
, NULL
);
948 ASSERT_TRUE(new_browser
);
949 tab_strip
= new_browser
->tab_strip_model();
950 ASSERT_EQ(1, tab_strip
->count());
951 web_contents
= tab_strip
->GetWebContentsAt(0);
952 EXPECT_EQ(GURL(chrome::kChromeUINewTabURL
), web_contents
->GetURL());
954 InfoBarService::FromWebContents(web_contents
)->infobar_count());
956 // The profile which normally opens URLs displays the new tab page.
957 ASSERT_EQ(1u, chrome::GetBrowserCount(profile_urls
,
958 browser()->host_desktop_type()));
959 new_browser
= FindOneOtherBrowserForProfile(profile_urls
, NULL
);
960 ASSERT_TRUE(new_browser
);
961 tab_strip
= new_browser
->tab_strip_model();
962 ASSERT_EQ(1, tab_strip
->count());
963 web_contents
= tab_strip
->GetWebContentsAt(0);
964 EXPECT_EQ(GURL(chrome::kChromeUINewTabURL
), web_contents
->GetURL());
966 InfoBarService::FromWebContents(web_contents
)->infobar_count());
969 class ManagedModeBrowserCreatorTest
: public InProcessBrowserTest
{
971 virtual void SetUpCommandLine(CommandLine
* command_line
) OVERRIDE
{
972 InProcessBrowserTest::SetUpCommandLine(command_line
);
973 command_line
->AppendSwitchASCII(switches::kManagedUserId
, "asdf");
977 IN_PROC_BROWSER_TEST_F(ManagedModeBrowserCreatorTest
,
978 StartupManagedModeProfile
) {
979 StartupBrowserCreator browser_creator
;
981 // Do a simple non-process-startup browser launch.
982 CommandLine
dummy(CommandLine::NO_PROGRAM
);
983 StartupBrowserCreatorImpl
launch(base::FilePath(), dummy
, &browser_creator
,
984 chrome::startup::IS_FIRST_RUN
);
985 content::WindowedNotificationObserver
observer(
986 content::NOTIFICATION_LOAD_STOP
,
987 content::NotificationService::AllSources());
988 ASSERT_TRUE(launch
.Launch(browser()->profile(), std::vector
<GURL
>(), false,
989 browser()->host_desktop_type()));
991 // This should have created a new browser window.
992 Browser
* new_browser
= FindOneOtherBrowser(browser());
993 ASSERT_TRUE(new_browser
);
995 TabStripModel
* tab_strip
= new_browser
->tab_strip_model();
996 // There should be only one tab.
997 EXPECT_EQ(1, tab_strip
->count());
1000 #endif // !defined(OS_CHROMEOS)
1002 // These tests are not applicable to Chrome OS as neither master_preferences nor
1003 // the sync promo exist there.
1004 #if !defined(OS_CHROMEOS)
1006 // On a branded Linux build, policy is required to suppress the first-run
1008 #if !defined(OS_LINUX) || !defined(GOOGLE_CHROME_BUILD) || \
1009 defined(ENABLE_CONFIGURATION_POLICY)
1011 class StartupBrowserCreatorFirstRunTest
: public InProcessBrowserTest
{
1013 virtual void SetUpCommandLine(CommandLine
* command_line
) OVERRIDE
;
1014 virtual void SetUpInProcessBrowserTestFixture() OVERRIDE
;
1016 #if defined(ENABLE_CONFIGURATION_POLICY)
1017 policy::MockConfigurationPolicyProvider provider_
;
1018 policy::PolicyMap policy_map_
;
1019 #endif // defined(ENABLE_CONFIGURATION_POLICY)
1022 void StartupBrowserCreatorFirstRunTest::SetUpCommandLine(
1023 CommandLine
* command_line
) {
1024 command_line
->AppendSwitch(switches::kForceFirstRun
);
1027 void StartupBrowserCreatorFirstRunTest::SetUpInProcessBrowserTestFixture() {
1028 #if defined(ENABLE_CONFIGURATION_POLICY)
1029 #if defined(OS_LINUX) && defined(GOOGLE_CHROME_BUILD)
1030 // Set a policy that prevents the first-run dialog from being shown.
1031 policy_map_
.Set(policy::key::kMetricsReportingEnabled
,
1032 policy::POLICY_LEVEL_MANDATORY
, policy::POLICY_SCOPE_USER
,
1033 base::Value::CreateBooleanValue(false), NULL
);
1034 provider_
.UpdateChromePolicy(policy_map_
);
1035 #endif // defined(OS_LINUX) && defined(GOOGLE_CHROME_BUILD)
1037 EXPECT_CALL(provider_
, IsInitializationComplete(_
))
1038 .WillRepeatedly(Return(true));
1039 policy::BrowserPolicyConnector::SetPolicyProviderForTesting(&provider_
);
1040 #endif // defined(ENABLE_CONFIGURATION_POLICY)
1043 #if defined(GOOGLE_CHROME_BUILD) && defined(OS_MACOSX)
1044 // http://crbug.com/314819
1045 #define MAYBE_SyncPromoForbidden DISABLED_SyncPromoForbidden
1047 #define MAYBE_SyncPromoForbidden SyncPromoForbidden
1049 IN_PROC_BROWSER_TEST_F(StartupBrowserCreatorFirstRunTest
,
1050 MAYBE_SyncPromoForbidden
) {
1051 // Consistently enable the welcome page on all platforms.
1052 first_run::SetShouldShowWelcomePage();
1054 // Simulate the following master_preferences:
1057 // "show_on_first_run_allowed": false
1060 StartupBrowserCreator browser_creator
;
1061 browser()->profile()->GetPrefs()->SetBoolean(
1062 prefs::kSignInPromoShowOnFirstRunAllowed
, false);
1064 // Do a process-startup browser launch.
1065 CommandLine
dummy(CommandLine::NO_PROGRAM
);
1066 StartupBrowserCreatorImpl
launch(base::FilePath(), dummy
, &browser_creator
,
1067 chrome::startup::IS_FIRST_RUN
);
1068 ASSERT_TRUE(launch
.Launch(browser()->profile(), std::vector
<GURL
>(), true,
1069 browser()->host_desktop_type()));
1071 // This should have created a new browser window.
1072 Browser
* new_browser
= FindOneOtherBrowser(browser());
1073 ASSERT_TRUE(new_browser
);
1075 // Verify that the NTP and the welcome page are shown.
1076 TabStripModel
* tab_strip
= new_browser
->tab_strip_model();
1077 ASSERT_EQ(2, tab_strip
->count());
1078 EXPECT_EQ(GURL(chrome::kChromeUINewTabURL
),
1079 tab_strip
->GetWebContentsAt(0)->GetURL());
1080 EXPECT_EQ(internals::GetWelcomePageURL(),
1081 tab_strip
->GetWebContentsAt(1)->GetURL());
1084 #if defined(GOOGLE_CHROME_BUILD) && defined(OS_MACOSX)
1085 // http://crbug.com/314819
1086 #define MAYBE_SyncPromoAllowed DISABLED_SyncPromoAllowed
1088 #define MAYBE_SyncPromoAllowed SyncPromoAllowed
1090 IN_PROC_BROWSER_TEST_F(StartupBrowserCreatorFirstRunTest
,
1091 MAYBE_SyncPromoAllowed
) {
1092 // Consistently enable the welcome page on all platforms.
1093 first_run::SetShouldShowWelcomePage();
1095 // Simulate the following master_preferences:
1098 // "show_on_first_run_allowed": true
1101 StartupBrowserCreator browser_creator
;
1102 browser()->profile()->GetPrefs()->SetBoolean(
1103 prefs::kSignInPromoShowOnFirstRunAllowed
, true);
1105 // Do a process-startup browser launch.
1106 CommandLine
dummy(CommandLine::NO_PROGRAM
);
1107 StartupBrowserCreatorImpl
launch(base::FilePath(), dummy
, &browser_creator
,
1108 chrome::startup::IS_FIRST_RUN
);
1109 ASSERT_TRUE(launch
.Launch(browser()->profile(), std::vector
<GURL
>(), true,
1110 browser()->host_desktop_type()));
1112 // This should have created a new browser window.
1113 Browser
* new_browser
= FindOneOtherBrowser(browser());
1114 ASSERT_TRUE(new_browser
);
1116 // Verify that the sync promo and the welcome page are shown.
1117 TabStripModel
* tab_strip
= new_browser
->tab_strip_model();
1118 ASSERT_EQ(2, tab_strip
->count());
1119 EXPECT_EQ(signin::GetPromoURL(signin::SOURCE_START_PAGE
, false),
1120 tab_strip
->GetWebContentsAt(0)->GetURL());
1121 EXPECT_EQ(internals::GetWelcomePageURL(),
1122 tab_strip
->GetWebContentsAt(1)->GetURL());
1125 #if defined(GOOGLE_CHROME_BUILD) && defined(OS_MACOSX)
1126 // http://crbug.com/314819
1127 #define MAYBE_FirstRunTabsPromoAllowed DISABLED_FirstRunTabsPromoAllowed
1129 #define MAYBE_FirstRunTabsPromoAllowed FirstRunTabsPromoAllowed
1131 IN_PROC_BROWSER_TEST_F(StartupBrowserCreatorFirstRunTest
,
1132 MAYBE_FirstRunTabsPromoAllowed
) {
1133 // Simulate the following master_preferences:
1135 // "first_run_tabs" : [
1136 // "files/title1.html"
1139 // "show_on_first_run_allowed": true
1142 StartupBrowserCreator browser_creator
;
1143 browser_creator
.AddFirstRunTab(test_server()->GetURL("files/title1.html"));
1144 browser()->profile()->GetPrefs()->SetBoolean(
1145 prefs::kSignInPromoShowOnFirstRunAllowed
, true);
1147 // Do a process-startup browser launch.
1148 CommandLine
dummy(CommandLine::NO_PROGRAM
);
1149 StartupBrowserCreatorImpl
launch(base::FilePath(), dummy
, &browser_creator
,
1150 chrome::startup::IS_FIRST_RUN
);
1151 ASSERT_TRUE(launch
.Launch(browser()->profile(), std::vector
<GURL
>(), true,
1152 browser()->host_desktop_type()));
1154 // This should have created a new browser window.
1155 Browser
* new_browser
= FindOneOtherBrowser(browser());
1156 ASSERT_TRUE(new_browser
);
1158 // Verify that the first-run tab is shown and the sync promo has been added.
1159 TabStripModel
* tab_strip
= new_browser
->tab_strip_model();
1160 ASSERT_EQ(2, tab_strip
->count());
1161 EXPECT_EQ(signin::GetPromoURL(signin::SOURCE_START_PAGE
, false),
1162 tab_strip
->GetWebContentsAt(0)->GetURL());
1163 EXPECT_EQ("title1.html",
1164 tab_strip
->GetWebContentsAt(1)->GetURL().ExtractFileName());
1167 #if defined(GOOGLE_CHROME_BUILD) && defined(OS_MACOSX)
1168 // http://crbug.com/314819
1169 #define MAYBE_FirstRunTabsContainSyncPromo \
1170 DISABLED_FirstRunTabsContainSyncPromo
1172 #define MAYBE_FirstRunTabsContainSyncPromo FirstRunTabsContainSyncPromo
1174 IN_PROC_BROWSER_TEST_F(StartupBrowserCreatorFirstRunTest
,
1175 MAYBE_FirstRunTabsContainSyncPromo
) {
1176 // Simulate the following master_preferences:
1178 // "first_run_tabs" : [
1179 // "files/title1.html",
1180 // "chrome://signin/?source=0&next_page=chrome%3A%2F%2Fnewtab%2F"
1183 // "show_on_first_run_allowed": true
1186 StartupBrowserCreator browser_creator
;
1187 browser_creator
.AddFirstRunTab(test_server()->GetURL("files/title1.html"));
1188 browser_creator
.AddFirstRunTab(signin::GetPromoURL(signin::SOURCE_START_PAGE
,
1190 browser()->profile()->GetPrefs()->SetBoolean(
1191 prefs::kSignInPromoShowOnFirstRunAllowed
, true);
1193 // Do a process-startup browser launch.
1194 CommandLine
dummy(CommandLine::NO_PROGRAM
);
1195 StartupBrowserCreatorImpl
launch(base::FilePath(), dummy
, &browser_creator
,
1196 chrome::startup::IS_FIRST_RUN
);
1197 ASSERT_TRUE(launch
.Launch(browser()->profile(), std::vector
<GURL
>(), true,
1198 browser()->host_desktop_type()));
1200 // This should have created a new browser window.
1201 Browser
* new_browser
= FindOneOtherBrowser(browser());
1202 ASSERT_TRUE(new_browser
);
1204 // Verify that the first-run tabs are shown and no sync promo has been added
1205 // as the first-run tabs contain it already.
1206 TabStripModel
* tab_strip
= new_browser
->tab_strip_model();
1207 ASSERT_EQ(2, tab_strip
->count());
1208 EXPECT_EQ("title1.html",
1209 tab_strip
->GetWebContentsAt(0)->GetURL().ExtractFileName());
1210 EXPECT_EQ(signin::GetPromoURL(signin::SOURCE_START_PAGE
, false),
1211 tab_strip
->GetWebContentsAt(1)->GetURL());
1214 #if defined(GOOGLE_CHROME_BUILD) && defined(OS_MACOSX)
1215 // http://crbug.com/314819
1216 #define MAYBE_FirstRunTabsContainNTPSyncPromoAllowed \
1217 DISABLED_FirstRunTabsContainNTPSyncPromoAllowed
1219 #define MAYBE_FirstRunTabsContainNTPSyncPromoAllowed \
1220 FirstRunTabsContainNTPSyncPromoAllowed
1222 IN_PROC_BROWSER_TEST_F(StartupBrowserCreatorFirstRunTest
,
1223 MAYBE_FirstRunTabsContainNTPSyncPromoAllowed
) {
1224 // Simulate the following master_preferences:
1226 // "first_run_tabs" : [
1228 // "files/title1.html"
1231 // "show_on_first_run_allowed": true
1234 StartupBrowserCreator browser_creator
;
1235 browser_creator
.AddFirstRunTab(GURL("new_tab_page"));
1236 browser_creator
.AddFirstRunTab(test_server()->GetURL("files/title1.html"));
1237 browser()->profile()->GetPrefs()->SetBoolean(
1238 prefs::kSignInPromoShowOnFirstRunAllowed
, true);
1240 // Do a process-startup browser launch.
1241 CommandLine
dummy(CommandLine::NO_PROGRAM
);
1242 StartupBrowserCreatorImpl
launch(base::FilePath(), dummy
, &browser_creator
,
1243 chrome::startup::IS_FIRST_RUN
);
1244 ASSERT_TRUE(launch
.Launch(browser()->profile(), std::vector
<GURL
>(), true,
1245 browser()->host_desktop_type()));
1247 // This should have created a new browser window.
1248 Browser
* new_browser
= FindOneOtherBrowser(browser());
1249 ASSERT_TRUE(new_browser
);
1251 // Verify that the first-run tabs are shown but the NTP that they contain has
1252 // been replaced by the sync promo.
1253 TabStripModel
* tab_strip
= new_browser
->tab_strip_model();
1254 ASSERT_EQ(2, tab_strip
->count());
1255 EXPECT_EQ(signin::GetPromoURL(signin::SOURCE_START_PAGE
, false),
1256 tab_strip
->GetWebContentsAt(0)->GetURL());
1257 EXPECT_EQ("title1.html",
1258 tab_strip
->GetWebContentsAt(1)->GetURL().ExtractFileName());
1261 #if defined(GOOGLE_CHROME_BUILD) && defined(OS_MACOSX)
1262 // http://crbug.com/314819
1263 #define MAYBE_FirstRunTabsContainNTPSyncPromoForbidden \
1264 DISABLED_FirstRunTabsContainNTPSyncPromoForbidden
1266 #define MAYBE_FirstRunTabsContainNTPSyncPromoForbidden \
1267 FirstRunTabsContainNTPSyncPromoForbidden
1269 IN_PROC_BROWSER_TEST_F(StartupBrowserCreatorFirstRunTest
,
1270 MAYBE_FirstRunTabsContainNTPSyncPromoForbidden
) {
1271 // Simulate the following master_preferences:
1273 // "first_run_tabs" : [
1275 // "files/title1.html"
1278 // "show_on_first_run_allowed": false
1281 StartupBrowserCreator browser_creator
;
1282 browser_creator
.AddFirstRunTab(GURL("new_tab_page"));
1283 browser_creator
.AddFirstRunTab(test_server()->GetURL("files/title1.html"));
1284 browser()->profile()->GetPrefs()->SetBoolean(
1285 prefs::kSignInPromoShowOnFirstRunAllowed
, false);
1287 // Do a process-startup browser launch.
1288 CommandLine
dummy(CommandLine::NO_PROGRAM
);
1289 StartupBrowserCreatorImpl
launch(base::FilePath(), dummy
, &browser_creator
,
1290 chrome::startup::IS_FIRST_RUN
);
1291 ASSERT_TRUE(launch
.Launch(browser()->profile(), std::vector
<GURL
>(), true,
1292 browser()->host_desktop_type()));
1294 // This should have created a new browser window.
1295 Browser
* new_browser
= FindOneOtherBrowser(browser());
1296 ASSERT_TRUE(new_browser
);
1298 // Verify that the first-run tabs are shown, the NTP that they contain has not
1299 // not been replaced by the sync promo and no sync promo has been added.
1300 TabStripModel
* tab_strip
= new_browser
->tab_strip_model();
1301 ASSERT_EQ(2, tab_strip
->count());
1302 EXPECT_EQ(GURL(chrome::kChromeUINewTabURL
),
1303 tab_strip
->GetWebContentsAt(0)->GetURL());
1304 EXPECT_EQ("title1.html",
1305 tab_strip
->GetWebContentsAt(1)->GetURL().ExtractFileName());
1308 #if defined(GOOGLE_CHROME_BUILD) && defined(OS_MACOSX)
1309 // http://crbug.com/314819
1310 #define MAYBE_FirstRunTabsSyncPromoForbidden \
1311 DISABLED_FirstRunTabsSyncPromoForbidden
1313 #define MAYBE_FirstRunTabsSyncPromoForbidden FirstRunTabsSyncPromoForbidden
1315 IN_PROC_BROWSER_TEST_F(StartupBrowserCreatorFirstRunTest
,
1316 MAYBE_FirstRunTabsSyncPromoForbidden
) {
1317 // Simulate the following master_preferences:
1319 // "first_run_tabs" : [
1320 // "files/title1.html"
1323 // "show_on_first_run_allowed": false
1326 StartupBrowserCreator browser_creator
;
1327 browser_creator
.AddFirstRunTab(test_server()->GetURL("files/title1.html"));
1328 browser()->profile()->GetPrefs()->SetBoolean(
1329 prefs::kSignInPromoShowOnFirstRunAllowed
, false);
1331 // Do a process-startup browser launch.
1332 CommandLine
dummy(CommandLine::NO_PROGRAM
);
1333 StartupBrowserCreatorImpl
launch(base::FilePath(), dummy
, &browser_creator
,
1334 chrome::startup::IS_FIRST_RUN
);
1335 ASSERT_TRUE(launch
.Launch(browser()->profile(), std::vector
<GURL
>(), true,
1336 browser()->host_desktop_type()));
1338 // This should have created a new browser window.
1339 Browser
* new_browser
= FindOneOtherBrowser(browser());
1340 ASSERT_TRUE(new_browser
);
1342 // Verify that the first-run tab is shown and no sync promo has been added.
1343 TabStripModel
* tab_strip
= new_browser
->tab_strip_model();
1344 ASSERT_EQ(1, tab_strip
->count());
1345 EXPECT_EQ("title1.html",
1346 tab_strip
->GetWebContentsAt(0)->GetURL().ExtractFileName());
1349 #if defined(ENABLE_CONFIGURATION_POLICY)
1350 #if defined(GOOGLE_CHROME_BUILD) && defined(OS_MACOSX)
1351 // http://crbug.com/314819
1352 #define MAYBE_RestoreOnStartupURLsPolicySpecified \
1353 DISABLED_RestoreOnStartupURLsPolicySpecified
1355 #define MAYBE_RestoreOnStartupURLsPolicySpecified \
1356 RestoreOnStartupURLsPolicySpecified
1358 IN_PROC_BROWSER_TEST_F(StartupBrowserCreatorFirstRunTest
,
1359 MAYBE_RestoreOnStartupURLsPolicySpecified
) {
1360 // Simulate the following master_preferences:
1363 // "show_on_first_run_allowed": true
1366 StartupBrowserCreator browser_creator
;
1367 browser()->profile()->GetPrefs()->SetBoolean(
1368 prefs::kSignInPromoShowOnFirstRunAllowed
, true);
1370 // Set the following user policies:
1371 // * RestoreOnStartup = RestoreOnStartupIsURLs
1372 // * RestoreOnStartupURLs = [ "files/title1.html" ]
1373 policy_map_
.Set(policy::key::kRestoreOnStartup
,
1374 policy::POLICY_LEVEL_MANDATORY
, policy::POLICY_SCOPE_USER
,
1375 base::Value::CreateIntegerValue(
1376 SessionStartupPref::kPrefValueURLs
),
1378 base::ListValue startup_urls
;
1379 startup_urls
.Append(base::Value::CreateStringValue(
1380 test_server()->GetURL("files/title1.html").spec()));
1381 policy_map_
.Set(policy::key::kRestoreOnStartupURLs
,
1382 policy::POLICY_LEVEL_MANDATORY
, policy::POLICY_SCOPE_USER
,
1383 startup_urls
.DeepCopy(), NULL
);
1384 provider_
.UpdateChromePolicy(policy_map_
);
1385 base::RunLoop().RunUntilIdle();
1387 // Do a process-startup browser launch.
1388 CommandLine
dummy(CommandLine::NO_PROGRAM
);
1389 StartupBrowserCreatorImpl
launch(base::FilePath(), dummy
, &browser_creator
,
1390 chrome::startup::IS_FIRST_RUN
);
1391 ASSERT_TRUE(launch
.Launch(browser()->profile(), std::vector
<GURL
>(), true,
1392 browser()->host_desktop_type()));
1394 // This should have created a new browser window.
1395 Browser
* new_browser
= FindOneOtherBrowser(browser());
1396 ASSERT_TRUE(new_browser
);
1398 // Verify that the URL specified through policy is shown and no sync promo has
1400 TabStripModel
* tab_strip
= new_browser
->tab_strip_model();
1401 ASSERT_EQ(1, tab_strip
->count());
1402 EXPECT_EQ("title1.html",
1403 tab_strip
->GetWebContentsAt(0)->GetURL().ExtractFileName());
1405 #endif // defined(ENABLE_CONFIGURATION_POLICY)
1407 #endif // !defined(OS_LINUX) || !defined(GOOGLE_CHROME_BUILD) ||
1408 // defined(ENABLE_CONFIGURATION_POLICY)
1410 #endif // !defined(OS_CHROMEOS)