Popular sites on the NTP: check that experiment group StartsWith (rather than IS...
[chromium-blink-merge.git] / chrome / browser / notifications / message_center_settings_controller_unittest.cc
blob1fd13f1710a18b7a7460c747cc08013d72e96ae5
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 // Baf is a hosted app which should not appear in the notifier list.
200 const std::string kBafId = "dddddddddddddddddddddddddddddddd";
202 foo_app.SetManifest(
203 extensions::DictionaryBuilder()
204 .Set("name", "Foo")
205 .Set("version", "1.0.0")
206 .Set("manifest_version", 2)
207 .Set("app", extensions::DictionaryBuilder().Set(
208 "background",
209 extensions::DictionaryBuilder().Set(
210 "scripts", extensions::ListBuilder().Append(
211 "background.js"))))
212 .Set("permissions",
213 extensions::ListBuilder().Append("notifications")));
214 foo_app.SetID(kFooId);
215 extension_service->AddExtension(foo_app.Build().get());
217 extensions::ExtensionBuilder bar_app;
218 bar_app.SetManifest(
219 extensions::DictionaryBuilder()
220 .Set("name", "Bar")
221 .Set("version", "1.0.0")
222 .Set("manifest_version", 2)
223 .Set("app", extensions::DictionaryBuilder().Set(
224 "background",
225 extensions::DictionaryBuilder().Set(
226 "scripts", extensions::ListBuilder().Append(
227 "background.js"))))
228 .Set("permissions",
229 extensions::ListBuilder().Append("notifications")));
230 bar_app.SetID(kBarId);
231 extension_service->AddExtension(bar_app.Build().get());
233 extensions::ExtensionBuilder baz_app;
234 baz_app.SetManifest(
235 extensions::DictionaryBuilder()
236 .Set("name", "baz")
237 .Set("version", "1.0.0")
238 .Set("manifest_version", 2)
239 .Set("app", extensions::DictionaryBuilder().Set(
240 "background",
241 extensions::DictionaryBuilder().Set(
242 "scripts", extensions::ListBuilder().Append(
243 "background.js")))));
244 baz_app.SetID(kBazId);
245 extension_service->AddExtension(baz_app.Build().get());
247 extensions::ExtensionBuilder baf_app;
248 baf_app.SetManifest(
249 extensions::DictionaryBuilder()
250 .Set("name", "baf")
251 .Set("version", "1.0.0")
252 .Set("manifest_version", 2)
253 .Set("app",
254 extensions::DictionaryBuilder().Set(
255 "urls",
256 extensions::ListBuilder().Append(
257 "http://localhost/extensions/hosted_app/main.html")))
258 .Set("launch",
259 extensions::DictionaryBuilder().Set(
260 "urls",
261 extensions::ListBuilder().Append(
262 "http://localhost/extensions/hosted_app/main.html"))));
264 baf_app.SetID(kBafId);
265 extension_service->AddExtension(baf_app.Build().get());
266 CreateController();
268 std::vector<message_center::Notifier*> notifiers;
269 controller()->GetNotifierList(&notifiers);
271 #if !defined(OS_CHROMEOS)
272 EXPECT_EQ(2u, notifiers.size());
273 #else
274 // ChromeOS always adds a system notifier to end of the list.
275 EXPECT_EQ(3u, notifiers.size());
276 EXPECT_EQ(ash::system_notifier::kNotifierScreenshot,
277 notifiers[2]->notifier_id.id);
278 #endif
280 EXPECT_EQ(kBarId, notifiers[0]->notifier_id.id);
281 EXPECT_EQ(kFooId, notifiers[1]->notifier_id.id);
283 STLDeleteElements(&notifiers);
286 TEST_F(MessageCenterSettingsControllerTest, SetWebPageNotifierEnabled) {
287 Profile* profile = CreateProfile("MyProfile");
288 CreateController();
290 GURL origin("https://example.com/");
292 message_center::NotifierId notifier_id(origin);
293 message_center::Notifier enabled_notifier(
294 notifier_id, base::string16(), true);
295 message_center::Notifier disabled_notifier(
296 notifier_id, base::string16(), false);
298 ContentSetting default_setting =
299 profile->GetHostContentSettingsMap()->GetDefaultContentSetting(
300 CONTENT_SETTINGS_TYPE_NOTIFICATIONS, NULL);
301 ASSERT_EQ(CONTENT_SETTING_ASK, default_setting);
303 // (1) Enable the permission when the default is to ask (expected to set).
304 controller()->SetNotifierEnabled(disabled_notifier, true);
305 EXPECT_EQ(CONTENT_SETTING_ALLOW,
306 DesktopNotificationProfileUtil::GetContentSetting(profile, origin));
308 // (2) Disable the permission when the default is to ask (expected to clear).
309 controller()->SetNotifierEnabled(enabled_notifier, false);
310 EXPECT_EQ(CONTENT_SETTING_ASK,
311 DesktopNotificationProfileUtil::GetContentSetting(profile, origin));
313 // Change the default content setting vaule for notifications to ALLOW.
314 profile->GetHostContentSettingsMap()->SetDefaultContentSetting(
315 CONTENT_SETTINGS_TYPE_NOTIFICATIONS, CONTENT_SETTING_ALLOW);
317 // (3) Disable the permission when the default is allowed (expected to set).
318 controller()->SetNotifierEnabled(enabled_notifier, false);
319 EXPECT_EQ(CONTENT_SETTING_BLOCK,
320 DesktopNotificationProfileUtil::GetContentSetting(profile, origin));
322 // (4) Enable the permission when the default is allowed (expected to clear).
323 controller()->SetNotifierEnabled(disabled_notifier, true);
324 EXPECT_EQ(CONTENT_SETTING_ALLOW,
325 DesktopNotificationProfileUtil::GetContentSetting(profile, origin));
327 // Now change the default content setting value to BLOCK.
328 profile->GetHostContentSettingsMap()->SetDefaultContentSetting(
329 CONTENT_SETTINGS_TYPE_NOTIFICATIONS, CONTENT_SETTING_BLOCK);
331 // (5) Enable the permission when the default is blocked (expected to set).
332 controller()->SetNotifierEnabled(disabled_notifier, true);
333 EXPECT_EQ(CONTENT_SETTING_ALLOW,
334 DesktopNotificationProfileUtil::GetContentSetting(profile, origin));
336 // (6) Disable the permission when the default is blocked (expected to clear).
337 controller()->SetNotifierEnabled(enabled_notifier, false);
338 EXPECT_EQ(CONTENT_SETTING_BLOCK,
339 DesktopNotificationProfileUtil::GetContentSetting(profile, origin));