Home | History | Annotate | Download | only in test
      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/stl_util.h"
     13 #include "base/strings/string16.h"
     14 #include "base/strings/utf_string_conversions.h"
     15 #include "components/user_manager/user_info.h"
     16 #include "testing/gtest/include/gtest/gtest.h"
     17 
     18 namespace ash {
     19 namespace test {
     20 
     21 namespace {
     22 
     23 // The the "canonicalized" user ID from a given |email| address.
     24 std::string GetUserIDFromEmail(const std::string& email) {
     25   std::string user_id = email;
     26   std::transform(user_id.begin(), user_id.end(), user_id.begin(), ::tolower);
     27   return user_id;
     28 }
     29 
     30 }  // namespace
     31 
     32 class MockUserInfo : public user_manager::UserInfo {
     33  public:
     34   explicit MockUserInfo(const std::string& id) : email_(id) {}
     35   virtual ~MockUserInfo() {}
     36 
     37   void SetUserImage(const gfx::ImageSkia& user_image) {
     38     user_image_ = user_image;
     39   }
     40 
     41   virtual base::string16 GetDisplayName() const OVERRIDE {
     42     return base::UTF8ToUTF16("ber tray ber tray ber tray ber tray");
     43   }
     44 
     45   virtual base::string16 GetGivenName() const OVERRIDE {
     46     return base::UTF8ToUTF16("ber ber ber ber");
     47   }
     48 
     49   virtual std::string GetEmail() const OVERRIDE { return email_; }
     50 
     51   virtual std::string GetUserID() const OVERRIDE {
     52     return GetUserIDFromEmail(GetEmail());
     53   }
     54 
     55   virtual const gfx::ImageSkia& GetImage() const OVERRIDE {
     56     return user_image_;
     57   }
     58 
     59   // A test user image.
     60   gfx::ImageSkia user_image_;
     61 
     62   std::string email_;
     63 
     64   DISALLOW_COPY_AND_ASSIGN(MockUserInfo);
     65 };
     66 
     67 TestSessionStateDelegate::TestSessionStateDelegate()
     68     : has_active_user_(false),
     69       active_user_session_started_(false),
     70       can_lock_screen_(true),
     71       should_lock_screen_before_suspending_(false),
     72       screen_locked_(false),
     73       user_adding_screen_running_(false),
     74       logged_in_users_(1),
     75       active_user_index_(0) {
     76   user_list_.push_back(
     77       new MockUserInfo("First@tray"));  // This is intended to be capitalized.
     78   user_list_.push_back(
     79       new MockUserInfo("Second@tray"));  // This is intended to be capitalized.
     80   user_list_.push_back(new MockUserInfo("third@tray"));
     81   user_list_.push_back(new MockUserInfo("someone@tray"));
     82 }
     83 
     84 TestSessionStateDelegate::~TestSessionStateDelegate() {
     85   STLDeleteElements(&user_list_);
     86 }
     87 
     88 void TestSessionStateDelegate::AddUser(const std::string user_id) {
     89   user_list_.push_back(new MockUserInfo(user_id));
     90 }
     91 
     92 const user_manager::UserInfo* TestSessionStateDelegate::GetActiveUserInfo()
     93     const {
     94   return user_list_[active_user_index_];
     95 }
     96 
     97 content::BrowserContext*
     98 TestSessionStateDelegate::GetBrowserContextByIndex(
     99     MultiProfileIndex index) {
    100   return NULL;
    101 }
    102 
    103 content::BrowserContext*
    104 TestSessionStateDelegate::GetBrowserContextForWindow(
    105     aura::Window* window) {
    106   return NULL;
    107 }
    108 
    109 int TestSessionStateDelegate::GetMaximumNumberOfLoggedInUsers() const {
    110   return 3;
    111 }
    112 
    113 int TestSessionStateDelegate::NumberOfLoggedInUsers() const {
    114   // TODO(skuhne): Add better test framework to test multiple profiles.
    115   return has_active_user_ ? logged_in_users_ : 0;
    116 }
    117 
    118 bool TestSessionStateDelegate::IsActiveUserSessionStarted() const {
    119   return active_user_session_started_;
    120 }
    121 
    122 bool TestSessionStateDelegate::CanLockScreen() const {
    123   return has_active_user_ && can_lock_screen_;
    124 }
    125 
    126 bool TestSessionStateDelegate::IsScreenLocked() const {
    127   return screen_locked_;
    128 }
    129 
    130 bool TestSessionStateDelegate::ShouldLockScreenBeforeSuspending() const {
    131   return should_lock_screen_before_suspending_;
    132 }
    133 
    134 void TestSessionStateDelegate::LockScreen() {
    135   if (CanLockScreen())
    136     screen_locked_ = true;
    137 }
    138 
    139 void TestSessionStateDelegate::UnlockScreen() {
    140   screen_locked_ = false;
    141 }
    142 
    143 bool TestSessionStateDelegate::IsUserSessionBlocked() const {
    144   return !IsActiveUserSessionStarted() || IsScreenLocked() ||
    145       user_adding_screen_running_;
    146 }
    147 
    148 SessionStateDelegate::SessionState TestSessionStateDelegate::GetSessionState()
    149     const {
    150   if (user_adding_screen_running_)
    151     return SESSION_STATE_LOGIN_SECONDARY;
    152 
    153   // Assuming that if session is not active we're at login.
    154   return IsActiveUserSessionStarted() ?
    155       SESSION_STATE_ACTIVE : SESSION_STATE_LOGIN_PRIMARY;
    156 }
    157 
    158 void TestSessionStateDelegate::SetHasActiveUser(bool has_active_user) {
    159   has_active_user_ = has_active_user;
    160   if (!has_active_user)
    161     active_user_session_started_ = false;
    162   else
    163     Shell::GetInstance()->ShowShelf();
    164 }
    165 
    166 void TestSessionStateDelegate::SetActiveUserSessionStarted(
    167     bool active_user_session_started) {
    168   active_user_session_started_ = active_user_session_started;
    169   if (active_user_session_started) {
    170     has_active_user_ = true;
    171     Shell::GetInstance()->CreateShelf();
    172     Shell::GetInstance()->UpdateAfterLoginStatusChange(
    173         user::LOGGED_IN_USER);
    174   }
    175 }
    176 
    177 void TestSessionStateDelegate::SetCanLockScreen(bool can_lock_screen) {
    178   can_lock_screen_ = can_lock_screen;
    179 }
    180 
    181 void TestSessionStateDelegate::SetShouldLockScreenBeforeSuspending(
    182     bool should_lock) {
    183   should_lock_screen_before_suspending_ = should_lock;
    184 }
    185 
    186 void TestSessionStateDelegate::SetUserAddingScreenRunning(
    187     bool user_adding_screen_running) {
    188   user_adding_screen_running_ = user_adding_screen_running;
    189 }
    190 
    191 void TestSessionStateDelegate::SetUserImage(
    192     const gfx::ImageSkia& user_image) {
    193   user_list_[active_user_index_]->SetUserImage(user_image);
    194 }
    195 
    196 const user_manager::UserInfo* TestSessionStateDelegate::GetUserInfo(
    197     MultiProfileIndex index) const {
    198   int max = static_cast<int>(user_list_.size());
    199   return user_list_[index < max ? index : max - 1];
    200 }
    201 
    202 const user_manager::UserInfo* TestSessionStateDelegate::GetUserInfo(
    203     content::BrowserContext* context) const {
    204   return user_list_[active_user_index_];
    205 }
    206 
    207 bool TestSessionStateDelegate::ShouldShowAvatar(aura::Window* window) const {
    208   return !GetActiveUserInfo()->GetImage().isNull();
    209 }
    210 
    211 void TestSessionStateDelegate::SwitchActiveUser(const std::string& user_id) {
    212   // Make sure this is a user id and not an email address.
    213   EXPECT_EQ(user_id, GetUserIDFromEmail(user_id));
    214   active_user_index_ = 0;
    215   for (std::vector<MockUserInfo*>::iterator iter = user_list_.begin();
    216        iter != user_list_.end();
    217        ++iter) {
    218     if ((*iter)->GetUserID() == user_id) {
    219       active_user_index_ = iter - user_list_.begin();
    220       return;
    221     }
    222   }
    223   NOTREACHED() << "Unknown user:" << user_id;
    224 }
    225 
    226 void TestSessionStateDelegate::CycleActiveUser(CycleUser cycle_user) {
    227   SwitchActiveUser("someone@tray");
    228 }
    229 
    230 bool TestSessionStateDelegate::IsMultiProfileAllowedByPrimaryUserPolicy()
    231     const {
    232   return true;
    233 }
    234 
    235 void TestSessionStateDelegate::AddSessionStateObserver(
    236     SessionStateObserver* observer) {
    237 }
    238 
    239 void TestSessionStateDelegate::RemoveSessionStateObserver(
    240     SessionStateObserver* observer) {
    241 }
    242 
    243 }  // namespace test
    244 }  // namespace ash
    245