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