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"
14 class LoginStateTest
: public testing::Test
,
15 public LoginState::Observer
{
17 LoginStateTest() : logged_in_user_type_(LoginState::LOGGED_IN_USER_NONE
),
18 login_state_changes_count_(0) {
20 virtual ~LoginStateTest() {
24 virtual void SetUp() OVERRIDE
{
25 LoginState::Initialize();
26 LoginState::Get()->set_always_logged_in(false);
27 LoginState::Get()->AddObserver(this);
30 virtual void TearDown() OVERRIDE
{
31 LoginState::Get()->RemoveObserver(this);
32 LoginState::Shutdown();
35 // LoginState::Observer
36 virtual void LoggedInStateChanged() OVERRIDE
{
37 ++login_state_changes_count_
;
38 logged_in_user_type_
= LoginState::Get()->GetLoggedInUserType();
42 // Returns number of times the login state changed since the last call to
44 unsigned int GetNewLoginStateChangesCount() {
45 unsigned int result
= login_state_changes_count_
;
46 login_state_changes_count_
= 0;
50 LoginState::LoggedInUserType logged_in_user_type_
;
53 unsigned int login_state_changes_count_
;
55 DISALLOW_COPY_AND_ASSIGN(LoginStateTest
);
58 TEST_F(LoginStateTest
, TestLoginState
) {
59 EXPECT_FALSE(LoginState::Get()->IsUserLoggedIn());
60 EXPECT_FALSE(LoginState::Get()->IsInSafeMode());
61 EXPECT_EQ(LoginState::LOGGED_IN_USER_NONE
, logged_in_user_type_
);
62 EXPECT_EQ(LoginState::LOGGED_IN_USER_NONE
,
63 LoginState::Get()->GetLoggedInUserType());
65 // Setting login state to ACTIVE.
66 LoginState::Get()->SetLoggedInState(LoginState::LOGGED_IN_ACTIVE
,
67 LoginState::LOGGED_IN_USER_REGULAR
);
68 EXPECT_EQ(LoginState::LOGGED_IN_USER_REGULAR
,
69 LoginState::Get()->GetLoggedInUserType());
70 EXPECT_TRUE(LoginState::Get()->IsUserLoggedIn());
71 EXPECT_FALSE(LoginState::Get()->IsInSafeMode());
73 EXPECT_EQ(1U, GetNewLoginStateChangesCount());
74 EXPECT_EQ(LoginState::LOGGED_IN_USER_REGULAR
, logged_in_user_type_
);
77 TEST_F(LoginStateTest
, TestSafeModeLoginState
) {
78 EXPECT_FALSE(LoginState::Get()->IsUserLoggedIn());
79 EXPECT_FALSE(LoginState::Get()->IsInSafeMode());
80 EXPECT_EQ(LoginState::LOGGED_IN_USER_NONE
, logged_in_user_type_
);
81 EXPECT_EQ(LoginState::LOGGED_IN_USER_NONE
,
82 LoginState::Get()->GetLoggedInUserType());
83 // Setting login state to SAFE MODE.
84 LoginState::Get()->SetLoggedInState(LoginState::LOGGED_IN_SAFE_MODE
,
85 LoginState::LOGGED_IN_USER_NONE
);
86 EXPECT_EQ(LoginState::LOGGED_IN_USER_NONE
,
87 LoginState::Get()->GetLoggedInUserType());
88 EXPECT_FALSE(LoginState::Get()->IsUserLoggedIn());
89 EXPECT_TRUE(LoginState::Get()->IsInSafeMode());
91 EXPECT_EQ(1U, GetNewLoginStateChangesCount());
92 EXPECT_EQ(LoginState::LOGGED_IN_USER_NONE
, logged_in_user_type_
);
94 // Setting login state to ACTIVE.
95 LoginState::Get()->SetLoggedInState(LoginState::LOGGED_IN_ACTIVE
,
96 LoginState::LOGGED_IN_USER_OWNER
);
97 EXPECT_EQ(LoginState::LOGGED_IN_USER_OWNER
,
98 LoginState::Get()->GetLoggedInUserType());
99 EXPECT_TRUE(LoginState::Get()->IsUserLoggedIn());
100 EXPECT_FALSE(LoginState::Get()->IsInSafeMode());
102 EXPECT_EQ(1U, GetNewLoginStateChangesCount());
103 EXPECT_EQ(LoginState::LOGGED_IN_USER_OWNER
, logged_in_user_type_
);
106 TEST_F(LoginStateTest
, TestLoggedInStateChangedObserverOnUserTypeChange
) {
107 LoginState::Get()->SetLoggedInState(LoginState::LOGGED_IN_ACTIVE
,
108 LoginState::LOGGED_IN_USER_REGULAR
);
110 EXPECT_EQ(1u, GetNewLoginStateChangesCount());
111 EXPECT_TRUE(LoginState::Get()->IsUserLoggedIn());
112 EXPECT_EQ(LoginState::LOGGED_IN_USER_REGULAR
, logged_in_user_type_
);
113 EXPECT_EQ(LoginState::LOGGED_IN_USER_REGULAR
,
114 LoginState::Get()->GetLoggedInUserType());
116 // Change the user type, without changing the logged in state.
117 LoginState::Get()->SetLoggedInState(LoginState::LOGGED_IN_ACTIVE
,
118 LoginState::LOGGED_IN_USER_OWNER
);
120 EXPECT_EQ(1u, GetNewLoginStateChangesCount());
121 EXPECT_TRUE(LoginState::Get()->IsUserLoggedIn());
122 EXPECT_EQ(LoginState::LOGGED_IN_USER_OWNER
, logged_in_user_type_
);
123 EXPECT_EQ(LoginState::LOGGED_IN_USER_OWNER
,
124 LoginState::Get()->GetLoggedInUserType());
127 } // namespace chromeos