Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / notifications / message_center_settings_controller_unittest.cc
blob2a671d5a4f80bd86bad59fbb9bbaaa618e5a714f
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/content_settings/host_content_settings_map_factory.h"
10 #include "chrome/browser/extensions/extension_service.h"
11 #include "chrome/browser/extensions/test_extension_system.h"
12 #include "chrome/browser/notifications/desktop_notification_profile_util.h"
13 #include "chrome/browser/notifications/message_center_settings_controller.h"
14 #include "chrome/browser/profiles/profile_info_cache.h"
15 #include "chrome/browser/profiles/profile_manager.h"
16 #include "chrome/test/base/testing_browser_process.h"
17 #include "chrome/test/base/testing_profile_manager.h"
18 #include "components/content_settings/core/browser/host_content_settings_map.h"
19 #include "content/public/test/test_browser_thread_bundle.h"
20 #include "extensions/common/extension.h"
21 #include "extensions/common/extension_builder.h"
22 #include "testing/gtest/include/gtest/gtest.h"
23 #include "ui/message_center/notifier_settings.h"
25 #if defined(OS_CHROMEOS)
26 #include "ash/system/system_notifier.h"
27 #include "chrome/browser/chromeos/login/users/fake_chrome_user_manager.h"
28 #include "chrome/browser/chromeos/login/users/scoped_user_manager_enabler.h"
29 #endif
31 class MessageCenterSettingsControllerBaseTest : public testing::Test {
32 protected:
33 MessageCenterSettingsControllerBaseTest()
34 : testing_profile_manager_(TestingBrowserProcess::GetGlobal()) {}
36 ~MessageCenterSettingsControllerBaseTest() override{};
38 base::FilePath GetProfilePath(const std::string& base_name) {
39 return testing_profile_manager_.profile_manager()->user_data_dir()
40 .AppendASCII(base_name);
43 void SetUp() override { ASSERT_TRUE(testing_profile_manager_.SetUp()); }
45 virtual TestingProfile* CreateProfile(const std::string& name) {
46 return testing_profile_manager_.CreateTestingProfile(name);
49 void CreateController() {
50 controller_.reset(new MessageCenterSettingsController(
51 testing_profile_manager_.profile_info_cache()));
54 void ResetController() {
55 controller_.reset();
58 MessageCenterSettingsController* controller() { return controller_.get(); }
60 private:
61 content::TestBrowserThreadBundle thread_bundle_;
62 TestingProfileManager testing_profile_manager_;
63 scoped_ptr<MessageCenterSettingsController> controller_;
65 DISALLOW_COPY_AND_ASSIGN(MessageCenterSettingsControllerBaseTest);
68 #if defined(OS_CHROMEOS)
70 class MessageCenterSettingsControllerChromeOSTest
71 : public MessageCenterSettingsControllerBaseTest {
72 protected:
73 MessageCenterSettingsControllerChromeOSTest() {}
74 ~MessageCenterSettingsControllerChromeOSTest() override {}
76 void SetUp() override {
77 MessageCenterSettingsControllerBaseTest::SetUp();
79 // Initialize the UserManager singleton to a fresh FakeUserManager instance.
80 user_manager_enabler_.reset(new chromeos::ScopedUserManagerEnabler(
81 new chromeos::FakeChromeUserManager));
84 void TearDown() override {
85 ResetController();
86 MessageCenterSettingsControllerBaseTest::TearDown();
89 TestingProfile* CreateProfile(const std::string& name) override {
90 TestingProfile* profile =
91 MessageCenterSettingsControllerBaseTest::CreateProfile(name);
93 GetFakeUserManager()->AddUser(name);
94 GetFakeUserManager()->LoginUser(name);
95 return profile;
98 void SwitchActiveUser(const std::string& name) {
99 GetFakeUserManager()->SwitchActiveUser(name);
100 controller()->ActiveUserChanged(GetFakeUserManager()->GetActiveUser());
103 private:
104 chromeos::FakeChromeUserManager* GetFakeUserManager() {
105 return static_cast<chromeos::FakeChromeUserManager*>(
106 user_manager::UserManager::Get());
109 scoped_ptr<chromeos::ScopedUserManagerEnabler> user_manager_enabler_;
111 DISALLOW_COPY_AND_ASSIGN(MessageCenterSettingsControllerChromeOSTest);
114 typedef MessageCenterSettingsControllerChromeOSTest
115 MessageCenterSettingsControllerTest;
116 #else
117 typedef MessageCenterSettingsControllerBaseTest
118 MessageCenterSettingsControllerTest;
119 #endif // OS_CHROMEOS
121 #if !defined(OS_CHROMEOS)
122 TEST_F(MessageCenterSettingsControllerTest, NotifierGroups) {
123 CreateProfile("Profile-1");
124 CreateProfile("Profile-2");
125 CreateController();
127 EXPECT_EQ(controller()->GetNotifierGroupCount(), 2u);
129 EXPECT_EQ(controller()->GetNotifierGroupAt(0).name,
130 base::UTF8ToUTF16("Profile-1"));
131 EXPECT_EQ(controller()->GetNotifierGroupAt(0).index, 0u);
133 EXPECT_EQ(controller()->GetNotifierGroupAt(1).name,
134 base::UTF8ToUTF16("Profile-2"));
135 EXPECT_EQ(controller()->GetNotifierGroupAt(1).index, 1u);
137 EXPECT_EQ(controller()->GetActiveNotifierGroup().name,
138 base::UTF8ToUTF16("Profile-1"));
139 EXPECT_EQ(controller()->GetActiveNotifierGroup().index, 0u);
141 controller()->SwitchToNotifierGroup(1);
142 EXPECT_EQ(controller()->GetActiveNotifierGroup().name,
143 base::UTF8ToUTF16("Profile-2"));
144 EXPECT_EQ(controller()->GetActiveNotifierGroup().index, 1u);
146 controller()->SwitchToNotifierGroup(0);
147 EXPECT_EQ(controller()->GetActiveNotifierGroup().name,
148 base::UTF8ToUTF16("Profile-1"));
150 #else // !defined(OS_CHROMEOS)
151 TEST_F(MessageCenterSettingsControllerChromeOSTest, NotifierGroups) {
152 CreateProfile("Profile-1");
153 CreateProfile("Profile-2");
154 CreateController();
156 EXPECT_EQ(controller()->GetNotifierGroupCount(), 1u);
158 EXPECT_EQ(controller()->GetNotifierGroupAt(0).name,
159 base::UTF8ToUTF16("Profile-1"));
160 EXPECT_EQ(controller()->GetNotifierGroupAt(0).index, 0u);
162 SwitchActiveUser("Profile-2");
163 EXPECT_EQ(controller()->GetNotifierGroupCount(), 1u);
164 EXPECT_EQ(controller()->GetNotifierGroupAt(0).name,
165 base::UTF8ToUTF16("Profile-2"));
166 EXPECT_EQ(controller()->GetNotifierGroupAt(0).index, 1u);
168 SwitchActiveUser("Profile-1");
169 EXPECT_EQ(controller()->GetNotifierGroupCount(), 1u);
170 EXPECT_EQ(controller()->GetNotifierGroupAt(0).name,
171 base::UTF8ToUTF16("Profile-1"));
172 EXPECT_EQ(controller()->GetNotifierGroupAt(0).index, 0u);
174 // TODO(mukai): write a test case to reproduce the actual guest session scenario
175 // in ChromeOS -- no profiles in the profile_info_cache.
176 #endif // !defined(OS_CHROMEOS)
178 TEST_F(MessageCenterSettingsControllerTest, NotifierSortOrder) {
179 TestingProfile* profile = CreateProfile("Profile-1");
180 extensions::TestExtensionSystem* test_extension_system =
181 static_cast<extensions::TestExtensionSystem*>(
182 extensions::ExtensionSystem::Get(profile));
183 base::CommandLine command_line(base::CommandLine::NO_PROGRAM);
184 ExtensionService* extension_service =
185 test_extension_system->CreateExtensionService(
186 &command_line, base::FilePath() /* install_directory */,
187 false /* autoupdate_enabled*/);
189 extensions::ExtensionBuilder foo_app;
190 // Foo is an app with name Foo and should appear second.
191 const std::string kFooId = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
193 // Bar is an app with name Bar and should appear first.
194 const std::string kBarId = "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb";
196 // Baz is an app with name Baz and should not appear in the notifier list
197 // since it doesn't have notifications permission.
198 const std::string kBazId = "cccccccccccccccccccccccccccccccc";
200 // Baf is a hosted app which should not appear in the notifier list.
201 const std::string kBafId = "dddddddddddddddddddddddddddddddd";
203 foo_app.SetManifest(
204 extensions::DictionaryBuilder()
205 .Set("name", "Foo")
206 .Set("version", "1.0.0")
207 .Set("manifest_version", 2)
208 .Set("app", extensions::DictionaryBuilder().Set(
209 "background",
210 extensions::DictionaryBuilder().Set(
211 "scripts", extensions::ListBuilder().Append(
212 "background.js"))))
213 .Set("permissions",
214 extensions::ListBuilder().Append("notifications")));
215 foo_app.SetID(kFooId);
216 extension_service->AddExtension(foo_app.Build().get());
218 extensions::ExtensionBuilder bar_app;
219 bar_app.SetManifest(
220 extensions::DictionaryBuilder()
221 .Set("name", "Bar")
222 .Set("version", "1.0.0")
223 .Set("manifest_version", 2)
224 .Set("app", extensions::DictionaryBuilder().Set(
225 "background",
226 extensions::DictionaryBuilder().Set(
227 "scripts", extensions::ListBuilder().Append(
228 "background.js"))))
229 .Set("permissions",
230 extensions::ListBuilder().Append("notifications")));
231 bar_app.SetID(kBarId);
232 extension_service->AddExtension(bar_app.Build().get());
234 extensions::ExtensionBuilder baz_app;
235 baz_app.SetManifest(
236 extensions::DictionaryBuilder()
237 .Set("name", "baz")
238 .Set("version", "1.0.0")
239 .Set("manifest_version", 2)
240 .Set("app", extensions::DictionaryBuilder().Set(
241 "background",
242 extensions::DictionaryBuilder().Set(
243 "scripts", extensions::ListBuilder().Append(
244 "background.js")))));
245 baz_app.SetID(kBazId);
246 extension_service->AddExtension(baz_app.Build().get());
248 extensions::ExtensionBuilder baf_app;
249 baf_app.SetManifest(
250 extensions::DictionaryBuilder()
251 .Set("name", "baf")
252 .Set("version", "1.0.0")
253 .Set("manifest_version", 2)
254 .Set("app",
255 extensions::DictionaryBuilder().Set(
256 "urls",
257 extensions::ListBuilder().Append(
258 "http://localhost/extensions/hosted_app/main.html")))
259 .Set("launch",
260 extensions::DictionaryBuilder().Set(
261 "urls",
262 extensions::ListBuilder().Append(
263 "http://localhost/extensions/hosted_app/main.html"))));
265 baf_app.SetID(kBafId);
266 extension_service->AddExtension(baf_app.Build().get());
267 CreateController();
269 std::vector<message_center::Notifier*> notifiers;
270 controller()->GetNotifierList(&notifiers);
272 #if !defined(OS_CHROMEOS)
273 EXPECT_EQ(2u, notifiers.size());
274 #else
275 // ChromeOS always adds a system notifier to end of the list.
276 EXPECT_EQ(3u, notifiers.size());
277 EXPECT_EQ(ash::system_notifier::kNotifierScreenshot,
278 notifiers[2]->notifier_id.id);
279 #endif
281 EXPECT_EQ(kBarId, notifiers[0]->notifier_id.id);
282 EXPECT_EQ(kFooId, notifiers[1]->notifier_id.id);
284 STLDeleteElements(&notifiers);
287 TEST_F(MessageCenterSettingsControllerTest, SetWebPageNotifierEnabled) {
288 Profile* profile = CreateProfile("MyProfile");
289 CreateController();
291 GURL origin("https://example.com/");
293 message_center::NotifierId notifier_id(origin);
294 message_center::Notifier enabled_notifier(
295 notifier_id, base::string16(), true);
296 message_center::Notifier disabled_notifier(
297 notifier_id, base::string16(), false);
299 ContentSetting default_setting =
300 HostContentSettingsMapFactory::GetForProfile(profile)
301 ->GetDefaultContentSetting(CONTENT_SETTINGS_TYPE_NOTIFICATIONS, NULL);
302 ASSERT_EQ(CONTENT_SETTING_ASK, default_setting);
304 // (1) Enable the permission when the default is to ask (expected to set).
305 controller()->SetNotifierEnabled(disabled_notifier, true);
306 EXPECT_EQ(CONTENT_SETTING_ALLOW,
307 DesktopNotificationProfileUtil::GetContentSetting(profile, origin));
309 // (2) Disable the permission when the default is to ask (expected to clear).
310 controller()->SetNotifierEnabled(enabled_notifier, false);
311 EXPECT_EQ(CONTENT_SETTING_ASK,
312 DesktopNotificationProfileUtil::GetContentSetting(profile, origin));
314 // Change the default content setting vaule for notifications to ALLOW.
315 HostContentSettingsMapFactory::GetForProfile(profile)
316 ->SetDefaultContentSetting(CONTENT_SETTINGS_TYPE_NOTIFICATIONS,
317 CONTENT_SETTING_ALLOW);
319 // (3) Disable the permission when the default is allowed (expected to set).
320 controller()->SetNotifierEnabled(enabled_notifier, false);
321 EXPECT_EQ(CONTENT_SETTING_BLOCK,
322 DesktopNotificationProfileUtil::GetContentSetting(profile, origin));
324 // (4) Enable the permission when the default is allowed (expected to clear).
325 controller()->SetNotifierEnabled(disabled_notifier, true);
326 EXPECT_EQ(CONTENT_SETTING_ALLOW,
327 DesktopNotificationProfileUtil::GetContentSetting(profile, origin));
329 // Now change the default content setting value to BLOCK.
330 HostContentSettingsMapFactory::GetForProfile(profile)
331 ->SetDefaultContentSetting(CONTENT_SETTINGS_TYPE_NOTIFICATIONS,
332 CONTENT_SETTING_BLOCK);
334 // (5) Enable the permission when the default is blocked (expected to set).
335 controller()->SetNotifierEnabled(disabled_notifier, true);
336 EXPECT_EQ(CONTENT_SETTING_ALLOW,
337 DesktopNotificationProfileUtil::GetContentSetting(profile, origin));
339 // (6) Disable the permission when the default is blocked (expected to clear).
340 controller()->SetNotifierEnabled(enabled_notifier, false);
341 EXPECT_EQ(CONTENT_SETTING_BLOCK,
342 DesktopNotificationProfileUtil::GetContentSetting(profile, origin));