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_state_delegate.h"
7 #include "ash/system/system_notifier.h"
8 #include "ash/test/ash_test_base.h"
9 #include "ash/test/test_shell_delegate.h"
10 #include "ash/wm/window_state.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 "ui/message_center/message_center.h"
16 #include "ui/message_center/notification.h"
18 class MultiUserNotificationBlockerChromeOSTest
19 : public ash::test::AshTestBase,
20 public message_center::NotificationBlocker::Observer {
22 MultiUserNotificationBlockerChromeOSTest()
23 : state_changed_count_(0),
24 testing_profile_manager_(TestingBrowserProcess::GetGlobal()),
26 virtual ~MultiUserNotificationBlockerChromeOSTest() {}
28 // ash::test::AshTestBase overrides:
29 virtual void SetUp() OVERRIDE {
30 ash::test::AshTestBase::SetUp();
31 ASSERT_TRUE(testing_profile_manager_.SetUp());
33 // MultiUserWindowManager is initialized after the log in.
34 testing_profile_manager_.CreateTestingProfile(GetDefaultUserId());
36 ash::test::TestShellDelegate* shell_delegate =
37 static_cast<ash::test::TestShellDelegate*>(
38 ash::Shell::GetInstance()->delegate());
39 shell_delegate->set_multi_profiles_enabled(true);
40 chrome::MultiUserWindowManager::CreateInstance();
42 // Disable any animations for the test.
43 GetMultiUserWindowManager()->SetAnimationsForTest(true);
44 GetMultiUserWindowManager()->notification_blocker_->AddObserver(this);
47 virtual void TearDown() OVERRIDE {
48 GetMultiUserWindowManager()->notification_blocker_->RemoveObserver(this);
49 if (chrome::MultiUserWindowManager::GetInstance())
50 chrome::MultiUserWindowManager::DeleteInstance();
51 ash::test::AshTestBase::TearDown();
54 // message_center::NotificationBlocker::Observer ovverrides:
55 virtual void OnBlockingStateChanged(
56 message_center::NotificationBlocker* blocker) OVERRIDE {
57 state_changed_count_++;
61 chrome::MultiUserWindowManagerChromeOS* GetMultiUserWindowManager() {
62 return static_cast<chrome::MultiUserWindowManagerChromeOS*>(
63 chrome::MultiUserWindowManager::GetInstance());
66 const std::string GetDefaultUserId() {
67 return ash::Shell::GetInstance()->session_state_delegate()->GetUserID(0);
70 const message_center::NotificationBlocker* blocker() {
71 return GetMultiUserWindowManager()->notification_blocker_.get();
74 void CreateProfile(const std::string& name) {
75 testing_profile_manager_.CreateTestingProfile(name);
78 void SwitchActiveUser(const std::string& name) {
79 ash::Shell::GetInstance()->session_state_delegate()->SwitchActiveUser(name);
80 if (chrome::MultiUserWindowManager::GetMultiProfileMode() ==
81 chrome::MultiUserWindowManager::MULTI_PROFILE_MODE_SEPARATED) {
82 static_cast<chrome::MultiUserWindowManagerChromeOS*>(
83 chrome::MultiUserWindowManager::GetInstance())->ActiveUserChanged(
88 int GetStateChangedCountAndReset() {
89 int result = state_changed_count_;
90 state_changed_count_ = 0;
94 bool ShouldShowNotificationAsPopup(
95 const message_center::NotifierId& notifier_id,
96 const std::string profile_id) {
97 message_center::NotifierId id_with_profile = notifier_id;
98 id_with_profile.profile_id = profile_id;
99 return blocker()->ShouldShowNotificationAsPopup(id_with_profile);
102 bool ShouldShowNotification(
103 const message_center::NotifierId& notifier_id,
104 const std::string profile_id) {
105 message_center::NotifierId id_with_profile = notifier_id;
106 id_with_profile.profile_id = profile_id;
107 return blocker()->ShouldShowNotification(id_with_profile);
110 aura::Window* CreateWindowForProfile(const std::string& name) {
111 aura::Window* window = CreateTestWindowInShellWithId(window_id_++);
112 chrome::MultiUserWindowManager::GetInstance()->SetWindowOwner(window, name);
117 int state_changed_count_;
118 TestingProfileManager testing_profile_manager_;
121 DISALLOW_COPY_AND_ASSIGN(MultiUserNotificationBlockerChromeOSTest);
124 TEST_F(MultiUserNotificationBlockerChromeOSTest, Basic) {
125 ASSERT_EQ(chrome::MultiUserWindowManager::MULTI_PROFILE_MODE_SEPARATED,
126 chrome::MultiUserWindowManager::GetMultiProfileMode());
128 message_center::NotifierId notifier_id(
129 message_center::NotifierId::APPLICATION, "test-app");
130 // Only allowed the system notifier.
131 message_center::NotifierId ash_system_notifier(
132 message_center::NotifierId::SYSTEM_COMPONENT,
133 ash::system_notifier::kNotifierDisplay);
134 // Other system notifiers should be treated as same as a normal notifier.
135 message_center::NotifierId random_system_notifier(
136 message_center::NotifierId::SYSTEM_COMPONENT, "random_system_component");
138 EXPECT_FALSE(ShouldShowNotificationAsPopup(notifier_id, ""));
139 EXPECT_TRUE(ShouldShowNotificationAsPopup(ash_system_notifier, ""));
140 EXPECT_FALSE(ShouldShowNotificationAsPopup(random_system_notifier, ""));
141 EXPECT_TRUE(ShouldShowNotificationAsPopup(notifier_id, GetDefaultUserId()));
142 EXPECT_FALSE(ShouldShowNotification(notifier_id, ""));
143 EXPECT_TRUE(ShouldShowNotification(ash_system_notifier, ""));
144 EXPECT_FALSE(ShouldShowNotification(random_system_notifier, ""));
145 EXPECT_TRUE(ShouldShowNotification(notifier_id, GetDefaultUserId()));
146 EXPECT_TRUE(ShouldShowNotification(random_system_notifier,
147 GetDefaultUserId()));
149 CreateProfile("test2@example.com");
150 EXPECT_EQ(0, GetStateChangedCountAndReset());
151 EXPECT_FALSE(ShouldShowNotificationAsPopup(notifier_id, ""));
152 EXPECT_TRUE(ShouldShowNotificationAsPopup(ash_system_notifier, ""));
153 EXPECT_FALSE(ShouldShowNotificationAsPopup(random_system_notifier, ""));
154 EXPECT_TRUE(ShouldShowNotificationAsPopup(notifier_id, GetDefaultUserId()));
155 EXPECT_FALSE(ShouldShowNotificationAsPopup(notifier_id, "test2@example.com"));
156 EXPECT_TRUE(ShouldShowNotificationAsPopup(random_system_notifier,
157 GetDefaultUserId()));
158 EXPECT_FALSE(ShouldShowNotificationAsPopup(random_system_notifier,
159 "test2@example.com"));
160 EXPECT_FALSE(ShouldShowNotification(notifier_id, ""));
161 EXPECT_TRUE(ShouldShowNotification(ash_system_notifier, ""));
162 EXPECT_FALSE(ShouldShowNotification(random_system_notifier, ""));
163 EXPECT_TRUE(ShouldShowNotification(notifier_id, GetDefaultUserId()));
164 EXPECT_FALSE(ShouldShowNotification(notifier_id, "test2@example.com"));
165 EXPECT_TRUE(ShouldShowNotification(random_system_notifier,
166 GetDefaultUserId()));
167 EXPECT_FALSE(ShouldShowNotification(random_system_notifier,
168 "test2@example.com"));
170 SwitchActiveUser("test2@example.com");
171 EXPECT_FALSE(ShouldShowNotificationAsPopup(notifier_id, ""));
172 EXPECT_TRUE(ShouldShowNotificationAsPopup(ash_system_notifier, ""));
173 EXPECT_FALSE(ShouldShowNotificationAsPopup(random_system_notifier, ""));
174 EXPECT_FALSE(ShouldShowNotificationAsPopup(notifier_id, GetDefaultUserId()));
175 EXPECT_TRUE(ShouldShowNotificationAsPopup(notifier_id, "test2@example.com"));
176 EXPECT_FALSE(ShouldShowNotificationAsPopup(random_system_notifier,
177 GetDefaultUserId()));
178 EXPECT_TRUE(ShouldShowNotificationAsPopup(random_system_notifier,
179 "test2@example.com"));
180 EXPECT_FALSE(ShouldShowNotification(notifier_id, ""));
181 EXPECT_TRUE(ShouldShowNotification(ash_system_notifier, ""));
182 EXPECT_FALSE(ShouldShowNotification(random_system_notifier, ""));
183 EXPECT_FALSE(ShouldShowNotification(notifier_id, GetDefaultUserId()));
184 EXPECT_TRUE(ShouldShowNotification(notifier_id, "test2@example.com"));
185 EXPECT_FALSE(ShouldShowNotification(random_system_notifier,
186 GetDefaultUserId()));
187 EXPECT_TRUE(ShouldShowNotification(random_system_notifier,
188 "test2@example.com"));
190 SwitchActiveUser(GetDefaultUserId());
191 EXPECT_FALSE(ShouldShowNotificationAsPopup(notifier_id, ""));
192 EXPECT_TRUE(ShouldShowNotificationAsPopup(ash_system_notifier, ""));
193 EXPECT_FALSE(ShouldShowNotificationAsPopup(random_system_notifier, ""));
194 EXPECT_TRUE(ShouldShowNotificationAsPopup(notifier_id, GetDefaultUserId()));
195 EXPECT_FALSE(ShouldShowNotificationAsPopup(notifier_id, "test2@example.com"));
196 EXPECT_TRUE(ShouldShowNotificationAsPopup(random_system_notifier,
197 GetDefaultUserId()));
198 EXPECT_FALSE(ShouldShowNotificationAsPopup(random_system_notifier,
199 "test2@example.com"));
200 EXPECT_FALSE(ShouldShowNotification(notifier_id, ""));
201 EXPECT_TRUE(ShouldShowNotification(ash_system_notifier, ""));
202 EXPECT_FALSE(ShouldShowNotification(random_system_notifier, ""));
203 EXPECT_TRUE(ShouldShowNotification(notifier_id, GetDefaultUserId()));
204 EXPECT_FALSE(ShouldShowNotification(notifier_id, "test2@example.com"));
205 EXPECT_TRUE(ShouldShowNotification(random_system_notifier,
206 GetDefaultUserId()));
207 EXPECT_FALSE(ShouldShowNotification(random_system_notifier,
208 "test2@example.com"));
211 TEST_F(MultiUserNotificationBlockerChromeOSTest, TeleportedWindows) {
212 ASSERT_EQ(chrome::MultiUserWindowManager::MULTI_PROFILE_MODE_SEPARATED,
213 chrome::MultiUserWindowManager::GetMultiProfileMode());
215 std::string u1 = GetDefaultUserId();
216 ash::SessionStateDelegate* delegate =
217 ash::Shell::GetInstance()->session_state_delegate();
218 std::string u2 = delegate->GetUserID(1);
219 std::string u3 = delegate->GetUserID(2);
223 chrome::MultiUserWindowManager* multi_user_window_manager =
224 chrome::MultiUserWindowManager::GetInstance();
226 message_center::NotifierId notifier_id(
227 message_center::NotifierId::APPLICATION, "test-app");
229 // Initial status: only notifications for u1 should be shown.
230 EXPECT_EQ(0, GetStateChangedCountAndReset());
231 EXPECT_TRUE(ShouldShowNotificationAsPopup(notifier_id, u1));
232 EXPECT_FALSE(ShouldShowNotificationAsPopup(notifier_id, u2));
233 EXPECT_FALSE(ShouldShowNotificationAsPopup(notifier_id, u3));
235 // Create a new window in u2.
236 SwitchActiveUser(u2);
237 scoped_ptr<aura::Window> w2(CreateWindowForProfile(u2));
238 EXPECT_EQ(2, GetStateChangedCountAndReset());
239 EXPECT_FALSE(ShouldShowNotificationAsPopup(notifier_id, u1));
240 EXPECT_TRUE(ShouldShowNotificationAsPopup(notifier_id, u2));
241 EXPECT_FALSE(ShouldShowNotificationAsPopup(notifier_id, u3));
243 // Moves w2 to u1 desktop.
244 multi_user_window_manager->ShowWindowForUser(w2.get(), u1);
245 EXPECT_EQ(1, GetStateChangedCountAndReset());
246 EXPECT_FALSE(ShouldShowNotificationAsPopup(notifier_id, u1));
247 EXPECT_TRUE(ShouldShowNotificationAsPopup(notifier_id, u2));
248 EXPECT_FALSE(ShouldShowNotificationAsPopup(notifier_id, u3));
250 // Switch back to u1 desktop. Notification for u2 should be shown as a popup
251 // because w2 is visiting to u1.
252 SwitchActiveUser(u1);
253 EXPECT_EQ(2, GetStateChangedCountAndReset());
254 EXPECT_TRUE(ShouldShowNotificationAsPopup(notifier_id, u1));
255 EXPECT_TRUE(ShouldShowNotificationAsPopup(notifier_id, u2));
256 EXPECT_FALSE(ShouldShowNotificationAsPopup(notifier_id, u3));
258 // Notifications for u2 is not shown in the center.
259 EXPECT_TRUE(ShouldShowNotification(notifier_id, u1));
260 EXPECT_FALSE(ShouldShowNotification(notifier_id, u2));
261 EXPECT_FALSE(ShouldShowNotification(notifier_id, u3));
264 multi_user_window_manager->ShowWindowForUser(w2.get(), u2);
265 EXPECT_EQ(1, GetStateChangedCountAndReset());
266 EXPECT_TRUE(ShouldShowNotificationAsPopup(notifier_id, u1));
267 EXPECT_FALSE(ShouldShowNotificationAsPopup(notifier_id, u2));
268 EXPECT_FALSE(ShouldShowNotificationAsPopup(notifier_id, u3));
270 // Close/remove the visiting window.
271 scoped_ptr<aura::Window> w22(CreateWindowForProfile(u2));
272 multi_user_window_manager->ShowWindowForUser(w22.get(), u1);
273 EXPECT_EQ(1, GetStateChangedCountAndReset());
274 EXPECT_TRUE(ShouldShowNotificationAsPopup(notifier_id, u1));
275 EXPECT_TRUE(ShouldShowNotificationAsPopup(notifier_id, u2));
276 EXPECT_FALSE(ShouldShowNotificationAsPopup(notifier_id, u3));
279 EXPECT_EQ(1, GetStateChangedCountAndReset());
280 EXPECT_TRUE(ShouldShowNotificationAsPopup(notifier_id, u1));
281 EXPECT_FALSE(ShouldShowNotificationAsPopup(notifier_id, u2));
282 EXPECT_FALSE(ShouldShowNotificationAsPopup(notifier_id, u3));
284 // Minimize the visiting window.
285 scoped_ptr<aura::Window> w23(CreateWindowForProfile(u2));
286 multi_user_window_manager->ShowWindowForUser(w23.get(), u1);
287 EXPECT_EQ(1, GetStateChangedCountAndReset());
289 ash::wm::GetWindowState(w23.get())->Minimize();
290 EXPECT_EQ(u1, multi_user_window_manager->GetUserPresentingWindow(w23.get()));
291 EXPECT_EQ(0, GetStateChangedCountAndReset());
292 EXPECT_TRUE(ShouldShowNotificationAsPopup(notifier_id, u1));
293 EXPECT_TRUE(ShouldShowNotificationAsPopup(notifier_id, u2));
294 EXPECT_FALSE(ShouldShowNotificationAsPopup(notifier_id, u3));