startup_browser_creator_browsertest.cc revision c2e0dbddbe15c98d52c4786dac06cb8952a8ae6d
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                                    chrome::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  ASSERT_NE(popup, observer.added_browser_);
164  BrowserList::RemoveObserver(&observer);
165}
166
167// We don't do non-process-startup browser launches on ChromeOS.
168// Session restore for process-startup browser launches is tested
169// in session_restore_uitest.
170#if !defined(OS_CHROMEOS)
171// Verify that startup URLs are honored when the process already exists but has
172// no tabbed browser windows (eg. as if the process is running only due to a
173// background application.
174IN_PROC_BROWSER_TEST_F(StartupBrowserCreatorTest,
175                       StartupURLsOnNewWindowWithNoTabbedBrowsers) {
176  // Use a couple same-site HTTP URLs.
177  ASSERT_TRUE(test_server()->Start());
178  std::vector<GURL> urls;
179  urls.push_back(test_server()->GetURL("files/title1.html"));
180  urls.push_back(test_server()->GetURL("files/title2.html"));
181
182  // Set the startup preference to open these URLs.
183  SessionStartupPref pref(SessionStartupPref::URLS);
184  pref.urls = urls;
185  SessionStartupPref::SetStartupPref(browser()->profile(), pref);
186
187  // Close the browser.
188  browser()->window()->Close();
189
190  // Do a simple non-process-startup browser launch.
191  CommandLine dummy(CommandLine::NO_PROGRAM);
192  chrome::startup::IsFirstRun first_run = first_run::IsChromeFirstRun() ?
193      chrome::startup::IS_FIRST_RUN : chrome::startup::IS_NOT_FIRST_RUN;
194  StartupBrowserCreatorImpl launch(base::FilePath(), dummy, first_run);
195  ASSERT_TRUE(launch.Launch(browser()->profile(), std::vector<GURL>(), false));
196
197  // This should have created a new browser window.  |browser()| is still
198  // around at this point, even though we've closed its window.
199  Browser* new_browser = FindOneOtherBrowser(browser());
200  ASSERT_TRUE(new_browser);
201
202  // The new browser should have one tab for each URL.
203  TabStripModel* tab_strip = new_browser->tab_strip_model();
204  ASSERT_EQ(static_cast<int>(urls.size()), tab_strip->count());
205  for (size_t i=0; i < urls.size(); i++) {
206    EXPECT_EQ(urls[i], tab_strip->GetWebContentsAt(i)->GetURL());
207  }
208
209  // The two tabs, despite having the same site, should be in different
210  // SiteInstances.
211  EXPECT_NE(tab_strip->GetWebContentsAt(0)->GetSiteInstance(),
212            tab_strip->GetWebContentsAt(1)->GetSiteInstance());
213}
214
215// Verify that startup URLs aren't used when the process already exists
216// and has other tabbed browser windows.  This is the common case of starting a
217// new browser.
218IN_PROC_BROWSER_TEST_F(StartupBrowserCreatorTest,
219                       StartupURLsOnNewWindow) {
220  // Use a couple arbitrary URLs.
221  std::vector<GURL> urls;
222  urls.push_back(ui_test_utils::GetTestUrl(
223      base::FilePath(base::FilePath::kCurrentDirectory),
224      base::FilePath(FILE_PATH_LITERAL("title1.html"))));
225  urls.push_back(ui_test_utils::GetTestUrl(
226      base::FilePath(base::FilePath::kCurrentDirectory),
227      base::FilePath(FILE_PATH_LITERAL("title2.html"))));
228
229  // Set the startup preference to open these URLs.
230  SessionStartupPref pref(SessionStartupPref::URLS);
231  pref.urls = urls;
232  SessionStartupPref::SetStartupPref(browser()->profile(), pref);
233
234  // Do a simple non-process-startup browser launch.
235  CommandLine dummy(CommandLine::NO_PROGRAM);
236  chrome::startup::IsFirstRun first_run = first_run::IsChromeFirstRun() ?
237      chrome::startup::IS_FIRST_RUN : chrome::startup::IS_NOT_FIRST_RUN;
238  StartupBrowserCreatorImpl launch(base::FilePath(), dummy, first_run);
239  ASSERT_TRUE(launch.Launch(browser()->profile(), std::vector<GURL>(), false));
240
241  // This should have created a new browser window.
242  Browser* new_browser = FindOneOtherBrowser(browser());
243  ASSERT_TRUE(new_browser);
244
245  // The new browser should have exactly one tab (not the startup URLs).
246  ASSERT_EQ(1, new_browser->tab_strip_model()->count());
247}
248
249// App shortcuts are not implemented on mac os.
250#if !defined(OS_MACOSX)
251IN_PROC_BROWSER_TEST_F(StartupBrowserCreatorTest, OpenAppShortcutNoPref) {
252  // Load an app with launch.container = 'tab'.
253  const Extension* extension_app = NULL;
254  ASSERT_NO_FATAL_FAILURE(LoadApp("app_with_tab_container", &extension_app));
255
256  // Add --app-id=<extension->id()> to the command line.
257  CommandLine command_line(CommandLine::NO_PROGRAM);
258  command_line.AppendSwitchASCII(switches::kAppId, extension_app->id());
259
260  chrome::startup::IsFirstRun first_run = first_run::IsChromeFirstRun() ?
261      chrome::startup::IS_FIRST_RUN : chrome::startup::IS_NOT_FIRST_RUN;
262  StartupBrowserCreatorImpl launch(base::FilePath(), command_line, first_run);
263  ASSERT_TRUE(launch.Launch(browser()->profile(), std::vector<GURL>(), false));
264
265  // No pref was set, so the app should have opened in a window.
266  // The launch should have created a new browser.
267  Browser* new_browser = FindOneOtherBrowser(browser());
268  ASSERT_TRUE(new_browser);
269
270  // Expect an app window.
271  EXPECT_TRUE(new_browser->is_app());
272
273  // The browser's app_name should include the app's ID.
274  EXPECT_NE(
275      new_browser->app_name_.find(extension_app->id()),
276      std::string::npos) << new_browser->app_name_;
277}
278
279IN_PROC_BROWSER_TEST_F(StartupBrowserCreatorTest, OpenAppShortcutWindowPref) {
280  const Extension* extension_app = NULL;
281  ASSERT_NO_FATAL_FAILURE(LoadApp("app_with_tab_container", &extension_app));
282
283  // Set a pref indicating that the user wants to open this app in a window.
284  SetAppLaunchPref(extension_app->id(),
285                   extensions::ExtensionPrefs::LAUNCH_WINDOW);
286
287  CommandLine command_line(CommandLine::NO_PROGRAM);
288  command_line.AppendSwitchASCII(switches::kAppId, extension_app->id());
289  chrome::startup::IsFirstRun first_run = first_run::IsChromeFirstRun() ?
290      chrome::startup::IS_FIRST_RUN : chrome::startup::IS_NOT_FIRST_RUN;
291  StartupBrowserCreatorImpl launch(base::FilePath(), command_line, first_run);
292  ASSERT_TRUE(launch.Launch(browser()->profile(), std::vector<GURL>(), false));
293
294  // Pref was set to open in a window, so the app should have opened in a
295  // window.  The launch should have created a new browser. Find the new
296  // browser.
297  Browser* new_browser = FindOneOtherBrowser(browser());
298  ASSERT_TRUE(new_browser);
299
300  // Expect an app window.
301  EXPECT_TRUE(new_browser->is_app());
302
303  // The browser's app_name should include the app's ID.
304  EXPECT_NE(
305      new_browser->app_name_.find(extension_app->id()),
306      std::string::npos) << new_browser->app_name_;
307}
308
309IN_PROC_BROWSER_TEST_F(StartupBrowserCreatorTest, OpenAppShortcutTabPref) {
310  // Load an app with launch.container = 'tab'.
311  const Extension* extension_app = NULL;
312  ASSERT_NO_FATAL_FAILURE(LoadApp("app_with_tab_container", &extension_app));
313
314  // Set a pref indicating that the user wants to open this app in a window.
315  SetAppLaunchPref(extension_app->id(),
316                   extensions::ExtensionPrefs::LAUNCH_REGULAR);
317
318  CommandLine command_line(CommandLine::NO_PROGRAM);
319  command_line.AppendSwitchASCII(switches::kAppId, extension_app->id());
320  chrome::startup::IsFirstRun first_run = first_run::IsChromeFirstRun() ?
321      chrome::startup::IS_FIRST_RUN : chrome::startup::IS_NOT_FIRST_RUN;
322  StartupBrowserCreatorImpl launch(base::FilePath(), command_line, first_run);
323  ASSERT_TRUE(launch.Launch(browser()->profile(), std::vector<GURL>(), false));
324
325  // When an app shortcut is open and the pref indicates a tab should
326  // open, the tab is open in a new browser window.  Expect a new window.
327  ASSERT_EQ(2u, chrome::GetBrowserCount(browser()->profile(),
328                                        browser()->host_desktop_type()));
329
330  Browser* new_browser = FindOneOtherBrowser(browser());
331  ASSERT_TRUE(new_browser);
332
333  // The tab should be in a tabbed window.
334  EXPECT_TRUE(new_browser->is_type_tabbed());
335
336  // The browser's app_name should not include the app's ID: It is in a
337  // normal browser.
338  EXPECT_EQ(
339      new_browser->app_name_.find(extension_app->id()),
340      std::string::npos) << new_browser->app_name_;
341}
342
343#endif  // !defined(OS_MACOSX)
344
345#endif  // !defined(OS_CHROMEOS)
346
347IN_PROC_BROWSER_TEST_F(StartupBrowserCreatorTest,
348                       ReadingWasRestartedAfterRestart) {
349  // Tests that StartupBrowserCreator::WasRestarted reads and resets the
350  // preference kWasRestarted correctly.
351  StartupBrowserCreator::was_restarted_read_ = false;
352  PrefService* pref_service = g_browser_process->local_state();
353  pref_service->SetBoolean(prefs::kWasRestarted, true);
354  EXPECT_TRUE(StartupBrowserCreator::WasRestarted());
355  EXPECT_FALSE(pref_service->GetBoolean(prefs::kWasRestarted));
356  EXPECT_TRUE(StartupBrowserCreator::WasRestarted());
357}
358
359IN_PROC_BROWSER_TEST_F(StartupBrowserCreatorTest,
360                       ReadingWasRestartedAfterNormalStart) {
361  // Tests that StartupBrowserCreator::WasRestarted reads and resets the
362  // preference kWasRestarted correctly.
363  StartupBrowserCreator::was_restarted_read_ = false;
364  PrefService* pref_service = g_browser_process->local_state();
365  pref_service->SetBoolean(prefs::kWasRestarted, false);
366  EXPECT_FALSE(StartupBrowserCreator::WasRestarted());
367  EXPECT_FALSE(pref_service->GetBoolean(prefs::kWasRestarted));
368  EXPECT_FALSE(StartupBrowserCreator::WasRestarted());
369}
370
371IN_PROC_BROWSER_TEST_F(StartupBrowserCreatorTest, AddFirstRunTab) {
372  StartupBrowserCreator browser_creator;
373  browser_creator.AddFirstRunTab(test_server()->GetURL("files/title1.html"));
374  browser_creator.AddFirstRunTab(test_server()->GetURL("files/title2.html"));
375
376  // Do a simple non-process-startup browser launch.
377  CommandLine dummy(CommandLine::NO_PROGRAM);
378  StartupBrowserCreatorImpl launch(base::FilePath(), dummy, &browser_creator,
379                                   chrome::startup::IS_FIRST_RUN);
380  ASSERT_TRUE(launch.Launch(browser()->profile(), std::vector<GURL>(), false));
381
382  // This should have created a new browser window.
383  Browser* new_browser = FindOneOtherBrowser(browser());
384  ASSERT_TRUE(new_browser);
385
386  TabStripModel* tab_strip = new_browser->tab_strip_model();
387  EXPECT_EQ(2, tab_strip->count());
388
389  EXPECT_EQ("title1.html",
390            tab_strip->GetWebContentsAt(0)->GetURL().ExtractFileName());
391  EXPECT_EQ("title2.html",
392            tab_strip->GetWebContentsAt(1)->GetURL().ExtractFileName());
393}
394
395// Test hard-coded special first run tabs (defined in
396// StartupBrowserCreatorImpl::AddStartupURLs()).
397IN_PROC_BROWSER_TEST_F(StartupBrowserCreatorTest, AddCustomFirstRunTab) {
398  StartupBrowserCreator browser_creator;
399  browser_creator.AddFirstRunTab(test_server()->GetURL("files/title1.html"));
400  browser_creator.AddFirstRunTab(GURL("http://new_tab_page"));
401  browser_creator.AddFirstRunTab(test_server()->GetURL("files/title2.html"));
402  browser_creator.AddFirstRunTab(GURL("http://welcome_page"));
403
404  // Do a simple non-process-startup browser launch.
405  CommandLine dummy(CommandLine::NO_PROGRAM);
406  StartupBrowserCreatorImpl launch(base::FilePath(), dummy, &browser_creator,
407                                   chrome::startup::IS_FIRST_RUN);
408  ASSERT_TRUE(launch.Launch(browser()->profile(), std::vector<GURL>(), false));
409
410  // This should have created a new browser window.
411  Browser* new_browser = FindOneOtherBrowser(browser());
412  ASSERT_TRUE(new_browser);
413
414  TabStripModel* tab_strip = new_browser->tab_strip_model();
415  EXPECT_EQ(4, tab_strip->count());
416
417  EXPECT_EQ("title1.html",
418            tab_strip->GetWebContentsAt(0)->GetURL().ExtractFileName());
419  EXPECT_EQ(GURL(chrome::kChromeUINewTabURL),
420            tab_strip->GetWebContentsAt(1)->GetURL());
421  EXPECT_EQ("title2.html",
422            tab_strip->GetWebContentsAt(2)->GetURL().ExtractFileName());
423  EXPECT_EQ(internals::GetWelcomePageURL(),
424            tab_strip->GetWebContentsAt(3)->GetURL());
425}
426
427IN_PROC_BROWSER_TEST_F(StartupBrowserCreatorTest, SyncPromoNoWelcomePage) {
428  // Trick this test into thinking the promo has been shown for this profile; so
429  // that it will show it again (otherwise it skips showing it since
430  // --no-first-run is specified in browser tests).
431  SyncPromoUI::DidShowSyncPromoAtStartup(browser()->profile());
432
433  // Do a simple non-process-startup browser launch.
434  CommandLine dummy(CommandLine::NO_PROGRAM);
435  StartupBrowserCreatorImpl launch(base::FilePath(), dummy,
436                                   chrome::startup::IS_FIRST_RUN);
437  ASSERT_TRUE(launch.Launch(browser()->profile(), std::vector<GURL>(), false));
438
439  // This should have created a new browser window.
440  Browser* new_browser = FindOneOtherBrowser(browser());
441  ASSERT_TRUE(new_browser);
442
443  TabStripModel* tab_strip = new_browser->tab_strip_model();
444  EXPECT_EQ(1, tab_strip->count());
445
446  if (SyncPromoUI::ShouldShowSyncPromoAtStartup(browser()->profile(), true)) {
447    EXPECT_EQ("signin", tab_strip->GetWebContentsAt(0)->GetURL().host());
448  } else {
449    EXPECT_EQ(GURL(chrome::kChromeUINewTabURL),
450              tab_strip->GetWebContentsAt(0)->GetURL());
451  }
452}
453
454IN_PROC_BROWSER_TEST_F(StartupBrowserCreatorTest, SyncPromoWithWelcomePage) {
455  // Trick this test into thinking the promo has been shown for this profile; so
456  // that it will show it again (otherwise it skips showing it since
457  // --no-first-run is specified in browser tests).
458  SyncPromoUI::DidShowSyncPromoAtStartup(browser()->profile());
459  first_run::SetShouldShowWelcomePage();
460
461  // Do a simple non-process-startup browser launch.
462  CommandLine dummy(CommandLine::NO_PROGRAM);
463  StartupBrowserCreatorImpl launch(base::FilePath(), dummy,
464                                   chrome::startup::IS_FIRST_RUN);
465  ASSERT_TRUE(launch.Launch(browser()->profile(), std::vector<GURL>(), false));
466
467  // This should have created a new browser window.
468  Browser* new_browser = FindOneOtherBrowser(browser());
469  ASSERT_TRUE(new_browser);
470
471  TabStripModel* tab_strip = new_browser->tab_strip_model();
472  EXPECT_EQ(2, tab_strip->count());
473
474  if (SyncPromoUI::ShouldShowSyncPromoAtStartup(browser()->profile(), true)) {
475    EXPECT_EQ("signin", tab_strip->GetWebContentsAt(0)->GetURL().host());
476  } else {
477    EXPECT_EQ(GURL(chrome::kChromeUINewTabURL),
478              tab_strip->GetWebContentsAt(0)->GetURL());
479  }
480  EXPECT_EQ(internals::GetWelcomePageURL(),
481            tab_strip->GetWebContentsAt(1)->GetURL());
482}
483
484IN_PROC_BROWSER_TEST_F(StartupBrowserCreatorTest, SyncPromoWithFirstRunTabs) {
485  StartupBrowserCreator browser_creator;
486  browser_creator.AddFirstRunTab(test_server()->GetURL("files/title1.html"));
487
488  // Trick this test into thinking the promo has been shown for this profile; so
489  // that it will show it again (otherwise it skips showing it since
490  // --no-first-run is specified in browser tests).
491  SyncPromoUI::DidShowSyncPromoAtStartup(browser()->profile());
492
493  // The welcome page should not be shown, even if
494  // first_run::ShouldShowWelcomePage() says so, when there are already
495  // more than 2 first run tabs.
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, &browser_creator,
501                                   chrome::startup::IS_FIRST_RUN);
502  ASSERT_TRUE(launch.Launch(browser()->profile(), std::vector<GURL>(), false));
503
504  // This should have created a new browser window.
505  Browser* new_browser = FindOneOtherBrowser(browser());
506  ASSERT_TRUE(new_browser);
507
508  TabStripModel* tab_strip = new_browser->tab_strip_model();
509  if (SyncPromoUI::ShouldShowSyncPromoAtStartup(browser()->profile(), true)) {
510    EXPECT_EQ(2, tab_strip->count());
511    EXPECT_EQ("signin", tab_strip->GetWebContentsAt(0)->GetURL().host());
512    EXPECT_EQ("title1.html",
513              tab_strip->GetWebContentsAt(1)->GetURL().ExtractFileName());
514  } else {
515    EXPECT_EQ(1, tab_strip->count());
516    EXPECT_EQ("title1.html",
517              tab_strip->GetWebContentsAt(0)->GetURL().ExtractFileName());
518  }
519}
520
521// The welcome page should still be shown if there are more than 2 first run
522// tabs, but the welcome page was explcitly added to the first run tabs.
523IN_PROC_BROWSER_TEST_F(StartupBrowserCreatorTest,
524                       SyncPromoWithFirstRunTabsIncludingWelcomePage) {
525  StartupBrowserCreator browser_creator;
526  browser_creator.AddFirstRunTab(test_server()->GetURL("files/title1.html"));
527  browser_creator.AddFirstRunTab(GURL("http://welcome_page"));
528
529  // Trick this test into thinking the promo has been shown for this profile; so
530  // that it will show it again (otherwise it skips showing it since
531  // --no-first-run is specified in browser tests).
532  SyncPromoUI::DidShowSyncPromoAtStartup(browser()->profile());
533
534  // Do a simple non-process-startup browser launch.
535  CommandLine dummy(CommandLine::NO_PROGRAM);
536  StartupBrowserCreatorImpl launch(base::FilePath(), dummy, &browser_creator,
537                                   chrome::startup::IS_FIRST_RUN);
538  ASSERT_TRUE(launch.Launch(browser()->profile(), std::vector<GURL>(), false));
539
540  // This should have created a new browser window.
541  Browser* new_browser = FindOneOtherBrowser(browser());
542  ASSERT_TRUE(new_browser);
543
544  TabStripModel* tab_strip = new_browser->tab_strip_model();
545  if (SyncPromoUI::ShouldShowSyncPromoAtStartup(browser()->profile(), true)) {
546    EXPECT_EQ(3, tab_strip->count());
547    EXPECT_EQ("signin", tab_strip->GetWebContentsAt(0)->GetURL().host());
548    EXPECT_EQ("title1.html",
549              tab_strip->GetWebContentsAt(1)->GetURL().ExtractFileName());
550    EXPECT_EQ(internals::GetWelcomePageURL(),
551              tab_strip->GetWebContentsAt(2)->GetURL());
552  } else {
553    EXPECT_EQ(2, tab_strip->count());
554    EXPECT_EQ("title1.html",
555              tab_strip->GetWebContentsAt(0)->GetURL().ExtractFileName());
556    EXPECT_EQ(internals::GetWelcomePageURL(),
557              tab_strip->GetWebContentsAt(1)->GetURL());
558  }
559}
560
561#if !defined(OS_CHROMEOS)
562IN_PROC_BROWSER_TEST_F(StartupBrowserCreatorTest, StartupURLsForTwoProfiles) {
563  Profile* default_profile = browser()->profile();
564
565  ProfileManager* profile_manager = g_browser_process->profile_manager();
566  // Create another profile.
567  base::FilePath dest_path = profile_manager->user_data_dir();
568  dest_path = dest_path.Append(FILE_PATH_LITERAL("New Profile 1"));
569
570  Profile* other_profile = profile_manager->GetProfile(dest_path);
571  ASSERT_TRUE(other_profile);
572
573  // Use a couple arbitrary URLs.
574  std::vector<GURL> urls1;
575  urls1.push_back(ui_test_utils::GetTestUrl(
576      base::FilePath(base::FilePath::kCurrentDirectory),
577      base::FilePath(FILE_PATH_LITERAL("title1.html"))));
578  std::vector<GURL> urls2;
579  urls2.push_back(ui_test_utils::GetTestUrl(
580      base::FilePath(base::FilePath::kCurrentDirectory),
581      base::FilePath(FILE_PATH_LITERAL("title2.html"))));
582
583  // Set different startup preferences for the 2 profiles.
584  SessionStartupPref pref1(SessionStartupPref::URLS);
585  pref1.urls = urls1;
586  SessionStartupPref::SetStartupPref(default_profile, pref1);
587  SessionStartupPref pref2(SessionStartupPref::URLS);
588  pref2.urls = urls2;
589  SessionStartupPref::SetStartupPref(other_profile, pref2);
590
591  // Close the browser.
592  browser()->window()->Close();
593
594  // Do a simple non-process-startup browser launch.
595  CommandLine dummy(CommandLine::NO_PROGRAM);
596
597  int return_code;
598  StartupBrowserCreator browser_creator;
599  std::vector<Profile*> last_opened_profiles;
600  last_opened_profiles.push_back(default_profile);
601  last_opened_profiles.push_back(other_profile);
602  browser_creator.Start(dummy, profile_manager->user_data_dir(),
603                        default_profile, last_opened_profiles, &return_code);
604
605  // urls1 were opened in a browser for default_profile, and urls2 were opened
606  // in a browser for other_profile.
607  Browser* new_browser = NULL;
608  // |browser()| is still around at this point, even though we've closed its
609  // window. Thus the browser count for default_profile is 2.
610  ASSERT_EQ(2u, chrome::GetBrowserCount(default_profile,
611                                        browser()->host_desktop_type()));
612  new_browser = FindOneOtherBrowserForProfile(default_profile, browser());
613  ASSERT_TRUE(new_browser);
614  TabStripModel* tab_strip = new_browser->tab_strip_model();
615  ASSERT_EQ(1, tab_strip->count());
616  EXPECT_EQ(urls1[0], tab_strip->GetWebContentsAt(0)->GetURL());
617
618  ASSERT_EQ(1u, chrome::GetBrowserCount(other_profile,
619                                        browser()->host_desktop_type()));
620  new_browser = FindOneOtherBrowserForProfile(other_profile, NULL);
621  ASSERT_TRUE(new_browser);
622  tab_strip = new_browser->tab_strip_model();
623  ASSERT_EQ(1, tab_strip->count());
624  EXPECT_EQ(urls2[0], tab_strip->GetWebContentsAt(0)->GetURL());
625}
626
627IN_PROC_BROWSER_TEST_F(StartupBrowserCreatorTest, UpdateWithTwoProfiles) {
628  // Make StartupBrowserCreator::WasRestarted() return true.
629  StartupBrowserCreator::was_restarted_read_ = false;
630  PrefService* pref_service = g_browser_process->local_state();
631  pref_service->SetBoolean(prefs::kWasRestarted, true);
632
633  ProfileManager* profile_manager = g_browser_process->profile_manager();
634
635  // Create two profiles.
636  base::FilePath dest_path = profile_manager->user_data_dir();
637
638  Profile* profile1 = profile_manager->GetProfile(
639      dest_path.Append(FILE_PATH_LITERAL("New Profile 1")));
640  ASSERT_TRUE(profile1);
641
642  Profile* profile2 = profile_manager->GetProfile(
643      dest_path.Append(FILE_PATH_LITERAL("New Profile 2")));
644  ASSERT_TRUE(profile2);
645
646  // Use a couple arbitrary URLs.
647  std::vector<GURL> urls1;
648  urls1.push_back(ui_test_utils::GetTestUrl(
649      base::FilePath(base::FilePath::kCurrentDirectory),
650      base::FilePath(FILE_PATH_LITERAL("title1.html"))));
651  std::vector<GURL> urls2;
652  urls2.push_back(ui_test_utils::GetTestUrl(
653      base::FilePath(base::FilePath::kCurrentDirectory),
654      base::FilePath(FILE_PATH_LITERAL("title2.html"))));
655
656  // Set different startup preferences for the 2 profiles.
657  SessionStartupPref pref1(SessionStartupPref::URLS);
658  pref1.urls = urls1;
659  SessionStartupPref::SetStartupPref(profile1, pref1);
660  SessionStartupPref pref2(SessionStartupPref::URLS);
661  pref2.urls = urls2;
662  SessionStartupPref::SetStartupPref(profile2, pref2);
663
664  // Simulate a launch after a browser update.
665  CommandLine dummy(CommandLine::NO_PROGRAM);
666  int return_code;
667  StartupBrowserCreator browser_creator;
668  std::vector<Profile*> last_opened_profiles;
669  last_opened_profiles.push_back(profile1);
670  last_opened_profiles.push_back(profile2);
671  browser_creator.Start(dummy, profile_manager->user_data_dir(), profile1,
672                        last_opened_profiles, &return_code);
673
674  while (SessionRestore::IsRestoring(profile1) ||
675         SessionRestore::IsRestoring(profile2))
676    MessageLoop::current()->RunUntilIdle();
677
678  // The startup URLs are ignored, and instead the last open sessions are
679  // restored.
680  EXPECT_TRUE(profile1->restored_last_session());
681  EXPECT_TRUE(profile2->restored_last_session());
682
683  Browser* new_browser = NULL;
684  ASSERT_EQ(1u, chrome::GetBrowserCount(profile1,
685                                        browser()->host_desktop_type()));
686  new_browser = FindOneOtherBrowserForProfile(profile1, NULL);
687  ASSERT_TRUE(new_browser);
688  TabStripModel* tab_strip = new_browser->tab_strip_model();
689  ASSERT_EQ(1, tab_strip->count());
690  EXPECT_EQ(GURL(chrome::kAboutBlankURL),
691            tab_strip->GetWebContentsAt(0)->GetURL());
692
693  ASSERT_EQ(1u, chrome::GetBrowserCount(profile2,
694                                        browser()->host_desktop_type()));
695  new_browser = FindOneOtherBrowserForProfile(profile2, NULL);
696  ASSERT_TRUE(new_browser);
697  tab_strip = new_browser->tab_strip_model();
698  ASSERT_EQ(1, tab_strip->count());
699  EXPECT_EQ(GURL(chrome::kAboutBlankURL),
700            tab_strip->GetWebContentsAt(0)->GetURL());
701}
702
703IN_PROC_BROWSER_TEST_F(StartupBrowserCreatorTest,
704                       ProfilesWithoutPagesNotLaunched) {
705  Profile* default_profile = browser()->profile();
706
707  ProfileManager* profile_manager = g_browser_process->profile_manager();
708
709  // Create 4 more profiles.
710  base::FilePath dest_path1 = profile_manager->user_data_dir().Append(
711      FILE_PATH_LITERAL("New Profile 1"));
712  base::FilePath dest_path2 = profile_manager->user_data_dir().Append(
713      FILE_PATH_LITERAL("New Profile 2"));
714  base::FilePath dest_path3 = profile_manager->user_data_dir().Append(
715      FILE_PATH_LITERAL("New Profile 3"));
716  base::FilePath dest_path4 = profile_manager->user_data_dir().Append(
717      FILE_PATH_LITERAL("New Profile 4"));
718
719  Profile* profile_home1 = profile_manager->GetProfile(dest_path1);
720  ASSERT_TRUE(profile_home1);
721  Profile* profile_home2 = profile_manager->GetProfile(dest_path2);
722  ASSERT_TRUE(profile_home2);
723  Profile* profile_last = profile_manager->GetProfile(dest_path3);
724  ASSERT_TRUE(profile_last);
725  Profile* profile_urls = profile_manager->GetProfile(dest_path4);
726  ASSERT_TRUE(profile_urls);
727
728  // Set the profiles to open urls, open last visited pages or display the home
729  // page.
730  SessionStartupPref pref_home(SessionStartupPref::DEFAULT);
731  SessionStartupPref::SetStartupPref(profile_home1, pref_home);
732  SessionStartupPref::SetStartupPref(profile_home2, pref_home);
733
734  SessionStartupPref pref_last(SessionStartupPref::LAST);
735  SessionStartupPref::SetStartupPref(profile_last, pref_last);
736
737  std::vector<GURL> urls;
738  urls.push_back(ui_test_utils::GetTestUrl(
739      base::FilePath(base::FilePath::kCurrentDirectory),
740      base::FilePath(FILE_PATH_LITERAL("title1.html"))));
741
742  SessionStartupPref pref_urls(SessionStartupPref::URLS);
743  pref_urls.urls = urls;
744  SessionStartupPref::SetStartupPref(profile_urls, pref_urls);
745
746  // Close the browser.
747  chrome::HostDesktopType original_desktop_type =
748      browser()->host_desktop_type();
749  browser()->window()->Close();
750
751  // Do a simple non-process-startup browser launch.
752  CommandLine dummy(CommandLine::NO_PROGRAM);
753
754  int return_code;
755  StartupBrowserCreator browser_creator;
756  std::vector<Profile*> last_opened_profiles;
757  last_opened_profiles.push_back(profile_home1);
758  last_opened_profiles.push_back(profile_home2);
759  last_opened_profiles.push_back(profile_last);
760  last_opened_profiles.push_back(profile_urls);
761  browser_creator.Start(dummy, profile_manager->user_data_dir(), profile_home1,
762                        last_opened_profiles, &return_code);
763
764  while (SessionRestore::IsRestoring(default_profile) ||
765         SessionRestore::IsRestoring(profile_home1) ||
766         SessionRestore::IsRestoring(profile_home2) ||
767         SessionRestore::IsRestoring(profile_last) ||
768         SessionRestore::IsRestoring(profile_urls))
769    MessageLoop::current()->RunUntilIdle();
770
771  Browser* new_browser = NULL;
772  // The last open profile (the profile_home1 in this case) will always be
773  // launched, even if it will open just the home page.
774  ASSERT_EQ(1u, chrome::GetBrowserCount(profile_home1, original_desktop_type));
775  new_browser = FindOneOtherBrowserForProfile(profile_home1, NULL);
776  ASSERT_TRUE(new_browser);
777  TabStripModel* tab_strip = new_browser->tab_strip_model();
778  ASSERT_EQ(1, tab_strip->count());
779  EXPECT_EQ(GURL(chrome::kChromeUINewTabURL),
780            tab_strip->GetWebContentsAt(0)->GetURL());
781
782  // profile_urls opened the urls.
783  ASSERT_EQ(1u, chrome::GetBrowserCount(profile_urls, original_desktop_type));
784  new_browser = FindOneOtherBrowserForProfile(profile_urls, NULL);
785  ASSERT_TRUE(new_browser);
786  tab_strip = new_browser->tab_strip_model();
787  ASSERT_EQ(1, tab_strip->count());
788  EXPECT_EQ(urls[0], tab_strip->GetWebContentsAt(0)->GetURL());
789
790  // profile_last opened the last open pages.
791  ASSERT_EQ(1u, chrome::GetBrowserCount(profile_last, original_desktop_type));
792  new_browser = FindOneOtherBrowserForProfile(profile_last, NULL);
793  ASSERT_TRUE(new_browser);
794  tab_strip = new_browser->tab_strip_model();
795  ASSERT_EQ(1, tab_strip->count());
796  EXPECT_EQ(GURL(chrome::kAboutBlankURL),
797            tab_strip->GetWebContentsAt(0)->GetURL());
798
799  // profile_home2 was not launched since it would've only opened the home page.
800  ASSERT_EQ(0u, chrome::GetBrowserCount(profile_home2, original_desktop_type));
801}
802
803IN_PROC_BROWSER_TEST_F(StartupBrowserCreatorTest, ProfilesLaunchedAfterCrash) {
804  // After an unclean exit, all profiles will be launched. However, they won't
805  // open any pages automatically.
806
807  ProfileManager* profile_manager = g_browser_process->profile_manager();
808
809  // Create 3 profiles.
810  base::FilePath dest_path1 = profile_manager->user_data_dir().Append(
811      FILE_PATH_LITERAL("New Profile 1"));
812  base::FilePath dest_path2 = profile_manager->user_data_dir().Append(
813      FILE_PATH_LITERAL("New Profile 2"));
814  base::FilePath dest_path3 = profile_manager->user_data_dir().Append(
815      FILE_PATH_LITERAL("New Profile 3"));
816
817  Profile* profile_home = profile_manager->GetProfile(dest_path1);
818  ASSERT_TRUE(profile_home);
819  Profile* profile_last = profile_manager->GetProfile(dest_path2);
820  ASSERT_TRUE(profile_last);
821  Profile* profile_urls = profile_manager->GetProfile(dest_path3);
822  ASSERT_TRUE(profile_urls);
823
824  // Set the profiles to open the home page, last visited pages or URLs.
825  SessionStartupPref pref_home(SessionStartupPref::DEFAULT);
826  SessionStartupPref::SetStartupPref(profile_home, pref_home);
827
828  SessionStartupPref pref_last(SessionStartupPref::LAST);
829  SessionStartupPref::SetStartupPref(profile_last, pref_last);
830
831  std::vector<GURL> urls;
832  urls.push_back(ui_test_utils::GetTestUrl(
833      base::FilePath(base::FilePath::kCurrentDirectory),
834      base::FilePath(FILE_PATH_LITERAL("title1.html"))));
835
836  SessionStartupPref pref_urls(SessionStartupPref::URLS);
837  pref_urls.urls = urls;
838  SessionStartupPref::SetStartupPref(profile_urls, pref_urls);
839
840  // Simulate a launch after an unclear exit.
841  browser()->window()->Close();
842  static_cast<ProfileImpl*>(profile_home)->last_session_exit_type_ =
843      Profile::EXIT_CRASHED;
844  static_cast<ProfileImpl*>(profile_last)->last_session_exit_type_ =
845      Profile::EXIT_CRASHED;
846  static_cast<ProfileImpl*>(profile_urls)->last_session_exit_type_ =
847      Profile::EXIT_CRASHED;
848
849  CommandLine dummy(CommandLine::NO_PROGRAM);
850  dummy.AppendSwitchASCII(switches::kTestType, "browser");
851  int return_code;
852  StartupBrowserCreator browser_creator;
853  std::vector<Profile*> last_opened_profiles;
854  last_opened_profiles.push_back(profile_home);
855  last_opened_profiles.push_back(profile_last);
856  last_opened_profiles.push_back(profile_urls);
857  browser_creator.Start(dummy, profile_manager->user_data_dir(), profile_home,
858                        last_opened_profiles, &return_code);
859
860  // No profiles are getting restored, since they all display the crash info
861  // bar.
862  EXPECT_FALSE(SessionRestore::IsRestoring(profile_home));
863  EXPECT_FALSE(SessionRestore::IsRestoring(profile_last));
864  EXPECT_FALSE(SessionRestore::IsRestoring(profile_urls));
865
866  // The profile which normally opens the home page displays the new tab page.
867  Browser* new_browser = NULL;
868  ASSERT_EQ(1u, chrome::GetBrowserCount(profile_home,
869                                        browser()->host_desktop_type()));
870  new_browser = FindOneOtherBrowserForProfile(profile_home, NULL);
871  ASSERT_TRUE(new_browser);
872  TabStripModel* tab_strip = new_browser->tab_strip_model();
873  ASSERT_EQ(1, tab_strip->count());
874  content::WebContents* web_contents = tab_strip->GetWebContentsAt(0);
875  EXPECT_EQ(GURL(chrome::kChromeUINewTabURL), web_contents->GetURL());
876  EXPECT_EQ(1U,
877            InfoBarService::FromWebContents(web_contents)->infobar_count());
878
879  // The profile which normally opens last open pages displays the new tab page.
880  ASSERT_EQ(1u, chrome::GetBrowserCount(profile_last,
881                                        browser()->host_desktop_type()));
882  new_browser = FindOneOtherBrowserForProfile(profile_last, NULL);
883  ASSERT_TRUE(new_browser);
884  tab_strip = new_browser->tab_strip_model();
885  ASSERT_EQ(1, tab_strip->count());
886  web_contents = tab_strip->GetWebContentsAt(0);
887  EXPECT_EQ(GURL(chrome::kChromeUINewTabURL), web_contents->GetURL());
888  EXPECT_EQ(1U,
889            InfoBarService::FromWebContents(web_contents)->infobar_count());
890
891  // The profile which normally opens URLs displays the new tab page.
892  ASSERT_EQ(1u, chrome::GetBrowserCount(profile_urls,
893                                        browser()->host_desktop_type()));
894  new_browser = FindOneOtherBrowserForProfile(profile_urls, NULL);
895  ASSERT_TRUE(new_browser);
896  tab_strip = new_browser->tab_strip_model();
897  ASSERT_EQ(1, tab_strip->count());
898  web_contents = tab_strip->GetWebContentsAt(0);
899  EXPECT_EQ(GURL(chrome::kChromeUINewTabURL), web_contents->GetURL());
900  EXPECT_EQ(1U,
901            InfoBarService::FromWebContents(web_contents)->infobar_count());
902}
903
904class ManagedModeBrowserCreatorTest : public InProcessBrowserTest {
905 protected:
906  virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
907    InProcessBrowserTest::SetUpCommandLine(command_line);
908    command_line->AppendSwitch(switches::kEnableManagedUsers);
909  }
910};
911
912#if defined(ENABLE_MANAGED_USERS)
913IN_PROC_BROWSER_TEST_F(ManagedModeBrowserCreatorTest,
914                       StartupManagedModeProfile) {
915  // Make this a managed profile.
916  ManagedUserService* managed_user_service =
917      ManagedUserServiceFactory::GetForProfile(browser()->profile());
918  managed_user_service->InitForTesting();
919
920  StartupBrowserCreator browser_creator;
921
922  // Do a simple non-process-startup browser launch.
923  CommandLine dummy(CommandLine::NO_PROGRAM);
924  StartupBrowserCreatorImpl launch(base::FilePath(), dummy, &browser_creator,
925                                   chrome::startup::IS_FIRST_RUN);
926  content::WindowedNotificationObserver observer(
927      content::NOTIFICATION_LOAD_STOP,
928      content::NotificationService::AllSources());
929  ASSERT_TRUE(launch.Launch(browser()->profile(), std::vector<GURL>(), false));
930
931  // This should have created a new browser window.
932  Browser* new_browser = FindOneOtherBrowser(browser());
933  ASSERT_TRUE(new_browser);
934
935  TabStripModel* tab_strip = new_browser->tab_strip_model();
936  // There should be only one tab.
937  EXPECT_EQ(1, tab_strip->count());
938
939  // And it should point to the managed user settings page.
940  content::WebContents* web_contents = tab_strip->GetWebContentsAt(0);
941  GURL expected(GURL(std::string(chrome::kChromeUISettingsURL) +
942                     chrome::kManagedUserSettingsSubPage));
943  EXPECT_EQ(GURL(expected), web_contents->GetURL());
944  observer.Wait();
945
946  // Managed user should be in elevated state.
947  bool is_elevated = ManagedModeNavigationObserver::FromWebContents(
948      web_contents)->is_elevated();
949  EXPECT_TRUE(is_elevated);
950}
951
952#endif  // ENABLE_MANAGED_USERS
953
954#endif  // !OS_CHROMEOS
955