summaryrefslogtreecommitdiffstats
path: root/chrome/browser/chromeos/login/crash_restore_browsertest.cc
blob: 8bcaaf5576c09cfdabaff64548010ca59ff3d3a1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <string>
#include <vector>

#include "base/command_line.h"
#include "base/memory/ref_counted.h"
#include "base/run_loop.h"
#include "chrome/browser/chromeos/login/user.h"
#include "chrome/browser/chromeos/login/user_manager.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chromeos/chromeos_switches.h"
#include "chromeos/dbus/cryptohome_client.h"
#include "chromeos/dbus/fake_dbus_thread_manager.h"
#include "chromeos/dbus/fake_session_manager_client.h"
#include "chromeos/dbus/session_manager_client.h"
#include "content/public/test/test_utils.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "third_party/cros_system_api/dbus/service_constants.h"

namespace chromeos {

namespace {

const char kUserId1[] = "user1@example.com";
const char kUserId2[] = "user2@example.com";
const char kUserId3[] = "user3@example.com";

}  // namespace

class CrashRestoreSimpleTest : public InProcessBrowserTest {
 protected:
  CrashRestoreSimpleTest() {}

  virtual ~CrashRestoreSimpleTest() {}

  virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
    command_line->AppendSwitch(::switches::kMultiProfiles);
    command_line->AppendSwitchASCII(switches::kLoginUser, kUserId1);
    command_line->AppendSwitchASCII(
        switches::kLoginProfile,
        CryptohomeClient::GetStubSanitizedUsername(kUserId1));
  }

  virtual void SetUpInProcessBrowserTestFixture() OVERRIDE {
    // Redirect session_manager DBus calls to FakeSessionManagerClient.
    FakeDBusThreadManager* dbus_thread_manager = new FakeDBusThreadManager;
    dbus_thread_manager->SetFakeClients();
    session_manager_client_ = new FakeSessionManagerClient;
    dbus_thread_manager->SetSessionManagerClient(
        scoped_ptr<SessionManagerClient>(session_manager_client_));
    DBusThreadManager::SetInstanceForTesting(dbus_thread_manager);
    session_manager_client_->StartSession(kUserId1);
  }

  FakeSessionManagerClient* session_manager_client_;
};

IN_PROC_BROWSER_TEST_F(CrashRestoreSimpleTest, RestoreSessionForOneUser) {
  UserManager* user_manager = UserManager::Get();
  User* user = user_manager->GetActiveUser();
  ASSERT_TRUE(user);
  EXPECT_EQ(kUserId1, user->email());
  EXPECT_EQ(CryptohomeClient::GetStubSanitizedUsername(kUserId1),
            user->username_hash());
  EXPECT_EQ(1UL, user_manager->GetLoggedInUsers().size());
}

// Observer that keeps track of user sessions restore event.
class UserSessionRestoreObserver :
    public UserManager::UserSessionStateObserver {
 public:
  UserSessionRestoreObserver()
      : running_loop_(false),
        user_sessions_restored_(UserManager::Get()->UserSessionsRestored()) {
    if (!user_sessions_restored_)
      UserManager::Get()->AddSessionStateObserver(this);
  }
  virtual ~UserSessionRestoreObserver() {}

  virtual void PendingUserSessionsRestoreFinished() OVERRIDE {
    user_sessions_restored_ = true;
    UserManager::Get()->RemoveSessionStateObserver(this);
    if (!running_loop_)
      return;

    message_loop_runner_->Quit();
    running_loop_ = false;
  }

  // Wait until the user sessions are restored. If that happened between the
  // construction of this object and this call or even before it was created
  // then it returns immediately.
  void Wait() {
    if (user_sessions_restored_)
      return;

    running_loop_ = true;
    message_loop_runner_ = new content::MessageLoopRunner();
    message_loop_runner_->Run();
  }

 private:
  bool running_loop_;
  bool user_sessions_restored_;
  scoped_refptr<content::MessageLoopRunner> message_loop_runner_;

  DISALLOW_COPY_AND_ASSIGN(UserSessionRestoreObserver);
};

class CrashRestoreComplexTest : public CrashRestoreSimpleTest {
 protected:
  CrashRestoreComplexTest() {}
  virtual ~CrashRestoreComplexTest() {}

  virtual void SetUpInProcessBrowserTestFixture() OVERRIDE {
    CrashRestoreSimpleTest::SetUpInProcessBrowserTestFixture();
    session_manager_client_->StartSession(kUserId2);
    session_manager_client_->StartSession(kUserId3);
  }
};

IN_PROC_BROWSER_TEST_F(CrashRestoreComplexTest, RestoreSessionForThreeUsers) {
  {
    UserSessionRestoreObserver restore_observer;
    restore_observer.Wait();
  }

  UserManager* user_manager = UserManager::Get();
  DCHECK(user_manager->UserSessionsRestored());

  // User that is last in the user sessions map becomes active. This behavior
  // will become better defined once each user gets a separate user desktop.
  User* user = user_manager->GetActiveUser();
  ASSERT_TRUE(user);
  EXPECT_EQ(kUserId3, user->email());
  EXPECT_EQ(CryptohomeClient::GetStubSanitizedUsername(kUserId3),
            user->username_hash());
  const UserList& users = user_manager->GetLoggedInUsers();
  ASSERT_EQ(3UL, users.size());

  // User that becomes active moves to the beginning of the list.
  EXPECT_EQ(kUserId3, users[0]->email());
  EXPECT_EQ(CryptohomeClient::GetStubSanitizedUsername(kUserId3),
            users[0]->username_hash());
  EXPECT_EQ(kUserId2, users[1]->email());
  EXPECT_EQ(CryptohomeClient::GetStubSanitizedUsername(kUserId2),
            users[1]->username_hash());
  EXPECT_EQ(kUserId1, users[2]->email());
  EXPECT_EQ(CryptohomeClient::GetStubSanitizedUsername(kUserId1),
            users[2]->username_hash());
}

}  // namespace chromeos