startup_browser_creator_browsertest.cc revision cedac228d2dd51db4b79ea1e72c7f249408ee061
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/managed_mode/managed_mode_navigation_observer.h"
19#include "chrome/browser/managed_mode/managed_user_service.h"
20#include "chrome/browser/managed_mode/managed_user_service_factory.h"
21#include "chrome/browser/prefs/session_startup_pref.h"
22#include "chrome/browser/profiles/profile.h"
23#include "chrome/browser/profiles/profile_impl.h"
24#include "chrome/browser/profiles/profile_manager.h"
25#include "chrome/browser/sessions/session_restore.h"
26#include "chrome/browser/signin/signin_promo.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,
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  ASSERT_TRUE(test_server()->Start());
661
662  // Create two profiles.
663  base::FilePath dest_path = profile_manager->user_data_dir();
664
665  Profile* profile1 = profile_manager->GetProfile(
666      dest_path.Append(FILE_PATH_LITERAL("New Profile 1")));
667  ASSERT_TRUE(profile1);
668
669  Profile* profile2 = profile_manager->GetProfile(
670      dest_path.Append(FILE_PATH_LITERAL("New Profile 2")));
671  ASSERT_TRUE(profile2);
672
673  // Open some urls with the browsers, and close them.
674  Browser* browser1 = new Browser(
675      Browser::CreateParams(Browser::TYPE_TABBED, profile1,
676                            browser()->host_desktop_type()));
677  chrome::NewTab(browser1);
678  ui_test_utils::NavigateToURL(browser1,
679                               test_server()->GetURL("files/empty.html"));
680  browser1->window()->Close();
681
682  Browser* browser2 = new Browser(
683      Browser::CreateParams(Browser::TYPE_TABBED, profile2,
684                            browser()->host_desktop_type()));
685  chrome::NewTab(browser2);
686  ui_test_utils::NavigateToURL(browser2,
687                               test_server()->GetURL("files/form.html"));
688  browser2->window()->Close();
689
690  // Set different startup preferences for the 2 profiles.
691  std::vector<GURL> urls1;
692  urls1.push_back(ui_test_utils::GetTestUrl(
693      base::FilePath(base::FilePath::kCurrentDirectory),
694      base::FilePath(FILE_PATH_LITERAL("title1.html"))));
695  std::vector<GURL> urls2;
696  urls2.push_back(ui_test_utils::GetTestUrl(
697      base::FilePath(base::FilePath::kCurrentDirectory),
698      base::FilePath(FILE_PATH_LITERAL("title2.html"))));
699
700  // Set different startup preferences for the 2 profiles.
701  SessionStartupPref pref1(SessionStartupPref::URLS);
702  pref1.urls = urls1;
703  SessionStartupPref::SetStartupPref(profile1, pref1);
704  SessionStartupPref pref2(SessionStartupPref::URLS);
705  pref2.urls = urls2;
706  SessionStartupPref::SetStartupPref(profile2, pref2);
707
708  profile1->GetPrefs()->CommitPendingWrite();
709  profile2->GetPrefs()->CommitPendingWrite();
710}
711
712#if defined (OS_MACOSX)
713// crbug.com/376184
714#define MAYBE_UpdateWithTwoProfiles DISABLED_UpdateWithTwoProfiles
715#else
716#define MAYBE_UpdateWithTwoProfiles UpdateWithTwoProfiles
717#endif
718
719IN_PROC_BROWSER_TEST_F(StartupBrowserCreatorTest, MAYBE_UpdateWithTwoProfiles) {
720#if defined(OS_WIN) && defined(USE_ASH)
721  // Disable this test in Metro+Ash for now (http://crbug.com/262796).
722  if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kAshBrowserTests))
723    return;
724#endif
725
726  // Make StartupBrowserCreator::WasRestarted() return true.
727  StartupBrowserCreator::was_restarted_read_ = false;
728  PrefService* pref_service = g_browser_process->local_state();
729  pref_service->SetBoolean(prefs::kWasRestarted, true);
730
731  ProfileManager* profile_manager = g_browser_process->profile_manager();
732
733  // Open the two profiles.
734  base::FilePath dest_path = profile_manager->user_data_dir();
735
736  Profile* profile1 = profile_manager->GetProfile(
737      dest_path.Append(FILE_PATH_LITERAL("New Profile 1")));
738  ASSERT_TRUE(profile1);
739
740  Profile* profile2 = profile_manager->GetProfile(
741      dest_path.Append(FILE_PATH_LITERAL("New Profile 2")));
742  ASSERT_TRUE(profile2);
743
744  // Simulate a launch after a browser update.
745  CommandLine dummy(CommandLine::NO_PROGRAM);
746  int return_code;
747  StartupBrowserCreator browser_creator;
748  std::vector<Profile*> last_opened_profiles;
749  last_opened_profiles.push_back(profile1);
750  last_opened_profiles.push_back(profile2);
751  browser_creator.Start(dummy, profile_manager->user_data_dir(), profile1,
752                        last_opened_profiles, &return_code);
753
754  while (SessionRestore::IsRestoring(profile1) ||
755         SessionRestore::IsRestoring(profile2))
756    base::MessageLoop::current()->RunUntilIdle();
757
758  // The startup URLs are ignored, and instead the last open sessions are
759  // restored.
760  EXPECT_TRUE(profile1->restored_last_session());
761  EXPECT_TRUE(profile2->restored_last_session());
762
763  Browser* new_browser = NULL;
764  ASSERT_EQ(1u, chrome::GetBrowserCount(profile1,
765                                        browser()->host_desktop_type()));
766  new_browser = FindOneOtherBrowserForProfile(profile1, NULL);
767  ASSERT_TRUE(new_browser);
768  TabStripModel* tab_strip = new_browser->tab_strip_model();
769  ASSERT_EQ(1, tab_strip->count());
770  EXPECT_EQ("/files/empty.html",
771            tab_strip->GetWebContentsAt(0)->GetURL().path());
772
773  ASSERT_EQ(1u, chrome::GetBrowserCount(profile2,
774                                        browser()->host_desktop_type()));
775  new_browser = FindOneOtherBrowserForProfile(profile2, NULL);
776  ASSERT_TRUE(new_browser);
777  tab_strip = new_browser->tab_strip_model();
778  ASSERT_EQ(1, tab_strip->count());
779  EXPECT_EQ("/files/form.html",
780            tab_strip->GetWebContentsAt(0)->GetURL().path());
781}
782
783IN_PROC_BROWSER_TEST_F(StartupBrowserCreatorTest,
784                       ProfilesWithoutPagesNotLaunched) {
785#if defined(OS_WIN) && defined(USE_ASH)
786  // Disable this test in Metro+Ash for now (http://crbug.com/262796).
787  if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kAshBrowserTests))
788    return;
789#endif
790
791  Profile* default_profile = browser()->profile();
792
793  ProfileManager* profile_manager = g_browser_process->profile_manager();
794
795  // Create 4 more profiles.
796  base::FilePath dest_path1 = profile_manager->user_data_dir().Append(
797      FILE_PATH_LITERAL("New Profile 1"));
798  base::FilePath dest_path2 = profile_manager->user_data_dir().Append(
799      FILE_PATH_LITERAL("New Profile 2"));
800  base::FilePath dest_path3 = profile_manager->user_data_dir().Append(
801      FILE_PATH_LITERAL("New Profile 3"));
802  base::FilePath dest_path4 = profile_manager->user_data_dir().Append(
803      FILE_PATH_LITERAL("New Profile 4"));
804
805  Profile* profile_home1 = profile_manager->GetProfile(dest_path1);
806  ASSERT_TRUE(profile_home1);
807  Profile* profile_home2 = profile_manager->GetProfile(dest_path2);
808  ASSERT_TRUE(profile_home2);
809  Profile* profile_last = profile_manager->GetProfile(dest_path3);
810  ASSERT_TRUE(profile_last);
811  Profile* profile_urls = profile_manager->GetProfile(dest_path4);
812  ASSERT_TRUE(profile_urls);
813
814  // Set the profiles to open urls, open last visited pages or display the home
815  // page.
816  SessionStartupPref pref_home(SessionStartupPref::DEFAULT);
817  SessionStartupPref::SetStartupPref(profile_home1, pref_home);
818  SessionStartupPref::SetStartupPref(profile_home2, pref_home);
819
820  SessionStartupPref pref_last(SessionStartupPref::LAST);
821  SessionStartupPref::SetStartupPref(profile_last, pref_last);
822
823  std::vector<GURL> urls;
824  urls.push_back(ui_test_utils::GetTestUrl(
825      base::FilePath(base::FilePath::kCurrentDirectory),
826      base::FilePath(FILE_PATH_LITERAL("title1.html"))));
827
828  SessionStartupPref pref_urls(SessionStartupPref::URLS);
829  pref_urls.urls = urls;
830  SessionStartupPref::SetStartupPref(profile_urls, pref_urls);
831
832  // Open a page with profile_last.
833  Browser* browser_last = new Browser(
834      Browser::CreateParams(Browser::TYPE_TABBED, profile_last,
835                            browser()->host_desktop_type()));
836  chrome::NewTab(browser_last);
837  ui_test_utils::NavigateToURL(browser_last,
838                               test_server()->GetURL("files/empty.html"));
839  browser_last->window()->Close();
840
841  // Close the main browser.
842  chrome::HostDesktopType original_desktop_type =
843      browser()->host_desktop_type();
844  browser()->window()->Close();
845
846  // Do a simple non-process-startup browser launch.
847  CommandLine dummy(CommandLine::NO_PROGRAM);
848
849  int return_code;
850  StartupBrowserCreator browser_creator;
851  std::vector<Profile*> last_opened_profiles;
852  last_opened_profiles.push_back(profile_home1);
853  last_opened_profiles.push_back(profile_home2);
854  last_opened_profiles.push_back(profile_last);
855  last_opened_profiles.push_back(profile_urls);
856  browser_creator.Start(dummy, profile_manager->user_data_dir(), profile_home1,
857                        last_opened_profiles, &return_code);
858
859  while (SessionRestore::IsRestoring(default_profile) ||
860         SessionRestore::IsRestoring(profile_home1) ||
861         SessionRestore::IsRestoring(profile_home2) ||
862         SessionRestore::IsRestoring(profile_last) ||
863         SessionRestore::IsRestoring(profile_urls))
864    base::MessageLoop::current()->RunUntilIdle();
865
866  Browser* new_browser = NULL;
867  // The last open profile (the profile_home1 in this case) will always be
868  // launched, even if it will open just the home page.
869  ASSERT_EQ(1u, chrome::GetBrowserCount(profile_home1, original_desktop_type));
870  new_browser = FindOneOtherBrowserForProfile(profile_home1, NULL);
871  ASSERT_TRUE(new_browser);
872  TabStripModel* tab_strip = new_browser->tab_strip_model();
873  ASSERT_EQ(1, tab_strip->count());
874  EXPECT_EQ(GURL(chrome::kChromeUINewTabURL),
875            tab_strip->GetWebContentsAt(0)->GetURL());
876
877  // profile_urls opened the urls.
878  ASSERT_EQ(1u, chrome::GetBrowserCount(profile_urls, original_desktop_type));
879  new_browser = FindOneOtherBrowserForProfile(profile_urls, NULL);
880  ASSERT_TRUE(new_browser);
881  tab_strip = new_browser->tab_strip_model();
882  ASSERT_EQ(1, tab_strip->count());
883  EXPECT_EQ(urls[0], tab_strip->GetWebContentsAt(0)->GetURL());
884
885  // profile_last opened the last open pages.
886  ASSERT_EQ(1u, chrome::GetBrowserCount(profile_last, original_desktop_type));
887  new_browser = FindOneOtherBrowserForProfile(profile_last, NULL);
888  ASSERT_TRUE(new_browser);
889  tab_strip = new_browser->tab_strip_model();
890  ASSERT_EQ(1, tab_strip->count());
891  EXPECT_EQ("/files/empty.html",
892            tab_strip->GetWebContentsAt(0)->GetURL().path());
893
894  // profile_home2 was not launched since it would've only opened the home page.
895  ASSERT_EQ(0u, chrome::GetBrowserCount(profile_home2, original_desktop_type));
896}
897
898IN_PROC_BROWSER_TEST_F(StartupBrowserCreatorTest, ProfilesLaunchedAfterCrash) {
899#if defined(OS_WIN) && defined(USE_ASH)
900  // Disable this test in Metro+Ash for now (http://crbug.com/262796).
901  if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kAshBrowserTests))
902    return;
903#endif
904
905  // After an unclean exit, all profiles will be launched. However, they won't
906  // open any pages automatically.
907
908  ProfileManager* profile_manager = g_browser_process->profile_manager();
909
910  // Create 3 profiles.
911  base::FilePath dest_path1 = profile_manager->user_data_dir().Append(
912      FILE_PATH_LITERAL("New Profile 1"));
913  base::FilePath dest_path2 = profile_manager->user_data_dir().Append(
914      FILE_PATH_LITERAL("New Profile 2"));
915  base::FilePath dest_path3 = profile_manager->user_data_dir().Append(
916      FILE_PATH_LITERAL("New Profile 3"));
917
918  Profile* profile_home = profile_manager->GetProfile(dest_path1);
919  ASSERT_TRUE(profile_home);
920  Profile* profile_last = profile_manager->GetProfile(dest_path2);
921  ASSERT_TRUE(profile_last);
922  Profile* profile_urls = profile_manager->GetProfile(dest_path3);
923  ASSERT_TRUE(profile_urls);
924
925  // Set the profiles to open the home page, last visited pages or URLs.
926  SessionStartupPref pref_home(SessionStartupPref::DEFAULT);
927  SessionStartupPref::SetStartupPref(profile_home, pref_home);
928
929  SessionStartupPref pref_last(SessionStartupPref::LAST);
930  SessionStartupPref::SetStartupPref(profile_last, pref_last);
931
932  std::vector<GURL> urls;
933  urls.push_back(ui_test_utils::GetTestUrl(
934      base::FilePath(base::FilePath::kCurrentDirectory),
935      base::FilePath(FILE_PATH_LITERAL("title1.html"))));
936
937  SessionStartupPref pref_urls(SessionStartupPref::URLS);
938  pref_urls.urls = urls;
939  SessionStartupPref::SetStartupPref(profile_urls, pref_urls);
940
941  // Simulate a launch after an unclear exit.
942  browser()->window()->Close();
943  static_cast<ProfileImpl*>(profile_home)->last_session_exit_type_ =
944      Profile::EXIT_CRASHED;
945  static_cast<ProfileImpl*>(profile_last)->last_session_exit_type_ =
946      Profile::EXIT_CRASHED;
947  static_cast<ProfileImpl*>(profile_urls)->last_session_exit_type_ =
948      Profile::EXIT_CRASHED;
949
950  CommandLine dummy(CommandLine::NO_PROGRAM);
951  dummy.AppendSwitchASCII(switches::kTestType, "browser");
952  int return_code;
953  StartupBrowserCreator browser_creator;
954  std::vector<Profile*> last_opened_profiles;
955  last_opened_profiles.push_back(profile_home);
956  last_opened_profiles.push_back(profile_last);
957  last_opened_profiles.push_back(profile_urls);
958  browser_creator.Start(dummy, profile_manager->user_data_dir(), profile_home,
959                        last_opened_profiles, &return_code);
960
961  // No profiles are getting restored, since they all display the crash info
962  // bar.
963  EXPECT_FALSE(SessionRestore::IsRestoring(profile_home));
964  EXPECT_FALSE(SessionRestore::IsRestoring(profile_last));
965  EXPECT_FALSE(SessionRestore::IsRestoring(profile_urls));
966
967  // The profile which normally opens the home page displays the new tab page.
968  Browser* new_browser = NULL;
969  ASSERT_EQ(1u, chrome::GetBrowserCount(profile_home,
970                                        browser()->host_desktop_type()));
971  new_browser = FindOneOtherBrowserForProfile(profile_home, NULL);
972  ASSERT_TRUE(new_browser);
973  TabStripModel* tab_strip = new_browser->tab_strip_model();
974  ASSERT_EQ(1, tab_strip->count());
975  content::WebContents* web_contents = tab_strip->GetWebContentsAt(0);
976  EXPECT_EQ(GURL(chrome::kChromeUINewTabURL), web_contents->GetURL());
977  InfoBarService* infobar_service =
978      InfoBarService::FromWebContents(web_contents);
979  EXPECT_EQ(1U, infobar_service->infobar_count());
980
981  // The profile which normally opens last open pages displays the new tab page.
982  ASSERT_EQ(1u, chrome::GetBrowserCount(profile_last,
983                                        browser()->host_desktop_type()));
984  new_browser = FindOneOtherBrowserForProfile(profile_last, NULL);
985  ASSERT_TRUE(new_browser);
986  tab_strip = new_browser->tab_strip_model();
987  ASSERT_EQ(1, tab_strip->count());
988  web_contents = tab_strip->GetWebContentsAt(0);
989  EXPECT_EQ(GURL(chrome::kChromeUINewTabURL), web_contents->GetURL());
990  infobar_service = InfoBarService::FromWebContents(web_contents);
991  EXPECT_EQ(1U, infobar_service->infobar_count());
992
993  // The profile which normally opens URLs displays the new tab page.
994  ASSERT_EQ(1u, chrome::GetBrowserCount(profile_urls,
995                                        browser()->host_desktop_type()));
996  new_browser = FindOneOtherBrowserForProfile(profile_urls, NULL);
997  ASSERT_TRUE(new_browser);
998  tab_strip = new_browser->tab_strip_model();
999  ASSERT_EQ(1, tab_strip->count());
1000  web_contents = tab_strip->GetWebContentsAt(0);
1001  EXPECT_EQ(GURL(chrome::kChromeUINewTabURL), web_contents->GetURL());
1002  infobar_service = InfoBarService::FromWebContents(web_contents);
1003  EXPECT_EQ(1U, infobar_service->infobar_count());
1004}
1005
1006class ManagedModeBrowserCreatorTest : public InProcessBrowserTest {
1007 protected:
1008  virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
1009    InProcessBrowserTest::SetUpCommandLine(command_line);
1010    command_line->AppendSwitchASCII(switches::kManagedUserId, "asdf");
1011  }
1012};
1013
1014IN_PROC_BROWSER_TEST_F(ManagedModeBrowserCreatorTest,
1015                       StartupManagedModeProfile) {
1016  StartupBrowserCreator browser_creator;
1017
1018  // Do a simple non-process-startup browser launch.
1019  CommandLine dummy(CommandLine::NO_PROGRAM);
1020  StartupBrowserCreatorImpl launch(base::FilePath(), dummy, &browser_creator,
1021                                   chrome::startup::IS_FIRST_RUN);
1022  content::WindowedNotificationObserver observer(
1023      content::NOTIFICATION_LOAD_STOP,
1024      content::NotificationService::AllSources());
1025  ASSERT_TRUE(launch.Launch(browser()->profile(), std::vector<GURL>(), false,
1026                            browser()->host_desktop_type()));
1027
1028  // This should have created a new browser window.
1029  Browser* new_browser = FindOneOtherBrowser(browser());
1030  ASSERT_TRUE(new_browser);
1031
1032  TabStripModel* tab_strip = new_browser->tab_strip_model();
1033  // There should be only one tab.
1034  EXPECT_EQ(1, tab_strip->count());
1035}
1036
1037#endif  // !defined(OS_CHROMEOS)
1038
1039// These tests are not applicable to Chrome OS as neither master_preferences nor
1040// the sync promo exist there.
1041#if !defined(OS_CHROMEOS)
1042
1043// On a branded Linux build, policy is required to suppress the first-run
1044// dialog.
1045#if !defined(OS_LINUX) || !defined(GOOGLE_CHROME_BUILD) || \
1046    defined(ENABLE_CONFIGURATION_POLICY)
1047
1048class StartupBrowserCreatorFirstRunTest : public InProcessBrowserTest {
1049 protected:
1050  virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE;
1051  virtual void SetUpInProcessBrowserTestFixture() OVERRIDE;
1052
1053#if defined(ENABLE_CONFIGURATION_POLICY)
1054  policy::MockConfigurationPolicyProvider provider_;
1055  policy::PolicyMap policy_map_;
1056#endif  // defined(ENABLE_CONFIGURATION_POLICY)
1057};
1058
1059void StartupBrowserCreatorFirstRunTest::SetUpCommandLine(
1060    CommandLine* command_line) {
1061  command_line->AppendSwitch(switches::kForceFirstRun);
1062}
1063
1064void StartupBrowserCreatorFirstRunTest::SetUpInProcessBrowserTestFixture() {
1065#if defined(ENABLE_CONFIGURATION_POLICY)
1066#if defined(OS_LINUX) && defined(GOOGLE_CHROME_BUILD)
1067  // Set a policy that prevents the first-run dialog from being shown.
1068  policy_map_.Set(policy::key::kMetricsReportingEnabled,
1069                  policy::POLICY_LEVEL_MANDATORY, policy::POLICY_SCOPE_USER,
1070                  base::Value::CreateBooleanValue(false), NULL);
1071  provider_.UpdateChromePolicy(policy_map_);
1072#endif  // defined(OS_LINUX) && defined(GOOGLE_CHROME_BUILD)
1073
1074  EXPECT_CALL(provider_, IsInitializationComplete(_))
1075      .WillRepeatedly(Return(true));
1076  policy::BrowserPolicyConnector::SetPolicyProviderForTesting(&provider_);
1077#endif  // defined(ENABLE_CONFIGURATION_POLICY)
1078}
1079
1080IN_PROC_BROWSER_TEST_F(StartupBrowserCreatorFirstRunTest, SyncPromoForbidden) {
1081  // Consistently enable the welcome page on all platforms.
1082  first_run::SetShouldShowWelcomePage();
1083
1084  // Simulate the following master_preferences:
1085  // {
1086  //  "sync_promo": {
1087  //    "show_on_first_run_allowed": false
1088  //  }
1089  // }
1090  StartupBrowserCreator browser_creator;
1091  browser()->profile()->GetPrefs()->SetBoolean(
1092      prefs::kSignInPromoShowOnFirstRunAllowed, false);
1093
1094  // Do a process-startup browser launch.
1095  CommandLine dummy(CommandLine::NO_PROGRAM);
1096  StartupBrowserCreatorImpl launch(base::FilePath(), dummy, &browser_creator,
1097                                   chrome::startup::IS_FIRST_RUN);
1098  ASSERT_TRUE(launch.Launch(browser()->profile(), std::vector<GURL>(), true,
1099                            browser()->host_desktop_type()));
1100
1101  // This should have created a new browser window.
1102  Browser* new_browser = FindOneOtherBrowser(browser());
1103  ASSERT_TRUE(new_browser);
1104
1105  // Verify that the NTP and the welcome page are shown.
1106  TabStripModel* tab_strip = new_browser->tab_strip_model();
1107  ASSERT_EQ(2, tab_strip->count());
1108  EXPECT_EQ(GURL(chrome::kChromeUINewTabURL),
1109            tab_strip->GetWebContentsAt(0)->GetURL());
1110  EXPECT_EQ(internals::GetWelcomePageURL(),
1111            tab_strip->GetWebContentsAt(1)->GetURL());
1112}
1113
1114IN_PROC_BROWSER_TEST_F(StartupBrowserCreatorFirstRunTest, SyncPromoAllowed) {
1115  // Consistently enable the welcome page on all platforms.
1116  first_run::SetShouldShowWelcomePage();
1117
1118  // Simulate the following master_preferences:
1119  // {
1120  //  "sync_promo": {
1121  //    "show_on_first_run_allowed": true
1122  //  }
1123  // }
1124  StartupBrowserCreator browser_creator;
1125  browser()->profile()->GetPrefs()->SetBoolean(
1126      prefs::kSignInPromoShowOnFirstRunAllowed, true);
1127
1128  // Do a process-startup browser launch.
1129  CommandLine dummy(CommandLine::NO_PROGRAM);
1130  StartupBrowserCreatorImpl launch(base::FilePath(), dummy, &browser_creator,
1131                                   chrome::startup::IS_FIRST_RUN);
1132  ASSERT_TRUE(launch.Launch(browser()->profile(), std::vector<GURL>(), true,
1133                            browser()->host_desktop_type()));
1134
1135  // This should have created a new browser window.
1136  Browser* new_browser = FindOneOtherBrowser(browser());
1137  ASSERT_TRUE(new_browser);
1138
1139  // Verify that the sync promo and the welcome page are shown.
1140  TabStripModel* tab_strip = new_browser->tab_strip_model();
1141  ASSERT_EQ(2, tab_strip->count());
1142  EXPECT_EQ(signin::GetPromoURL(signin::SOURCE_START_PAGE, false),
1143            tab_strip->GetWebContentsAt(0)->GetURL());
1144  EXPECT_EQ(internals::GetWelcomePageURL(),
1145            tab_strip->GetWebContentsAt(1)->GetURL());
1146}
1147
1148IN_PROC_BROWSER_TEST_F(StartupBrowserCreatorFirstRunTest,
1149                       FirstRunTabsPromoAllowed) {
1150  // Simulate the following master_preferences:
1151  // {
1152  //  "first_run_tabs" : [
1153  //    "files/title1.html"
1154  //  ],
1155  //  "sync_promo": {
1156  //    "show_on_first_run_allowed": true
1157  //  }
1158  // }
1159  StartupBrowserCreator browser_creator;
1160  browser_creator.AddFirstRunTab(test_server()->GetURL("files/title1.html"));
1161  browser()->profile()->GetPrefs()->SetBoolean(
1162      prefs::kSignInPromoShowOnFirstRunAllowed, true);
1163
1164  // Do a process-startup browser launch.
1165  CommandLine dummy(CommandLine::NO_PROGRAM);
1166  StartupBrowserCreatorImpl launch(base::FilePath(), dummy, &browser_creator,
1167                                   chrome::startup::IS_FIRST_RUN);
1168  ASSERT_TRUE(launch.Launch(browser()->profile(), std::vector<GURL>(), true,
1169                            browser()->host_desktop_type()));
1170
1171  // This should have created a new browser window.
1172  Browser* new_browser = FindOneOtherBrowser(browser());
1173  ASSERT_TRUE(new_browser);
1174
1175  // Verify that the first-run tab is shown and the sync promo has been added.
1176  TabStripModel* tab_strip = new_browser->tab_strip_model();
1177  ASSERT_EQ(2, tab_strip->count());
1178  EXPECT_EQ(signin::GetPromoURL(signin::SOURCE_START_PAGE, false),
1179            tab_strip->GetWebContentsAt(0)->GetURL());
1180  EXPECT_EQ("title1.html",
1181            tab_strip->GetWebContentsAt(1)->GetURL().ExtractFileName());
1182}
1183
1184IN_PROC_BROWSER_TEST_F(StartupBrowserCreatorFirstRunTest,
1185                       FirstRunTabsContainSyncPromo) {
1186  // Simulate the following master_preferences:
1187  // {
1188  //  "first_run_tabs" : [
1189  //    "files/title1.html",
1190  //    "chrome://signin/?source=0&next_page=chrome%3A%2F%2Fnewtab%2F"
1191  //  ],
1192  //  "sync_promo": {
1193  //    "show_on_first_run_allowed": true
1194  //  }
1195  // }
1196  ASSERT_TRUE(test_server()->Start());
1197  StartupBrowserCreator browser_creator;
1198  browser_creator.AddFirstRunTab(test_server()->GetURL("files/title1.html"));
1199  browser_creator.AddFirstRunTab(signin::GetPromoURL(signin::SOURCE_START_PAGE,
1200                                                     false));
1201  browser()->profile()->GetPrefs()->SetBoolean(
1202      prefs::kSignInPromoShowOnFirstRunAllowed, true);
1203
1204  // Do a process-startup browser launch.
1205  CommandLine dummy(CommandLine::NO_PROGRAM);
1206  StartupBrowserCreatorImpl launch(base::FilePath(), dummy, &browser_creator,
1207                                   chrome::startup::IS_FIRST_RUN);
1208  ASSERT_TRUE(launch.Launch(browser()->profile(), std::vector<GURL>(), true,
1209                            browser()->host_desktop_type()));
1210
1211  // This should have created a new browser window.
1212  Browser* new_browser = FindOneOtherBrowser(browser());
1213  ASSERT_TRUE(new_browser);
1214
1215  // Verify that the first-run tabs are shown and no sync promo has been added
1216  // as the first-run tabs contain it already.
1217  TabStripModel* tab_strip = new_browser->tab_strip_model();
1218  ASSERT_EQ(2, tab_strip->count());
1219  EXPECT_EQ("title1.html",
1220            tab_strip->GetWebContentsAt(0)->GetURL().ExtractFileName());
1221  EXPECT_EQ(signin::GetPromoURL(signin::SOURCE_START_PAGE, false),
1222            tab_strip->GetWebContentsAt(1)->GetURL());
1223}
1224
1225IN_PROC_BROWSER_TEST_F(StartupBrowserCreatorFirstRunTest,
1226                       FirstRunTabsContainNTPSyncPromoAllowed) {
1227  // Simulate the following master_preferences:
1228  // {
1229  //  "first_run_tabs" : [
1230  //    "new_tab_page",
1231  //    "files/title1.html"
1232  //  ],
1233  //  "sync_promo": {
1234  //    "show_on_first_run_allowed": true
1235  //  }
1236  // }
1237  StartupBrowserCreator browser_creator;
1238  browser_creator.AddFirstRunTab(GURL("http://new_tab_page"));
1239  browser_creator.AddFirstRunTab(test_server()->GetURL("files/title1.html"));
1240  browser()->profile()->GetPrefs()->SetBoolean(
1241      prefs::kSignInPromoShowOnFirstRunAllowed, true);
1242
1243  // Do a process-startup browser launch.
1244  CommandLine dummy(CommandLine::NO_PROGRAM);
1245  StartupBrowserCreatorImpl launch(base::FilePath(), dummy, &browser_creator,
1246                                   chrome::startup::IS_FIRST_RUN);
1247  ASSERT_TRUE(launch.Launch(browser()->profile(), std::vector<GURL>(), true,
1248                            browser()->host_desktop_type()));
1249
1250  // This should have created a new browser window.
1251  Browser* new_browser = FindOneOtherBrowser(browser());
1252  ASSERT_TRUE(new_browser);
1253
1254  // Verify that the first-run tabs are shown but the NTP that they contain has
1255  // been replaced by the sync promo.
1256  TabStripModel* tab_strip = new_browser->tab_strip_model();
1257  ASSERT_EQ(2, tab_strip->count());
1258  EXPECT_EQ(signin::GetPromoURL(signin::SOURCE_START_PAGE, false),
1259            tab_strip->GetWebContentsAt(0)->GetURL());
1260  EXPECT_EQ("title1.html",
1261            tab_strip->GetWebContentsAt(1)->GetURL().ExtractFileName());
1262}
1263
1264IN_PROC_BROWSER_TEST_F(StartupBrowserCreatorFirstRunTest,
1265                       FirstRunTabsContainNTPSyncPromoForbidden) {
1266  // Simulate the following master_preferences:
1267  // {
1268  //  "first_run_tabs" : [
1269  //    "new_tab_page",
1270  //    "files/title1.html"
1271  //  ],
1272  //  "sync_promo": {
1273  //    "show_on_first_run_allowed": false
1274  //  }
1275  // }
1276  StartupBrowserCreator browser_creator;
1277  browser_creator.AddFirstRunTab(GURL("http://new_tab_page"));
1278  browser_creator.AddFirstRunTab(test_server()->GetURL("files/title1.html"));
1279  browser()->profile()->GetPrefs()->SetBoolean(
1280      prefs::kSignInPromoShowOnFirstRunAllowed, false);
1281
1282  // Do a process-startup browser launch.
1283  CommandLine dummy(CommandLine::NO_PROGRAM);
1284  StartupBrowserCreatorImpl launch(base::FilePath(), dummy, &browser_creator,
1285                                   chrome::startup::IS_FIRST_RUN);
1286  ASSERT_TRUE(launch.Launch(browser()->profile(), std::vector<GURL>(), true,
1287                            browser()->host_desktop_type()));
1288
1289  // This should have created a new browser window.
1290  Browser* new_browser = FindOneOtherBrowser(browser());
1291  ASSERT_TRUE(new_browser);
1292
1293  // Verify that the first-run tabs are shown, the NTP that they contain has not
1294  // not been replaced by the sync promo and no sync promo has been added.
1295  TabStripModel* tab_strip = new_browser->tab_strip_model();
1296  ASSERT_EQ(2, tab_strip->count());
1297  EXPECT_EQ(GURL(chrome::kChromeUINewTabURL),
1298            tab_strip->GetWebContentsAt(0)->GetURL());
1299  EXPECT_EQ("title1.html",
1300            tab_strip->GetWebContentsAt(1)->GetURL().ExtractFileName());
1301}
1302
1303IN_PROC_BROWSER_TEST_F(StartupBrowserCreatorFirstRunTest,
1304                       FirstRunTabsSyncPromoForbidden) {
1305  // Simulate the following master_preferences:
1306  // {
1307  //  "first_run_tabs" : [
1308  //    "files/title1.html"
1309  //  ],
1310  //  "sync_promo": {
1311  //    "show_on_first_run_allowed": false
1312  //  }
1313  // }
1314  StartupBrowserCreator browser_creator;
1315  browser_creator.AddFirstRunTab(test_server()->GetURL("files/title1.html"));
1316  browser()->profile()->GetPrefs()->SetBoolean(
1317      prefs::kSignInPromoShowOnFirstRunAllowed, false);
1318
1319  // Do a process-startup browser launch.
1320  CommandLine dummy(CommandLine::NO_PROGRAM);
1321  StartupBrowserCreatorImpl launch(base::FilePath(), dummy, &browser_creator,
1322                                   chrome::startup::IS_FIRST_RUN);
1323  ASSERT_TRUE(launch.Launch(browser()->profile(), std::vector<GURL>(), true,
1324                            browser()->host_desktop_type()));
1325
1326  // This should have created a new browser window.
1327  Browser* new_browser = FindOneOtherBrowser(browser());
1328  ASSERT_TRUE(new_browser);
1329
1330  // Verify that the first-run tab is shown and no sync promo has been added.
1331  TabStripModel* tab_strip = new_browser->tab_strip_model();
1332  ASSERT_EQ(1, tab_strip->count());
1333  EXPECT_EQ("title1.html",
1334            tab_strip->GetWebContentsAt(0)->GetURL().ExtractFileName());
1335}
1336
1337#if defined(ENABLE_CONFIGURATION_POLICY)
1338IN_PROC_BROWSER_TEST_F(StartupBrowserCreatorFirstRunTest,
1339                       RestoreOnStartupURLsPolicySpecified) {
1340  // Simulate the following master_preferences:
1341  // {
1342  //  "sync_promo": {
1343  //    "show_on_first_run_allowed": true
1344  //  }
1345  // }
1346  StartupBrowserCreator browser_creator;
1347  browser()->profile()->GetPrefs()->SetBoolean(
1348      prefs::kSignInPromoShowOnFirstRunAllowed, true);
1349
1350  // Set the following user policies:
1351  // * RestoreOnStartup = RestoreOnStartupIsURLs
1352  // * RestoreOnStartupURLs = [ "files/title1.html" ]
1353  policy_map_.Set(policy::key::kRestoreOnStartup,
1354                  policy::POLICY_LEVEL_MANDATORY, policy::POLICY_SCOPE_USER,
1355                  base::Value::CreateIntegerValue(
1356                      SessionStartupPref::kPrefValueURLs),
1357                  NULL);
1358  base::ListValue startup_urls;
1359  startup_urls.Append(base::Value::CreateStringValue(
1360      test_server()->GetURL("files/title1.html").spec()));
1361  policy_map_.Set(policy::key::kRestoreOnStartupURLs,
1362                  policy::POLICY_LEVEL_MANDATORY, policy::POLICY_SCOPE_USER,
1363                  startup_urls.DeepCopy(), NULL);
1364  provider_.UpdateChromePolicy(policy_map_);
1365  base::RunLoop().RunUntilIdle();
1366
1367  // Do a process-startup browser launch.
1368  CommandLine dummy(CommandLine::NO_PROGRAM);
1369  StartupBrowserCreatorImpl launch(base::FilePath(), dummy, &browser_creator,
1370                                   chrome::startup::IS_FIRST_RUN);
1371  ASSERT_TRUE(launch.Launch(browser()->profile(), std::vector<GURL>(), true,
1372                            browser()->host_desktop_type()));
1373
1374  // This should have created a new browser window.
1375  Browser* new_browser = FindOneOtherBrowser(browser());
1376  ASSERT_TRUE(new_browser);
1377
1378  // Verify that the URL specified through policy is shown and no sync promo has
1379  // been added.
1380  TabStripModel* tab_strip = new_browser->tab_strip_model();
1381  ASSERT_EQ(1, tab_strip->count());
1382  EXPECT_EQ("title1.html",
1383            tab_strip->GetWebContentsAt(0)->GetURL().ExtractFileName());
1384}
1385#endif  // defined(ENABLE_CONFIGURATION_POLICY)
1386
1387#endif  // !defined(OS_LINUX) || !defined(GOOGLE_CHROME_BUILD) ||
1388        // defined(ENABLE_CONFIGURATION_POLICY)
1389
1390#endif  // !defined(OS_CHROMEOS)
1391