startup_browser_creator_browsertest.cc revision 90dce4d38c5ff5333bea97d859d4e484e27edf0c
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 "base/command_line.h"
6#include "base/files/file_path.h"
7#include "base/prefs/pref_service.h"
8#include "chrome/browser/browser_process.h"
9#include "chrome/browser/extensions/extension_browsertest.h"
10#include "chrome/browser/extensions/extension_service.h"
11#include "chrome/browser/extensions/extension_system.h"
12#include "chrome/browser/first_run/first_run.h"
13#include "chrome/browser/infobars/infobar_service.h"
14#include "chrome/browser/prefs/session_startup_pref.h"
15#include "chrome/browser/profiles/profile.h"
16#include "chrome/browser/profiles/profile_impl.h"
17#include "chrome/browser/profiles/profile_manager.h"
18#include "chrome/browser/sessions/session_restore.h"
19#include "chrome/browser/ui/browser.h"
20#include "chrome/browser/ui/browser_finder.h"
21#include "chrome/browser/ui/browser_iterator.h"
22#include "chrome/browser/ui/browser_list.h"
23#include "chrome/browser/ui/browser_list_observer.h"
24#include "chrome/browser/ui/browser_window.h"
25#include "chrome/browser/ui/host_desktop.h"
26#include "chrome/browser/ui/startup/startup_browser_creator.h"
27#include "chrome/browser/ui/startup/startup_browser_creator_impl.h"
28#include "chrome/browser/ui/tabs/tab_strip_model.h"
29#include "chrome/browser/ui/webui/sync_promo/sync_promo_ui.h"
30#include "chrome/common/chrome_switches.h"
31#include "chrome/common/pref_names.h"
32#include "chrome/common/url_constants.h"
33#include "chrome/test/base/in_process_browser_test.h"
34#include "chrome/test/base/ui_test_utils.h"
35#include "content/public/browser/web_contents.h"
36#include "testing/gtest/include/gtest/gtest.h"
37
38#if defined(ENABLE_MANAGED_USERS)
39#include "chrome/browser/managed_mode/managed_mode_navigation_observer.h"
40#include "chrome/browser/managed_mode/managed_user_service.h"
41#include "chrome/browser/managed_mode/managed_user_service_factory.h"
42#endif
43
44using extensions::Extension;
45
46namespace {
47
48// Check that there are two browsers. Find the one that is not |browser|.
49Browser* FindOneOtherBrowser(Browser* browser) {
50  // There should only be one other browser.
51  EXPECT_EQ(2u, chrome::GetBrowserCount(browser->profile(),
52                                        browser->host_desktop_type()));
53
54  // Find the new browser.
55  Browser* other_browser = NULL;
56  for (chrome::BrowserIterator it; !it.done() && !other_browser; it.Next()) {
57    if (*it != browser)
58      other_browser = *it;
59  }
60  return other_browser;
61}
62
63}  // namespace
64
65class StartupBrowserCreatorTest : public ExtensionBrowserTest {
66 protected:
67  virtual bool SetUpUserDataDirectory() OVERRIDE {
68    // Make sure the first run sentinel file exists before running these tests,
69    // since some of them customize the session startup pref whose value can
70    // be different than the default during the first run.
71    // TODO(bauerb): set the first run flag instead of creating a sentinel file.
72    first_run::CreateSentinel();
73    return ExtensionBrowserTest::SetUpUserDataDirectory();
74  }
75
76  virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
77    ExtensionBrowserTest::SetUpCommandLine(command_line);
78    command_line->AppendSwitch(switches::kEnablePanels);
79    command_line->AppendSwitchASCII(switches::kHomePage,
80                                    content::kAboutBlankURL);
81#if defined(OS_CHROMEOS)
82    // TODO(nkostylev): Investigate if we can remove this switch.
83    command_line->AppendSwitch(switches::kCreateBrowserOnStartupForTests);
84#endif
85  }
86
87  // Helper functions return void so that we can ASSERT*().
88  // Use ASSERT_NO_FATAL_FAILURE around calls to these functions to stop the
89  // test if an assert fails.
90  void LoadApp(const std::string& app_name,
91               const Extension** out_app_extension) {
92    ASSERT_TRUE(LoadExtension(test_data_dir_.AppendASCII(app_name.c_str())));
93
94    ExtensionService* service = extensions::ExtensionSystem::Get(
95        browser()->profile())->extension_service();
96    *out_app_extension = service->GetExtensionById(
97        last_loaded_extension_id_, false);
98    ASSERT_TRUE(*out_app_extension);
99
100    // Code that opens a new browser assumes we start with exactly one.
101    ASSERT_EQ(1u, chrome::GetBrowserCount(browser()->profile(),
102                                          browser()->host_desktop_type()));
103  }
104
105  void SetAppLaunchPref(const std::string& app_id,
106                        extensions::ExtensionPrefs::LaunchType launch_type) {
107    ExtensionService* service = extensions::ExtensionSystem::Get(
108        browser()->profile())->extension_service();
109    service->extension_prefs()->SetLaunchType(app_id, launch_type);
110  }
111
112  Browser* FindOneOtherBrowserForProfile(Profile* profile,
113                                         Browser* not_this_browser) {
114    for (chrome::BrowserIterator it; !it.done(); it.Next()) {
115      if (*it != not_this_browser && it->profile() == profile)
116        return *it;
117    }
118    return NULL;
119  }
120};
121
122class OpenURLsPopupObserver : public chrome::BrowserListObserver {
123 public:
124  OpenURLsPopupObserver() : added_browser_(NULL) { }
125
126  virtual void OnBrowserAdded(Browser* browser) OVERRIDE {
127    added_browser_ = browser;
128  }
129
130  virtual void OnBrowserRemoved(Browser* browser) OVERRIDE { }
131
132  Browser* added_browser_;
133};
134
135// Test that when there is a popup as the active browser any requests to
136// StartupBrowserCreatorImpl::OpenURLsInBrowser don't crash because there's no
137// explicit profile given.
138IN_PROC_BROWSER_TEST_F(StartupBrowserCreatorTest, OpenURLsPopup) {
139  std::vector<GURL> urls;
140  urls.push_back(GURL("http://localhost"));
141
142  // Note that in our testing we do not ever query the BrowserList for the "last
143  // active" browser. That's because the browsers are set as "active" by
144  // platform UI toolkit messages, and those messages are not sent during unit
145  // testing sessions.
146
147  OpenURLsPopupObserver observer;
148  BrowserList::AddObserver(&observer);
149
150  Browser* popup = new Browser(
151      Browser::CreateParams(Browser::TYPE_POPUP, browser()->profile(),
152                            browser()->host_desktop_type()));
153  ASSERT_TRUE(popup->is_type_popup());
154  ASSERT_EQ(popup, observer.added_browser_);
155
156  CommandLine dummy(CommandLine::NO_PROGRAM);
157  chrome::startup::IsFirstRun first_run = first_run::IsChromeFirstRun() ?
158      chrome::startup::IS_FIRST_RUN : chrome::startup::IS_NOT_FIRST_RUN;
159  StartupBrowserCreatorImpl launch(base::FilePath(), dummy, first_run);
160  // This should create a new window, but re-use the profile from |popup|. If
161  // it used a NULL or invalid profile, it would crash.
162  launch.OpenURLsInBrowser(popup, false, urls,
163                           chrome::HOST_DESKTOP_TYPE_NATIVE);
164  ASSERT_NE(popup, observer.added_browser_);
165  BrowserList::RemoveObserver(&observer);
166}
167
168// We don't do non-process-startup browser launches on ChromeOS.
169// Session restore for process-startup browser launches is tested
170// in session_restore_uitest.
171#if !defined(OS_CHROMEOS)
172// Verify that startup URLs are honored when the process already exists but has
173// no tabbed browser windows (eg. as if the process is running only due to a
174// background application.
175IN_PROC_BROWSER_TEST_F(StartupBrowserCreatorTest,
176                       StartupURLsOnNewWindowWithNoTabbedBrowsers) {
177  // Use a couple same-site HTTP URLs.
178  ASSERT_TRUE(test_server()->Start());
179  std::vector<GURL> urls;
180  urls.push_back(test_server()->GetURL("files/title1.html"));
181  urls.push_back(test_server()->GetURL("files/title2.html"));
182
183  // Set the startup preference to open these URLs.
184  SessionStartupPref pref(SessionStartupPref::URLS);
185  pref.urls = urls;
186  SessionStartupPref::SetStartupPref(browser()->profile(), pref);
187
188  // Close the browser.
189  browser()->window()->Close();
190
191  // Do a simple non-process-startup browser launch.
192  CommandLine dummy(CommandLine::NO_PROGRAM);
193  chrome::startup::IsFirstRun first_run = first_run::IsChromeFirstRun() ?
194      chrome::startup::IS_FIRST_RUN : chrome::startup::IS_NOT_FIRST_RUN;
195  StartupBrowserCreatorImpl launch(base::FilePath(), dummy, first_run);
196  ASSERT_TRUE(launch.Launch(browser()->profile(), std::vector<GURL>(), false,
197                            browser()->host_desktop_type()));
198
199  // This should have created a new browser window.  |browser()| is still
200  // around at this point, even though we've closed its window.
201  Browser* new_browser = FindOneOtherBrowser(browser());
202  ASSERT_TRUE(new_browser);
203
204  // The new browser should have one tab for each URL.
205  TabStripModel* tab_strip = new_browser->tab_strip_model();
206  ASSERT_EQ(static_cast<int>(urls.size()), tab_strip->count());
207  for (size_t i=0; i < urls.size(); i++) {
208    EXPECT_EQ(urls[i], tab_strip->GetWebContentsAt(i)->GetURL());
209  }
210
211  // The two tabs, despite having the same site, should be in different
212  // SiteInstances.
213  EXPECT_NE(tab_strip->GetWebContentsAt(0)->GetSiteInstance(),
214            tab_strip->GetWebContentsAt(1)->GetSiteInstance());
215}
216
217// Verify that startup URLs aren't used when the process already exists
218// and has other tabbed browser windows.  This is the common case of starting a
219// new browser.
220IN_PROC_BROWSER_TEST_F(StartupBrowserCreatorTest,
221                       StartupURLsOnNewWindow) {
222  // Use a couple arbitrary URLs.
223  std::vector<GURL> urls;
224  urls.push_back(ui_test_utils::GetTestUrl(
225      base::FilePath(base::FilePath::kCurrentDirectory),
226      base::FilePath(FILE_PATH_LITERAL("title1.html"))));
227  urls.push_back(ui_test_utils::GetTestUrl(
228      base::FilePath(base::FilePath::kCurrentDirectory),
229      base::FilePath(FILE_PATH_LITERAL("title2.html"))));
230
231  // Set the startup preference to open these URLs.
232  SessionStartupPref pref(SessionStartupPref::URLS);
233  pref.urls = urls;
234  SessionStartupPref::SetStartupPref(browser()->profile(), pref);
235
236  // Do a simple non-process-startup browser launch.
237  CommandLine dummy(CommandLine::NO_PROGRAM);
238  chrome::startup::IsFirstRun first_run = first_run::IsChromeFirstRun() ?
239      chrome::startup::IS_FIRST_RUN : chrome::startup::IS_NOT_FIRST_RUN;
240  StartupBrowserCreatorImpl launch(base::FilePath(), dummy, first_run);
241  ASSERT_TRUE(launch.Launch(browser()->profile(), std::vector<GURL>(), false,
242                            browser()->host_desktop_type()));
243
244  // This should have created a new browser window.
245  Browser* new_browser = FindOneOtherBrowser(browser());
246  ASSERT_TRUE(new_browser);
247
248  // The new browser should have exactly one tab (not the startup URLs).
249  ASSERT_EQ(1, new_browser->tab_strip_model()->count());
250}
251
252// App shortcuts are not implemented on mac os.
253#if !defined(OS_MACOSX)
254IN_PROC_BROWSER_TEST_F(StartupBrowserCreatorTest, OpenAppShortcutNoPref) {
255  // Load an app with launch.container = 'tab'.
256  const Extension* extension_app = NULL;
257  ASSERT_NO_FATAL_FAILURE(LoadApp("app_with_tab_container", &extension_app));
258
259  // Add --app-id=<extension->id()> to the command line.
260  CommandLine command_line(CommandLine::NO_PROGRAM);
261  command_line.AppendSwitchASCII(switches::kAppId, extension_app->id());
262
263  chrome::startup::IsFirstRun first_run = first_run::IsChromeFirstRun() ?
264      chrome::startup::IS_FIRST_RUN : chrome::startup::IS_NOT_FIRST_RUN;
265  StartupBrowserCreatorImpl launch(base::FilePath(), command_line, first_run);
266  ASSERT_TRUE(launch.Launch(browser()->profile(), std::vector<GURL>(), false,
267                            browser()->host_desktop_type()));
268
269  // No pref was set, so the app should have opened in a window.
270  // The launch should have created a new browser.
271  Browser* new_browser = FindOneOtherBrowser(browser());
272  ASSERT_TRUE(new_browser);
273
274  // Expect an app window.
275  EXPECT_TRUE(new_browser->is_app());
276
277  // The browser's app_name should include the app's ID.
278  EXPECT_NE(
279      new_browser->app_name_.find(extension_app->id()),
280      std::string::npos) << new_browser->app_name_;
281}
282
283IN_PROC_BROWSER_TEST_F(StartupBrowserCreatorTest, OpenAppShortcutWindowPref) {
284  const Extension* extension_app = NULL;
285  ASSERT_NO_FATAL_FAILURE(LoadApp("app_with_tab_container", &extension_app));
286
287  // Set a pref indicating that the user wants to open this app in a window.
288  SetAppLaunchPref(extension_app->id(),
289                   extensions::ExtensionPrefs::LAUNCH_WINDOW);
290
291  CommandLine command_line(CommandLine::NO_PROGRAM);
292  command_line.AppendSwitchASCII(switches::kAppId, extension_app->id());
293  chrome::startup::IsFirstRun first_run = first_run::IsChromeFirstRun() ?
294      chrome::startup::IS_FIRST_RUN : chrome::startup::IS_NOT_FIRST_RUN;
295  StartupBrowserCreatorImpl launch(base::FilePath(), command_line, first_run);
296  ASSERT_TRUE(launch.Launch(browser()->profile(), std::vector<GURL>(), false,
297                            browser()->host_desktop_type()));
298
299  // Pref was set to open in a window, so the app should have opened in a
300  // window.  The launch should have created a new browser. Find the new
301  // browser.
302  Browser* new_browser = FindOneOtherBrowser(browser());
303  ASSERT_TRUE(new_browser);
304
305  // Expect an app window.
306  EXPECT_TRUE(new_browser->is_app());
307
308  // The browser's app_name should include the app's ID.
309  EXPECT_NE(
310      new_browser->app_name_.find(extension_app->id()),
311      std::string::npos) << new_browser->app_name_;
312}
313
314IN_PROC_BROWSER_TEST_F(StartupBrowserCreatorTest, OpenAppShortcutTabPref) {
315  // Load an app with launch.container = 'tab'.
316  const Extension* extension_app = NULL;
317  ASSERT_NO_FATAL_FAILURE(LoadApp("app_with_tab_container", &extension_app));
318
319  // Set a pref indicating that the user wants to open this app in a window.
320  SetAppLaunchPref(extension_app->id(),
321                   extensions::ExtensionPrefs::LAUNCH_REGULAR);
322
323  CommandLine command_line(CommandLine::NO_PROGRAM);
324  command_line.AppendSwitchASCII(switches::kAppId, extension_app->id());
325  chrome::startup::IsFirstRun first_run = first_run::IsChromeFirstRun() ?
326      chrome::startup::IS_FIRST_RUN : chrome::startup::IS_NOT_FIRST_RUN;
327  StartupBrowserCreatorImpl launch(base::FilePath(), command_line, first_run);
328  ASSERT_TRUE(launch.Launch(browser()->profile(), std::vector<GURL>(), false,
329                            browser()->host_desktop_type()));
330
331  // When an app shortcut is open and the pref indicates a tab should
332  // open, the tab is open in a new browser window.  Expect a new window.
333  ASSERT_EQ(2u, chrome::GetBrowserCount(browser()->profile(),
334                                        browser()->host_desktop_type()));
335
336  Browser* new_browser = FindOneOtherBrowser(browser());
337  ASSERT_TRUE(new_browser);
338
339  // The tab should be in a tabbed window.
340  EXPECT_TRUE(new_browser->is_type_tabbed());
341
342  // The browser's app_name should not include the app's ID: It is in a
343  // normal browser.
344  EXPECT_EQ(
345      new_browser->app_name_.find(extension_app->id()),
346      std::string::npos) << new_browser->app_name_;
347}
348
349#endif  // !defined(OS_MACOSX)
350
351#endif  // !defined(OS_CHROMEOS)
352
353IN_PROC_BROWSER_TEST_F(StartupBrowserCreatorTest,
354                       ReadingWasRestartedAfterRestart) {
355  // Tests that StartupBrowserCreator::WasRestarted reads and resets the
356  // preference kWasRestarted correctly.
357  StartupBrowserCreator::was_restarted_read_ = false;
358  PrefService* pref_service = g_browser_process->local_state();
359  pref_service->SetBoolean(prefs::kWasRestarted, true);
360  EXPECT_TRUE(StartupBrowserCreator::WasRestarted());
361  EXPECT_FALSE(pref_service->GetBoolean(prefs::kWasRestarted));
362  EXPECT_TRUE(StartupBrowserCreator::WasRestarted());
363}
364
365IN_PROC_BROWSER_TEST_F(StartupBrowserCreatorTest,
366                       ReadingWasRestartedAfterNormalStart) {
367  // Tests that StartupBrowserCreator::WasRestarted reads and resets the
368  // preference kWasRestarted correctly.
369  StartupBrowserCreator::was_restarted_read_ = false;
370  PrefService* pref_service = g_browser_process->local_state();
371  pref_service->SetBoolean(prefs::kWasRestarted, false);
372  EXPECT_FALSE(StartupBrowserCreator::WasRestarted());
373  EXPECT_FALSE(pref_service->GetBoolean(prefs::kWasRestarted));
374  EXPECT_FALSE(StartupBrowserCreator::WasRestarted());
375}
376
377IN_PROC_BROWSER_TEST_F(StartupBrowserCreatorTest, AddFirstRunTab) {
378  StartupBrowserCreator browser_creator;
379  browser_creator.AddFirstRunTab(test_server()->GetURL("files/title1.html"));
380  browser_creator.AddFirstRunTab(test_server()->GetURL("files/title2.html"));
381
382  // Do a simple non-process-startup browser launch.
383  CommandLine dummy(CommandLine::NO_PROGRAM);
384  StartupBrowserCreatorImpl launch(base::FilePath(), dummy, &browser_creator,
385                                   chrome::startup::IS_FIRST_RUN);
386  ASSERT_TRUE(launch.Launch(browser()->profile(), std::vector<GURL>(), false,
387                            browser()->host_desktop_type()));
388
389  // This should have created a new browser window.
390  Browser* new_browser = FindOneOtherBrowser(browser());
391  ASSERT_TRUE(new_browser);
392
393  TabStripModel* tab_strip = new_browser->tab_strip_model();
394  EXPECT_EQ(2, tab_strip->count());
395
396  EXPECT_EQ("title1.html",
397            tab_strip->GetWebContentsAt(0)->GetURL().ExtractFileName());
398  EXPECT_EQ("title2.html",
399            tab_strip->GetWebContentsAt(1)->GetURL().ExtractFileName());
400}
401
402// Test hard-coded special first run tabs (defined in
403// StartupBrowserCreatorImpl::AddStartupURLs()).
404IN_PROC_BROWSER_TEST_F(StartupBrowserCreatorTest, AddCustomFirstRunTab) {
405  StartupBrowserCreator browser_creator;
406  browser_creator.AddFirstRunTab(test_server()->GetURL("files/title1.html"));
407  browser_creator.AddFirstRunTab(GURL("http://new_tab_page"));
408  browser_creator.AddFirstRunTab(test_server()->GetURL("files/title2.html"));
409  browser_creator.AddFirstRunTab(GURL("http://welcome_page"));
410
411  // Do a simple non-process-startup browser launch.
412  CommandLine dummy(CommandLine::NO_PROGRAM);
413  StartupBrowserCreatorImpl launch(base::FilePath(), dummy, &browser_creator,
414                                   chrome::startup::IS_FIRST_RUN);
415  ASSERT_TRUE(launch.Launch(browser()->profile(), std::vector<GURL>(), false,
416                            browser()->host_desktop_type()));
417
418  // This should have created a new browser window.
419  Browser* new_browser = FindOneOtherBrowser(browser());
420  ASSERT_TRUE(new_browser);
421
422  TabStripModel* tab_strip = new_browser->tab_strip_model();
423  EXPECT_EQ(4, tab_strip->count());
424
425  EXPECT_EQ("title1.html",
426            tab_strip->GetWebContentsAt(0)->GetURL().ExtractFileName());
427  EXPECT_EQ(GURL(chrome::kChromeUINewTabURL),
428            tab_strip->GetWebContentsAt(1)->GetURL());
429  EXPECT_EQ("title2.html",
430            tab_strip->GetWebContentsAt(2)->GetURL().ExtractFileName());
431  EXPECT_EQ(internals::GetWelcomePageURL(),
432            tab_strip->GetWebContentsAt(3)->GetURL());
433}
434
435IN_PROC_BROWSER_TEST_F(StartupBrowserCreatorTest, SyncPromoNoWelcomePage) {
436  // Trick this test into thinking the promo has been shown for this profile; so
437  // that it will show it again (otherwise it skips showing it since
438  // --no-first-run is specified in browser tests).
439  SyncPromoUI::DidShowSyncPromoAtStartup(browser()->profile());
440
441  // Do a simple non-process-startup browser launch.
442  CommandLine dummy(CommandLine::NO_PROGRAM);
443  StartupBrowserCreatorImpl launch(base::FilePath(), dummy,
444                                   chrome::startup::IS_FIRST_RUN);
445  ASSERT_TRUE(launch.Launch(browser()->profile(), std::vector<GURL>(), false,
446                            browser()->host_desktop_type()));
447
448  // This should have created a new browser window.
449  Browser* new_browser = FindOneOtherBrowser(browser());
450  ASSERT_TRUE(new_browser);
451
452  TabStripModel* tab_strip = new_browser->tab_strip_model();
453  EXPECT_EQ(1, tab_strip->count());
454
455  if (SyncPromoUI::ShouldShowSyncPromoAtStartup(browser()->profile(), true)) {
456    EXPECT_EQ("signin", tab_strip->GetWebContentsAt(0)->GetURL().host());
457  } else {
458    EXPECT_EQ(GURL(chrome::kChromeUINewTabURL),
459              tab_strip->GetWebContentsAt(0)->GetURL());
460  }
461}
462
463IN_PROC_BROWSER_TEST_F(StartupBrowserCreatorTest, SyncPromoWithWelcomePage) {
464  // Trick this test into thinking the promo has been shown for this profile; so
465  // that it will show it again (otherwise it skips showing it since
466  // --no-first-run is specified in browser tests).
467  SyncPromoUI::DidShowSyncPromoAtStartup(browser()->profile());
468  first_run::SetShouldShowWelcomePage();
469
470  // Do a simple non-process-startup browser launch.
471  CommandLine dummy(CommandLine::NO_PROGRAM);
472  StartupBrowserCreatorImpl launch(base::FilePath(), dummy,
473                                   chrome::startup::IS_FIRST_RUN);
474  ASSERT_TRUE(launch.Launch(browser()->profile(), std::vector<GURL>(), false,
475                            browser()->host_desktop_type()));
476
477  // This should have created a new browser window.
478  Browser* new_browser = FindOneOtherBrowser(browser());
479  ASSERT_TRUE(new_browser);
480
481  TabStripModel* tab_strip = new_browser->tab_strip_model();
482  EXPECT_EQ(2, tab_strip->count());
483
484  if (SyncPromoUI::ShouldShowSyncPromoAtStartup(browser()->profile(), true)) {
485    EXPECT_EQ("signin", tab_strip->GetWebContentsAt(0)->GetURL().host());
486  } else {
487    EXPECT_EQ(GURL(chrome::kChromeUINewTabURL),
488              tab_strip->GetWebContentsAt(0)->GetURL());
489  }
490  EXPECT_EQ(internals::GetWelcomePageURL(),
491            tab_strip->GetWebContentsAt(1)->GetURL());
492}
493
494IN_PROC_BROWSER_TEST_F(StartupBrowserCreatorTest, SyncPromoWithFirstRunTabs) {
495  StartupBrowserCreator browser_creator;
496  browser_creator.AddFirstRunTab(test_server()->GetURL("files/title1.html"));
497
498  // Trick this test into thinking the promo has been shown for this profile; so
499  // that it will show it again (otherwise it skips showing it since
500  // --no-first-run is specified in browser tests).
501  SyncPromoUI::DidShowSyncPromoAtStartup(browser()->profile());
502
503  // The welcome page should not be shown, even if
504  // first_run::ShouldShowWelcomePage() says so, when there are already
505  // more than 2 first run tabs.
506  first_run::SetShouldShowWelcomePage();
507
508  // Do a simple non-process-startup browser launch.
509  CommandLine dummy(CommandLine::NO_PROGRAM);
510  StartupBrowserCreatorImpl launch(base::FilePath(), dummy, &browser_creator,
511                                   chrome::startup::IS_FIRST_RUN);
512  ASSERT_TRUE(launch.Launch(browser()->profile(), std::vector<GURL>(), false,
513                            browser()->host_desktop_type()));
514
515  // This should have created a new browser window.
516  Browser* new_browser = FindOneOtherBrowser(browser());
517  ASSERT_TRUE(new_browser);
518
519  TabStripModel* tab_strip = new_browser->tab_strip_model();
520  if (SyncPromoUI::ShouldShowSyncPromoAtStartup(browser()->profile(), true)) {
521    EXPECT_EQ(2, tab_strip->count());
522    EXPECT_EQ("signin", tab_strip->GetWebContentsAt(0)->GetURL().host());
523    EXPECT_EQ("title1.html",
524              tab_strip->GetWebContentsAt(1)->GetURL().ExtractFileName());
525  } else {
526    EXPECT_EQ(1, tab_strip->count());
527    EXPECT_EQ("title1.html",
528              tab_strip->GetWebContentsAt(0)->GetURL().ExtractFileName());
529  }
530}
531
532// The welcome page should still be shown if there are more than 2 first run
533// tabs, but the welcome page was explcitly added to the first run tabs.
534IN_PROC_BROWSER_TEST_F(StartupBrowserCreatorTest,
535                       SyncPromoWithFirstRunTabsIncludingWelcomePage) {
536  StartupBrowserCreator browser_creator;
537  browser_creator.AddFirstRunTab(test_server()->GetURL("files/title1.html"));
538  browser_creator.AddFirstRunTab(GURL("http://welcome_page"));
539
540  // Trick this test into thinking the promo has been shown for this profile; so
541  // that it will show it again (otherwise it skips showing it since
542  // --no-first-run is specified in browser tests).
543  SyncPromoUI::DidShowSyncPromoAtStartup(browser()->profile());
544
545  // Do a simple non-process-startup browser launch.
546  CommandLine dummy(CommandLine::NO_PROGRAM);
547  StartupBrowserCreatorImpl launch(base::FilePath(), dummy, &browser_creator,
548                                   chrome::startup::IS_FIRST_RUN);
549  ASSERT_TRUE(launch.Launch(browser()->profile(), std::vector<GURL>(), false,
550                            browser()->host_desktop_type()));
551
552  // This should have created a new browser window.
553  Browser* new_browser = FindOneOtherBrowser(browser());
554  ASSERT_TRUE(new_browser);
555
556  TabStripModel* tab_strip = new_browser->tab_strip_model();
557  if (SyncPromoUI::ShouldShowSyncPromoAtStartup(browser()->profile(), true)) {
558    EXPECT_EQ(3, tab_strip->count());
559    EXPECT_EQ("signin", tab_strip->GetWebContentsAt(0)->GetURL().host());
560    EXPECT_EQ("title1.html",
561              tab_strip->GetWebContentsAt(1)->GetURL().ExtractFileName());
562    EXPECT_EQ(internals::GetWelcomePageURL(),
563              tab_strip->GetWebContentsAt(2)->GetURL());
564  } else {
565    EXPECT_EQ(2, tab_strip->count());
566    EXPECT_EQ("title1.html",
567              tab_strip->GetWebContentsAt(0)->GetURL().ExtractFileName());
568    EXPECT_EQ(internals::GetWelcomePageURL(),
569              tab_strip->GetWebContentsAt(1)->GetURL());
570  }
571}
572
573#if !defined(OS_CHROMEOS)
574IN_PROC_BROWSER_TEST_F(StartupBrowserCreatorTest, StartupURLsForTwoProfiles) {
575  Profile* default_profile = browser()->profile();
576
577  ProfileManager* profile_manager = g_browser_process->profile_manager();
578  // Create another profile.
579  base::FilePath dest_path = profile_manager->user_data_dir();
580  dest_path = dest_path.Append(FILE_PATH_LITERAL("New Profile 1"));
581
582  Profile* other_profile = profile_manager->GetProfile(dest_path);
583  ASSERT_TRUE(other_profile);
584
585  // Use a couple arbitrary URLs.
586  std::vector<GURL> urls1;
587  urls1.push_back(ui_test_utils::GetTestUrl(
588      base::FilePath(base::FilePath::kCurrentDirectory),
589      base::FilePath(FILE_PATH_LITERAL("title1.html"))));
590  std::vector<GURL> urls2;
591  urls2.push_back(ui_test_utils::GetTestUrl(
592      base::FilePath(base::FilePath::kCurrentDirectory),
593      base::FilePath(FILE_PATH_LITERAL("title2.html"))));
594
595  // Set different startup preferences for the 2 profiles.
596  SessionStartupPref pref1(SessionStartupPref::URLS);
597  pref1.urls = urls1;
598  SessionStartupPref::SetStartupPref(default_profile, pref1);
599  SessionStartupPref pref2(SessionStartupPref::URLS);
600  pref2.urls = urls2;
601  SessionStartupPref::SetStartupPref(other_profile, pref2);
602
603  // Close the browser.
604  browser()->window()->Close();
605
606  // Do a simple non-process-startup browser launch.
607  CommandLine dummy(CommandLine::NO_PROGRAM);
608
609  int return_code;
610  StartupBrowserCreator browser_creator;
611  std::vector<Profile*> last_opened_profiles;
612  last_opened_profiles.push_back(default_profile);
613  last_opened_profiles.push_back(other_profile);
614  browser_creator.Start(dummy, profile_manager->user_data_dir(),
615                        default_profile, last_opened_profiles, &return_code);
616
617  // urls1 were opened in a browser for default_profile, and urls2 were opened
618  // in a browser for other_profile.
619  Browser* new_browser = NULL;
620  // |browser()| is still around at this point, even though we've closed its
621  // window. Thus the browser count for default_profile is 2.
622  ASSERT_EQ(2u, chrome::GetBrowserCount(default_profile,
623                                        browser()->host_desktop_type()));
624  new_browser = FindOneOtherBrowserForProfile(default_profile, browser());
625  ASSERT_TRUE(new_browser);
626  TabStripModel* tab_strip = new_browser->tab_strip_model();
627  ASSERT_EQ(1, tab_strip->count());
628  EXPECT_EQ(urls1[0], tab_strip->GetWebContentsAt(0)->GetURL());
629
630  ASSERT_EQ(1u, chrome::GetBrowserCount(other_profile,
631                                        browser()->host_desktop_type()));
632  new_browser = FindOneOtherBrowserForProfile(other_profile, NULL);
633  ASSERT_TRUE(new_browser);
634  tab_strip = new_browser->tab_strip_model();
635  ASSERT_EQ(1, tab_strip->count());
636  EXPECT_EQ(urls2[0], tab_strip->GetWebContentsAt(0)->GetURL());
637}
638
639IN_PROC_BROWSER_TEST_F(StartupBrowserCreatorTest, PRE_UpdateWithTwoProfiles) {
640  // Simulate a browser restart by creating the profiles in the PRE_ part.
641  ProfileManager* profile_manager = g_browser_process->profile_manager();
642
643  // Create two profiles.
644  base::FilePath dest_path = profile_manager->user_data_dir();
645
646  Profile* profile1 = profile_manager->GetProfile(
647      dest_path.Append(FILE_PATH_LITERAL("New Profile 1")));
648  ASSERT_TRUE(profile1);
649
650  Profile* profile2 = profile_manager->GetProfile(
651      dest_path.Append(FILE_PATH_LITERAL("New Profile 2")));
652  ASSERT_TRUE(profile2);
653
654  // Use a couple arbitrary URLs.
655  std::vector<GURL> urls1;
656  urls1.push_back(ui_test_utils::GetTestUrl(
657      base::FilePath(base::FilePath::kCurrentDirectory),
658      base::FilePath(FILE_PATH_LITERAL("title1.html"))));
659  std::vector<GURL> urls2;
660  urls2.push_back(ui_test_utils::GetTestUrl(
661      base::FilePath(base::FilePath::kCurrentDirectory),
662      base::FilePath(FILE_PATH_LITERAL("title2.html"))));
663
664  // Set different startup preferences for the 2 profiles.
665  SessionStartupPref pref1(SessionStartupPref::URLS);
666  pref1.urls = urls1;
667  SessionStartupPref::SetStartupPref(profile1, pref1);
668  SessionStartupPref pref2(SessionStartupPref::URLS);
669  pref2.urls = urls2;
670  SessionStartupPref::SetStartupPref(profile2, pref2);
671
672  profile1->GetPrefs()->CommitPendingWrite();
673  profile2->GetPrefs()->CommitPendingWrite();
674}
675
676IN_PROC_BROWSER_TEST_F(StartupBrowserCreatorTest, UpdateWithTwoProfiles) {
677  // Make StartupBrowserCreator::WasRestarted() return true.
678  StartupBrowserCreator::was_restarted_read_ = false;
679  PrefService* pref_service = g_browser_process->local_state();
680  pref_service->SetBoolean(prefs::kWasRestarted, true);
681
682  ProfileManager* profile_manager = g_browser_process->profile_manager();
683
684  // Open the two profiles.
685  base::FilePath dest_path = profile_manager->user_data_dir();
686
687  Profile* profile1 = profile_manager->GetProfile(
688      dest_path.Append(FILE_PATH_LITERAL("New Profile 1")));
689  ASSERT_TRUE(profile1);
690
691  Profile* profile2 = profile_manager->GetProfile(
692      dest_path.Append(FILE_PATH_LITERAL("New Profile 2")));
693  ASSERT_TRUE(profile2);
694
695  // Simulate a launch after a browser update.
696  CommandLine dummy(CommandLine::NO_PROGRAM);
697  int return_code;
698  StartupBrowserCreator browser_creator;
699  std::vector<Profile*> last_opened_profiles;
700  last_opened_profiles.push_back(profile1);
701  last_opened_profiles.push_back(profile2);
702  browser_creator.Start(dummy, profile_manager->user_data_dir(), profile1,
703                        last_opened_profiles, &return_code);
704
705  while (SessionRestore::IsRestoring(profile1) ||
706         SessionRestore::IsRestoring(profile2))
707    base::MessageLoop::current()->RunUntilIdle();
708
709  // The startup URLs are ignored, and instead the last open sessions are
710  // restored.
711  EXPECT_TRUE(profile1->restored_last_session());
712  EXPECT_TRUE(profile2->restored_last_session());
713
714  Browser* new_browser = NULL;
715  ASSERT_EQ(1u, chrome::GetBrowserCount(profile1,
716                                        browser()->host_desktop_type()));
717  new_browser = FindOneOtherBrowserForProfile(profile1, NULL);
718  ASSERT_TRUE(new_browser);
719  TabStripModel* tab_strip = new_browser->tab_strip_model();
720  ASSERT_EQ(1, tab_strip->count());
721  EXPECT_EQ(GURL(content::kAboutBlankURL),
722            tab_strip->GetWebContentsAt(0)->GetURL());
723
724  ASSERT_EQ(1u, chrome::GetBrowserCount(profile2,
725                                        browser()->host_desktop_type()));
726  new_browser = FindOneOtherBrowserForProfile(profile2, NULL);
727  ASSERT_TRUE(new_browser);
728  tab_strip = new_browser->tab_strip_model();
729  ASSERT_EQ(1, tab_strip->count());
730  EXPECT_EQ(GURL(content::kAboutBlankURL),
731            tab_strip->GetWebContentsAt(0)->GetURL());
732}
733
734IN_PROC_BROWSER_TEST_F(StartupBrowserCreatorTest,
735                       ProfilesWithoutPagesNotLaunched) {
736  Profile* default_profile = browser()->profile();
737
738  ProfileManager* profile_manager = g_browser_process->profile_manager();
739
740  // Create 4 more profiles.
741  base::FilePath dest_path1 = profile_manager->user_data_dir().Append(
742      FILE_PATH_LITERAL("New Profile 1"));
743  base::FilePath dest_path2 = profile_manager->user_data_dir().Append(
744      FILE_PATH_LITERAL("New Profile 2"));
745  base::FilePath dest_path3 = profile_manager->user_data_dir().Append(
746      FILE_PATH_LITERAL("New Profile 3"));
747  base::FilePath dest_path4 = profile_manager->user_data_dir().Append(
748      FILE_PATH_LITERAL("New Profile 4"));
749
750  Profile* profile_home1 = profile_manager->GetProfile(dest_path1);
751  ASSERT_TRUE(profile_home1);
752  Profile* profile_home2 = profile_manager->GetProfile(dest_path2);
753  ASSERT_TRUE(profile_home2);
754  Profile* profile_last = profile_manager->GetProfile(dest_path3);
755  ASSERT_TRUE(profile_last);
756  Profile* profile_urls = profile_manager->GetProfile(dest_path4);
757  ASSERT_TRUE(profile_urls);
758
759  // Set the profiles to open urls, open last visited pages or display the home
760  // page.
761  SessionStartupPref pref_home(SessionStartupPref::DEFAULT);
762  SessionStartupPref::SetStartupPref(profile_home1, pref_home);
763  SessionStartupPref::SetStartupPref(profile_home2, pref_home);
764
765  SessionStartupPref pref_last(SessionStartupPref::LAST);
766  SessionStartupPref::SetStartupPref(profile_last, pref_last);
767
768  std::vector<GURL> urls;
769  urls.push_back(ui_test_utils::GetTestUrl(
770      base::FilePath(base::FilePath::kCurrentDirectory),
771      base::FilePath(FILE_PATH_LITERAL("title1.html"))));
772
773  SessionStartupPref pref_urls(SessionStartupPref::URLS);
774  pref_urls.urls = urls;
775  SessionStartupPref::SetStartupPref(profile_urls, pref_urls);
776
777  // Close the browser.
778  chrome::HostDesktopType original_desktop_type =
779      browser()->host_desktop_type();
780  browser()->window()->Close();
781
782  // Do a simple non-process-startup browser launch.
783  CommandLine dummy(CommandLine::NO_PROGRAM);
784
785  int return_code;
786  StartupBrowserCreator browser_creator;
787  std::vector<Profile*> last_opened_profiles;
788  last_opened_profiles.push_back(profile_home1);
789  last_opened_profiles.push_back(profile_home2);
790  last_opened_profiles.push_back(profile_last);
791  last_opened_profiles.push_back(profile_urls);
792  browser_creator.Start(dummy, profile_manager->user_data_dir(), profile_home1,
793                        last_opened_profiles, &return_code);
794
795  while (SessionRestore::IsRestoring(default_profile) ||
796         SessionRestore::IsRestoring(profile_home1) ||
797         SessionRestore::IsRestoring(profile_home2) ||
798         SessionRestore::IsRestoring(profile_last) ||
799         SessionRestore::IsRestoring(profile_urls))
800    base::MessageLoop::current()->RunUntilIdle();
801
802  Browser* new_browser = NULL;
803  // The last open profile (the profile_home1 in this case) will always be
804  // launched, even if it will open just the home page.
805  ASSERT_EQ(1u, chrome::GetBrowserCount(profile_home1, original_desktop_type));
806  new_browser = FindOneOtherBrowserForProfile(profile_home1, NULL);
807  ASSERT_TRUE(new_browser);
808  TabStripModel* tab_strip = new_browser->tab_strip_model();
809  ASSERT_EQ(1, tab_strip->count());
810  EXPECT_EQ(GURL(chrome::kChromeUINewTabURL),
811            tab_strip->GetWebContentsAt(0)->GetURL());
812
813  // profile_urls opened the urls.
814  ASSERT_EQ(1u, chrome::GetBrowserCount(profile_urls, original_desktop_type));
815  new_browser = FindOneOtherBrowserForProfile(profile_urls, NULL);
816  ASSERT_TRUE(new_browser);
817  tab_strip = new_browser->tab_strip_model();
818  ASSERT_EQ(1, tab_strip->count());
819  EXPECT_EQ(urls[0], tab_strip->GetWebContentsAt(0)->GetURL());
820
821  // profile_last opened the last open pages.
822  ASSERT_EQ(1u, chrome::GetBrowserCount(profile_last, original_desktop_type));
823  new_browser = FindOneOtherBrowserForProfile(profile_last, NULL);
824  ASSERT_TRUE(new_browser);
825  tab_strip = new_browser->tab_strip_model();
826  ASSERT_EQ(1, tab_strip->count());
827  EXPECT_EQ(GURL(content::kAboutBlankURL),
828            tab_strip->GetWebContentsAt(0)->GetURL());
829
830  // profile_home2 was not launched since it would've only opened the home page.
831  ASSERT_EQ(0u, chrome::GetBrowserCount(profile_home2, original_desktop_type));
832}
833
834IN_PROC_BROWSER_TEST_F(StartupBrowserCreatorTest, ProfilesLaunchedAfterCrash) {
835  // After an unclean exit, all profiles will be launched. However, they won't
836  // open any pages automatically.
837
838  ProfileManager* profile_manager = g_browser_process->profile_manager();
839
840  // Create 3 profiles.
841  base::FilePath dest_path1 = profile_manager->user_data_dir().Append(
842      FILE_PATH_LITERAL("New Profile 1"));
843  base::FilePath dest_path2 = profile_manager->user_data_dir().Append(
844      FILE_PATH_LITERAL("New Profile 2"));
845  base::FilePath dest_path3 = profile_manager->user_data_dir().Append(
846      FILE_PATH_LITERAL("New Profile 3"));
847
848  Profile* profile_home = profile_manager->GetProfile(dest_path1);
849  ASSERT_TRUE(profile_home);
850  Profile* profile_last = profile_manager->GetProfile(dest_path2);
851  ASSERT_TRUE(profile_last);
852  Profile* profile_urls = profile_manager->GetProfile(dest_path3);
853  ASSERT_TRUE(profile_urls);
854
855  // Set the profiles to open the home page, last visited pages or URLs.
856  SessionStartupPref pref_home(SessionStartupPref::DEFAULT);
857  SessionStartupPref::SetStartupPref(profile_home, pref_home);
858
859  SessionStartupPref pref_last(SessionStartupPref::LAST);
860  SessionStartupPref::SetStartupPref(profile_last, pref_last);
861
862  std::vector<GURL> urls;
863  urls.push_back(ui_test_utils::GetTestUrl(
864      base::FilePath(base::FilePath::kCurrentDirectory),
865      base::FilePath(FILE_PATH_LITERAL("title1.html"))));
866
867  SessionStartupPref pref_urls(SessionStartupPref::URLS);
868  pref_urls.urls = urls;
869  SessionStartupPref::SetStartupPref(profile_urls, pref_urls);
870
871  // Simulate a launch after an unclear exit.
872  browser()->window()->Close();
873  static_cast<ProfileImpl*>(profile_home)->last_session_exit_type_ =
874      Profile::EXIT_CRASHED;
875  static_cast<ProfileImpl*>(profile_last)->last_session_exit_type_ =
876      Profile::EXIT_CRASHED;
877  static_cast<ProfileImpl*>(profile_urls)->last_session_exit_type_ =
878      Profile::EXIT_CRASHED;
879
880  CommandLine dummy(CommandLine::NO_PROGRAM);
881  dummy.AppendSwitchASCII(switches::kTestType, "browser");
882  int return_code;
883  StartupBrowserCreator browser_creator;
884  std::vector<Profile*> last_opened_profiles;
885  last_opened_profiles.push_back(profile_home);
886  last_opened_profiles.push_back(profile_last);
887  last_opened_profiles.push_back(profile_urls);
888  browser_creator.Start(dummy, profile_manager->user_data_dir(), profile_home,
889                        last_opened_profiles, &return_code);
890
891  // No profiles are getting restored, since they all display the crash info
892  // bar.
893  EXPECT_FALSE(SessionRestore::IsRestoring(profile_home));
894  EXPECT_FALSE(SessionRestore::IsRestoring(profile_last));
895  EXPECT_FALSE(SessionRestore::IsRestoring(profile_urls));
896
897  // The profile which normally opens the home page displays the new tab page.
898  Browser* new_browser = NULL;
899  ASSERT_EQ(1u, chrome::GetBrowserCount(profile_home,
900                                        browser()->host_desktop_type()));
901  new_browser = FindOneOtherBrowserForProfile(profile_home, NULL);
902  ASSERT_TRUE(new_browser);
903  TabStripModel* tab_strip = new_browser->tab_strip_model();
904  ASSERT_EQ(1, tab_strip->count());
905  content::WebContents* web_contents = tab_strip->GetWebContentsAt(0);
906  EXPECT_EQ(GURL(chrome::kChromeUINewTabURL), web_contents->GetURL());
907  EXPECT_EQ(1U,
908            InfoBarService::FromWebContents(web_contents)->infobar_count());
909
910  // The profile which normally opens last open pages displays the new tab page.
911  ASSERT_EQ(1u, chrome::GetBrowserCount(profile_last,
912                                        browser()->host_desktop_type()));
913  new_browser = FindOneOtherBrowserForProfile(profile_last, NULL);
914  ASSERT_TRUE(new_browser);
915  tab_strip = new_browser->tab_strip_model();
916  ASSERT_EQ(1, tab_strip->count());
917  web_contents = tab_strip->GetWebContentsAt(0);
918  EXPECT_EQ(GURL(chrome::kChromeUINewTabURL), web_contents->GetURL());
919  EXPECT_EQ(1U,
920            InfoBarService::FromWebContents(web_contents)->infobar_count());
921
922  // The profile which normally opens URLs displays the new tab page.
923  ASSERT_EQ(1u, chrome::GetBrowserCount(profile_urls,
924                                        browser()->host_desktop_type()));
925  new_browser = FindOneOtherBrowserForProfile(profile_urls, NULL);
926  ASSERT_TRUE(new_browser);
927  tab_strip = new_browser->tab_strip_model();
928  ASSERT_EQ(1, tab_strip->count());
929  web_contents = tab_strip->GetWebContentsAt(0);
930  EXPECT_EQ(GURL(chrome::kChromeUINewTabURL), web_contents->GetURL());
931  EXPECT_EQ(1U,
932            InfoBarService::FromWebContents(web_contents)->infobar_count());
933}
934
935class ManagedModeBrowserCreatorTest : public InProcessBrowserTest {
936 protected:
937  virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
938    InProcessBrowserTest::SetUpCommandLine(command_line);
939    command_line->AppendSwitch(switches::kEnableManagedUsers);
940  }
941};
942
943#if defined(ENABLE_MANAGED_USERS)
944IN_PROC_BROWSER_TEST_F(ManagedModeBrowserCreatorTest,
945                       StartupManagedModeProfile) {
946  // Make this a managed profile.
947  ManagedUserService* managed_user_service =
948      ManagedUserServiceFactory::GetForProfile(browser()->profile());
949  managed_user_service->InitForTesting();
950
951  StartupBrowserCreator browser_creator;
952
953  // Do a simple non-process-startup browser launch.
954  CommandLine dummy(CommandLine::NO_PROGRAM);
955  StartupBrowserCreatorImpl launch(base::FilePath(), dummy, &browser_creator,
956                                   chrome::startup::IS_FIRST_RUN);
957  content::WindowedNotificationObserver observer(
958      content::NOTIFICATION_LOAD_STOP,
959      content::NotificationService::AllSources());
960  ASSERT_TRUE(launch.Launch(browser()->profile(), std::vector<GURL>(), false,
961                            browser()->host_desktop_type()));
962
963  // This should have created a new browser window.
964  Browser* new_browser = FindOneOtherBrowser(browser());
965  ASSERT_TRUE(new_browser);
966
967  TabStripModel* tab_strip = new_browser->tab_strip_model();
968  // There should be only one tab.
969  EXPECT_EQ(1, tab_strip->count());
970}
971
972#endif  // ENABLE_MANAGED_USERS
973
974#endif  // !OS_CHROMEOS
975