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"
13 const char kTestUserHash
[] = "testuserhash";
18 class LoginStateTest
: public testing::Test
,
19 public LoginState::Observer
{
21 LoginStateTest() : logged_in_user_type_(LoginState::LOGGED_IN_USER_NONE
),
22 login_state_changes_count_(0) {
24 virtual ~LoginStateTest() {
28 virtual void SetUp() OVERRIDE
{
29 LoginState::Initialize();
30 LoginState::Get()->set_always_logged_in(false);
31 LoginState::Get()->AddObserver(this);
34 virtual void TearDown() OVERRIDE
{
35 LoginState::Get()->RemoveObserver(this);
36 LoginState::Shutdown();
39 // LoginState::Observer
40 virtual void LoggedInStateChanged() OVERRIDE
{
41 ++login_state_changes_count_
;
42 logged_in_user_type_
= LoginState::Get()->GetLoggedInUserType();
46 // Returns number of times the login state changed since the last call to
48 unsigned int GetNewLoginStateChangesCount() {
49 unsigned int result
= login_state_changes_count_
;
50 login_state_changes_count_
= 0;
54 LoginState::LoggedInUserType logged_in_user_type_
;
57 unsigned int login_state_changes_count_
;
59 DISALLOW_COPY_AND_ASSIGN(LoginStateTest
);
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());
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());
77 EXPECT_EQ(1U, GetNewLoginStateChangesCount());
78 EXPECT_EQ(LoginState::LOGGED_IN_USER_REGULAR
, logged_in_user_type_
);
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());
95 EXPECT_EQ(1U, GetNewLoginStateChangesCount());
96 EXPECT_EQ(LoginState::LOGGED_IN_USER_NONE
, logged_in_user_type_
);
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());
106 EXPECT_EQ(1U, GetNewLoginStateChangesCount());
107 EXPECT_EQ(LoginState::LOGGED_IN_USER_OWNER
, logged_in_user_type_
);
110 TEST_F(LoginStateTest
, TestLoggedInStateChangedObserverOnUserTypeChange
) {
111 LoginState::Get()->SetLoggedInState(LoginState::LOGGED_IN_ACTIVE
,
112 LoginState::LOGGED_IN_USER_REGULAR
);
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());
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
);
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());
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());
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
,
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());
149 EXPECT_EQ(1U, GetNewLoginStateChangesCount());
150 EXPECT_EQ(LoginState::LOGGED_IN_USER_REGULAR
, logged_in_user_type_
);
153 } // namespace chromeos