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