Roll src/third_party/WebKit 6b63e20:35e1984 (svn 201060:201061)
[chromium-blink-merge.git] / chromeos / login / login_state_unittest.cc
bloba6a5d4a1ce6040268f9c6cee1def2e6dc83a2882
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.
5 #include "chromeos/login/login_state.h"
7 #include "base/command_line.h"
8 #include "base/compiler_specific.h"
9 #include "chromeos/chromeos_switches.h"
10 #include "testing/gtest/include/gtest/gtest.h"
12 namespace {
13 const char kTestUserHash[] = "testuserhash";
14 } // namespace
16 namespace chromeos {
18 class LoginStateTest : public testing::Test,
19 public LoginState::Observer {
20 public:
21 LoginStateTest() : logged_in_user_type_(LoginState::LOGGED_IN_USER_NONE),
22 login_state_changes_count_(0) {
24 ~LoginStateTest() override {}
26 // testing::Test
27 void SetUp() override {
28 LoginState::Initialize();
29 LoginState::Get()->set_always_logged_in(false);
30 LoginState::Get()->AddObserver(this);
33 void TearDown() override {
34 LoginState::Get()->RemoveObserver(this);
35 LoginState::Shutdown();
38 // LoginState::Observer
39 void LoggedInStateChanged() override {
40 ++login_state_changes_count_;
41 logged_in_user_type_ = LoginState::Get()->GetLoggedInUserType();
44 protected:
45 // Returns number of times the login state changed since the last call to
46 // this method.
47 unsigned int GetNewLoginStateChangesCount() {
48 unsigned int result = login_state_changes_count_;
49 login_state_changes_count_ = 0;
50 return result;
53 LoginState::LoggedInUserType logged_in_user_type_;
55 private:
56 unsigned int login_state_changes_count_;
58 DISALLOW_COPY_AND_ASSIGN(LoginStateTest);
61 TEST_F(LoginStateTest, TestLoginState) {
62 EXPECT_FALSE(LoginState::Get()->IsUserLoggedIn());
63 EXPECT_FALSE(LoginState::Get()->IsInSafeMode());
64 EXPECT_EQ(LoginState::LOGGED_IN_USER_NONE, logged_in_user_type_);
65 EXPECT_EQ(LoginState::LOGGED_IN_USER_NONE,
66 LoginState::Get()->GetLoggedInUserType());
68 // Setting login state to ACTIVE.
69 LoginState::Get()->SetLoggedInState(LoginState::LOGGED_IN_ACTIVE,
70 LoginState::LOGGED_IN_USER_REGULAR);
71 EXPECT_EQ(LoginState::LOGGED_IN_USER_REGULAR,
72 LoginState::Get()->GetLoggedInUserType());
73 EXPECT_TRUE(LoginState::Get()->IsUserLoggedIn());
74 EXPECT_FALSE(LoginState::Get()->IsInSafeMode());
76 EXPECT_EQ(1U, GetNewLoginStateChangesCount());
77 EXPECT_EQ(LoginState::LOGGED_IN_USER_REGULAR, logged_in_user_type_);
80 TEST_F(LoginStateTest, TestSafeModeLoginState) {
81 EXPECT_FALSE(LoginState::Get()->IsUserLoggedIn());
82 EXPECT_FALSE(LoginState::Get()->IsInSafeMode());
83 EXPECT_EQ(LoginState::LOGGED_IN_USER_NONE, logged_in_user_type_);
84 EXPECT_EQ(LoginState::LOGGED_IN_USER_NONE,
85 LoginState::Get()->GetLoggedInUserType());
86 // Setting login state to SAFE MODE.
87 LoginState::Get()->SetLoggedInState(LoginState::LOGGED_IN_SAFE_MODE,
88 LoginState::LOGGED_IN_USER_NONE);
89 EXPECT_EQ(LoginState::LOGGED_IN_USER_NONE,
90 LoginState::Get()->GetLoggedInUserType());
91 EXPECT_FALSE(LoginState::Get()->IsUserLoggedIn());
92 EXPECT_TRUE(LoginState::Get()->IsInSafeMode());
94 EXPECT_EQ(1U, GetNewLoginStateChangesCount());
95 EXPECT_EQ(LoginState::LOGGED_IN_USER_NONE, logged_in_user_type_);
97 // Setting login state to ACTIVE.
98 LoginState::Get()->SetLoggedInState(LoginState::LOGGED_IN_ACTIVE,
99 LoginState::LOGGED_IN_USER_OWNER);
100 EXPECT_EQ(LoginState::LOGGED_IN_USER_OWNER,
101 LoginState::Get()->GetLoggedInUserType());
102 EXPECT_TRUE(LoginState::Get()->IsUserLoggedIn());
103 EXPECT_FALSE(LoginState::Get()->IsInSafeMode());
105 EXPECT_EQ(1U, GetNewLoginStateChangesCount());
106 EXPECT_EQ(LoginState::LOGGED_IN_USER_OWNER, logged_in_user_type_);
109 TEST_F(LoginStateTest, TestLoggedInStateChangedObserverOnUserTypeChange) {
110 LoginState::Get()->SetLoggedInState(LoginState::LOGGED_IN_ACTIVE,
111 LoginState::LOGGED_IN_USER_REGULAR);
113 EXPECT_EQ(1u, GetNewLoginStateChangesCount());
114 EXPECT_TRUE(LoginState::Get()->IsUserLoggedIn());
115 EXPECT_EQ(LoginState::LOGGED_IN_USER_REGULAR, logged_in_user_type_);
116 EXPECT_EQ(LoginState::LOGGED_IN_USER_REGULAR,
117 LoginState::Get()->GetLoggedInUserType());
119 // Change the user type, without changing the logged in state.
120 LoginState::Get()->SetLoggedInState(LoginState::LOGGED_IN_ACTIVE,
121 LoginState::LOGGED_IN_USER_OWNER);
123 EXPECT_EQ(1u, GetNewLoginStateChangesCount());
124 EXPECT_TRUE(LoginState::Get()->IsUserLoggedIn());
125 EXPECT_EQ(LoginState::LOGGED_IN_USER_OWNER, logged_in_user_type_);
126 EXPECT_EQ(LoginState::LOGGED_IN_USER_OWNER,
127 LoginState::Get()->GetLoggedInUserType());
130 TEST_F(LoginStateTest, TestPrimaryUser) {
131 EXPECT_FALSE(LoginState::Get()->IsUserLoggedIn());
132 EXPECT_FALSE(LoginState::Get()->IsInSafeMode());
133 EXPECT_EQ(LoginState::LOGGED_IN_USER_NONE, logged_in_user_type_);
134 EXPECT_EQ(LoginState::LOGGED_IN_USER_NONE,
135 LoginState::Get()->GetLoggedInUserType());
137 // Setting login state to ACTIVE and setting the primary user.
138 LoginState::Get()->SetLoggedInStateAndPrimaryUser(
139 LoginState::LOGGED_IN_ACTIVE,
140 LoginState::LOGGED_IN_USER_REGULAR,
141 kTestUserHash);
142 EXPECT_EQ(LoginState::LOGGED_IN_USER_REGULAR,
143 LoginState::Get()->GetLoggedInUserType());
144 EXPECT_TRUE(LoginState::Get()->IsUserLoggedIn());
145 EXPECT_FALSE(LoginState::Get()->IsInSafeMode());
146 EXPECT_EQ(kTestUserHash, LoginState::Get()->primary_user_hash());
148 EXPECT_EQ(1U, GetNewLoginStateChangesCount());
149 EXPECT_EQ(LoginState::LOGGED_IN_USER_REGULAR, logged_in_user_type_);
152 } // namespace chromeos