multi_user_notification_blocker_chromeos_unittest.cc revision 0529e5d033099cbfc42635f6f6183833b09dff6e
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_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_shell_delegate.h"
10#include "chrome/browser/ui/ash/multi_user/multi_user_notification_blocker_chromeos.h"
11#include "chrome/browser/ui/ash/multi_user/multi_user_window_manager_chromeos.h"
12#include "chrome/test/base/testing_browser_process.h"
13#include "chrome/test/base/testing_profile_manager.h"
14#include "ui/message_center/message_center.h"
15#include "ui/message_center/notification.h"
16
17class MultiUserNotificationBlockerChromeOSTest
18    : public ash::test::AshTestBase,
19      public message_center::NotificationBlocker::Observer {
20 public:
21  MultiUserNotificationBlockerChromeOSTest()
22      : state_changed_count_(0),
23        testing_profile_manager_(TestingBrowserProcess::GetGlobal()),
24        window_id_(0) {}
25  virtual ~MultiUserNotificationBlockerChromeOSTest() {}
26
27  // ash::test::AshTestBase overrides:
28  virtual void SetUp() OVERRIDE {
29    ash::test::AshTestBase::SetUp();
30    ASSERT_TRUE(testing_profile_manager_.SetUp());
31
32    // MultiUserWindowManager is initialized after the log in.
33    testing_profile_manager_.CreateTestingProfile(GetDefaultUserId());
34
35    ash::test::TestShellDelegate* shell_delegate =
36        static_cast<ash::test::TestShellDelegate*>(
37            ash::Shell::GetInstance()->delegate());
38    shell_delegate->set_multi_profiles_enabled(true);
39    chrome::MultiUserWindowManager::CreateInstance();
40
41    // Disable any animations for the test.
42    GetMultiUserWindowManager()->SetAnimationSpeedForTest(
43        chrome::MultiUserWindowManagerChromeOS::ANIMATION_SPEED_DISABLED);
44    GetMultiUserWindowManager()->notification_blocker_->AddObserver(this);
45  }
46
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();
52  }
53
54  // message_center::NotificationBlocker::Observer ovverrides:
55  virtual void OnBlockingStateChanged(
56      message_center::NotificationBlocker* blocker) OVERRIDE {
57    state_changed_count_++;
58  }
59
60 protected:
61  chrome::MultiUserWindowManagerChromeOS* GetMultiUserWindowManager() {
62    return static_cast<chrome::MultiUserWindowManagerChromeOS*>(
63        chrome::MultiUserWindowManager::GetInstance());
64  }
65
66  const std::string GetDefaultUserId() {
67    return ash::Shell::GetInstance()->session_state_delegate()->GetUserID(0);
68  }
69
70  const message_center::NotificationBlocker* blocker() {
71    return GetMultiUserWindowManager()->notification_blocker_.get();
72  }
73
74  void CreateProfile(const std::string& name) {
75    testing_profile_manager_.CreateTestingProfile(name);
76  }
77
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(
84              name);
85    }
86  }
87
88  int GetStateChangedCountAndReset() {
89    int result = state_changed_count_;
90    state_changed_count_ = 0;
91    return result;
92  }
93
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);
100  }
101
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);
108  }
109
110  aura::Window* CreateWindowForProfile(const std::string& name) {
111    aura::Window* window = CreateTestWindowInShellWithId(window_id_++);
112    chrome::MultiUserWindowManager::GetInstance()->SetWindowOwner(window, name);
113    return window;
114  }
115
116 private:
117  int state_changed_count_;
118  TestingProfileManager testing_profile_manager_;
119  int window_id_;
120
121  DISALLOW_COPY_AND_ASSIGN(MultiUserNotificationBlockerChromeOSTest);
122};
123
124TEST_F(MultiUserNotificationBlockerChromeOSTest, Basic) {
125  ASSERT_EQ(chrome::MultiUserWindowManager::MULTI_PROFILE_MODE_SEPARATED,
126            chrome::MultiUserWindowManager::GetMultiProfileMode());
127
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");
137
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()));
148
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"));
169
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"));
189
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"));
209}
210