• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2013 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 "ash/test/test_session_state_delegate.h"
6 
7 #include <algorithm>
8 #include <string>
9 
10 #include "ash/shell.h"
11 #include "ash/system/user/login_status.h"
12 #include "base/strings/string16.h"
13 #include "base/strings/utf_string_conversions.h"
14 #include "testing/gtest/include/gtest/gtest.h"
15 
16 namespace {
17 
18 // The the "canonicalized" user ID from a given |email| address.
GetUserIDFromEmail(const std::string & email)19 std::string GetUserIDFromEmail(const std::string& email) {
20   std::string user_id = email;
21   std::transform(user_id.begin(), user_id.end(), user_id.begin(), ::tolower);
22   return user_id;
23 }
24 
25 }  // namespace
26 
27 namespace ash {
28 namespace test {
29 
TestSessionStateDelegate()30 TestSessionStateDelegate::TestSessionStateDelegate()
31     : has_active_user_(false),
32       active_user_session_started_(false),
33       can_lock_screen_(true),
34       should_lock_screen_before_suspending_(false),
35       screen_locked_(false),
36       user_adding_screen_running_(false),
37       logged_in_users_(1),
38       num_transfer_to_desktop_of_user_calls_(0) {
39 }
40 
~TestSessionStateDelegate()41 TestSessionStateDelegate::~TestSessionStateDelegate() {
42 }
43 
GetMaximumNumberOfLoggedInUsers() const44 int TestSessionStateDelegate::GetMaximumNumberOfLoggedInUsers() const {
45   return 3;
46 }
47 
NumberOfLoggedInUsers() const48 int TestSessionStateDelegate::NumberOfLoggedInUsers() const {
49   // TODO(skuhne): Add better test framework to test multiple profiles.
50   return has_active_user_ ? logged_in_users_ : 0;
51 }
52 
IsActiveUserSessionStarted() const53 bool TestSessionStateDelegate::IsActiveUserSessionStarted() const {
54   return active_user_session_started_;
55 }
56 
CanLockScreen() const57 bool TestSessionStateDelegate::CanLockScreen() const {
58   return has_active_user_ && can_lock_screen_;
59 }
60 
IsScreenLocked() const61 bool TestSessionStateDelegate::IsScreenLocked() const {
62   return screen_locked_;
63 }
64 
ShouldLockScreenBeforeSuspending() const65 bool TestSessionStateDelegate::ShouldLockScreenBeforeSuspending() const {
66   return should_lock_screen_before_suspending_;
67 }
68 
LockScreen()69 void TestSessionStateDelegate::LockScreen() {
70   if (CanLockScreen())
71     screen_locked_ = true;
72 }
73 
UnlockScreen()74 void TestSessionStateDelegate::UnlockScreen() {
75   screen_locked_ = false;
76 }
77 
IsUserSessionBlocked() const78 bool TestSessionStateDelegate::IsUserSessionBlocked() const {
79   return !IsActiveUserSessionStarted() || IsScreenLocked() ||
80       user_adding_screen_running_;
81 }
82 
SetHasActiveUser(bool has_active_user)83 void TestSessionStateDelegate::SetHasActiveUser(bool has_active_user) {
84   has_active_user_ = has_active_user;
85   if (!has_active_user)
86     active_user_session_started_ = false;
87   else
88     Shell::GetInstance()->ShowLauncher();
89 }
90 
SetActiveUserSessionStarted(bool active_user_session_started)91 void TestSessionStateDelegate::SetActiveUserSessionStarted(
92     bool active_user_session_started) {
93   active_user_session_started_ = active_user_session_started;
94   if (active_user_session_started) {
95     has_active_user_ = true;
96     Shell::GetInstance()->CreateLauncher();
97     Shell::GetInstance()->UpdateAfterLoginStatusChange(
98         user::LOGGED_IN_USER);
99   }
100 }
101 
SetCanLockScreen(bool can_lock_screen)102 void TestSessionStateDelegate::SetCanLockScreen(bool can_lock_screen) {
103   can_lock_screen_ = can_lock_screen;
104 }
105 
SetShouldLockScreenBeforeSuspending(bool should_lock)106 void TestSessionStateDelegate::SetShouldLockScreenBeforeSuspending(
107     bool should_lock) {
108   should_lock_screen_before_suspending_ = should_lock;
109 }
110 
SetUserAddingScreenRunning(bool user_adding_screen_running)111 void TestSessionStateDelegate::SetUserAddingScreenRunning(
112     bool user_adding_screen_running) {
113   user_adding_screen_running_ = user_adding_screen_running;
114 }
115 
GetUserDisplayName(MultiProfileIndex index) const116 const base::string16 TestSessionStateDelegate::GetUserDisplayName(
117     MultiProfileIndex index) const {
118   return UTF8ToUTF16("Über tray Über tray Über tray Über tray");
119 }
120 
GetUserEmail(MultiProfileIndex index) const121 const std::string TestSessionStateDelegate::GetUserEmail(
122     MultiProfileIndex index) const {
123   switch (index) {
124     case 0: return "First@tray";  // This is intended to be capitalized.
125     case 1: return "Second@tray";  // This is intended to be capitalized.
126     case 2: return "third@tray";
127     default: return "someone@tray";
128   }
129 }
130 
GetUserID(MultiProfileIndex index) const131 const std::string TestSessionStateDelegate::GetUserID(
132     MultiProfileIndex index) const {
133   return GetUserIDFromEmail(GetUserEmail(index));
134 }
135 
GetUserImage(MultiProfileIndex index) const136 const gfx::ImageSkia& TestSessionStateDelegate::GetUserImage(
137     MultiProfileIndex index) const {
138   return null_image_;
139 }
140 
GetLoggedInUsers(UserIdList * users)141 void TestSessionStateDelegate::GetLoggedInUsers(UserIdList* users) {
142 }
143 
SwitchActiveUser(const std::string & user_id)144 void TestSessionStateDelegate::SwitchActiveUser(const std::string& user_id) {
145   // Make sure this is a user id and not an email address.
146   EXPECT_EQ(user_id, GetUserIDFromEmail(user_id));
147   activated_user_ = user_id;
148 }
149 
CycleActiveUser(CycleUser cycle_user)150 void TestSessionStateDelegate::CycleActiveUser(CycleUser cycle_user) {
151   activated_user_ = "someone@tray";
152 }
153 
AddSessionStateObserver(SessionStateObserver * observer)154 void TestSessionStateDelegate::AddSessionStateObserver(
155     SessionStateObserver* observer) {
156 }
157 
RemoveSessionStateObserver(SessionStateObserver * observer)158 void TestSessionStateDelegate::RemoveSessionStateObserver(
159     SessionStateObserver* observer) {
160 }
161 
TransferWindowToDesktopOfUser(aura::Window * window,ash::MultiProfileIndex index)162 bool TestSessionStateDelegate::TransferWindowToDesktopOfUser(
163     aura::Window* window,
164     ash::MultiProfileIndex index) {
165   num_transfer_to_desktop_of_user_calls_++;
166   return false;
167 }
168 
169 }  // namespace test
170 }  // namespace ash
171