startup_browser_creator_browsertest.cc revision a36e5920737c6adbddd3e43b760e5de8431db6e0
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/extension_system.h"
16#include "chrome/browser/first_run/first_run.h"
17#include "chrome/browser/infobars/infobar_service.h"
18#include "chrome/browser/prefs/session_startup_pref.h"
19#include "chrome/browser/profiles/profile.h"
20#include "chrome/browser/profiles/profile_impl.h"
21#include "chrome/browser/profiles/profile_manager.h"
22#include "chrome/browser/sessions/session_restore.h"
23#include "chrome/browser/signin/signin_promo.h"
24#include "chrome/browser/ui/browser.h"
25#include "chrome/browser/ui/browser_finder.h"
26#include "chrome/browser/ui/browser_iterator.h"
27#include "chrome/browser/ui/browser_list.h"
28#include "chrome/browser/ui/browser_list_observer.h"
29#include "chrome/browser/ui/browser_window.h"
30#include "chrome/browser/ui/host_desktop.h"
31#include "chrome/browser/ui/startup/startup_browser_creator.h"
32#include "chrome/browser/ui/startup/startup_browser_creator_impl.h"
33#include "chrome/browser/ui/tabs/tab_strip_model.h"
34#include "chrome/common/chrome_switches.h"
35#include "chrome/common/pref_names.h"
36#include "chrome/common/url_constants.h"
37#include "chrome/test/base/in_process_browser_test.h"
38#include "chrome/test/base/test_switches.h"
39#include "chrome/test/base/ui_test_utils.h"
40#include "content/public/browser/web_contents.h"
41#include "testing/gtest/include/gtest/gtest.h"
42
43#if defined(ENABLE_MANAGED_USERS)
44#include "chrome/browser/managed_mode/managed_mode_navigation_observer.h"
45#include "chrome/browser/managed_mode/managed_user_service.h"
46#include "chrome/browser/managed_mode/managed_user_service_factory.h"
47#endif  // defined(ENABLE_MANAGED_USERS)
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 "chrome/browser/policy/browser_policy_connector.h"
54#include "chrome/browser/policy/external_data_fetcher.h"
55#include "chrome/browser/policy/mock_configuration_policy_provider.h"
56#include "chrome/browser/policy/policy_map.h"
57#include "chrome/browser/policy/policy_types.h"
58#include "policy/policy_constants.h"
59#include "testing/gmock/include/gmock/gmock.h"
60
61using testing::_;
62using testing::AnyNumber;
63using testing::Return;
64#endif  // defined(ENABLE_CONFIGURATION_POLICY) && !defined(OS_CHROMEOS)
65
66using extensions::Extension;
67
68namespace {
69
70// Check that there are two browsers. Find the one that is not |browser|.
71Browser* FindOneOtherBrowser(Browser* browser) {
72  // There should only be one other browser.
73  EXPECT_EQ(2u, chrome::GetBrowserCount(browser->profile(),
74                                        browser->host_desktop_type()));
75
76  // Find the new browser.
77  Browser* other_browser = NULL;
78  for (chrome::BrowserIterator it; !it.done() && !other_browser; it.Next()) {
79    if (*it != browser)
80      other_browser = *it;
81  }
82  return other_browser;
83}
84
85}  // namespace
86
87class StartupBrowserCreatorTest : public ExtensionBrowserTest {
88 protected:
89  virtual bool SetUpUserDataDirectory() OVERRIDE {
90    return ExtensionBrowserTest::SetUpUserDataDirectory();
91  }
92
93  virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
94    ExtensionBrowserTest::SetUpCommandLine(command_line);
95    command_line->AppendSwitch(switches::kEnablePanels);
96    command_line->AppendSwitchASCII(switches::kHomePage,
97                                    content::kAboutBlankURL);
98#if defined(OS_CHROMEOS)
99    // TODO(nkostylev): Investigate if we can remove this switch.
100    command_line->AppendSwitch(switches::kCreateBrowserOnStartupForTests);
101#endif
102  }
103
104  // Helper functions return void so that we can ASSERT*().
105  // Use ASSERT_NO_FATAL_FAILURE around calls to these functions to stop the
106  // test if an assert fails.
107  void LoadApp(const std::string& app_name,
108               const Extension** out_app_extension) {
109    ASSERT_TRUE(LoadExtension(test_data_dir_.AppendASCII(app_name.c_str())));
110
111    ExtensionService* service = extensions::ExtensionSystem::Get(
112        browser()->profile())->extension_service();
113    *out_app_extension = service->GetExtensionById(
114        last_loaded_extension_id_, false);
115    ASSERT_TRUE(*out_app_extension);
116
117    // Code that opens a new browser assumes we start with exactly one.
118    ASSERT_EQ(1u, chrome::GetBrowserCount(browser()->profile(),
119                                          browser()->host_desktop_type()));
120  }
121
122  void SetAppLaunchPref(const std::string& app_id,
123                        extensions::ExtensionPrefs::LaunchType launch_type) {
124    ExtensionService* service = extensions::ExtensionSystem::Get(
125        browser()->profile())->extension_service();
126    service->extension_prefs()->SetLaunchType(app_id, launch_type);
127  }
128
129  Browser* FindOneOtherBrowserForProfile(Profile* profile,
130                                         Browser* not_this_browser) {
131    for (chrome::BrowserIterator it; !it.done(); it.Next()) {
132      if (*it != not_this_browser && it->profile() == profile)
133        return *it;
134    }
135    return NULL;
136  }
137};
138
139class OpenURLsPopupObserver : public chrome::BrowserListObserver {
140 public:
141  OpenURLsPopupObserver() : added_browser_(NULL) { }
142
143  virtual void OnBrowserAdded(Browser* browser) OVERRIDE {
144    added_browser_ = browser;
145  }
146
147  virtual void OnBrowserRemoved(Browser* browser) OVERRIDE { }
148
149  Browser* added_browser_;
150};
151
152// Test that when there is a popup as the active browser any requests to
153// StartupBrowserCreatorImpl::OpenURLsInBrowser don't crash because there's no
154// explicit profile given.
155IN_PROC_BROWSER_TEST_F(StartupBrowserCreatorTest, OpenURLsPopup) {
156  std::vector<GURL> urls;
157  urls.push_back(GURL("http://localhost"));
158
159  // Note that in our testing we do not ever query the BrowserList for the "last
160  // active" browser. That's because the browsers are set as "active" by
161  // platform UI toolkit messages, and those messages are not sent during unit
162  // testing sessions.
163
164  OpenURLsPopupObserver observer;
165  BrowserList::AddObserver(&observer);
166
167  Browser* popup = new Browser(
168      Browser::CreateParams(Browser::TYPE_POPUP, browser()->profile(),
169                            browser()->host_desktop_type()));
170  ASSERT_TRUE(popup->is_type_popup());
171  ASSERT_EQ(popup, observer.added_browser_);
172
173  CommandLine dummy(CommandLine::NO_PROGRAM);
174  chrome::startup::IsFirstRun first_run = first_run::IsChromeFirstRun() ?
175      chrome::startup::IS_FIRST_RUN : chrome::startup::IS_NOT_FIRST_RUN;
176  StartupBrowserCreatorImpl launch(base::FilePath(), dummy, first_run);
177  // This should create a new window, but re-use the profile from |popup|. If
178  // it used a NULL or invalid profile, it would crash.
179  launch.OpenURLsInBrowser(popup, false, urls, chrome::GetActiveDesktop());
180  ASSERT_NE(popup, observer.added_browser_);
181  BrowserList::RemoveObserver(&observer);
182}
183
184// We don't do non-process-startup browser launches on ChromeOS.
185// Session restore for process-startup browser launches is tested
186// in session_restore_uitest.
187#if !defined(OS_CHROMEOS)
188// Verify that startup URLs are honored when the process already exists but has
189// no tabbed browser windows (eg. as if the process is running only due to a
190// background application.
191IN_PROC_BROWSER_TEST_F(StartupBrowserCreatorTest,
192                       StartupURLsOnNewWindowWithNoTabbedBrowsers) {
193  // Use a couple same-site HTTP URLs.
194  ASSERT_TRUE(test_server()->Start());
195  std::vector<GURL> urls;
196  urls.push_back(test_server()->GetURL("files/title1.html"));
197  urls.push_back(test_server()->GetURL("files/title2.html"));
198
199  // Set the startup preference to open these URLs.
200  SessionStartupPref pref(SessionStartupPref::URLS);
201  pref.urls = urls;
202  SessionStartupPref::SetStartupPref(browser()->profile(), pref);
203
204  // Close the browser.
205  browser()->window()->Close();
206
207  // Do a simple non-process-startup browser launch.
208  CommandLine dummy(CommandLine::NO_PROGRAM);
209  chrome::startup::IsFirstRun first_run = first_run::IsChromeFirstRun() ?
210      chrome::startup::IS_FIRST_RUN : chrome::startup::IS_NOT_FIRST_RUN;
211  StartupBrowserCreatorImpl launch(base::FilePath(), dummy, first_run);
212  ASSERT_TRUE(launch.Launch(browser()->profile(), std::vector<GURL>(), false,
213                            browser()->host_desktop_type()));
214
215  // This should have created a new browser window.  |browser()| is still
216  // around at this point, even though we've closed its window.
217  Browser* new_browser = FindOneOtherBrowser(browser());
218  ASSERT_TRUE(new_browser);
219
220  // The new browser should have one tab for each URL.
221  TabStripModel* tab_strip = new_browser->tab_strip_model();
222  ASSERT_EQ(static_cast<int>(urls.size()), tab_strip->count());
223  for (size_t i=0; i < urls.size(); i++) {
224    EXPECT_EQ(urls[i], tab_strip->GetWebContentsAt(i)->GetURL());
225  }
226
227  // The two tabs, despite having the same site, should be in different
228  // SiteInstances.
229  EXPECT_NE(tab_strip->GetWebContentsAt(0)->GetSiteInstance(),
230            tab_strip->GetWebContentsAt(1)->GetSiteInstance());
231}
232
233// Verify that startup URLs aren't used when the process already exists
234// and has other tabbed browser windows.  This is the common case of starting a
235// new browser.
236IN_PROC_BROWSER_TEST_F(StartupBrowserCreatorTest,
237                       StartupURLsOnNewWindow) {
238  // Use a couple arbitrary URLs.
239  std::vector<GURL> urls;
240  urls.push_back(ui_test_utils::GetTestUrl(
241      base::FilePath(base::FilePath::kCurrentDirectory),
242      base::FilePath(FILE_PATH_LITERAL("title1.html"))));
243  urls.push_back(ui_test_utils::GetTestUrl(
244      base::FilePath(base::FilePath::kCurrentDirectory),
245      base::FilePath(FILE_PATH_LITERAL("title2.html"))));
246
247  // Set the startup preference to open these URLs.
248  SessionStartupPref pref(SessionStartupPref::URLS);
249  pref.urls = urls;
250  SessionStartupPref::SetStartupPref(browser()->profile(), pref);
251
252  // Do a simple non-process-startup browser launch.
253  CommandLine dummy(CommandLine::NO_PROGRAM);
254  chrome::startup::IsFirstRun first_run = first_run::IsChromeFirstRun() ?
255      chrome::startup::IS_FIRST_RUN : chrome::startup::IS_NOT_FIRST_RUN;
256  StartupBrowserCreatorImpl launch(base::FilePath(), dummy, first_run);
257  ASSERT_TRUE(launch.Launch(browser()->profile(), std::vector<GURL>(), false,
258                            browser()->host_desktop_type()));
259
260  // This should have created a new browser window.
261  Browser* new_browser = FindOneOtherBrowser(browser());
262  ASSERT_TRUE(new_browser);
263
264  // The new browser should have exactly one tab (not the startup URLs).
265  ASSERT_EQ(1, new_browser->tab_strip_model()->count());
266}
267
268// App shortcuts are not implemented on mac os.
269#if !defined(OS_MACOSX)
270IN_PROC_BROWSER_TEST_F(StartupBrowserCreatorTest, OpenAppShortcutNoPref) {
271  // Load an app with launch.container = 'tab'.
272  const Extension* extension_app = NULL;
273  ASSERT_NO_FATAL_FAILURE(LoadApp("app_with_tab_container", &extension_app));
274
275  // Add --app-id=<extension->id()> to the command line.
276  CommandLine command_line(CommandLine::NO_PROGRAM);
277  command_line.AppendSwitchASCII(switches::kAppId, extension_app->id());
278
279  chrome::startup::IsFirstRun first_run = first_run::IsChromeFirstRun() ?
280      chrome::startup::IS_FIRST_RUN : chrome::startup::IS_NOT_FIRST_RUN;
281  StartupBrowserCreatorImpl launch(base::FilePath(), command_line, first_run);
282  ASSERT_TRUE(launch.Launch(browser()->profile(), std::vector<GURL>(), false,
283                            browser()->host_desktop_type()));
284
285  // No pref was set, so the app should have opened in a window.
286  // The launch should have created a new browser.
287  Browser* new_browser = FindOneOtherBrowser(browser());
288  ASSERT_TRUE(new_browser);
289
290  // Expect an app window.
291  EXPECT_TRUE(new_browser->is_app());
292
293  // The browser's app_name should include the app's ID.
294  EXPECT_NE(
295      new_browser->app_name_.find(extension_app->id()),
296      std::string::npos) << new_browser->app_name_;
297}
298
299IN_PROC_BROWSER_TEST_F(StartupBrowserCreatorTest, OpenAppShortcutWindowPref) {
300  const Extension* extension_app = NULL;
301  ASSERT_NO_FATAL_FAILURE(LoadApp("app_with_tab_container", &extension_app));
302
303  // Set a pref indicating that the user wants to open this app in a window.
304  SetAppLaunchPref(extension_app->id(),
305                   extensions::ExtensionPrefs::LAUNCH_WINDOW);
306
307  CommandLine command_line(CommandLine::NO_PROGRAM);
308  command_line.AppendSwitchASCII(switches::kAppId, extension_app->id());
309  chrome::startup::IsFirstRun first_run = first_run::IsChromeFirstRun() ?
310      chrome::startup::IS_FIRST_RUN : chrome::startup::IS_NOT_FIRST_RUN;
311  StartupBrowserCreatorImpl launch(base::FilePath(), command_line, first_run);
312  ASSERT_TRUE(launch.Launch(browser()->profile(), std::vector<GURL>(), false,
313                            browser()->host_desktop_type()));
314
315  // Pref was set to open in a window, so the app should have opened in a
316  // window.  The launch should have created a new browser. Find the new
317  // browser.
318  Browser* new_browser = FindOneOtherBrowser(browser());
319  ASSERT_TRUE(new_browser);
320
321  // Expect an app window.
322  EXPECT_TRUE(new_browser->is_app());
323
324  // The browser's app_name should include the app's ID.
325  EXPECT_NE(
326      new_browser->app_name_.find(extension_app->id()),
327      std::string::npos) << new_browser->app_name_;
328}
329
330IN_PROC_BROWSER_TEST_F(StartupBrowserCreatorTest, OpenAppShortcutTabPref) {
331  // Load an app with launch.container = 'tab'.
332  const Extension* extension_app = NULL;
333  ASSERT_NO_FATAL_FAILURE(LoadApp("app_with_tab_container", &extension_app));
334
335  // Set a pref indicating that the user wants to open this app in a window.
336  SetAppLaunchPref(extension_app->id(),
337                   extensions::ExtensionPrefs::LAUNCH_REGULAR);
338
339  CommandLine command_line(CommandLine::NO_PROGRAM);
340  command_line.AppendSwitchASCII(switches::kAppId, extension_app->id());
341  chrome::startup::IsFirstRun first_run = first_run::IsChromeFirstRun() ?
342      chrome::startup::IS_FIRST_RUN : chrome::startup::IS_NOT_FIRST_RUN;
343  StartupBrowserCreatorImpl launch(base::FilePath(), command_line, first_run);
344  ASSERT_TRUE(launch.Launch(browser()->profile(), std::vector<GURL>(), false,
345                            browser()->host_desktop_type()));
346
347  // When an app shortcut is open and the pref indicates a tab should
348  // open, the tab is open in a new browser window.  Expect a new window.
349  ASSERT_EQ(2u, chrome::GetBrowserCount(browser()->profile(),
350                                        browser()->host_desktop_type()));
351
352  Browser* new_browser = FindOneOtherBrowser(browser());
353  ASSERT_TRUE(new_browser);
354
355  // The tab should be in a tabbed window.
356  EXPECT_TRUE(new_browser->is_type_tabbed());
357
358  // The browser's app_name should not include the app's ID: It is in a
359  // normal browser.
360  EXPECT_EQ(
361      new_browser->app_name_.find(extension_app->id()),
362      std::string::npos) << new_browser->app_name_;
363}
364
365#endif  // !defined(OS_MACOSX)
366
367#endif  // !defined(OS_CHROMEOS)
368
369IN_PROC_BROWSER_TEST_F(StartupBrowserCreatorTest,
370                       ReadingWasRestartedAfterRestart) {
371  // Tests that StartupBrowserCreator::WasRestarted reads and resets the
372  // preference kWasRestarted correctly.
373  StartupBrowserCreator::was_restarted_read_ = false;
374  PrefService* pref_service = g_browser_process->local_state();
375  pref_service->SetBoolean(prefs::kWasRestarted, true);
376  EXPECT_TRUE(StartupBrowserCreator::WasRestarted());
377  EXPECT_FALSE(pref_service->GetBoolean(prefs::kWasRestarted));
378  EXPECT_TRUE(StartupBrowserCreator::WasRestarted());
379}
380
381IN_PROC_BROWSER_TEST_F(StartupBrowserCreatorTest,
382                       ReadingWasRestartedAfterNormalStart) {
383  // Tests that StartupBrowserCreator::WasRestarted reads and resets the
384  // preference kWasRestarted correctly.
385  StartupBrowserCreator::was_restarted_read_ = false;
386  PrefService* pref_service = g_browser_process->local_state();
387  pref_service->SetBoolean(prefs::kWasRestarted, false);
388  EXPECT_FALSE(StartupBrowserCreator::WasRestarted());
389  EXPECT_FALSE(pref_service->GetBoolean(prefs::kWasRestarted));
390  EXPECT_FALSE(StartupBrowserCreator::WasRestarted());
391}
392
393IN_PROC_BROWSER_TEST_F(StartupBrowserCreatorTest, AddFirstRunTab) {
394  StartupBrowserCreator browser_creator;
395  browser_creator.AddFirstRunTab(test_server()->GetURL("files/title1.html"));
396  browser_creator.AddFirstRunTab(test_server()->GetURL("files/title2.html"));
397
398  // Do a simple non-process-startup browser launch.
399  CommandLine dummy(CommandLine::NO_PROGRAM);
400  StartupBrowserCreatorImpl launch(base::FilePath(), dummy, &browser_creator,
401                                   chrome::startup::IS_FIRST_RUN);
402  ASSERT_TRUE(launch.Launch(browser()->profile(), std::vector<GURL>(), false,
403                            browser()->host_desktop_type()));
404
405  // This should have created a new browser window.
406  Browser* new_browser = FindOneOtherBrowser(browser());
407  ASSERT_TRUE(new_browser);
408
409  TabStripModel* tab_strip = new_browser->tab_strip_model();
410  EXPECT_EQ(2, tab_strip->count());
411
412  EXPECT_EQ("title1.html",
413            tab_strip->GetWebContentsAt(0)->GetURL().ExtractFileName());
414  EXPECT_EQ("title2.html",
415            tab_strip->GetWebContentsAt(1)->GetURL().ExtractFileName());
416}
417
418// Test hard-coded special first run tabs (defined in
419// StartupBrowserCreatorImpl::AddStartupURLs()).
420IN_PROC_BROWSER_TEST_F(StartupBrowserCreatorTest, AddCustomFirstRunTab) {
421  StartupBrowserCreator browser_creator;
422  browser_creator.AddFirstRunTab(test_server()->GetURL("files/title1.html"));
423  browser_creator.AddFirstRunTab(GURL("http://new_tab_page"));
424  browser_creator.AddFirstRunTab(test_server()->GetURL("files/title2.html"));
425  browser_creator.AddFirstRunTab(GURL("http://welcome_page"));
426
427  // Do a simple non-process-startup browser launch.
428  CommandLine dummy(CommandLine::NO_PROGRAM);
429  StartupBrowserCreatorImpl launch(base::FilePath(), dummy, &browser_creator,
430                                   chrome::startup::IS_FIRST_RUN);
431  ASSERT_TRUE(launch.Launch(browser()->profile(), std::vector<GURL>(), false,
432                            browser()->host_desktop_type()));
433
434  // This should have created a new browser window.
435  Browser* new_browser = FindOneOtherBrowser(browser());
436  ASSERT_TRUE(new_browser);
437
438  TabStripModel* tab_strip = new_browser->tab_strip_model();
439  EXPECT_EQ(4, tab_strip->count());
440
441  EXPECT_EQ("title1.html",
442            tab_strip->GetWebContentsAt(0)->GetURL().ExtractFileName());
443  EXPECT_EQ(GURL(chrome::kChromeUINewTabURL),
444            tab_strip->GetWebContentsAt(1)->GetURL());
445  EXPECT_EQ("title2.html",
446            tab_strip->GetWebContentsAt(2)->GetURL().ExtractFileName());
447  EXPECT_EQ(internals::GetWelcomePageURL(),
448            tab_strip->GetWebContentsAt(3)->GetURL());
449}
450
451IN_PROC_BROWSER_TEST_F(StartupBrowserCreatorTest, SyncPromoNoWelcomePage) {
452#if defined(OS_WIN) && defined(USE_ASH)
453  // Disable this test in Metro+Ash for now (http://crbug.com/262796).
454  if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kAshBrowserTests))
455    return;
456#endif
457
458  // Trick this test into thinking the promo has been shown for this profile; so
459  // that it will show it again (otherwise it skips showing it since
460  // --no-first-run is specified in browser tests).
461  signin::DidShowPromoAtStartup(browser()->profile());
462
463  // Do a simple non-process-startup browser launch.
464  CommandLine dummy(CommandLine::NO_PROGRAM);
465  StartupBrowserCreatorImpl launch(base::FilePath(), dummy,
466                                   chrome::startup::IS_FIRST_RUN);
467  ASSERT_TRUE(launch.Launch(browser()->profile(), std::vector<GURL>(), false,
468                            browser()->host_desktop_type()));
469
470  // This should have created a new browser window.
471  Browser* new_browser = FindOneOtherBrowser(browser());
472  ASSERT_TRUE(new_browser);
473
474  TabStripModel* tab_strip = new_browser->tab_strip_model();
475  EXPECT_EQ(1, tab_strip->count());
476
477  if (signin::ShouldShowPromoAtStartup(browser()->profile(), true)) {
478    EXPECT_EQ("signin", tab_strip->GetWebContentsAt(0)->GetURL().host());
479  } else {
480    EXPECT_EQ(GURL(chrome::kChromeUINewTabURL),
481              tab_strip->GetWebContentsAt(0)->GetURL());
482  }
483}
484
485IN_PROC_BROWSER_TEST_F(StartupBrowserCreatorTest, SyncPromoWithWelcomePage) {
486#if defined(OS_WIN) && defined(USE_ASH)
487  // Disable this test in Metro+Ash for now (http://crbug.com/262796).
488  if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kAshBrowserTests))
489    return;
490#endif
491
492  // Trick this test into thinking the promo has been shown for this profile; so
493  // that it will show it again (otherwise it skips showing it since
494  // --no-first-run is specified in browser tests).
495  signin::DidShowPromoAtStartup(browser()->profile());
496  first_run::SetShouldShowWelcomePage();
497
498  // Do a simple non-process-startup browser launch.
499  CommandLine dummy(CommandLine::NO_PROGRAM);
500  StartupBrowserCreatorImpl launch(base::FilePath(), dummy,
501                                   chrome::startup::IS_FIRST_RUN);
502  ASSERT_TRUE(launch.Launch(browser()->profile(), std::vector<GURL>(), false,
503                            browser()->host_desktop_type()));
504
505  // This should have created a new browser window.
506  Browser* new_browser = FindOneOtherBrowser(browser());
507  ASSERT_TRUE(new_browser);
508
509  TabStripModel* tab_strip = new_browser->tab_strip_model();
510  EXPECT_EQ(2, tab_strip->count());
511
512  if (signin::ShouldShowPromoAtStartup(browser()->profile(), true)) {
513    EXPECT_EQ("signin", tab_strip->GetWebContentsAt(0)->GetURL().host());
514  } else {
515    EXPECT_EQ(GURL(chrome::kChromeUINewTabURL),
516              tab_strip->GetWebContentsAt(0)->GetURL());
517  }
518  EXPECT_EQ(internals::GetWelcomePageURL(),
519            tab_strip->GetWebContentsAt(1)->GetURL());
520}
521
522IN_PROC_BROWSER_TEST_F(StartupBrowserCreatorTest, SyncPromoWithFirstRunTabs) {
523#if defined(OS_WIN) && defined(USE_ASH)
524  // Disable this test in Metro+Ash for now (http://crbug.com/262796).
525  if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kAshBrowserTests))
526    return;
527#endif
528
529  StartupBrowserCreator browser_creator;
530  browser_creator.AddFirstRunTab(test_server()->GetURL("files/title1.html"));
531
532  // Trick this test into thinking the promo has been shown for this profile; so
533  // that it will show it again (otherwise it skips showing it since
534  // --no-first-run is specified in browser tests).
535  signin::DidShowPromoAtStartup(browser()->profile());
536
537  // The welcome page should not be shown, even if
538  // first_run::ShouldShowWelcomePage() says so, when there are already
539  // more than 2 first run tabs.
540  first_run::SetShouldShowWelcomePage();
541
542  // Do a simple non-process-startup browser launch.
543  CommandLine dummy(CommandLine::NO_PROGRAM);
544  StartupBrowserCreatorImpl launch(base::FilePath(), dummy, &browser_creator,
545                                   chrome::startup::IS_FIRST_RUN);
546  ASSERT_TRUE(launch.Launch(browser()->profile(), std::vector<GURL>(), false,
547                            browser()->host_desktop_type()));
548
549  // This should have created a new browser window.
550  Browser* new_browser = FindOneOtherBrowser(browser());
551  ASSERT_TRUE(new_browser);
552
553  TabStripModel* tab_strip = new_browser->tab_strip_model();
554  if (signin::ShouldShowPromoAtStartup(browser()->profile(), true)) {
555    EXPECT_EQ(2, tab_strip->count());
556    EXPECT_EQ("signin", tab_strip->GetWebContentsAt(0)->GetURL().host());
557    EXPECT_EQ("title1.html",
558              tab_strip->GetWebContentsAt(1)->GetURL().ExtractFileName());
559  } else {
560    EXPECT_EQ(1, tab_strip->count());
561    EXPECT_EQ("title1.html",
562              tab_strip->GetWebContentsAt(0)->GetURL().ExtractFileName());
563  }
564}
565
566// The welcome page should still be shown if there are more than 2 first run
567// tabs, but the welcome page was explcitly added to the first run tabs.
568IN_PROC_BROWSER_TEST_F(StartupBrowserCreatorTest,
569                       SyncPromoWithFirstRunTabsIncludingWelcomePage) {
570#if defined(OS_WIN) && defined(USE_ASH)
571  // Disable this test in Metro+Ash for now (http://crbug.com/262796).
572  if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kAshBrowserTests))
573    return;
574#endif
575
576  StartupBrowserCreator browser_creator;
577  browser_creator.AddFirstRunTab(test_server()->GetURL("files/title1.html"));
578  browser_creator.AddFirstRunTab(GURL("http://welcome_page"));
579
580  // Trick this test into thinking the promo has been shown for this profile; so
581  // that it will show it again (otherwise it skips showing it since
582  // --no-first-run is specified in browser tests).
583  signin::DidShowPromoAtStartup(browser()->profile());
584
585  // Do a simple non-process-startup browser launch.
586  CommandLine dummy(CommandLine::NO_PROGRAM);
587  StartupBrowserCreatorImpl launch(base::FilePath(), dummy, &browser_creator,
588                                   chrome::startup::IS_FIRST_RUN);
589  ASSERT_TRUE(launch.Launch(browser()->profile(), std::vector<GURL>(), false,
590                            browser()->host_desktop_type()));
591
592  // This should have created a new browser window.
593  Browser* new_browser = FindOneOtherBrowser(browser());
594  ASSERT_TRUE(new_browser);
595
596  TabStripModel* tab_strip = new_browser->tab_strip_model();
597  if (signin::ShouldShowPromoAtStartup(browser()->profile(), true)) {
598    EXPECT_EQ(3, tab_strip->count());
599    EXPECT_EQ("signin", tab_strip->GetWebContentsAt(0)->GetURL().host());
600    EXPECT_EQ("title1.html",
601              tab_strip->GetWebContentsAt(1)->GetURL().ExtractFileName());
602    EXPECT_EQ(internals::GetWelcomePageURL(),
603              tab_strip->GetWebContentsAt(2)->GetURL());
604  } else {
605    EXPECT_EQ(2, tab_strip->count());
606    EXPECT_EQ("title1.html",
607              tab_strip->GetWebContentsAt(0)->GetURL().ExtractFileName());
608    EXPECT_EQ(internals::GetWelcomePageURL(),
609              tab_strip->GetWebContentsAt(1)->GetURL());
610  }
611}
612
613#if !defined(OS_CHROMEOS)
614IN_PROC_BROWSER_TEST_F(StartupBrowserCreatorTest, StartupURLsForTwoProfiles) {
615#if defined(OS_WIN) && defined(USE_ASH)
616  // Disable this test in Metro+Ash for now (http://crbug.com/262796).
617  if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kAshBrowserTests))
618    return;
619#endif
620
621  Profile* default_profile = browser()->profile();
622
623  ProfileManager* profile_manager = g_browser_process->profile_manager();
624  // Create another profile.
625  base::FilePath dest_path = profile_manager->user_data_dir();
626  dest_path = dest_path.Append(FILE_PATH_LITERAL("New Profile 1"));
627
628  Profile* other_profile = profile_manager->GetProfile(dest_path);
629  ASSERT_TRUE(other_profile);
630
631  // Use a couple arbitrary URLs.
632  std::vector<GURL> urls1;
633  urls1.push_back(ui_test_utils::GetTestUrl(
634      base::FilePath(base::FilePath::kCurrentDirectory),
635      base::FilePath(FILE_PATH_LITERAL("title1.html"))));
636  std::vector<GURL> urls2;
637  urls2.push_back(ui_test_utils::GetTestUrl(
638      base::FilePath(base::FilePath::kCurrentDirectory),
639      base::FilePath(FILE_PATH_LITERAL("title2.html"))));
640
641  // Set different startup preferences for the 2 profiles.
642  SessionStartupPref pref1(SessionStartupPref::URLS);
643  pref1.urls = urls1;
644  SessionStartupPref::SetStartupPref(default_profile, pref1);
645  SessionStartupPref pref2(SessionStartupPref::URLS);
646  pref2.urls = urls2;
647  SessionStartupPref::SetStartupPref(other_profile, pref2);
648
649  // Close the browser.
650  browser()->window()->Close();
651
652  // Do a simple non-process-startup browser launch.
653  CommandLine dummy(CommandLine::NO_PROGRAM);
654
655  int return_code;
656  StartupBrowserCreator browser_creator;
657  std::vector<Profile*> last_opened_profiles;
658  last_opened_profiles.push_back(default_profile);
659  last_opened_profiles.push_back(other_profile);
660  browser_creator.Start(dummy, profile_manager->user_data_dir(),
661                        default_profile, last_opened_profiles, &return_code);
662
663  // urls1 were opened in a browser for default_profile, and urls2 were opened
664  // in a browser for other_profile.
665  Browser* new_browser = NULL;
666  // |browser()| is still around at this point, even though we've closed its
667  // window. Thus the browser count for default_profile is 2.
668  ASSERT_EQ(2u, chrome::GetBrowserCount(default_profile,
669                                        browser()->host_desktop_type()));
670  new_browser = FindOneOtherBrowserForProfile(default_profile, browser());
671  ASSERT_TRUE(new_browser);
672  TabStripModel* tab_strip = new_browser->tab_strip_model();
673  ASSERT_EQ(1, tab_strip->count());
674  EXPECT_EQ(urls1[0], tab_strip->GetWebContentsAt(0)->GetURL());
675
676  ASSERT_EQ(1u, chrome::GetBrowserCount(other_profile,
677                                        browser()->host_desktop_type()));
678  new_browser = FindOneOtherBrowserForProfile(other_profile, NULL);
679  ASSERT_TRUE(new_browser);
680  tab_strip = new_browser->tab_strip_model();
681  ASSERT_EQ(1, tab_strip->count());
682  EXPECT_EQ(urls2[0], tab_strip->GetWebContentsAt(0)->GetURL());
683}
684
685IN_PROC_BROWSER_TEST_F(StartupBrowserCreatorTest, PRE_UpdateWithTwoProfiles) {
686  // Simulate a browser restart by creating the profiles in the PRE_ part.
687  ProfileManager* profile_manager = g_browser_process->profile_manager();
688
689  // Create two profiles.
690  base::FilePath dest_path = profile_manager->user_data_dir();
691
692  Profile* profile1 = profile_manager->GetProfile(
693      dest_path.Append(FILE_PATH_LITERAL("New Profile 1")));
694  ASSERT_TRUE(profile1);
695
696  Profile* profile2 = profile_manager->GetProfile(
697      dest_path.Append(FILE_PATH_LITERAL("New Profile 2")));
698  ASSERT_TRUE(profile2);
699
700  // Use a couple arbitrary URLs.
701  std::vector<GURL> urls1;
702  urls1.push_back(ui_test_utils::GetTestUrl(
703      base::FilePath(base::FilePath::kCurrentDirectory),
704      base::FilePath(FILE_PATH_LITERAL("title1.html"))));
705  std::vector<GURL> urls2;
706  urls2.push_back(ui_test_utils::GetTestUrl(
707      base::FilePath(base::FilePath::kCurrentDirectory),
708      base::FilePath(FILE_PATH_LITERAL("title2.html"))));
709
710  // Set different startup preferences for the 2 profiles.
711  SessionStartupPref pref1(SessionStartupPref::URLS);
712  pref1.urls = urls1;
713  SessionStartupPref::SetStartupPref(profile1, pref1);
714  SessionStartupPref pref2(SessionStartupPref::URLS);
715  pref2.urls = urls2;
716  SessionStartupPref::SetStartupPref(profile2, pref2);
717
718  profile1->GetPrefs()->CommitPendingWrite();
719  profile2->GetPrefs()->CommitPendingWrite();
720}
721
722IN_PROC_BROWSER_TEST_F(StartupBrowserCreatorTest, UpdateWithTwoProfiles) {
723#if defined(OS_WIN) && defined(USE_ASH)
724  // Disable this test in Metro+Ash for now (http://crbug.com/262796).
725  if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kAshBrowserTests))
726    return;
727#endif
728
729  // Make StartupBrowserCreator::WasRestarted() return true.
730  StartupBrowserCreator::was_restarted_read_ = false;
731  PrefService* pref_service = g_browser_process->local_state();
732  pref_service->SetBoolean(prefs::kWasRestarted, true);
733
734  ProfileManager* profile_manager = g_browser_process->profile_manager();
735
736  // Open the two profiles.
737  base::FilePath dest_path = profile_manager->user_data_dir();
738
739  Profile* profile1 = profile_manager->GetProfile(
740      dest_path.Append(FILE_PATH_LITERAL("New Profile 1")));
741  ASSERT_TRUE(profile1);
742
743  Profile* profile2 = profile_manager->GetProfile(
744      dest_path.Append(FILE_PATH_LITERAL("New Profile 2")));
745  ASSERT_TRUE(profile2);
746
747  // Simulate a launch after a browser update.
748  CommandLine dummy(CommandLine::NO_PROGRAM);
749  int return_code;
750  StartupBrowserCreator browser_creator;
751  std::vector<Profile*> last_opened_profiles;
752  last_opened_profiles.push_back(profile1);
753  last_opened_profiles.push_back(profile2);
754  browser_creator.Start(dummy, profile_manager->user_data_dir(), profile1,
755                        last_opened_profiles, &return_code);
756
757  while (SessionRestore::IsRestoring(profile1) ||
758         SessionRestore::IsRestoring(profile2))
759    base::MessageLoop::current()->RunUntilIdle();
760
761  // The startup URLs are ignored, and instead the last open sessions are
762  // restored.
763  EXPECT_TRUE(profile1->restored_last_session());
764  EXPECT_TRUE(profile2->restored_last_session());
765
766  Browser* new_browser = NULL;
767  ASSERT_EQ(1u, chrome::GetBrowserCount(profile1,
768                                        browser()->host_desktop_type()));
769  new_browser = FindOneOtherBrowserForProfile(profile1, NULL);
770  ASSERT_TRUE(new_browser);
771  TabStripModel* tab_strip = new_browser->tab_strip_model();
772  ASSERT_EQ(1, tab_strip->count());
773  EXPECT_EQ(GURL(content::kAboutBlankURL),
774            tab_strip->GetWebContentsAt(0)->GetURL());
775
776  ASSERT_EQ(1u, chrome::GetBrowserCount(profile2,
777                                        browser()->host_desktop_type()));
778  new_browser = FindOneOtherBrowserForProfile(profile2, NULL);
779  ASSERT_TRUE(new_browser);
780  tab_strip = new_browser->tab_strip_model();
781  ASSERT_EQ(1, tab_strip->count());
782  EXPECT_EQ(GURL(content::kAboutBlankURL),
783            tab_strip->GetWebContentsAt(0)->GetURL());
784}
785
786IN_PROC_BROWSER_TEST_F(StartupBrowserCreatorTest,
787                       ProfilesWithoutPagesNotLaunched) {
788#if defined(OS_WIN) && defined(USE_ASH)
789  // Disable this test in Metro+Ash for now (http://crbug.com/262796).
790  if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kAshBrowserTests))
791    return;
792#endif
793
794  Profile* default_profile = browser()->profile();
795
796  ProfileManager* profile_manager = g_browser_process->profile_manager();
797
798  // Create 4 more profiles.
799  base::FilePath dest_path1 = profile_manager->user_data_dir().Append(
800      FILE_PATH_LITERAL("New Profile 1"));
801  base::FilePath dest_path2 = profile_manager->user_data_dir().Append(
802      FILE_PATH_LITERAL("New Profile 2"));
803  base::FilePath dest_path3 = profile_manager->user_data_dir().Append(
804      FILE_PATH_LITERAL("New Profile 3"));
805  base::FilePath dest_path4 = profile_manager->user_data_dir().Append(
806      FILE_PATH_LITERAL("New Profile 4"));
807
808  Profile* profile_home1 = profile_manager->GetProfile(dest_path1);
809  ASSERT_TRUE(profile_home1);
810  Profile* profile_home2 = profile_manager->GetProfile(dest_path2);
811  ASSERT_TRUE(profile_home2);
812  Profile* profile_last = profile_manager->GetProfile(dest_path3);
813  ASSERT_TRUE(profile_last);
814  Profile* profile_urls = profile_manager->GetProfile(dest_path4);
815  ASSERT_TRUE(profile_urls);
816
817  // Set the profiles to open urls, open last visited pages or display the home
818  // page.
819  SessionStartupPref pref_home(SessionStartupPref::DEFAULT);
820  SessionStartupPref::SetStartupPref(profile_home1, pref_home);
821  SessionStartupPref::SetStartupPref(profile_home2, pref_home);
822
823  SessionStartupPref pref_last(SessionStartupPref::LAST);
824  SessionStartupPref::SetStartupPref(profile_last, pref_last);
825
826  std::vector<GURL> urls;
827  urls.push_back(ui_test_utils::GetTestUrl(
828      base::FilePath(base::FilePath::kCurrentDirectory),
829      base::FilePath(FILE_PATH_LITERAL("title1.html"))));
830
831  SessionStartupPref pref_urls(SessionStartupPref::URLS);
832  pref_urls.urls = urls;
833  SessionStartupPref::SetStartupPref(profile_urls, pref_urls);
834
835  // Close the browser.
836  chrome::HostDesktopType original_desktop_type =
837      browser()->host_desktop_type();
838  browser()->window()->Close();
839
840  // Do a simple non-process-startup browser launch.
841  CommandLine dummy(CommandLine::NO_PROGRAM);
842
843  int return_code;
844  StartupBrowserCreator browser_creator;
845  std::vector<Profile*> last_opened_profiles;
846  last_opened_profiles.push_back(profile_home1);
847  last_opened_profiles.push_back(profile_home2);
848  last_opened_profiles.push_back(profile_last);
849  last_opened_profiles.push_back(profile_urls);
850  browser_creator.Start(dummy, profile_manager->user_data_dir(), profile_home1,
851                        last_opened_profiles, &return_code);
852
853  while (SessionRestore::IsRestoring(default_profile) ||
854         SessionRestore::IsRestoring(profile_home1) ||
855         SessionRestore::IsRestoring(profile_home2) ||
856         SessionRestore::IsRestoring(profile_last) ||
857         SessionRestore::IsRestoring(profile_urls))
858    base::MessageLoop::current()->RunUntilIdle();
859
860  Browser* new_browser = NULL;
861  // The last open profile (the profile_home1 in this case) will always be
862  // launched, even if it will open just the home page.
863  ASSERT_EQ(1u, chrome::GetBrowserCount(profile_home1, original_desktop_type));
864  new_browser = FindOneOtherBrowserForProfile(profile_home1, NULL);
865  ASSERT_TRUE(new_browser);
866  TabStripModel* tab_strip = new_browser->tab_strip_model();
867  ASSERT_EQ(1, tab_strip->count());
868  EXPECT_EQ(GURL(chrome::kChromeUINewTabURL),
869            tab_strip->GetWebContentsAt(0)->GetURL());
870
871  // profile_urls opened the urls.
872  ASSERT_EQ(1u, chrome::GetBrowserCount(profile_urls, original_desktop_type));
873  new_browser = FindOneOtherBrowserForProfile(profile_urls, NULL);
874  ASSERT_TRUE(new_browser);
875  tab_strip = new_browser->tab_strip_model();
876  ASSERT_EQ(1, tab_strip->count());
877  EXPECT_EQ(urls[0], tab_strip->GetWebContentsAt(0)->GetURL());
878
879  // profile_last opened the last open pages.
880  ASSERT_EQ(1u, chrome::GetBrowserCount(profile_last, original_desktop_type));
881  new_browser = FindOneOtherBrowserForProfile(profile_last, NULL);
882  ASSERT_TRUE(new_browser);
883  tab_strip = new_browser->tab_strip_model();
884  ASSERT_EQ(1, tab_strip->count());
885  EXPECT_EQ(GURL(content::kAboutBlankURL),
886            tab_strip->GetWebContentsAt(0)->GetURL());
887
888  // profile_home2 was not launched since it would've only opened the home page.
889  ASSERT_EQ(0u, chrome::GetBrowserCount(profile_home2, original_desktop_type));
890}
891
892IN_PROC_BROWSER_TEST_F(StartupBrowserCreatorTest, ProfilesLaunchedAfterCrash) {
893#if defined(OS_WIN) && defined(USE_ASH)
894  // Disable this test in Metro+Ash for now (http://crbug.com/262796).
895  if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kAshBrowserTests))
896    return;
897#endif
898
899  // After an unclean exit, all profiles will be launched. However, they won't
900  // open any pages automatically.
901
902  ProfileManager* profile_manager = g_browser_process->profile_manager();
903
904  // Create 3 profiles.
905  base::FilePath dest_path1 = profile_manager->user_data_dir().Append(
906      FILE_PATH_LITERAL("New Profile 1"));
907  base::FilePath dest_path2 = profile_manager->user_data_dir().Append(
908      FILE_PATH_LITERAL("New Profile 2"));
909  base::FilePath dest_path3 = profile_manager->user_data_dir().Append(
910      FILE_PATH_LITERAL("New Profile 3"));
911
912  Profile* profile_home = profile_manager->GetProfile(dest_path1);
913  ASSERT_TRUE(profile_home);
914  Profile* profile_last = profile_manager->GetProfile(dest_path2);
915  ASSERT_TRUE(profile_last);
916  Profile* profile_urls = profile_manager->GetProfile(dest_path3);
917  ASSERT_TRUE(profile_urls);
918
919  // Set the profiles to open the home page, last visited pages or URLs.
920  SessionStartupPref pref_home(SessionStartupPref::DEFAULT);
921  SessionStartupPref::SetStartupPref(profile_home, pref_home);
922
923  SessionStartupPref pref_last(SessionStartupPref::LAST);
924  SessionStartupPref::SetStartupPref(profile_last, pref_last);
925
926  std::vector<GURL> urls;
927  urls.push_back(ui_test_utils::GetTestUrl(
928      base::FilePath(base::FilePath::kCurrentDirectory),
929      base::FilePath(FILE_PATH_LITERAL("title1.html"))));
930
931  SessionStartupPref pref_urls(SessionStartupPref::URLS);
932  pref_urls.urls = urls;
933  SessionStartupPref::SetStartupPref(profile_urls, pref_urls);
934
935  // Simulate a launch after an unclear exit.
936  browser()->window()->Close();
937  static_cast<ProfileImpl*>(profile_home)->last_session_exit_type_ =
938      Profile::EXIT_CRASHED;
939  static_cast<ProfileImpl*>(profile_last)->last_session_exit_type_ =
940      Profile::EXIT_CRASHED;
941  static_cast<ProfileImpl*>(profile_urls)->last_session_exit_type_ =
942      Profile::EXIT_CRASHED;
943
944  CommandLine dummy(CommandLine::NO_PROGRAM);
945  dummy.AppendSwitchASCII(switches::kTestType, "browser");
946  int return_code;
947  StartupBrowserCreator browser_creator;
948  std::vector<Profile*> last_opened_profiles;
949  last_opened_profiles.push_back(profile_home);
950  last_opened_profiles.push_back(profile_last);
951  last_opened_profiles.push_back(profile_urls);
952  browser_creator.Start(dummy, profile_manager->user_data_dir(), profile_home,
953                        last_opened_profiles, &return_code);
954
955  // No profiles are getting restored, since they all display the crash info
956  // bar.
957  EXPECT_FALSE(SessionRestore::IsRestoring(profile_home));
958  EXPECT_FALSE(SessionRestore::IsRestoring(profile_last));
959  EXPECT_FALSE(SessionRestore::IsRestoring(profile_urls));
960
961  // The profile which normally opens the home page displays the new tab page.
962  Browser* new_browser = NULL;
963  ASSERT_EQ(1u, chrome::GetBrowserCount(profile_home,
964                                        browser()->host_desktop_type()));
965  new_browser = FindOneOtherBrowserForProfile(profile_home, NULL);
966  ASSERT_TRUE(new_browser);
967  TabStripModel* tab_strip = new_browser->tab_strip_model();
968  ASSERT_EQ(1, tab_strip->count());
969  content::WebContents* web_contents = tab_strip->GetWebContentsAt(0);
970  EXPECT_EQ(GURL(chrome::kChromeUINewTabURL), web_contents->GetURL());
971  EXPECT_EQ(1U,
972            InfoBarService::FromWebContents(web_contents)->infobar_count());
973
974  // The profile which normally opens last open pages displays the new tab page.
975  ASSERT_EQ(1u, chrome::GetBrowserCount(profile_last,
976                                        browser()->host_desktop_type()));
977  new_browser = FindOneOtherBrowserForProfile(profile_last, NULL);
978  ASSERT_TRUE(new_browser);
979  tab_strip = new_browser->tab_strip_model();
980  ASSERT_EQ(1, tab_strip->count());
981  web_contents = tab_strip->GetWebContentsAt(0);
982  EXPECT_EQ(GURL(chrome::kChromeUINewTabURL), web_contents->GetURL());
983  EXPECT_EQ(1U,
984            InfoBarService::FromWebContents(web_contents)->infobar_count());
985
986  // The profile which normally opens URLs displays the new tab page.
987  ASSERT_EQ(1u, chrome::GetBrowserCount(profile_urls,
988                                        browser()->host_desktop_type()));
989  new_browser = FindOneOtherBrowserForProfile(profile_urls, NULL);
990  ASSERT_TRUE(new_browser);
991  tab_strip = new_browser->tab_strip_model();
992  ASSERT_EQ(1, tab_strip->count());
993  web_contents = tab_strip->GetWebContentsAt(0);
994  EXPECT_EQ(GURL(chrome::kChromeUINewTabURL), web_contents->GetURL());
995  EXPECT_EQ(1U,
996            InfoBarService::FromWebContents(web_contents)->infobar_count());
997}
998
999class ManagedModeBrowserCreatorTest : public InProcessBrowserTest {
1000 protected:
1001  virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
1002    InProcessBrowserTest::SetUpCommandLine(command_line);
1003    command_line->AppendSwitch(switches::kEnableManagedUsers);
1004  }
1005};
1006
1007#if defined(ENABLE_MANAGED_USERS)
1008IN_PROC_BROWSER_TEST_F(ManagedModeBrowserCreatorTest,
1009                       StartupManagedModeProfile) {
1010  // Make this a managed profile.
1011  ManagedUserService* managed_user_service =
1012      ManagedUserServiceFactory::GetForProfile(browser()->profile());
1013  managed_user_service->InitForTesting();
1014
1015  StartupBrowserCreator browser_creator;
1016
1017  // Do a simple non-process-startup browser launch.
1018  CommandLine dummy(CommandLine::NO_PROGRAM);
1019  StartupBrowserCreatorImpl launch(base::FilePath(), dummy, &browser_creator,
1020                                   chrome::startup::IS_FIRST_RUN);
1021  content::WindowedNotificationObserver observer(
1022      content::NOTIFICATION_LOAD_STOP,
1023      content::NotificationService::AllSources());
1024  ASSERT_TRUE(launch.Launch(browser()->profile(), std::vector<GURL>(), false,
1025                            browser()->host_desktop_type()));
1026
1027  // This should have created a new browser window.
1028  Browser* new_browser = FindOneOtherBrowser(browser());
1029  ASSERT_TRUE(new_browser);
1030
1031  TabStripModel* tab_strip = new_browser->tab_strip_model();
1032  // There should be only one tab.
1033  EXPECT_EQ(1, tab_strip->count());
1034}
1035
1036#endif  // defined(ENABLE_MANAGED_USERS)
1037
1038#endif  // !defined(OS_CHROMEOS)
1039
1040// These tests are not applicable to Chrome OS as neither master_preferences nor
1041// the sync promo exist there.
1042#if !defined(OS_CHROMEOS)
1043
1044// On a branded Linux build, policy is required to suppress the first-run
1045// dialog.
1046#if !defined(OS_LINUX) || !defined(GOOGLE_CHROME_BUILD) || \
1047    defined(ENABLE_CONFIGURATION_POLICY)
1048
1049class StartupBrowserCreatorFirstRunTest : public InProcessBrowserTest {
1050 protected:
1051  virtual void SetUpInProcessBrowserTestFixture() OVERRIDE;
1052
1053#if defined(ENABLE_CONFIGURATION_POLICY)
1054  policy::MockConfigurationPolicyProvider provider_;
1055  policy::PolicyMap policy_map_;
1056#endif  // defined(ENABLE_CONFIGURATION_POLICY)
1057};
1058
1059void StartupBrowserCreatorFirstRunTest::SetUpInProcessBrowserTestFixture() {
1060  // Remove the --no-first-run flag from the command line.
1061  CommandLine* command_line = CommandLine::ForCurrentProcess();
1062  CommandLine::StringVector argv = command_line->argv();
1063  const std::string first_run_flag = std::string("--") + switches::kNoFirstRun;
1064#if defined(OS_WIN)
1065  argv.erase(std::find(argv.begin(), argv.end(), ASCIIToWide(first_run_flag)));
1066#else
1067  argv.erase(std::find(argv.begin(), argv.end(), first_run_flag));
1068#endif
1069  command_line->InitFromArgv(argv);
1070
1071#if defined(ENABLE_CONFIGURATION_POLICY)
1072#if defined(OS_LINUX) && defined(GOOGLE_CHROME_BUILD)
1073  // Set a policy that prevents the first-run dialog from being shown.
1074  policy_map_.Set(policy::key::kMetricsReportingEnabled,
1075                  policy::POLICY_LEVEL_MANDATORY, policy::POLICY_SCOPE_USER,
1076                  base::Value::CreateBooleanValue(false), NULL);
1077  provider_.UpdateChromePolicy(policy_map_);
1078#endif  // defined(OS_LINUX) && defined(GOOGLE_CHROME_BUILD)
1079
1080  EXPECT_CALL(provider_, IsInitializationComplete(_))
1081      .WillRepeatedly(Return(true));
1082  EXPECT_CALL(provider_, RegisterPolicyDomain(_)).Times(AnyNumber());
1083  policy::BrowserPolicyConnector::SetPolicyProviderForTesting(&provider_);
1084#endif  // defined(ENABLE_CONFIGURATION_POLICY)
1085}
1086
1087IN_PROC_BROWSER_TEST_F(StartupBrowserCreatorFirstRunTest, SyncPromoForbidden) {
1088  // Consistently enable the welcome page on all platforms.
1089  first_run::SetShouldShowWelcomePage();
1090
1091  // Simulate the following master_preferences:
1092  // {
1093  //  "sync_promo": {
1094  //    "show_on_first_run_allowed": false
1095  //  }
1096  // }
1097  StartupBrowserCreator browser_creator;
1098  browser()->profile()->GetPrefs()->SetBoolean(
1099      prefs::kSyncPromoShowOnFirstRunAllowed, false);
1100
1101  // Do a process-startup browser launch.
1102  CommandLine dummy(CommandLine::NO_PROGRAM);
1103  StartupBrowserCreatorImpl launch(base::FilePath(), dummy, &browser_creator,
1104                                   chrome::startup::IS_FIRST_RUN);
1105  ASSERT_TRUE(launch.Launch(browser()->profile(), std::vector<GURL>(), true,
1106                            browser()->host_desktop_type()));
1107
1108  // This should have created a new browser window.
1109  Browser* new_browser = FindOneOtherBrowser(browser());
1110  ASSERT_TRUE(new_browser);
1111
1112  // Verify that the NTP and the welcome page are shown.
1113  TabStripModel* tab_strip = new_browser->tab_strip_model();
1114  ASSERT_EQ(2, tab_strip->count());
1115  EXPECT_EQ(GURL(chrome::kChromeUINewTabURL),
1116            tab_strip->GetWebContentsAt(0)->GetURL());
1117  EXPECT_EQ(internals::GetWelcomePageURL(),
1118            tab_strip->GetWebContentsAt(1)->GetURL());
1119}
1120
1121IN_PROC_BROWSER_TEST_F(StartupBrowserCreatorFirstRunTest, SyncPromoAllowed) {
1122  // Consistently enable the welcome page on all platforms.
1123  first_run::SetShouldShowWelcomePage();
1124
1125  // Simulate the following master_preferences:
1126  // {
1127  //  "sync_promo": {
1128  //    "show_on_first_run_allowed": true
1129  //  }
1130  // }
1131  StartupBrowserCreator browser_creator;
1132  browser()->profile()->GetPrefs()->SetBoolean(
1133      prefs::kSyncPromoShowOnFirstRunAllowed, true);
1134
1135  // Do a process-startup browser launch.
1136  CommandLine dummy(CommandLine::NO_PROGRAM);
1137  StartupBrowserCreatorImpl launch(base::FilePath(), dummy, &browser_creator,
1138                                   chrome::startup::IS_FIRST_RUN);
1139  ASSERT_TRUE(launch.Launch(browser()->profile(), std::vector<GURL>(), true,
1140                            browser()->host_desktop_type()));
1141
1142  // This should have created a new browser window.
1143  Browser* new_browser = FindOneOtherBrowser(browser());
1144  ASSERT_TRUE(new_browser);
1145
1146  // Verify that the sync promo and the welcome page are shown.
1147  TabStripModel* tab_strip = new_browser->tab_strip_model();
1148  ASSERT_EQ(2, tab_strip->count());
1149  EXPECT_EQ("accounts.google.com",
1150            tab_strip->GetWebContentsAt(0)->GetURL().host());
1151  EXPECT_EQ(internals::GetWelcomePageURL(),
1152            tab_strip->GetWebContentsAt(1)->GetURL());
1153}
1154
1155IN_PROC_BROWSER_TEST_F(StartupBrowserCreatorFirstRunTest,
1156                       FirstRunTabsPromoAllowed) {
1157  // Simulate the following master_preferences:
1158  // {
1159  //  "first_run_tabs" : [
1160  //    "files/title1.html"
1161  //  ],
1162  //  "sync_promo": {
1163  //    "show_on_first_run_allowed": true
1164  //  }
1165  // }
1166  StartupBrowserCreator browser_creator;
1167  browser_creator.AddFirstRunTab(test_server()->GetURL("files/title1.html"));
1168  browser()->profile()->GetPrefs()->SetBoolean(
1169      prefs::kSyncPromoShowOnFirstRunAllowed, true);
1170
1171  // Do a process-startup browser launch.
1172  CommandLine dummy(CommandLine::NO_PROGRAM);
1173  StartupBrowserCreatorImpl launch(base::FilePath(), dummy, &browser_creator,
1174                                   chrome::startup::IS_FIRST_RUN);
1175  ASSERT_TRUE(launch.Launch(browser()->profile(), std::vector<GURL>(), true,
1176                            browser()->host_desktop_type()));
1177
1178  // This should have created a new browser window.
1179  Browser* new_browser = FindOneOtherBrowser(browser());
1180  ASSERT_TRUE(new_browser);
1181
1182  // Verify that the first-run tab is shown and the sync promo has been added.
1183  TabStripModel* tab_strip = new_browser->tab_strip_model();
1184  ASSERT_EQ(2, tab_strip->count());
1185  EXPECT_EQ("accounts.google.com",
1186            tab_strip->GetWebContentsAt(0)->GetURL().host());
1187  EXPECT_EQ("title1.html",
1188            tab_strip->GetWebContentsAt(1)->GetURL().ExtractFileName());
1189}
1190
1191IN_PROC_BROWSER_TEST_F(StartupBrowserCreatorFirstRunTest,
1192                       FirstRunTabsContainSyncPromo) {
1193  // Simulate the following master_preferences:
1194  // {
1195  //  "first_run_tabs" : [
1196  //    "files/title1.html",
1197  //    "chrome://signin/?source=0&next_page=chrome%3A%2F%2Fnewtab%2F"
1198  //  ],
1199  //  "sync_promo": {
1200  //    "show_on_first_run_allowed": true
1201  //  }
1202  // }
1203  StartupBrowserCreator browser_creator;
1204  browser_creator.AddFirstRunTab(test_server()->GetURL("files/title1.html"));
1205  browser_creator.AddFirstRunTab(signin::GetPromoURL(signin::SOURCE_START_PAGE,
1206                                                     false));
1207  browser()->profile()->GetPrefs()->SetBoolean(
1208      prefs::kSyncPromoShowOnFirstRunAllowed, true);
1209
1210  // Do a process-startup browser launch.
1211  CommandLine dummy(CommandLine::NO_PROGRAM);
1212  StartupBrowserCreatorImpl launch(base::FilePath(), dummy, &browser_creator,
1213                                   chrome::startup::IS_FIRST_RUN);
1214  ASSERT_TRUE(launch.Launch(browser()->profile(), std::vector<GURL>(), true,
1215                            browser()->host_desktop_type()));
1216
1217  // This should have created a new browser window.
1218  Browser* new_browser = FindOneOtherBrowser(browser());
1219  ASSERT_TRUE(new_browser);
1220
1221  // Verify that the first-run tabs are shown and no sync promo has been added
1222  // as the first-run tabs contain it already.
1223  TabStripModel* tab_strip = new_browser->tab_strip_model();
1224  ASSERT_EQ(2, tab_strip->count());
1225  EXPECT_EQ("title1.html",
1226            tab_strip->GetWebContentsAt(0)->GetURL().ExtractFileName());
1227  EXPECT_EQ("accounts.google.com",
1228            tab_strip->GetWebContentsAt(1)->GetURL().host());
1229}
1230
1231IN_PROC_BROWSER_TEST_F(StartupBrowserCreatorFirstRunTest,
1232                       FirstRunTabsContainNTPSyncPromoAllowed) {
1233  // Simulate the following master_preferences:
1234  // {
1235  //  "first_run_tabs" : [
1236  //    "new_tab_page",
1237  //    "files/title1.html"
1238  //  ],
1239  //  "sync_promo": {
1240  //    "show_on_first_run_allowed": true
1241  //  }
1242  // }
1243  StartupBrowserCreator browser_creator;
1244  browser_creator.AddFirstRunTab(GURL("new_tab_page"));
1245  browser_creator.AddFirstRunTab(test_server()->GetURL("files/title1.html"));
1246  browser()->profile()->GetPrefs()->SetBoolean(
1247      prefs::kSyncPromoShowOnFirstRunAllowed, true);
1248
1249  // Do a process-startup browser launch.
1250  CommandLine dummy(CommandLine::NO_PROGRAM);
1251  StartupBrowserCreatorImpl launch(base::FilePath(), dummy, &browser_creator,
1252                                   chrome::startup::IS_FIRST_RUN);
1253  ASSERT_TRUE(launch.Launch(browser()->profile(), std::vector<GURL>(), true,
1254                            browser()->host_desktop_type()));
1255
1256  // This should have created a new browser window.
1257  Browser* new_browser = FindOneOtherBrowser(browser());
1258  ASSERT_TRUE(new_browser);
1259
1260  // Verify that the first-run tabs are shown but the NTP that they contain has
1261  // been replaced by the sync promo.
1262  TabStripModel* tab_strip = new_browser->tab_strip_model();
1263  ASSERT_EQ(2, tab_strip->count());
1264  EXPECT_EQ("accounts.google.com",
1265            tab_strip->GetWebContentsAt(0)->GetURL().host());
1266  EXPECT_EQ("title1.html",
1267            tab_strip->GetWebContentsAt(1)->GetURL().ExtractFileName());
1268}
1269
1270IN_PROC_BROWSER_TEST_F(StartupBrowserCreatorFirstRunTest,
1271                       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::kSyncPromoShowOnFirstRunAllowed, 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
1309IN_PROC_BROWSER_TEST_F(StartupBrowserCreatorFirstRunTest,
1310                       FirstRunTabsSyncPromoForbidden) {
1311  // Simulate the following master_preferences:
1312  // {
1313  //  "first_run_tabs" : [
1314  //    "files/title1.html"
1315  //  ],
1316  //  "sync_promo": {
1317  //    "show_on_first_run_allowed": false
1318  //  }
1319  // }
1320  StartupBrowserCreator browser_creator;
1321  browser_creator.AddFirstRunTab(test_server()->GetURL("files/title1.html"));
1322  browser()->profile()->GetPrefs()->SetBoolean(
1323      prefs::kSyncPromoShowOnFirstRunAllowed, false);
1324
1325  // Do a process-startup browser launch.
1326  CommandLine dummy(CommandLine::NO_PROGRAM);
1327  StartupBrowserCreatorImpl launch(base::FilePath(), dummy, &browser_creator,
1328                                   chrome::startup::IS_FIRST_RUN);
1329  ASSERT_TRUE(launch.Launch(browser()->profile(), std::vector<GURL>(), true,
1330                            browser()->host_desktop_type()));
1331
1332  // This should have created a new browser window.
1333  Browser* new_browser = FindOneOtherBrowser(browser());
1334  ASSERT_TRUE(new_browser);
1335
1336  // Verify that the first-run tab is shown and no sync promo has been added.
1337  TabStripModel* tab_strip = new_browser->tab_strip_model();
1338  ASSERT_EQ(1, tab_strip->count());
1339  EXPECT_EQ("title1.html",
1340            tab_strip->GetWebContentsAt(0)->GetURL().ExtractFileName());
1341}
1342
1343#if defined(ENABLE_CONFIGURATION_POLICY)
1344IN_PROC_BROWSER_TEST_F(StartupBrowserCreatorFirstRunTest,
1345                       RestoreOnStartupURLsPolicySpecified) {
1346  // Simulate the following master_preferences:
1347  // {
1348  //  "sync_promo": {
1349  //    "show_on_first_run_allowed": true
1350  //  }
1351  // }
1352  StartupBrowserCreator browser_creator;
1353  browser()->profile()->GetPrefs()->SetBoolean(
1354      prefs::kSyncPromoShowOnFirstRunAllowed, true);
1355
1356  // Set the following user policies:
1357  // * RestoreOnStartup = RestoreOnStartupIsURLs
1358  // * RestoreOnStartupURLs = [ "files/title1.html" ]
1359  policy_map_.Set(policy::key::kRestoreOnStartup,
1360                  policy::POLICY_LEVEL_MANDATORY, policy::POLICY_SCOPE_USER,
1361                  base::Value::CreateIntegerValue(
1362                      SessionStartupPref::kPrefValueURLs),
1363                  NULL);
1364  base::ListValue startup_urls;
1365  startup_urls.Append(base::Value::CreateStringValue(
1366      test_server()->GetURL("files/title1.html").spec()));
1367  policy_map_.Set(policy::key::kRestoreOnStartupURLs,
1368                  policy::POLICY_LEVEL_MANDATORY, policy::POLICY_SCOPE_USER,
1369                  startup_urls.DeepCopy(), NULL);
1370  provider_.UpdateChromePolicy(policy_map_);
1371  base::RunLoop().RunUntilIdle();
1372
1373  // Do a process-startup browser launch.
1374  CommandLine dummy(CommandLine::NO_PROGRAM);
1375  StartupBrowserCreatorImpl launch(base::FilePath(), dummy, &browser_creator,
1376                                   chrome::startup::IS_FIRST_RUN);
1377  ASSERT_TRUE(launch.Launch(browser()->profile(), std::vector<GURL>(), true,
1378                            browser()->host_desktop_type()));
1379
1380  // This should have created a new browser window.
1381  Browser* new_browser = FindOneOtherBrowser(browser());
1382  ASSERT_TRUE(new_browser);
1383
1384  // Verify that the URL specified through policy is shown and no sync promo has
1385  // been added.
1386  TabStripModel* tab_strip = new_browser->tab_strip_model();
1387  ASSERT_EQ(1, tab_strip->count());
1388  EXPECT_EQ("title1.html",
1389            tab_strip->GetWebContentsAt(0)->GetURL().ExtractFileName());
1390}
1391#endif  // defined(ENABLE_CONFIGURATION_POLICY)
1392
1393#endif  // !defined(OS_LINUX) || !defined(GOOGLE_CHROME_BUILD) ||
1394        // defined(ENABLE_CONFIGURATION_POLICY)
1395
1396#endif  // !defined(OS_CHROMEOS)
1397