Don't show supervised user as "already on this device" while they're being imported.
[chromium-blink-merge.git] / chrome / browser / notifications / message_center_settings_controller_unittest.cc
blob40ccfa5a0a616b6bca0acc246d37335b747de21c
1 // Copyright 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.
5 #include <string>
7 #include "base/command_line.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "chrome/browser/extensions/extension_service.h"
10 #include "chrome/browser/extensions/test_extension_system.h"
11 #include "chrome/browser/notifications/desktop_notification_profile_util.h"
12 #include "chrome/browser/notifications/message_center_settings_controller.h"
13 #include "chrome/browser/profiles/profile_info_cache.h"
14 #include "chrome/browser/profiles/profile_manager.h"
15 #include "chrome/test/base/testing_browser_process.h"
16 #include "chrome/test/base/testing_profile_manager.h"
17 #include "components/content_settings/core/browser/host_content_settings_map.h"
18 #include "content/public/test/test_browser_thread_bundle.h"
19 #include "extensions/common/extension.h"
20 #include "extensions/common/extension_builder.h"
21 #include "testing/gtest/include/gtest/gtest.h"
22 #include "ui/message_center/notifier_settings.h"
24 #if defined(OS_CHROMEOS)
25 #include "ash/system/system_notifier.h"
26 #include "chrome/browser/chromeos/login/users/fake_chrome_user_manager.h"
27 #include "chrome/browser/chromeos/login/users/scoped_user_manager_enabler.h"
28 #endif
30 class MessageCenterSettingsControllerBaseTest : public testing::Test {
31 protected:
32 MessageCenterSettingsControllerBaseTest()
33 : testing_profile_manager_(TestingBrowserProcess::GetGlobal()) {}
35 ~MessageCenterSettingsControllerBaseTest() override{};
37 base::FilePath GetProfilePath(const std::string& base_name) {
38 return testing_profile_manager_.profile_manager()->user_data_dir()
39 .AppendASCII(base_name);
42 void SetUp() override { ASSERT_TRUE(testing_profile_manager_.SetUp()); }
44 virtual TestingProfile* CreateProfile(const std::string& name) {
45 return testing_profile_manager_.CreateTestingProfile(name);
48 void CreateController() {
49 controller_.reset(new MessageCenterSettingsController(
50 testing_profile_manager_.profile_info_cache()));
53 void ResetController() {
54 controller_.reset();
57 MessageCenterSettingsController* controller() { return controller_.get(); }
59 private:
60 content::TestBrowserThreadBundle thread_bundle_;
61 TestingProfileManager testing_profile_manager_;
62 scoped_ptr<MessageCenterSettingsController> controller_;
64 DISALLOW_COPY_AND_ASSIGN(MessageCenterSettingsControllerBaseTest);
67 #if defined(OS_CHROMEOS)
69 class MessageCenterSettingsControllerChromeOSTest
70 : public MessageCenterSettingsControllerBaseTest {
71 protected:
72 MessageCenterSettingsControllerChromeOSTest() {}
73 ~MessageCenterSettingsControllerChromeOSTest() override {}
75 void SetUp() override {
76 MessageCenterSettingsControllerBaseTest::SetUp();
78 // Initialize the UserManager singleton to a fresh FakeUserManager instance.
79 user_manager_enabler_.reset(new chromeos::ScopedUserManagerEnabler(
80 new chromeos::FakeChromeUserManager));
83 void TearDown() override {
84 ResetController();
85 MessageCenterSettingsControllerBaseTest::TearDown();
88 TestingProfile* CreateProfile(const std::string& name) override {
89 TestingProfile* profile =
90 MessageCenterSettingsControllerBaseTest::CreateProfile(name);
92 GetFakeUserManager()->AddUser(name);
93 GetFakeUserManager()->LoginUser(name);
94 return profile;
97 void SwitchActiveUser(const std::string& name) {
98 GetFakeUserManager()->SwitchActiveUser(name);
99 controller()->ActiveUserChanged(GetFakeUserManager()->GetActiveUser());
102 private:
103 chromeos::FakeChromeUserManager* GetFakeUserManager() {
104 return static_cast<chromeos::FakeChromeUserManager*>(
105 user_manager::UserManager::Get());
108 scoped_ptr<chromeos::ScopedUserManagerEnabler> user_manager_enabler_;
110 DISALLOW_COPY_AND_ASSIGN(MessageCenterSettingsControllerChromeOSTest);
113 typedef MessageCenterSettingsControllerChromeOSTest
114 MessageCenterSettingsControllerTest;
115 #else
116 typedef MessageCenterSettingsControllerBaseTest
117 MessageCenterSettingsControllerTest;
118 #endif // OS_CHROMEOS
120 #if !defined(OS_CHROMEOS)
121 TEST_F(MessageCenterSettingsControllerTest, NotifierGroups) {
122 CreateProfile("Profile-1");
123 CreateProfile("Profile-2");
124 CreateController();
126 EXPECT_EQ(controller()->GetNotifierGroupCount(), 2u);
128 EXPECT_EQ(controller()->GetNotifierGroupAt(0).name,
129 base::UTF8ToUTF16("Profile-1"));
130 EXPECT_EQ(controller()->GetNotifierGroupAt(0).index, 0u);
132 EXPECT_EQ(controller()->GetNotifierGroupAt(1).name,
133 base::UTF8ToUTF16("Profile-2"));
134 EXPECT_EQ(controller()->GetNotifierGroupAt(1).index, 1u);
136 EXPECT_EQ(controller()->GetActiveNotifierGroup().name,
137 base::UTF8ToUTF16("Profile-1"));
138 EXPECT_EQ(controller()->GetActiveNotifierGroup().index, 0u);
140 controller()->SwitchToNotifierGroup(1);
141 EXPECT_EQ(controller()->GetActiveNotifierGroup().name,
142 base::UTF8ToUTF16("Profile-2"));
143 EXPECT_EQ(controller()->GetActiveNotifierGroup().index, 1u);
145 controller()->SwitchToNotifierGroup(0);
146 EXPECT_EQ(controller()->GetActiveNotifierGroup().name,
147 base::UTF8ToUTF16("Profile-1"));
149 #else // !defined(OS_CHROMEOS)
150 TEST_F(MessageCenterSettingsControllerChromeOSTest, NotifierGroups) {
151 CreateProfile("Profile-1");
152 CreateProfile("Profile-2");
153 CreateController();
155 EXPECT_EQ(controller()->GetNotifierGroupCount(), 1u);
157 EXPECT_EQ(controller()->GetNotifierGroupAt(0).name,
158 base::UTF8ToUTF16("Profile-1"));
159 EXPECT_EQ(controller()->GetNotifierGroupAt(0).index, 0u);
161 SwitchActiveUser("Profile-2");
162 EXPECT_EQ(controller()->GetNotifierGroupCount(), 1u);
163 EXPECT_EQ(controller()->GetNotifierGroupAt(0).name,
164 base::UTF8ToUTF16("Profile-2"));
165 EXPECT_EQ(controller()->GetNotifierGroupAt(0).index, 1u);
167 SwitchActiveUser("Profile-1");
168 EXPECT_EQ(controller()->GetNotifierGroupCount(), 1u);
169 EXPECT_EQ(controller()->GetNotifierGroupAt(0).name,
170 base::UTF8ToUTF16("Profile-1"));
171 EXPECT_EQ(controller()->GetNotifierGroupAt(0).index, 0u);
173 // TODO(mukai): write a test case to reproduce the actual guest session scenario
174 // in ChromeOS -- no profiles in the profile_info_cache.
175 #endif // !defined(OS_CHROMEOS)
177 TEST_F(MessageCenterSettingsControllerTest, NotifierSortOrder) {
178 TestingProfile* profile = CreateProfile("Profile-1");
179 extensions::TestExtensionSystem* test_extension_system =
180 static_cast<extensions::TestExtensionSystem*>(
181 extensions::ExtensionSystem::Get(profile));
182 base::CommandLine command_line(base::CommandLine::NO_PROGRAM);
183 ExtensionService* extension_service =
184 test_extension_system->CreateExtensionService(
185 &command_line, base::FilePath() /* install_directory */,
186 false /* autoupdate_enabled*/);
188 extensions::ExtensionBuilder foo_app;
189 // Foo is an app with name Foo and should appear second.
190 const std::string kFooId = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
192 // Bar is an app with name Bar and should appear first.
193 const std::string kBarId = "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb";
195 // Baz is an app with name Baz and should not appear in the notifier list
196 // since it doesn't have notifications permission.
197 const std::string kBazId = "cccccccccccccccccccccccccccccccc";
199 foo_app.SetManifest(
200 extensions::DictionaryBuilder()
201 .Set("name", "Foo")
202 .Set("version", "1.0.0")
203 .Set("manifest_version", 2)
204 .Set("app", extensions::DictionaryBuilder().Set(
205 "background",
206 extensions::DictionaryBuilder().Set(
207 "scripts", extensions::ListBuilder().Append(
208 "background.js"))))
209 .Set("permissions",
210 extensions::ListBuilder().Append("notifications")));
211 foo_app.SetID(kFooId);
212 extension_service->AddExtension(foo_app.Build().get());
214 extensions::ExtensionBuilder bar_app;
215 bar_app.SetManifest(
216 extensions::DictionaryBuilder()
217 .Set("name", "Bar")
218 .Set("version", "1.0.0")
219 .Set("manifest_version", 2)
220 .Set("app", extensions::DictionaryBuilder().Set(
221 "background",
222 extensions::DictionaryBuilder().Set(
223 "scripts", extensions::ListBuilder().Append(
224 "background.js"))))
225 .Set("permissions",
226 extensions::ListBuilder().Append("notifications")));
227 bar_app.SetID(kBarId);
228 extension_service->AddExtension(bar_app.Build().get());
230 extensions::ExtensionBuilder baz_app;
231 baz_app.SetManifest(
232 extensions::DictionaryBuilder()
233 .Set("name", "baz")
234 .Set("version", "1.0.0")
235 .Set("manifest_version", 2)
236 .Set("app", extensions::DictionaryBuilder().Set(
237 "background",
238 extensions::DictionaryBuilder().Set(
239 "scripts", extensions::ListBuilder().Append(
240 "background.js")))));
241 baz_app.SetID(kBazId);
242 extension_service->AddExtension(baz_app.Build().get());
243 CreateController();
245 std::vector<message_center::Notifier*> notifiers;
246 controller()->GetNotifierList(&notifiers);
248 #if !defined(OS_CHROMEOS)
249 EXPECT_EQ(2u, notifiers.size());
250 #else
251 // ChromeOS always adds a system notifier to end of the list.
252 EXPECT_EQ(3u, notifiers.size());
253 EXPECT_EQ(ash::system_notifier::kNotifierScreenshot,
254 notifiers[2]->notifier_id.id);
255 #endif
257 EXPECT_EQ(kBarId, notifiers[0]->notifier_id.id);
258 EXPECT_EQ(kFooId, notifiers[1]->notifier_id.id);
260 STLDeleteElements(&notifiers);
263 TEST_F(MessageCenterSettingsControllerTest, SetWebPageNotifierEnabled) {
264 Profile* profile = CreateProfile("MyProfile");
265 CreateController();
267 GURL origin("https://example.com/");
269 message_center::NotifierId notifier_id(origin);
270 message_center::Notifier enabled_notifier(
271 notifier_id, base::string16(), true);
272 message_center::Notifier disabled_notifier(
273 notifier_id, base::string16(), false);
275 ContentSetting default_setting =
276 profile->GetHostContentSettingsMap()->GetDefaultContentSetting(
277 CONTENT_SETTINGS_TYPE_NOTIFICATIONS, NULL);
278 ASSERT_EQ(CONTENT_SETTING_ASK, default_setting);
280 // (1) Enable the permission when the default is to ask (expected to set).
281 controller()->SetNotifierEnabled(disabled_notifier, true);
282 EXPECT_EQ(CONTENT_SETTING_ALLOW,
283 DesktopNotificationProfileUtil::GetContentSetting(profile, origin));
285 // (2) Disable the permission when the default is to ask (expected to clear).
286 controller()->SetNotifierEnabled(enabled_notifier, false);
287 EXPECT_EQ(CONTENT_SETTING_ASK,
288 DesktopNotificationProfileUtil::GetContentSetting(profile, origin));
290 // Change the default content setting vaule for notifications to ALLOW.
291 profile->GetHostContentSettingsMap()->SetDefaultContentSetting(
292 CONTENT_SETTINGS_TYPE_NOTIFICATIONS, CONTENT_SETTING_ALLOW);
294 // (3) Disable the permission when the default is allowed (expected to set).
295 controller()->SetNotifierEnabled(enabled_notifier, false);
296 EXPECT_EQ(CONTENT_SETTING_BLOCK,
297 DesktopNotificationProfileUtil::GetContentSetting(profile, origin));
299 // (4) Enable the permission when the default is allowed (expected to clear).
300 controller()->SetNotifierEnabled(disabled_notifier, true);
301 EXPECT_EQ(CONTENT_SETTING_ALLOW,
302 DesktopNotificationProfileUtil::GetContentSetting(profile, origin));
304 // Now change the default content setting value to BLOCK.
305 profile->GetHostContentSettingsMap()->SetDefaultContentSetting(
306 CONTENT_SETTINGS_TYPE_NOTIFICATIONS, CONTENT_SETTING_BLOCK);
308 // (5) Enable the permission when the default is blocked (expected to set).
309 controller()->SetNotifierEnabled(disabled_notifier, true);
310 EXPECT_EQ(CONTENT_SETTING_ALLOW,
311 DesktopNotificationProfileUtil::GetContentSetting(profile, origin));
313 // (6) Disable the permission when the default is blocked (expected to clear).
314 controller()->SetNotifierEnabled(enabled_notifier, false);
315 EXPECT_EQ(CONTENT_SETTING_BLOCK,
316 DesktopNotificationProfileUtil::GetContentSetting(profile, origin));