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