1 // Copyright (c) 2013 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.
7 #include "base/command_line.h"
8 #include "base/files/file_path.h"
9 #include "base/message_loop/message_loop.h"
10 #include "chrome/browser/browser_process.h"
11 #include "chrome/browser/prefs/session_startup_pref.h"
12 #include "chrome/browser/profiles/profile_manager.h"
13 #include "chrome/browser/sessions/session_restore.h"
14 #include "chrome/browser/ui/browser.h"
15 #include "chrome/browser/ui/browser_finder.h"
16 #include "chrome/browser/ui/browser_window.h"
17 #include "chrome/browser/ui/startup/startup_browser_creator.h"
18 #include "chrome/test/base/in_process_browser_test.h"
19 #include "chrome/test/base/ui_test_utils.h"
20 #include "testing/gtest/include/gtest/gtest.h"
23 typedef InProcessBrowserTest StartupBrowserCreatorTest
;
25 // Chrome OS doesn't support multiprofile.
26 // And BrowserWindow::IsActive() always returns false in tests on MAC.
27 // And this test is useless without that functionality.
28 #if !defined(OS_CHROMEOS) && !defined(OS_MACOSX)
29 IN_PROC_BROWSER_TEST_F(StartupBrowserCreatorTest
, LastUsedProfileActivated
) {
30 ProfileManager
* profile_manager
= g_browser_process
->profile_manager();
32 // Create 4 profiles, they will be scheduled for destruction when the last
33 // browser window they are associated to will be closed.
34 Profile
* profile_1
= profile_manager
->GetProfile(
35 profile_manager
->user_data_dir().Append(FILE_PATH_LITERAL(
37 ASSERT_TRUE(profile_1
);
38 Profile
* profile_2
= profile_manager
->GetProfile(
39 profile_manager
->user_data_dir().Append(FILE_PATH_LITERAL(
41 ASSERT_TRUE(profile_2
);
42 Profile
* profile_3
= profile_manager
->GetProfile(
43 profile_manager
->user_data_dir().Append(FILE_PATH_LITERAL(
45 ASSERT_TRUE(profile_3
);
46 Profile
* profile_4
= profile_manager
->GetProfile(
47 profile_manager
->user_data_dir().Append(FILE_PATH_LITERAL(
49 ASSERT_TRUE(profile_4
);
51 SessionStartupPref
pref_urls(SessionStartupPref::URLS
);
52 pref_urls
.urls
.push_back(ui_test_utils::GetTestUrl(
53 base::FilePath(base::FilePath::kCurrentDirectory
),
54 base::FilePath(FILE_PATH_LITERAL("title1.html"))));
55 SessionStartupPref::SetStartupPref(profile_1
, pref_urls
);
56 SessionStartupPref::SetStartupPref(profile_2
, pref_urls
);
57 SessionStartupPref::SetStartupPref(profile_3
, pref_urls
);
58 SessionStartupPref::SetStartupPref(profile_4
, pref_urls
);
60 // Do a simple non-process-startup browser launch.
61 base::CommandLine
dummy(base::CommandLine::NO_PROGRAM
);
63 StartupBrowserCreator browser_creator
;
64 std::vector
<Profile
*> last_opened_profiles
;
65 last_opened_profiles
.push_back(profile_1
);
66 last_opened_profiles
.push_back(profile_2
);
67 last_opened_profiles
.push_back(profile_3
);
68 last_opened_profiles
.push_back(profile_4
);
69 browser_creator
.Start(dummy
, profile_manager
->user_data_dir(), profile_2
,
70 last_opened_profiles
);
72 while (!browser_creator
.ActivatedProfile())
73 base::MessageLoop::current()->RunUntilIdle();
75 Browser
* new_browser
= NULL
;
77 // The last used profile (the profile_2 in this case) must be active.
78 ASSERT_EQ(1u, chrome::GetBrowserCount(profile_2
,
79 browser()->host_desktop_type()));
80 new_browser
= FindBrowserWithProfile(profile_2
,
81 browser()->host_desktop_type());
82 ASSERT_TRUE(new_browser
);
83 EXPECT_TRUE(new_browser
->window()->IsActive());
85 // All other profiles browser should not be active.
86 ASSERT_EQ(1u, chrome::GetBrowserCount(profile_1
,
87 browser()->host_desktop_type()));
88 new_browser
= FindBrowserWithProfile(profile_1
,
89 browser()->host_desktop_type());
90 ASSERT_TRUE(new_browser
);
91 EXPECT_FALSE(new_browser
->window()->IsActive());
93 ASSERT_EQ(1u, chrome::GetBrowserCount(profile_3
,
94 browser()->host_desktop_type()));
95 new_browser
= FindBrowserWithProfile(profile_3
,
96 browser()->host_desktop_type());
97 ASSERT_TRUE(new_browser
);
98 EXPECT_FALSE(new_browser
->window()->IsActive());
100 ASSERT_EQ(1u, chrome::GetBrowserCount(profile_4
,
101 browser()->host_desktop_type()));
102 new_browser
= FindBrowserWithProfile(profile_4
,
103 browser()->host_desktop_type());
104 ASSERT_TRUE(new_browser
);
105 EXPECT_FALSE(new_browser
->window()->IsActive());
107 #endif // !OS_MACOSX && !OS_CHROMEOS