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