Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / startup / startup_browser_creator_browsertest.cc
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.
4
5 #include <algorithm>
6 #include <string>
7
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/launch_util.h"
16 #include "chrome/browser/first_run/first_run.h"
17 #include "chrome/browser/infobars/infobar_service.h"
18 #include "chrome/browser/managed_mode/managed_mode_navigation_observer.h"
19 #include "chrome/browser/managed_mode/managed_user_service.h"
20 #include "chrome/browser/managed_mode/managed_user_service_factory.h"
21 #include "chrome/browser/prefs/session_startup_pref.h"
22 #include "chrome/browser/profiles/profile.h"
23 #include "chrome/browser/profiles/profile_impl.h"
24 #include "chrome/browser/profiles/profile_manager.h"
25 #include "chrome/browser/sessions/session_restore.h"
26 #include "chrome/browser/signin/signin_promo.h"
27 #include "chrome/browser/ui/browser.h"
28 #include "chrome/browser/ui/browser_finder.h"
29 #include "chrome/browser/ui/browser_iterator.h"
30 #include "chrome/browser/ui/browser_list.h"
31 #include "chrome/browser/ui/browser_list_observer.h"
32 #include "chrome/browser/ui/browser_window.h"
33 #include "chrome/browser/ui/host_desktop.h"
34 #include "chrome/browser/ui/startup/startup_browser_creator.h"
35 #include "chrome/browser/ui/startup/startup_browser_creator_impl.h"
36 #include "chrome/browser/ui/tabs/tab_strip_model.h"
37 #include "chrome/common/chrome_switches.h"
38 #include "chrome/common/extensions/extension_constants.h"
39 #include "chrome/common/pref_names.h"
40 #include "chrome/common/url_constants.h"
41 #include "chrome/test/base/in_process_browser_test.h"
42 #include "chrome/test/base/test_switches.h"
43 #include "chrome/test/base/ui_test_utils.h"
44 #include "content/public/browser/web_contents.h"
45 #include "extensions/browser/extension_system.h"
46 #include "testing/gtest/include/gtest/gtest.h"
47 #include "url/gurl.h"
48
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 "components/policy/core/browser/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"
60
61 using testing::_;
62 using testing::Return;
63 #endif  // defined(ENABLE_CONFIGURATION_POLICY) && !defined(OS_CHROMEOS)
64
65 using extensions::Extension;
66
67 namespace {
68
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()));
74
75   // Find the new browser.
76   Browser* other_browser = NULL;
77   for (chrome::BrowserIterator it; !it.done() && !other_browser; it.Next()) {
78     if (*it != browser)
79       other_browser = *it;
80   }
81   return other_browser;
82 }
83
84 }  // namespace
85
86 class StartupBrowserCreatorTest : public ExtensionBrowserTest {
87  protected:
88   virtual bool SetUpUserDataDirectory() OVERRIDE {
89     return ExtensionBrowserTest::SetUpUserDataDirectory();
90   }
91
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);
100 #endif
101   }
102
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())));
109
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);
115
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()));
119   }
120
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);
126   }
127
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)
132         return *it;
133     }
134     return NULL;
135   }
136 };
137
138 class OpenURLsPopupObserver : public chrome::BrowserListObserver {
139  public:
140   OpenURLsPopupObserver() : added_browser_(NULL) { }
141
142   virtual void OnBrowserAdded(Browser* browser) OVERRIDE {
143     added_browser_ = browser;
144   }
145
146   virtual void OnBrowserRemoved(Browser* browser) OVERRIDE { }
147
148   Browser* added_browser_;
149 };
150
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"));
157
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
161   // testing sessions.
162
163   OpenURLsPopupObserver observer;
164   BrowserList::AddObserver(&observer);
165
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_);
171
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);
181 }
182
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"));
197
198   // Set the startup preference to open these URLs.
199   SessionStartupPref pref(SessionStartupPref::URLS);
200   pref.urls = urls;
201   SessionStartupPref::SetStartupPref(browser()->profile(), pref);
202
203   // Close the browser.
204   browser()->window()->Close();
205
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()));
213
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);
218
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());
224   }
225
226   // The two tabs, despite having the same site, should be in different
227   // SiteInstances.
228   EXPECT_NE(tab_strip->GetWebContentsAt(0)->GetSiteInstance(),
229             tab_strip->GetWebContentsAt(1)->GetSiteInstance());
230 }
231
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
234 // new browser.
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"))));
245
246   // Set the startup preference to open these URLs.
247   SessionStartupPref pref(SessionStartupPref::URLS);
248   pref.urls = urls;
249   SessionStartupPref::SetStartupPref(browser()->profile(), pref);
250
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()));
258
259   // This should have created a new browser window.
260   Browser* new_browser = FindOneOtherBrowser(browser());
261   ASSERT_TRUE(new_browser);
262
263   // The new browser should have exactly one tab (not the startup URLs).
264   ASSERT_EQ(1, new_browser->tab_strip_model()->count());
265 }
266
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));
273
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());
277
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()));
283
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);
288
289   // Expect an app window.
290   EXPECT_TRUE(new_browser->is_app());
291
292   // The browser's app_name should include the app's ID.
293   EXPECT_NE(
294       new_browser->app_name_.find(extension_app->id()),
295       std::string::npos) << new_browser->app_name_;
296 }
297
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));
301
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);
304
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()));
312
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
315   // browser.
316   Browser* new_browser = FindOneOtherBrowser(browser());
317   ASSERT_TRUE(new_browser);
318
319   // Expect an app window.
320   EXPECT_TRUE(new_browser->is_app());
321
322   // The browser's app_name should include the app's ID.
323   EXPECT_NE(
324       new_browser->app_name_.find(extension_app->id()),
325       std::string::npos) << new_browser->app_name_;
326 }
327
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));
332
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);
335
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()));
343
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()));
348
349   Browser* new_browser = FindOneOtherBrowser(browser());
350   ASSERT_TRUE(new_browser);
351
352   // The tab should be in a tabbed window.
353   EXPECT_TRUE(new_browser->is_type_tabbed());
354
355   // The browser's app_name should not include the app's ID: It is in a
356   // normal browser.
357   EXPECT_EQ(
358       new_browser->app_name_.find(extension_app->id()),
359       std::string::npos) << new_browser->app_name_;
360 }
361
362 #endif  // !defined(OS_MACOSX)
363
364 #endif  // !defined(OS_CHROMEOS)
365
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());
376 }
377
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());
388 }
389
390 // Fails on official builds. See http://crbug.com/313856
391 #if defined(GOOGLE_CHROME_BUILD)
392 #define MAYBE_AddFirstRunTab DISABLED_AddFirstRunTab
393 #else
394 #define MAYBE_AddFirstRunTab AddFirstRunTab
395 #endif
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"));
400
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()));
407
408   // This should have created a new browser window.
409   Browser* new_browser = FindOneOtherBrowser(browser());
410   ASSERT_TRUE(new_browser);
411
412   TabStripModel* tab_strip = new_browser->tab_strip_model();
413   EXPECT_EQ(2, tab_strip->count());
414
415   EXPECT_EQ("title1.html",
416             tab_strip->GetWebContentsAt(0)->GetURL().ExtractFileName());
417   EXPECT_EQ("title2.html",
418             tab_strip->GetWebContentsAt(1)->GetURL().ExtractFileName());
419 }
420
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
426 #else
427 #define MAYBE_AddCustomFirstRunTab AddCustomFirstRunTab
428 #endif
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"));
435
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()));
442
443   // This should have created a new browser window.
444   Browser* new_browser = FindOneOtherBrowser(browser());
445   ASSERT_TRUE(new_browser);
446
447   TabStripModel* tab_strip = new_browser->tab_strip_model();
448   EXPECT_EQ(4, tab_strip->count());
449
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());
458 }
459
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()));
467
468   // This should have created a new browser window.
469   Browser* new_browser = FindOneOtherBrowser(browser());
470   ASSERT_TRUE(new_browser);
471
472   TabStripModel* tab_strip = new_browser->tab_strip_model();
473   EXPECT_EQ(1, tab_strip->count());
474
475   if (signin::ShouldShowPromoAtStartup(browser()->profile(), true)) {
476     EXPECT_EQ(signin::GetPromoURL(signin::SOURCE_START_PAGE, false),
477               tab_strip->GetWebContentsAt(0)->GetURL());
478   } else {
479     EXPECT_EQ(GURL(chrome::kChromeUINewTabURL),
480               tab_strip->GetWebContentsAt(0)->GetURL());
481   }
482 }
483
484 IN_PROC_BROWSER_TEST_F(StartupBrowserCreatorTest, SyncPromoWithWelcomePage) {
485   first_run::SetShouldShowWelcomePage();
486
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()));
493
494   // This should have created a new browser window.
495   Browser* new_browser = FindOneOtherBrowser(browser());
496   ASSERT_TRUE(new_browser);
497
498   TabStripModel* tab_strip = new_browser->tab_strip_model();
499   EXPECT_EQ(2, tab_strip->count());
500
501   if (signin::ShouldShowPromoAtStartup(browser()->profile(), true)) {
502     EXPECT_EQ(signin::GetPromoURL(signin::SOURCE_START_PAGE, false),
503               tab_strip->GetWebContentsAt(0)->GetURL());
504   } else {
505     EXPECT_EQ(GURL(chrome::kChromeUINewTabURL),
506               tab_strip->GetWebContentsAt(0)->GetURL());
507   }
508   EXPECT_EQ(internals::GetWelcomePageURL(),
509             tab_strip->GetWebContentsAt(1)->GetURL());
510 }
511
512 IN_PROC_BROWSER_TEST_F(StartupBrowserCreatorTest, SyncPromoWithFirstRunTabs) {
513   StartupBrowserCreator browser_creator;
514   browser_creator.AddFirstRunTab(test_server()->GetURL("files/title1.html"));
515
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();
520
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()));
527
528   // This should have created a new browser window.
529   Browser* new_browser = FindOneOtherBrowser(browser());
530   ASSERT_TRUE(new_browser);
531
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());
539   } else {
540     EXPECT_EQ(1, tab_strip->count());
541     EXPECT_EQ("title1.html",
542               tab_strip->GetWebContentsAt(0)->GetURL().ExtractFileName());
543   }
544 }
545
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"));
553
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()));
560
561   // This should have created a new browser window.
562   Browser* new_browser = FindOneOtherBrowser(browser());
563   ASSERT_TRUE(new_browser);
564
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());
574   } else {
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());
580   }
581 }
582
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))
588     return;
589 #endif
590
591   Profile* default_profile = browser()->profile();
592
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"));
597
598   Profile* other_profile = profile_manager->GetProfile(dest_path);
599   ASSERT_TRUE(other_profile);
600
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"))));
610
611   // Set different startup preferences for the 2 profiles.
612   SessionStartupPref pref1(SessionStartupPref::URLS);
613   pref1.urls = urls1;
614   SessionStartupPref::SetStartupPref(default_profile, pref1);
615   SessionStartupPref pref2(SessionStartupPref::URLS);
616   pref2.urls = urls2;
617   SessionStartupPref::SetStartupPref(other_profile, pref2);
618
619   // Close the browser.
620   browser()->window()->Close();
621
622   // Do a simple non-process-startup browser launch.
623   CommandLine dummy(CommandLine::NO_PROGRAM);
624
625   int return_code;
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);
632
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());
645
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());
653 }
654
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();
658
659   // Create two profiles.
660   base::FilePath dest_path = profile_manager->user_data_dir();
661
662   Profile* profile1 = profile_manager->GetProfile(
663       dest_path.Append(FILE_PATH_LITERAL("New Profile 1")));
664   ASSERT_TRUE(profile1);
665
666   Profile* profile2 = profile_manager->GetProfile(
667       dest_path.Append(FILE_PATH_LITERAL("New Profile 2")));
668   ASSERT_TRUE(profile2);
669
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"))));
679
680   // Set different startup preferences for the 2 profiles.
681   SessionStartupPref pref1(SessionStartupPref::URLS);
682   pref1.urls = urls1;
683   SessionStartupPref::SetStartupPref(profile1, pref1);
684   SessionStartupPref pref2(SessionStartupPref::URLS);
685   pref2.urls = urls2;
686   SessionStartupPref::SetStartupPref(profile2, pref2);
687
688   profile1->GetPrefs()->CommitPendingWrite();
689   profile2->GetPrefs()->CommitPendingWrite();
690 }
691
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))
696     return;
697 #endif
698
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);
703
704   ProfileManager* profile_manager = g_browser_process->profile_manager();
705
706   // Open the two profiles.
707   base::FilePath dest_path = profile_manager->user_data_dir();
708
709   Profile* profile1 = profile_manager->GetProfile(
710       dest_path.Append(FILE_PATH_LITERAL("New Profile 1")));
711   ASSERT_TRUE(profile1);
712
713   Profile* profile2 = profile_manager->GetProfile(
714       dest_path.Append(FILE_PATH_LITERAL("New Profile 2")));
715   ASSERT_TRUE(profile2);
716
717   // Simulate a launch after a browser update.
718   CommandLine dummy(CommandLine::NO_PROGRAM);
719   int return_code;
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);
726
727   while (SessionRestore::IsRestoring(profile1) ||
728          SessionRestore::IsRestoring(profile2))
729     base::MessageLoop::current()->RunUntilIdle();
730
731   // The startup URLs are ignored, and instead the last open sessions are
732   // restored.
733   EXPECT_TRUE(profile1->restored_last_session());
734   EXPECT_TRUE(profile2->restored_last_session());
735
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());
745
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());
754 }
755
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))
761     return;
762 #endif
763
764   Profile* default_profile = browser()->profile();
765
766   ProfileManager* profile_manager = g_browser_process->profile_manager();
767
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"));
777
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);
786
787   // Set the profiles to open urls, open last visited pages or display the home
788   // page.
789   SessionStartupPref pref_home(SessionStartupPref::DEFAULT);
790   SessionStartupPref::SetStartupPref(profile_home1, pref_home);
791   SessionStartupPref::SetStartupPref(profile_home2, pref_home);
792
793   SessionStartupPref pref_last(SessionStartupPref::LAST);
794   SessionStartupPref::SetStartupPref(profile_last, pref_last);
795
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"))));
800
801   SessionStartupPref pref_urls(SessionStartupPref::URLS);
802   pref_urls.urls = urls;
803   SessionStartupPref::SetStartupPref(profile_urls, pref_urls);
804
805   // Close the browser.
806   chrome::HostDesktopType original_desktop_type =
807       browser()->host_desktop_type();
808   browser()->window()->Close();
809
810   // Do a simple non-process-startup browser launch.
811   CommandLine dummy(CommandLine::NO_PROGRAM);
812
813   int return_code;
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);
822
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();
829
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());
840
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());
848
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());
857
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));
860 }
861
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))
866     return;
867 #endif
868
869   // After an unclean exit, all profiles will be launched. However, they won't
870   // open any pages automatically.
871
872   ProfileManager* profile_manager = g_browser_process->profile_manager();
873
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"));
881
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);
888
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);
892
893   SessionStartupPref pref_last(SessionStartupPref::LAST);
894   SessionStartupPref::SetStartupPref(profile_last, pref_last);
895
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"))));
900
901   SessionStartupPref pref_urls(SessionStartupPref::URLS);
902   pref_urls.urls = urls;
903   SessionStartupPref::SetStartupPref(profile_urls, pref_urls);
904
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;
913
914   CommandLine dummy(CommandLine::NO_PROGRAM);
915   dummy.AppendSwitchASCII(switches::kTestType, "browser");
916   int return_code;
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);
924
925   // No profiles are getting restored, since they all display the crash info
926   // bar.
927   EXPECT_FALSE(SessionRestore::IsRestoring(profile_home));
928   EXPECT_FALSE(SessionRestore::IsRestoring(profile_last));
929   EXPECT_FALSE(SessionRestore::IsRestoring(profile_urls));
930
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());
941   InfoBarService* infobar_service =
942       InfoBarService::FromWebContents(web_contents);
943   EXPECT_EQ(1U, infobar_service->infobar_count());
944
945   // The profile which normally opens last open pages displays the new tab page.
946   ASSERT_EQ(1u, chrome::GetBrowserCount(profile_last,
947                                         browser()->host_desktop_type()));
948   new_browser = FindOneOtherBrowserForProfile(profile_last, NULL);
949   ASSERT_TRUE(new_browser);
950   tab_strip = new_browser->tab_strip_model();
951   ASSERT_EQ(1, tab_strip->count());
952   web_contents = tab_strip->GetWebContentsAt(0);
953   EXPECT_EQ(GURL(chrome::kChromeUINewTabURL), web_contents->GetURL());
954   infobar_service = InfoBarService::FromWebContents(web_contents);
955   EXPECT_EQ(1U, infobar_service->infobar_count());
956
957   // The profile which normally opens URLs displays the new tab page.
958   ASSERT_EQ(1u, chrome::GetBrowserCount(profile_urls,
959                                         browser()->host_desktop_type()));
960   new_browser = FindOneOtherBrowserForProfile(profile_urls, NULL);
961   ASSERT_TRUE(new_browser);
962   tab_strip = new_browser->tab_strip_model();
963   ASSERT_EQ(1, tab_strip->count());
964   web_contents = tab_strip->GetWebContentsAt(0);
965   EXPECT_EQ(GURL(chrome::kChromeUINewTabURL), web_contents->GetURL());
966   infobar_service = InfoBarService::FromWebContents(web_contents);
967   EXPECT_EQ(1U, infobar_service->infobar_count());
968 }
969
970 class ManagedModeBrowserCreatorTest : public InProcessBrowserTest {
971  protected:
972   virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
973     InProcessBrowserTest::SetUpCommandLine(command_line);
974     command_line->AppendSwitchASCII(switches::kManagedUserId, "asdf");
975   }
976 };
977
978 IN_PROC_BROWSER_TEST_F(ManagedModeBrowserCreatorTest,
979                        StartupManagedModeProfile) {
980   StartupBrowserCreator browser_creator;
981
982   // Do a simple non-process-startup browser launch.
983   CommandLine dummy(CommandLine::NO_PROGRAM);
984   StartupBrowserCreatorImpl launch(base::FilePath(), dummy, &browser_creator,
985                                    chrome::startup::IS_FIRST_RUN);
986   content::WindowedNotificationObserver observer(
987       content::NOTIFICATION_LOAD_STOP,
988       content::NotificationService::AllSources());
989   ASSERT_TRUE(launch.Launch(browser()->profile(), std::vector<GURL>(), false,
990                             browser()->host_desktop_type()));
991
992   // This should have created a new browser window.
993   Browser* new_browser = FindOneOtherBrowser(browser());
994   ASSERT_TRUE(new_browser);
995
996   TabStripModel* tab_strip = new_browser->tab_strip_model();
997   // There should be only one tab.
998   EXPECT_EQ(1, tab_strip->count());
999 }
1000
1001 #endif  // !defined(OS_CHROMEOS)
1002
1003 // These tests are not applicable to Chrome OS as neither master_preferences nor
1004 // the sync promo exist there.
1005 #if !defined(OS_CHROMEOS)
1006
1007 // On a branded Linux build, policy is required to suppress the first-run
1008 // dialog.
1009 #if !defined(OS_LINUX) || !defined(GOOGLE_CHROME_BUILD) || \
1010     defined(ENABLE_CONFIGURATION_POLICY)
1011
1012 class StartupBrowserCreatorFirstRunTest : public InProcessBrowserTest {
1013  protected:
1014   virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE;
1015   virtual void SetUpInProcessBrowserTestFixture() OVERRIDE;
1016
1017 #if defined(ENABLE_CONFIGURATION_POLICY)
1018   policy::MockConfigurationPolicyProvider provider_;
1019   policy::PolicyMap policy_map_;
1020 #endif  // defined(ENABLE_CONFIGURATION_POLICY)
1021 };
1022
1023 void StartupBrowserCreatorFirstRunTest::SetUpCommandLine(
1024     CommandLine* command_line) {
1025   command_line->AppendSwitch(switches::kForceFirstRun);
1026 }
1027
1028 void StartupBrowserCreatorFirstRunTest::SetUpInProcessBrowserTestFixture() {
1029 #if defined(ENABLE_CONFIGURATION_POLICY)
1030 #if defined(OS_LINUX) && defined(GOOGLE_CHROME_BUILD)
1031   // Set a policy that prevents the first-run dialog from being shown.
1032   policy_map_.Set(policy::key::kMetricsReportingEnabled,
1033                   policy::POLICY_LEVEL_MANDATORY, policy::POLICY_SCOPE_USER,
1034                   base::Value::CreateBooleanValue(false), NULL);
1035   provider_.UpdateChromePolicy(policy_map_);
1036 #endif  // defined(OS_LINUX) && defined(GOOGLE_CHROME_BUILD)
1037
1038   EXPECT_CALL(provider_, IsInitializationComplete(_))
1039       .WillRepeatedly(Return(true));
1040   policy::BrowserPolicyConnector::SetPolicyProviderForTesting(&provider_);
1041 #endif  // defined(ENABLE_CONFIGURATION_POLICY)
1042 }
1043
1044 #if defined(GOOGLE_CHROME_BUILD) && defined(OS_MACOSX)
1045 // http://crbug.com/314819
1046 #define MAYBE_SyncPromoForbidden DISABLED_SyncPromoForbidden
1047 #else
1048 #define MAYBE_SyncPromoForbidden SyncPromoForbidden
1049 #endif
1050 IN_PROC_BROWSER_TEST_F(StartupBrowserCreatorFirstRunTest,
1051                        MAYBE_SyncPromoForbidden) {
1052   // Consistently enable the welcome page on all platforms.
1053   first_run::SetShouldShowWelcomePage();
1054
1055   // Simulate the following master_preferences:
1056   // {
1057   //  "sync_promo": {
1058   //    "show_on_first_run_allowed": false
1059   //  }
1060   // }
1061   StartupBrowserCreator browser_creator;
1062   browser()->profile()->GetPrefs()->SetBoolean(
1063       prefs::kSignInPromoShowOnFirstRunAllowed, false);
1064
1065   // Do a process-startup browser launch.
1066   CommandLine dummy(CommandLine::NO_PROGRAM);
1067   StartupBrowserCreatorImpl launch(base::FilePath(), dummy, &browser_creator,
1068                                    chrome::startup::IS_FIRST_RUN);
1069   ASSERT_TRUE(launch.Launch(browser()->profile(), std::vector<GURL>(), true,
1070                             browser()->host_desktop_type()));
1071
1072   // This should have created a new browser window.
1073   Browser* new_browser = FindOneOtherBrowser(browser());
1074   ASSERT_TRUE(new_browser);
1075
1076   // Verify that the NTP and the welcome page are shown.
1077   TabStripModel* tab_strip = new_browser->tab_strip_model();
1078   ASSERT_EQ(2, tab_strip->count());
1079   EXPECT_EQ(GURL(chrome::kChromeUINewTabURL),
1080             tab_strip->GetWebContentsAt(0)->GetURL());
1081   EXPECT_EQ(internals::GetWelcomePageURL(),
1082             tab_strip->GetWebContentsAt(1)->GetURL());
1083 }
1084
1085 #if defined(GOOGLE_CHROME_BUILD) && defined(OS_MACOSX)
1086 // http://crbug.com/314819
1087 #define MAYBE_SyncPromoAllowed DISABLED_SyncPromoAllowed
1088 #else
1089 #define MAYBE_SyncPromoAllowed SyncPromoAllowed
1090 #endif
1091 IN_PROC_BROWSER_TEST_F(StartupBrowserCreatorFirstRunTest,
1092                        MAYBE_SyncPromoAllowed) {
1093   // Consistently enable the welcome page on all platforms.
1094   first_run::SetShouldShowWelcomePage();
1095
1096   // Simulate the following master_preferences:
1097   // {
1098   //  "sync_promo": {
1099   //    "show_on_first_run_allowed": true
1100   //  }
1101   // }
1102   StartupBrowserCreator browser_creator;
1103   browser()->profile()->GetPrefs()->SetBoolean(
1104       prefs::kSignInPromoShowOnFirstRunAllowed, true);
1105
1106   // Do a process-startup browser launch.
1107   CommandLine dummy(CommandLine::NO_PROGRAM);
1108   StartupBrowserCreatorImpl launch(base::FilePath(), dummy, &browser_creator,
1109                                    chrome::startup::IS_FIRST_RUN);
1110   ASSERT_TRUE(launch.Launch(browser()->profile(), std::vector<GURL>(), true,
1111                             browser()->host_desktop_type()));
1112
1113   // This should have created a new browser window.
1114   Browser* new_browser = FindOneOtherBrowser(browser());
1115   ASSERT_TRUE(new_browser);
1116
1117   // Verify that the sync promo and the welcome page are shown.
1118   TabStripModel* tab_strip = new_browser->tab_strip_model();
1119   ASSERT_EQ(2, tab_strip->count());
1120   EXPECT_EQ(signin::GetPromoURL(signin::SOURCE_START_PAGE, false),
1121             tab_strip->GetWebContentsAt(0)->GetURL());
1122   EXPECT_EQ(internals::GetWelcomePageURL(),
1123             tab_strip->GetWebContentsAt(1)->GetURL());
1124 }
1125
1126 #if defined(GOOGLE_CHROME_BUILD) && defined(OS_MACOSX)
1127 // http://crbug.com/314819
1128 #define MAYBE_FirstRunTabsPromoAllowed DISABLED_FirstRunTabsPromoAllowed
1129 #else
1130 #define MAYBE_FirstRunTabsPromoAllowed FirstRunTabsPromoAllowed
1131 #endif
1132 IN_PROC_BROWSER_TEST_F(StartupBrowserCreatorFirstRunTest,
1133                        MAYBE_FirstRunTabsPromoAllowed) {
1134   // Simulate the following master_preferences:
1135   // {
1136   //  "first_run_tabs" : [
1137   //    "files/title1.html"
1138   //  ],
1139   //  "sync_promo": {
1140   //    "show_on_first_run_allowed": true
1141   //  }
1142   // }
1143   StartupBrowserCreator browser_creator;
1144   browser_creator.AddFirstRunTab(test_server()->GetURL("files/title1.html"));
1145   browser()->profile()->GetPrefs()->SetBoolean(
1146       prefs::kSignInPromoShowOnFirstRunAllowed, true);
1147
1148   // Do a process-startup browser launch.
1149   CommandLine dummy(CommandLine::NO_PROGRAM);
1150   StartupBrowserCreatorImpl launch(base::FilePath(), dummy, &browser_creator,
1151                                    chrome::startup::IS_FIRST_RUN);
1152   ASSERT_TRUE(launch.Launch(browser()->profile(), std::vector<GURL>(), true,
1153                             browser()->host_desktop_type()));
1154
1155   // This should have created a new browser window.
1156   Browser* new_browser = FindOneOtherBrowser(browser());
1157   ASSERT_TRUE(new_browser);
1158
1159   // Verify that the first-run tab is shown and the sync promo has been added.
1160   TabStripModel* tab_strip = new_browser->tab_strip_model();
1161   ASSERT_EQ(2, tab_strip->count());
1162   EXPECT_EQ(signin::GetPromoURL(signin::SOURCE_START_PAGE, false),
1163             tab_strip->GetWebContentsAt(0)->GetURL());
1164   EXPECT_EQ("title1.html",
1165             tab_strip->GetWebContentsAt(1)->GetURL().ExtractFileName());
1166 }
1167
1168 #if defined(GOOGLE_CHROME_BUILD) && defined(OS_MACOSX)
1169 // http://crbug.com/314819
1170 #define MAYBE_FirstRunTabsContainSyncPromo \
1171     DISABLED_FirstRunTabsContainSyncPromo
1172 #else
1173 #define MAYBE_FirstRunTabsContainSyncPromo FirstRunTabsContainSyncPromo
1174 #endif
1175 IN_PROC_BROWSER_TEST_F(StartupBrowserCreatorFirstRunTest,
1176                        MAYBE_FirstRunTabsContainSyncPromo) {
1177   // Simulate the following master_preferences:
1178   // {
1179   //  "first_run_tabs" : [
1180   //    "files/title1.html",
1181   //    "chrome://signin/?source=0&next_page=chrome%3A%2F%2Fnewtab%2F"
1182   //  ],
1183   //  "sync_promo": {
1184   //    "show_on_first_run_allowed": true
1185   //  }
1186   // }
1187   StartupBrowserCreator browser_creator;
1188   browser_creator.AddFirstRunTab(test_server()->GetURL("files/title1.html"));
1189   browser_creator.AddFirstRunTab(signin::GetPromoURL(signin::SOURCE_START_PAGE,
1190                                                      false));
1191   browser()->profile()->GetPrefs()->SetBoolean(
1192       prefs::kSignInPromoShowOnFirstRunAllowed, true);
1193
1194   // Do a process-startup browser launch.
1195   CommandLine dummy(CommandLine::NO_PROGRAM);
1196   StartupBrowserCreatorImpl launch(base::FilePath(), dummy, &browser_creator,
1197                                    chrome::startup::IS_FIRST_RUN);
1198   ASSERT_TRUE(launch.Launch(browser()->profile(), std::vector<GURL>(), true,
1199                             browser()->host_desktop_type()));
1200
1201   // This should have created a new browser window.
1202   Browser* new_browser = FindOneOtherBrowser(browser());
1203   ASSERT_TRUE(new_browser);
1204
1205   // Verify that the first-run tabs are shown and no sync promo has been added
1206   // as the first-run tabs contain it already.
1207   TabStripModel* tab_strip = new_browser->tab_strip_model();
1208   ASSERT_EQ(2, tab_strip->count());
1209   EXPECT_EQ("title1.html",
1210             tab_strip->GetWebContentsAt(0)->GetURL().ExtractFileName());
1211   EXPECT_EQ(signin::GetPromoURL(signin::SOURCE_START_PAGE, false),
1212             tab_strip->GetWebContentsAt(1)->GetURL());
1213 }
1214
1215 #if defined(GOOGLE_CHROME_BUILD) && defined(OS_MACOSX)
1216 // http://crbug.com/314819
1217 #define MAYBE_FirstRunTabsContainNTPSyncPromoAllowed \
1218     DISABLED_FirstRunTabsContainNTPSyncPromoAllowed
1219 #else
1220 #define MAYBE_FirstRunTabsContainNTPSyncPromoAllowed \
1221     FirstRunTabsContainNTPSyncPromoAllowed
1222 #endif
1223 IN_PROC_BROWSER_TEST_F(StartupBrowserCreatorFirstRunTest,
1224                        MAYBE_FirstRunTabsContainNTPSyncPromoAllowed) {
1225   // Simulate the following master_preferences:
1226   // {
1227   //  "first_run_tabs" : [
1228   //    "new_tab_page",
1229   //    "files/title1.html"
1230   //  ],
1231   //  "sync_promo": {
1232   //    "show_on_first_run_allowed": true
1233   //  }
1234   // }
1235   StartupBrowserCreator browser_creator;
1236   browser_creator.AddFirstRunTab(GURL("new_tab_page"));
1237   browser_creator.AddFirstRunTab(test_server()->GetURL("files/title1.html"));
1238   browser()->profile()->GetPrefs()->SetBoolean(
1239       prefs::kSignInPromoShowOnFirstRunAllowed, true);
1240
1241   // Do a process-startup browser launch.
1242   CommandLine dummy(CommandLine::NO_PROGRAM);
1243   StartupBrowserCreatorImpl launch(base::FilePath(), dummy, &browser_creator,
1244                                    chrome::startup::IS_FIRST_RUN);
1245   ASSERT_TRUE(launch.Launch(browser()->profile(), std::vector<GURL>(), true,
1246                             browser()->host_desktop_type()));
1247
1248   // This should have created a new browser window.
1249   Browser* new_browser = FindOneOtherBrowser(browser());
1250   ASSERT_TRUE(new_browser);
1251
1252   // Verify that the first-run tabs are shown but the NTP that they contain has
1253   // been replaced by the sync promo.
1254   TabStripModel* tab_strip = new_browser->tab_strip_model();
1255   ASSERT_EQ(2, tab_strip->count());
1256   EXPECT_EQ(signin::GetPromoURL(signin::SOURCE_START_PAGE, false),
1257             tab_strip->GetWebContentsAt(0)->GetURL());
1258   EXPECT_EQ("title1.html",
1259             tab_strip->GetWebContentsAt(1)->GetURL().ExtractFileName());
1260 }
1261
1262 #if defined(GOOGLE_CHROME_BUILD) && defined(OS_MACOSX)
1263 // http://crbug.com/314819
1264 #define MAYBE_FirstRunTabsContainNTPSyncPromoForbidden \
1265     DISABLED_FirstRunTabsContainNTPSyncPromoForbidden
1266 #else
1267 #define MAYBE_FirstRunTabsContainNTPSyncPromoForbidden \
1268     FirstRunTabsContainNTPSyncPromoForbidden
1269 #endif
1270 IN_PROC_BROWSER_TEST_F(StartupBrowserCreatorFirstRunTest,
1271                        MAYBE_FirstRunTabsContainNTPSyncPromoForbidden) {
1272   // Simulate the following master_preferences:
1273   // {
1274   //  "first_run_tabs" : [
1275   //    "new_tab_page",
1276   //    "files/title1.html"
1277   //  ],
1278   //  "sync_promo": {
1279   //    "show_on_first_run_allowed": false
1280   //  }
1281   // }
1282   StartupBrowserCreator browser_creator;
1283   browser_creator.AddFirstRunTab(GURL("new_tab_page"));
1284   browser_creator.AddFirstRunTab(test_server()->GetURL("files/title1.html"));
1285   browser()->profile()->GetPrefs()->SetBoolean(
1286       prefs::kSignInPromoShowOnFirstRunAllowed, false);
1287
1288   // Do a process-startup browser launch.
1289   CommandLine dummy(CommandLine::NO_PROGRAM);
1290   StartupBrowserCreatorImpl launch(base::FilePath(), dummy, &browser_creator,
1291                                    chrome::startup::IS_FIRST_RUN);
1292   ASSERT_TRUE(launch.Launch(browser()->profile(), std::vector<GURL>(), true,
1293                             browser()->host_desktop_type()));
1294
1295   // This should have created a new browser window.
1296   Browser* new_browser = FindOneOtherBrowser(browser());
1297   ASSERT_TRUE(new_browser);
1298
1299   // Verify that the first-run tabs are shown, the NTP that they contain has not
1300   // not been replaced by the sync promo and no sync promo has been added.
1301   TabStripModel* tab_strip = new_browser->tab_strip_model();
1302   ASSERT_EQ(2, tab_strip->count());
1303   EXPECT_EQ(GURL(chrome::kChromeUINewTabURL),
1304             tab_strip->GetWebContentsAt(0)->GetURL());
1305   EXPECT_EQ("title1.html",
1306             tab_strip->GetWebContentsAt(1)->GetURL().ExtractFileName());
1307 }
1308
1309 #if defined(GOOGLE_CHROME_BUILD) && defined(OS_MACOSX)
1310 // http://crbug.com/314819
1311 #define MAYBE_FirstRunTabsSyncPromoForbidden \
1312     DISABLED_FirstRunTabsSyncPromoForbidden
1313 #else
1314 #define MAYBE_FirstRunTabsSyncPromoForbidden FirstRunTabsSyncPromoForbidden
1315 #endif
1316 IN_PROC_BROWSER_TEST_F(StartupBrowserCreatorFirstRunTest,
1317                        MAYBE_FirstRunTabsSyncPromoForbidden) {
1318   // Simulate the following master_preferences:
1319   // {
1320   //  "first_run_tabs" : [
1321   //    "files/title1.html"
1322   //  ],
1323   //  "sync_promo": {
1324   //    "show_on_first_run_allowed": false
1325   //  }
1326   // }
1327   StartupBrowserCreator browser_creator;
1328   browser_creator.AddFirstRunTab(test_server()->GetURL("files/title1.html"));
1329   browser()->profile()->GetPrefs()->SetBoolean(
1330       prefs::kSignInPromoShowOnFirstRunAllowed, false);
1331
1332   // Do a process-startup browser launch.
1333   CommandLine dummy(CommandLine::NO_PROGRAM);
1334   StartupBrowserCreatorImpl launch(base::FilePath(), dummy, &browser_creator,
1335                                    chrome::startup::IS_FIRST_RUN);
1336   ASSERT_TRUE(launch.Launch(browser()->profile(), std::vector<GURL>(), true,
1337                             browser()->host_desktop_type()));
1338
1339   // This should have created a new browser window.
1340   Browser* new_browser = FindOneOtherBrowser(browser());
1341   ASSERT_TRUE(new_browser);
1342
1343   // Verify that the first-run tab is shown and no sync promo has been added.
1344   TabStripModel* tab_strip = new_browser->tab_strip_model();
1345   ASSERT_EQ(1, tab_strip->count());
1346   EXPECT_EQ("title1.html",
1347             tab_strip->GetWebContentsAt(0)->GetURL().ExtractFileName());
1348 }
1349
1350 #if defined(ENABLE_CONFIGURATION_POLICY)
1351 #if defined(GOOGLE_CHROME_BUILD) && defined(OS_MACOSX)
1352 // http://crbug.com/314819
1353 #define MAYBE_RestoreOnStartupURLsPolicySpecified \
1354     DISABLED_RestoreOnStartupURLsPolicySpecified
1355 #else
1356 #define MAYBE_RestoreOnStartupURLsPolicySpecified \
1357     RestoreOnStartupURLsPolicySpecified
1358 #endif
1359 IN_PROC_BROWSER_TEST_F(StartupBrowserCreatorFirstRunTest,
1360                        MAYBE_RestoreOnStartupURLsPolicySpecified) {
1361   // Simulate the following master_preferences:
1362   // {
1363   //  "sync_promo": {
1364   //    "show_on_first_run_allowed": true
1365   //  }
1366   // }
1367   StartupBrowserCreator browser_creator;
1368   browser()->profile()->GetPrefs()->SetBoolean(
1369       prefs::kSignInPromoShowOnFirstRunAllowed, true);
1370
1371   // Set the following user policies:
1372   // * RestoreOnStartup = RestoreOnStartupIsURLs
1373   // * RestoreOnStartupURLs = [ "files/title1.html" ]
1374   policy_map_.Set(policy::key::kRestoreOnStartup,
1375                   policy::POLICY_LEVEL_MANDATORY, policy::POLICY_SCOPE_USER,
1376                   base::Value::CreateIntegerValue(
1377                       SessionStartupPref::kPrefValueURLs),
1378                   NULL);
1379   base::ListValue startup_urls;
1380   startup_urls.Append(base::Value::CreateStringValue(
1381       test_server()->GetURL("files/title1.html").spec()));
1382   policy_map_.Set(policy::key::kRestoreOnStartupURLs,
1383                   policy::POLICY_LEVEL_MANDATORY, policy::POLICY_SCOPE_USER,
1384                   startup_urls.DeepCopy(), NULL);
1385   provider_.UpdateChromePolicy(policy_map_);
1386   base::RunLoop().RunUntilIdle();
1387
1388   // Do a process-startup browser launch.
1389   CommandLine dummy(CommandLine::NO_PROGRAM);
1390   StartupBrowserCreatorImpl launch(base::FilePath(), dummy, &browser_creator,
1391                                    chrome::startup::IS_FIRST_RUN);
1392   ASSERT_TRUE(launch.Launch(browser()->profile(), std::vector<GURL>(), true,
1393                             browser()->host_desktop_type()));
1394
1395   // This should have created a new browser window.
1396   Browser* new_browser = FindOneOtherBrowser(browser());
1397   ASSERT_TRUE(new_browser);
1398
1399   // Verify that the URL specified through policy is shown and no sync promo has
1400   // been added.
1401   TabStripModel* tab_strip = new_browser->tab_strip_model();
1402   ASSERT_EQ(1, tab_strip->count());
1403   EXPECT_EQ("title1.html",
1404             tab_strip->GetWebContentsAt(0)->GetURL().ExtractFileName());
1405 }
1406 #endif  // defined(ENABLE_CONFIGURATION_POLICY)
1407
1408 #endif  // !defined(OS_LINUX) || !defined(GOOGLE_CHROME_BUILD) ||
1409         // defined(ENABLE_CONFIGURATION_POLICY)
1410
1411 #endif  // !defined(OS_CHROMEOS)