1 // Copyright 2014 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/session/session_state_delegate.h" 6 #include "ash/shell.h" 7 #include "ash/system/system_notifier.h" 8 #include "ash/test/ash_test_base.h" 9 #include "ash/test/test_session_state_delegate.h" 10 #include "ash/test/test_shell_delegate.h" 11 #include "chrome/browser/ui/ash/multi_user/multi_user_notification_blocker_chromeos.h" 12 #include "chrome/browser/ui/ash/multi_user/multi_user_window_manager_chromeos.h" 13 #include "chrome/test/base/testing_browser_process.h" 14 #include "chrome/test/base/testing_profile_manager.h" 15 #include "components/user_manager/user_info.h" 16 #include "ui/message_center/message_center.h" 17 #include "ui/message_center/notification.h" 18 19 class MultiUserNotificationBlockerChromeOSTest 20 : public ash::test::AshTestBase, 21 public message_center::NotificationBlocker::Observer { 22 public: 23 MultiUserNotificationBlockerChromeOSTest() 24 : state_changed_count_(0), 25 testing_profile_manager_(TestingBrowserProcess::GetGlobal()), 26 window_id_(0) {} 27 virtual ~MultiUserNotificationBlockerChromeOSTest() {} 28 29 // ash::test::AshTestBase overrides: 30 virtual void SetUp() OVERRIDE { 31 ash::test::AshTestBase::SetUp(); 32 ASSERT_TRUE(testing_profile_manager_.SetUp()); 33 34 // MultiUserWindowManager is initialized after the log in. 35 testing_profile_manager_.CreateTestingProfile(GetDefaultUserId()); 36 37 ash::test::TestShellDelegate* shell_delegate = 38 static_cast<ash::test::TestShellDelegate*>( 39 ash::Shell::GetInstance()->delegate()); 40 shell_delegate->set_multi_profiles_enabled(true); 41 chrome::MultiUserWindowManager::CreateInstance(); 42 43 ash::test::TestSessionStateDelegate* session_state_delegate = 44 static_cast<ash::test::TestSessionStateDelegate*>( 45 ash::Shell::GetInstance()->session_state_delegate()); 46 session_state_delegate->AddUser("test2 (at) example.com"); 47 48 // Disable any animations for the test. 49 GetMultiUserWindowManager()->SetAnimationSpeedForTest( 50 chrome::MultiUserWindowManagerChromeOS::ANIMATION_SPEED_DISABLED); 51 GetMultiUserWindowManager()->notification_blocker_->AddObserver(this); 52 } 53 54 virtual void TearDown() OVERRIDE { 55 GetMultiUserWindowManager()->notification_blocker_->RemoveObserver(this); 56 if (chrome::MultiUserWindowManager::GetInstance()) 57 chrome::MultiUserWindowManager::DeleteInstance(); 58 ash::test::AshTestBase::TearDown(); 59 } 60 61 // message_center::NotificationBlocker::Observer ovverrides: 62 virtual void OnBlockingStateChanged( 63 message_center::NotificationBlocker* blocker) OVERRIDE { 64 state_changed_count_++; 65 } 66 67 protected: 68 chrome::MultiUserWindowManagerChromeOS* GetMultiUserWindowManager() { 69 return static_cast<chrome::MultiUserWindowManagerChromeOS*>( 70 chrome::MultiUserWindowManager::GetInstance()); 71 } 72 73 const std::string GetDefaultUserId() { 74 return ash::Shell::GetInstance() 75 ->session_state_delegate() 76 ->GetUserInfo(0) 77 ->GetUserID(); 78 } 79 80 const message_center::NotificationBlocker* blocker() { 81 return GetMultiUserWindowManager()->notification_blocker_.get(); 82 } 83 84 void CreateProfile(const std::string& name) { 85 testing_profile_manager_.CreateTestingProfile(name); 86 } 87 88 void SwitchActiveUser(const std::string& name) { 89 ash::Shell::GetInstance()->session_state_delegate()->SwitchActiveUser(name); 90 if (chrome::MultiUserWindowManager::GetMultiProfileMode() == 91 chrome::MultiUserWindowManager::MULTI_PROFILE_MODE_SEPARATED) { 92 static_cast<chrome::MultiUserWindowManagerChromeOS*>( 93 chrome::MultiUserWindowManager::GetInstance())->ActiveUserChanged( 94 name); 95 } 96 } 97 98 int GetStateChangedCountAndReset() { 99 int result = state_changed_count_; 100 state_changed_count_ = 0; 101 return result; 102 } 103 104 bool ShouldShowNotificationAsPopup( 105 const message_center::NotifierId& notifier_id, 106 const std::string profile_id) { 107 message_center::NotifierId id_with_profile = notifier_id; 108 id_with_profile.profile_id = profile_id; 109 return blocker()->ShouldShowNotificationAsPopup(id_with_profile); 110 } 111 112 bool ShouldShowNotification( 113 const message_center::NotifierId& notifier_id, 114 const std::string profile_id) { 115 message_center::NotifierId id_with_profile = notifier_id; 116 id_with_profile.profile_id = profile_id; 117 return blocker()->ShouldShowNotification(id_with_profile); 118 } 119 120 aura::Window* CreateWindowForProfile(const std::string& name) { 121 aura::Window* window = CreateTestWindowInShellWithId(window_id_++); 122 chrome::MultiUserWindowManager::GetInstance()->SetWindowOwner(window, name); 123 return window; 124 } 125 126 private: 127 int state_changed_count_; 128 TestingProfileManager testing_profile_manager_; 129 int window_id_; 130 131 DISALLOW_COPY_AND_ASSIGN(MultiUserNotificationBlockerChromeOSTest); 132 }; 133 134 TEST_F(MultiUserNotificationBlockerChromeOSTest, Basic) { 135 ASSERT_EQ(chrome::MultiUserWindowManager::MULTI_PROFILE_MODE_SEPARATED, 136 chrome::MultiUserWindowManager::GetMultiProfileMode()); 137 138 message_center::NotifierId notifier_id( 139 message_center::NotifierId::APPLICATION, "test-app"); 140 // Only allowed the system notifier. 141 message_center::NotifierId ash_system_notifier( 142 message_center::NotifierId::SYSTEM_COMPONENT, 143 ash::system_notifier::kNotifierDisplay); 144 // Other system notifiers should be treated as same as a normal notifier. 145 message_center::NotifierId random_system_notifier( 146 message_center::NotifierId::SYSTEM_COMPONENT, "random_system_component"); 147 148 EXPECT_FALSE(ShouldShowNotificationAsPopup(notifier_id, "")); 149 EXPECT_TRUE(ShouldShowNotificationAsPopup(ash_system_notifier, "")); 150 EXPECT_FALSE(ShouldShowNotificationAsPopup(random_system_notifier, "")); 151 EXPECT_TRUE(ShouldShowNotificationAsPopup(notifier_id, GetDefaultUserId())); 152 EXPECT_FALSE(ShouldShowNotification(notifier_id, "")); 153 EXPECT_TRUE(ShouldShowNotification(ash_system_notifier, "")); 154 EXPECT_FALSE(ShouldShowNotification(random_system_notifier, "")); 155 EXPECT_TRUE(ShouldShowNotification(notifier_id, GetDefaultUserId())); 156 EXPECT_TRUE(ShouldShowNotification(random_system_notifier, 157 GetDefaultUserId())); 158 159 CreateProfile("test2 (at) example.com"); 160 EXPECT_EQ(0, GetStateChangedCountAndReset()); 161 EXPECT_FALSE(ShouldShowNotificationAsPopup(notifier_id, "")); 162 EXPECT_TRUE(ShouldShowNotificationAsPopup(ash_system_notifier, "")); 163 EXPECT_FALSE(ShouldShowNotificationAsPopup(random_system_notifier, "")); 164 EXPECT_TRUE(ShouldShowNotificationAsPopup(notifier_id, GetDefaultUserId())); 165 EXPECT_FALSE(ShouldShowNotificationAsPopup(notifier_id, "test2 (at) example.com")); 166 EXPECT_TRUE(ShouldShowNotificationAsPopup(random_system_notifier, 167 GetDefaultUserId())); 168 EXPECT_FALSE(ShouldShowNotificationAsPopup(random_system_notifier, 169 "test2 (at) example.com")); 170 EXPECT_FALSE(ShouldShowNotification(notifier_id, "")); 171 EXPECT_TRUE(ShouldShowNotification(ash_system_notifier, "")); 172 EXPECT_FALSE(ShouldShowNotification(random_system_notifier, "")); 173 EXPECT_TRUE(ShouldShowNotification(notifier_id, GetDefaultUserId())); 174 EXPECT_FALSE(ShouldShowNotification(notifier_id, "test2 (at) example.com")); 175 EXPECT_TRUE(ShouldShowNotification(random_system_notifier, 176 GetDefaultUserId())); 177 EXPECT_FALSE(ShouldShowNotification(random_system_notifier, 178 "test2 (at) example.com")); 179 180 SwitchActiveUser("test2 (at) example.com"); 181 EXPECT_FALSE(ShouldShowNotificationAsPopup(notifier_id, "")); 182 EXPECT_TRUE(ShouldShowNotificationAsPopup(ash_system_notifier, "")); 183 EXPECT_FALSE(ShouldShowNotificationAsPopup(random_system_notifier, "")); 184 EXPECT_FALSE(ShouldShowNotificationAsPopup(notifier_id, GetDefaultUserId())); 185 EXPECT_TRUE(ShouldShowNotificationAsPopup(notifier_id, "test2 (at) example.com")); 186 EXPECT_FALSE(ShouldShowNotificationAsPopup(random_system_notifier, 187 GetDefaultUserId())); 188 EXPECT_TRUE(ShouldShowNotificationAsPopup(random_system_notifier, 189 "test2 (at) example.com")); 190 EXPECT_FALSE(ShouldShowNotification(notifier_id, "")); 191 EXPECT_TRUE(ShouldShowNotification(ash_system_notifier, "")); 192 EXPECT_FALSE(ShouldShowNotification(random_system_notifier, "")); 193 EXPECT_FALSE(ShouldShowNotification(notifier_id, GetDefaultUserId())); 194 EXPECT_TRUE(ShouldShowNotification(notifier_id, "test2 (at) example.com")); 195 EXPECT_FALSE(ShouldShowNotification(random_system_notifier, 196 GetDefaultUserId())); 197 EXPECT_TRUE(ShouldShowNotification(random_system_notifier, 198 "test2 (at) example.com")); 199 200 SwitchActiveUser(GetDefaultUserId()); 201 EXPECT_FALSE(ShouldShowNotificationAsPopup(notifier_id, "")); 202 EXPECT_TRUE(ShouldShowNotificationAsPopup(ash_system_notifier, "")); 203 EXPECT_FALSE(ShouldShowNotificationAsPopup(random_system_notifier, "")); 204 EXPECT_TRUE(ShouldShowNotificationAsPopup(notifier_id, GetDefaultUserId())); 205 EXPECT_FALSE(ShouldShowNotificationAsPopup(notifier_id, "test2 (at) example.com")); 206 EXPECT_TRUE(ShouldShowNotificationAsPopup(random_system_notifier, 207 GetDefaultUserId())); 208 EXPECT_FALSE(ShouldShowNotificationAsPopup(random_system_notifier, 209 "test2 (at) example.com")); 210 EXPECT_FALSE(ShouldShowNotification(notifier_id, "")); 211 EXPECT_TRUE(ShouldShowNotification(ash_system_notifier, "")); 212 EXPECT_FALSE(ShouldShowNotification(random_system_notifier, "")); 213 EXPECT_TRUE(ShouldShowNotification(notifier_id, GetDefaultUserId())); 214 EXPECT_FALSE(ShouldShowNotification(notifier_id, "test2 (at) example.com")); 215 EXPECT_TRUE(ShouldShowNotification(random_system_notifier, 216 GetDefaultUserId())); 217 EXPECT_FALSE(ShouldShowNotification(random_system_notifier, 218 "test2 (at) example.com")); 219 } 220