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.
5 #include "ash/session/session_state_delegate.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"
19 class MultiUserNotificationBlockerChromeOSTest
20 : public ash::test::AshTestBase,
21 public message_center::NotificationBlocker::Observer {
23 MultiUserNotificationBlockerChromeOSTest()
24 : state_changed_count_(0),
25 testing_profile_manager_(TestingBrowserProcess::GetGlobal()),
27 virtual ~MultiUserNotificationBlockerChromeOSTest() {}
29 // ash::test::AshTestBase overrides:
30 virtual void SetUp() override {
31 ash::test::AshTestBase::SetUp();
32 ASSERT_TRUE(testing_profile_manager_.SetUp());
34 // MultiUserWindowManager is initialized after the log in.
35 testing_profile_manager_.CreateTestingProfile(GetDefaultUserId());
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();
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@example.com");
48 // Disable any animations for the test.
49 GetMultiUserWindowManager()->SetAnimationSpeedForTest(
50 chrome::MultiUserWindowManagerChromeOS::ANIMATION_SPEED_DISABLED);
51 GetMultiUserWindowManager()->notification_blocker_->AddObserver(this);
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();
61 // message_center::NotificationBlocker::Observer ovverrides:
62 virtual void OnBlockingStateChanged(
63 message_center::NotificationBlocker* blocker) override {
64 state_changed_count_++;
68 chrome::MultiUserWindowManagerChromeOS* GetMultiUserWindowManager() {
69 return static_cast<chrome::MultiUserWindowManagerChromeOS*>(
70 chrome::MultiUserWindowManager::GetInstance());
73 const std::string GetDefaultUserId() {
74 return ash::Shell::GetInstance()
75 ->session_state_delegate()
80 const message_center::NotificationBlocker* blocker() {
81 return GetMultiUserWindowManager()->notification_blocker_.get();
84 void CreateProfile(const std::string& name) {
85 testing_profile_manager_.CreateTestingProfile(name);
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(
98 int GetStateChangedCountAndReset() {
99 int result = state_changed_count_;
100 state_changed_count_ = 0;
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);
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);
120 aura::Window* CreateWindowForProfile(const std::string& name) {
121 aura::Window* window = CreateTestWindowInShellWithId(window_id_++);
122 chrome::MultiUserWindowManager::GetInstance()->SetWindowOwner(window, name);
127 int state_changed_count_;
128 TestingProfileManager testing_profile_manager_;
131 DISALLOW_COPY_AND_ASSIGN(MultiUserNotificationBlockerChromeOSTest);
134 TEST_F(MultiUserNotificationBlockerChromeOSTest, Basic) {
135 ASSERT_EQ(chrome::MultiUserWindowManager::MULTI_PROFILE_MODE_SEPARATED,
136 chrome::MultiUserWindowManager::GetMultiProfileMode());
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");
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()));
159 CreateProfile("test2@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@example.com"));
166 EXPECT_TRUE(ShouldShowNotificationAsPopup(random_system_notifier,
167 GetDefaultUserId()));
168 EXPECT_FALSE(ShouldShowNotificationAsPopup(random_system_notifier,
169 "test2@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@example.com"));
175 EXPECT_TRUE(ShouldShowNotification(random_system_notifier,
176 GetDefaultUserId()));
177 EXPECT_FALSE(ShouldShowNotification(random_system_notifier,
178 "test2@example.com"));
180 SwitchActiveUser("test2@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@example.com"));
186 EXPECT_FALSE(ShouldShowNotificationAsPopup(random_system_notifier,
187 GetDefaultUserId()));
188 EXPECT_TRUE(ShouldShowNotificationAsPopup(random_system_notifier,
189 "test2@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@example.com"));
195 EXPECT_FALSE(ShouldShowNotification(random_system_notifier,
196 GetDefaultUserId()));
197 EXPECT_TRUE(ShouldShowNotification(random_system_notifier,
198 "test2@example.com"));
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@example.com"));
206 EXPECT_TRUE(ShouldShowNotificationAsPopup(random_system_notifier,
207 GetDefaultUserId()));
208 EXPECT_FALSE(ShouldShowNotificationAsPopup(random_system_notifier,
209 "test2@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@example.com"));
215 EXPECT_TRUE(ShouldShowNotification(random_system_notifier,
216 GetDefaultUserId()));
217 EXPECT_FALSE(ShouldShowNotification(random_system_notifier,
218 "test2@example.com"));