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.
4
5 #include "chromeos/login/login_state.h"
6
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"
11
12 namespace {
13 const char kTestUserHash[] = "testuserhash";
14 } // namespace
15
16 namespace chromeos {
17
18 class LoginStateTest : public testing::Test,
19 public LoginState::Observer {
20 public:
LoginStateTest()21 LoginStateTest() : logged_in_user_type_(LoginState::LOGGED_IN_USER_NONE),
22 login_state_changes_count_(0) {
23 }
~LoginStateTest()24 virtual ~LoginStateTest() {
25 }
26
27 // testing::Test
SetUp()28 virtual void SetUp() OVERRIDE {
29 LoginState::Initialize();
30 LoginState::Get()->set_always_logged_in(false);
31 LoginState::Get()->AddObserver(this);
32 }
33
TearDown()34 virtual void TearDown() OVERRIDE {
35 LoginState::Get()->RemoveObserver(this);
36 LoginState::Shutdown();
37 }
38
39 // LoginState::Observer
LoggedInStateChanged()40 virtual void LoggedInStateChanged() OVERRIDE {
41 ++login_state_changes_count_;
42 logged_in_user_type_ = LoginState::Get()->GetLoggedInUserType();
43 }
44
45 protected:
46 // Returns number of times the login state changed since the last call to
47 // this method.
GetNewLoginStateChangesCount()48 unsigned int GetNewLoginStateChangesCount() {
49 unsigned int result = login_state_changes_count_;
50 login_state_changes_count_ = 0;
51 return result;
52 }
53
54 LoginState::LoggedInUserType logged_in_user_type_;
55
56 private:
57 unsigned int login_state_changes_count_;
58
59 DISALLOW_COPY_AND_ASSIGN(LoginStateTest);
60 };
61
TEST_F(LoginStateTest,TestLoginState)62 TEST_F(LoginStateTest, TestLoginState) {
63 EXPECT_FALSE(LoginState::Get()->IsUserLoggedIn());
64 EXPECT_FALSE(LoginState::Get()->IsInSafeMode());
65 EXPECT_EQ(LoginState::LOGGED_IN_USER_NONE, logged_in_user_type_);
66 EXPECT_EQ(LoginState::LOGGED_IN_USER_NONE,
67 LoginState::Get()->GetLoggedInUserType());
68
69 // Setting login state to ACTIVE.
70 LoginState::Get()->SetLoggedInState(LoginState::LOGGED_IN_ACTIVE,
71 LoginState::LOGGED_IN_USER_REGULAR);
72 EXPECT_EQ(LoginState::LOGGED_IN_USER_REGULAR,
73 LoginState::Get()->GetLoggedInUserType());
74 EXPECT_TRUE(LoginState::Get()->IsUserLoggedIn());
75 EXPECT_FALSE(LoginState::Get()->IsInSafeMode());
76
77 EXPECT_EQ(1U, GetNewLoginStateChangesCount());
78 EXPECT_EQ(LoginState::LOGGED_IN_USER_REGULAR, logged_in_user_type_);
79 }
80
TEST_F(LoginStateTest,TestSafeModeLoginState)81 TEST_F(LoginStateTest, TestSafeModeLoginState) {
82 EXPECT_FALSE(LoginState::Get()->IsUserLoggedIn());
83 EXPECT_FALSE(LoginState::Get()->IsInSafeMode());
84 EXPECT_EQ(LoginState::LOGGED_IN_USER_NONE, logged_in_user_type_);
85 EXPECT_EQ(LoginState::LOGGED_IN_USER_NONE,
86 LoginState::Get()->GetLoggedInUserType());
87 // Setting login state to SAFE MODE.
88 LoginState::Get()->SetLoggedInState(LoginState::LOGGED_IN_SAFE_MODE,
89 LoginState::LOGGED_IN_USER_NONE);
90 EXPECT_EQ(LoginState::LOGGED_IN_USER_NONE,
91 LoginState::Get()->GetLoggedInUserType());
92 EXPECT_FALSE(LoginState::Get()->IsUserLoggedIn());
93 EXPECT_TRUE(LoginState::Get()->IsInSafeMode());
94
95 EXPECT_EQ(1U, GetNewLoginStateChangesCount());
96 EXPECT_EQ(LoginState::LOGGED_IN_USER_NONE, logged_in_user_type_);
97
98 // Setting login state to ACTIVE.
99 LoginState::Get()->SetLoggedInState(LoginState::LOGGED_IN_ACTIVE,
100 LoginState::LOGGED_IN_USER_OWNER);
101 EXPECT_EQ(LoginState::LOGGED_IN_USER_OWNER,
102 LoginState::Get()->GetLoggedInUserType());
103 EXPECT_TRUE(LoginState::Get()->IsUserLoggedIn());
104 EXPECT_FALSE(LoginState::Get()->IsInSafeMode());
105
106 EXPECT_EQ(1U, GetNewLoginStateChangesCount());
107 EXPECT_EQ(LoginState::LOGGED_IN_USER_OWNER, logged_in_user_type_);
108 }
109
TEST_F(LoginStateTest,TestLoggedInStateChangedObserverOnUserTypeChange)110 TEST_F(LoginStateTest, TestLoggedInStateChangedObserverOnUserTypeChange) {
111 LoginState::Get()->SetLoggedInState(LoginState::LOGGED_IN_ACTIVE,
112 LoginState::LOGGED_IN_USER_REGULAR);
113
114 EXPECT_EQ(1u, GetNewLoginStateChangesCount());
115 EXPECT_TRUE(LoginState::Get()->IsUserLoggedIn());
116 EXPECT_EQ(LoginState::LOGGED_IN_USER_REGULAR, logged_in_user_type_);
117 EXPECT_EQ(LoginState::LOGGED_IN_USER_REGULAR,
118 LoginState::Get()->GetLoggedInUserType());
119
120 // Change the user type, without changing the logged in state.
121 LoginState::Get()->SetLoggedInState(LoginState::LOGGED_IN_ACTIVE,
122 LoginState::LOGGED_IN_USER_OWNER);
123
124 EXPECT_EQ(1u, GetNewLoginStateChangesCount());
125 EXPECT_TRUE(LoginState::Get()->IsUserLoggedIn());
126 EXPECT_EQ(LoginState::LOGGED_IN_USER_OWNER, logged_in_user_type_);
127 EXPECT_EQ(LoginState::LOGGED_IN_USER_OWNER,
128 LoginState::Get()->GetLoggedInUserType());
129 }
130
TEST_F(LoginStateTest,TestPrimaryUser)131 TEST_F(LoginStateTest, TestPrimaryUser) {
132 EXPECT_FALSE(LoginState::Get()->IsUserLoggedIn());
133 EXPECT_FALSE(LoginState::Get()->IsInSafeMode());
134 EXPECT_EQ(LoginState::LOGGED_IN_USER_NONE, logged_in_user_type_);
135 EXPECT_EQ(LoginState::LOGGED_IN_USER_NONE,
136 LoginState::Get()->GetLoggedInUserType());
137
138 // Setting login state to ACTIVE and setting the primary user.
139 LoginState::Get()->SetLoggedInStateAndPrimaryUser(
140 LoginState::LOGGED_IN_ACTIVE,
141 LoginState::LOGGED_IN_USER_REGULAR,
142 kTestUserHash);
143 EXPECT_EQ(LoginState::LOGGED_IN_USER_REGULAR,
144 LoginState::Get()->GetLoggedInUserType());
145 EXPECT_TRUE(LoginState::Get()->IsUserLoggedIn());
146 EXPECT_FALSE(LoginState::Get()->IsInSafeMode());
147 EXPECT_EQ(kTestUserHash, LoginState::Get()->primary_user_hash());
148
149 EXPECT_EQ(1U, GetNewLoginStateChangesCount());
150 EXPECT_EQ(LoginState::LOGGED_IN_USER_REGULAR, logged_in_user_type_);
151 }
152
153 } // namespace chromeos
154