1 // Copyright 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.
8 #include "base/command_line.h"
9 #include "base/memory/ref_counted.h"
10 #include "base/run_loop.h"
11 #include "chrome/browser/chromeos/login/session/user_session_manager.h"
12 #include "chrome/browser/chromeos/login/session/user_session_manager_test_api.h"
13 #include "chrome/test/base/in_process_browser_test.h"
14 #include "chromeos/chromeos_switches.h"
15 #include "chromeos/dbus/cryptohome_client.h"
16 #include "chromeos/dbus/dbus_thread_manager.h"
17 #include "chromeos/dbus/fake_session_manager_client.h"
18 #include "chromeos/dbus/session_manager_client.h"
19 #include "components/user_manager/user.h"
20 #include "components/user_manager/user_manager.h"
21 #include "content/public/test/test_utils.h"
22 #include "testing/gmock/include/gmock/gmock.h"
23 #include "third_party/cros_system_api/dbus/service_constants.h"
29 const char kUserId1
[] = "user1@example.com";
30 const char kUserId2
[] = "user2@example.com";
31 const char kUserId3
[] = "user3@example.com";
35 class CrashRestoreSimpleTest
: public InProcessBrowserTest
{
37 CrashRestoreSimpleTest() {}
39 ~CrashRestoreSimpleTest() override
{}
41 void SetUpCommandLine(base::CommandLine
* command_line
) override
{
42 command_line
->AppendSwitchASCII(switches::kLoginUser
, kUserId1
);
43 command_line
->AppendSwitchASCII(
44 switches::kLoginProfile
,
45 CryptohomeClient::GetStubSanitizedUsername(kUserId1
));
48 void SetUpInProcessBrowserTestFixture() override
{
49 // Redirect session_manager DBus calls to FakeSessionManagerClient.
50 session_manager_client_
= new FakeSessionManagerClient
;
51 chromeos::DBusThreadManager::GetSetterForTesting()->SetSessionManagerClient(
52 scoped_ptr
<SessionManagerClient
>(session_manager_client_
));
53 session_manager_client_
->StartSession(kUserId1
);
56 FakeSessionManagerClient
* session_manager_client_
;
59 IN_PROC_BROWSER_TEST_F(CrashRestoreSimpleTest
, RestoreSessionForOneUser
) {
60 user_manager::UserManager
* user_manager
= user_manager::UserManager::Get();
61 user_manager::User
* user
= user_manager
->GetActiveUser();
63 EXPECT_EQ(kUserId1
, user
->email());
64 EXPECT_EQ(CryptohomeClient::GetStubSanitizedUsername(kUserId1
),
65 user
->username_hash());
66 EXPECT_EQ(1UL, user_manager
->GetLoggedInUsers().size());
69 // Observer that keeps track of user sessions restore event.
70 class UserSessionRestoreObserver
: public UserSessionStateObserver
{
72 UserSessionRestoreObserver()
73 : running_loop_(false),
74 user_sessions_restored_(
75 UserSessionManager::GetInstance()->UserSessionsRestored()) {
76 if (!user_sessions_restored_
)
77 UserSessionManager::GetInstance()->AddSessionStateObserver(this);
79 ~UserSessionRestoreObserver() override
{}
81 void PendingUserSessionsRestoreFinished() override
{
82 user_sessions_restored_
= true;
83 UserSessionManager::GetInstance()->RemoveSessionStateObserver(this);
87 message_loop_runner_
->Quit();
88 running_loop_
= false;
91 // Wait until the user sessions are restored. If that happened between the
92 // construction of this object and this call or even before it was created
93 // then it returns immediately.
95 if (user_sessions_restored_
)
99 message_loop_runner_
= new content::MessageLoopRunner();
100 message_loop_runner_
->Run();
105 bool user_sessions_restored_
;
106 scoped_refptr
<content::MessageLoopRunner
> message_loop_runner_
;
108 DISALLOW_COPY_AND_ASSIGN(UserSessionRestoreObserver
);
111 class CrashRestoreComplexTest
: public CrashRestoreSimpleTest
{
113 CrashRestoreComplexTest() {}
114 ~CrashRestoreComplexTest() override
{}
116 void SetUpInProcessBrowserTestFixture() override
{
117 CrashRestoreSimpleTest::SetUpInProcessBrowserTestFixture();
118 session_manager_client_
->StartSession(kUserId2
);
119 session_manager_client_
->StartSession(kUserId3
);
123 IN_PROC_BROWSER_TEST_F(CrashRestoreComplexTest
, RestoreSessionForThreeUsers
) {
125 UserSessionRestoreObserver restore_observer
;
126 restore_observer
.Wait();
129 chromeos::test::UserSessionManagerTestApi
session_manager_test_api(
130 chromeos::UserSessionManager::GetInstance());
131 session_manager_test_api
.SetShouldObtainTokenHandleInTests(false);
133 DCHECK(UserSessionManager::GetInstance()->UserSessionsRestored());
135 // User that is last in the user sessions map becomes active. This behavior
136 // will become better defined once each user gets a separate user desktop.
137 user_manager::UserManager
* user_manager
= user_manager::UserManager::Get();
138 user_manager::User
* user
= user_manager
->GetActiveUser();
140 EXPECT_EQ(kUserId3
, user
->email());
141 EXPECT_EQ(CryptohomeClient::GetStubSanitizedUsername(kUserId3
),
142 user
->username_hash());
143 const user_manager::UserList
& users
= user_manager
->GetLoggedInUsers();
144 ASSERT_EQ(3UL, users
.size());
146 // User that becomes active moves to the beginning of the list.
147 EXPECT_EQ(kUserId3
, users
[0]->email());
148 EXPECT_EQ(CryptohomeClient::GetStubSanitizedUsername(kUserId3
),
149 users
[0]->username_hash());
150 EXPECT_EQ(kUserId2
, users
[1]->email());
151 EXPECT_EQ(CryptohomeClient::GetStubSanitizedUsername(kUserId2
),
152 users
[1]->username_hash());
153 EXPECT_EQ(kUserId1
, users
[2]->email());
154 EXPECT_EQ(CryptohomeClient::GetStubSanitizedUsername(kUserId1
),
155 users
[2]->username_hash());
158 } // namespace chromeos