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