Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / chromeos / login / crash_restore_browsertest.cc
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.
4
5 #include <string>
6 #include <vector>
7
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/test/base/in_process_browser_test.h"
13 #include "chromeos/chromeos_switches.h"
14 #include "chromeos/dbus/cryptohome_client.h"
15 #include "chromeos/dbus/fake_dbus_thread_manager.h"
16 #include "chromeos/dbus/fake_session_manager_client.h"
17 #include "chromeos/dbus/session_manager_client.h"
18 #include "components/user_manager/user.h"
19 #include "components/user_manager/user_manager.h"
20 #include "content/public/test/test_utils.h"
21 #include "testing/gmock/include/gmock/gmock.h"
22 #include "third_party/cros_system_api/dbus/service_constants.h"
23
24 namespace chromeos {
25
26 namespace {
27
28 const char kUserId1[] = "user1@example.com";
29 const char kUserId2[] = "user2@example.com";
30 const char kUserId3[] = "user3@example.com";
31
32 }  // namespace
33
34 class CrashRestoreSimpleTest : public InProcessBrowserTest {
35  protected:
36   CrashRestoreSimpleTest() {}
37
38   virtual ~CrashRestoreSimpleTest() {}
39
40   virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
41     command_line->AppendSwitchASCII(switches::kLoginUser, kUserId1);
42     command_line->AppendSwitchASCII(
43         switches::kLoginProfile,
44         CryptohomeClient::GetStubSanitizedUsername(kUserId1));
45   }
46
47   virtual void SetUpInProcessBrowserTestFixture() OVERRIDE {
48     // Redirect session_manager DBus calls to FakeSessionManagerClient.
49     FakeDBusThreadManager* dbus_thread_manager = new FakeDBusThreadManager;
50     dbus_thread_manager->SetFakeClients();
51     session_manager_client_ = new FakeSessionManagerClient;
52     dbus_thread_manager->SetSessionManagerClient(
53         scoped_ptr<SessionManagerClient>(session_manager_client_));
54     DBusThreadManager::SetInstanceForTesting(dbus_thread_manager);
55     session_manager_client_->StartSession(kUserId1);
56   }
57
58   FakeSessionManagerClient* session_manager_client_;
59 };
60
61 IN_PROC_BROWSER_TEST_F(CrashRestoreSimpleTest, RestoreSessionForOneUser) {
62   user_manager::UserManager* user_manager = user_manager::UserManager::Get();
63   user_manager::User* user = user_manager->GetActiveUser();
64   ASSERT_TRUE(user);
65   EXPECT_EQ(kUserId1, user->email());
66   EXPECT_EQ(CryptohomeClient::GetStubSanitizedUsername(kUserId1),
67             user->username_hash());
68   EXPECT_EQ(1UL, user_manager->GetLoggedInUsers().size());
69 }
70
71 // Observer that keeps track of user sessions restore event.
72 class UserSessionRestoreObserver : public UserSessionStateObserver {
73  public:
74   UserSessionRestoreObserver()
75       : running_loop_(false),
76         user_sessions_restored_(
77             UserSessionManager::GetInstance()->UserSessionsRestored()) {
78     if (!user_sessions_restored_)
79       UserSessionManager::GetInstance()->AddSessionStateObserver(this);
80   }
81   virtual ~UserSessionRestoreObserver() {}
82
83   virtual void PendingUserSessionsRestoreFinished() OVERRIDE {
84     user_sessions_restored_ = true;
85     UserSessionManager::GetInstance()->RemoveSessionStateObserver(this);
86     if (!running_loop_)
87       return;
88
89     message_loop_runner_->Quit();
90     running_loop_ = false;
91   }
92
93   // Wait until the user sessions are restored. If that happened between the
94   // construction of this object and this call or even before it was created
95   // then it returns immediately.
96   void Wait() {
97     if (user_sessions_restored_)
98       return;
99
100     running_loop_ = true;
101     message_loop_runner_ = new content::MessageLoopRunner();
102     message_loop_runner_->Run();
103   }
104
105  private:
106   bool running_loop_;
107   bool user_sessions_restored_;
108   scoped_refptr<content::MessageLoopRunner> message_loop_runner_;
109
110   DISALLOW_COPY_AND_ASSIGN(UserSessionRestoreObserver);
111 };
112
113 class CrashRestoreComplexTest : public CrashRestoreSimpleTest {
114  protected:
115   CrashRestoreComplexTest() {}
116   virtual ~CrashRestoreComplexTest() {}
117
118   virtual void SetUpInProcessBrowserTestFixture() OVERRIDE {
119     CrashRestoreSimpleTest::SetUpInProcessBrowserTestFixture();
120     session_manager_client_->StartSession(kUserId2);
121     session_manager_client_->StartSession(kUserId3);
122   }
123 };
124
125 IN_PROC_BROWSER_TEST_F(CrashRestoreComplexTest, RestoreSessionForThreeUsers) {
126   {
127     UserSessionRestoreObserver restore_observer;
128     restore_observer.Wait();
129   }
130
131   DCHECK(UserSessionManager::GetInstance()->UserSessionsRestored());
132
133   // User that is last in the user sessions map becomes active. This behavior
134   // will become better defined once each user gets a separate user desktop.
135   user_manager::UserManager* user_manager = user_manager::UserManager::Get();
136   user_manager::User* user = user_manager->GetActiveUser();
137   ASSERT_TRUE(user);
138   EXPECT_EQ(kUserId3, user->email());
139   EXPECT_EQ(CryptohomeClient::GetStubSanitizedUsername(kUserId3),
140             user->username_hash());
141   const user_manager::UserList& users = user_manager->GetLoggedInUsers();
142   ASSERT_EQ(3UL, users.size());
143
144   // User that becomes active moves to the beginning of the list.
145   EXPECT_EQ(kUserId3, users[0]->email());
146   EXPECT_EQ(CryptohomeClient::GetStubSanitizedUsername(kUserId3),
147             users[0]->username_hash());
148   EXPECT_EQ(kUserId2, users[1]->email());
149   EXPECT_EQ(CryptohomeClient::GetStubSanitizedUsername(kUserId2),
150             users[1]->username_hash());
151   EXPECT_EQ(kUserId1, users[2]->email());
152   EXPECT_EQ(CryptohomeClient::GetStubSanitizedUsername(kUserId1),
153             users[2]->username_hash());
154 }
155
156 }  // namespace chromeos