Check USB device path access when prompting users to select a device.
[chromium-blink-merge.git] / chrome / browser / notifications / message_center_settings_controller_unittest.cc
blobcd8e18b8656882e7d73ce10fbe188fce2bd271a4
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/message_center_settings_controller.h"
12 #include "chrome/browser/profiles/profile_info_cache.h"
13 #include "chrome/browser/profiles/profile_manager.h"
14 #include "chrome/test/base/testing_browser_process.h"
15 #include "chrome/test/base/testing_profile_manager.h"
16 #include "content/public/test/test_browser_thread_bundle.h"
17 #include "extensions/common/extension.h"
18 #include "extensions/common/extension_builder.h"
19 #include "testing/gtest/include/gtest/gtest.h"
20 #include "ui/message_center/notifier_settings.h"
22 #if defined(OS_CHROMEOS)
23 #include "ash/system/system_notifier.h"
24 #include "chrome/browser/chromeos/login/users/fake_chrome_user_manager.h"
25 #include "chrome/browser/chromeos/login/users/scoped_user_manager_enabler.h"
26 #endif
28 class MessageCenterSettingsControllerBaseTest : public testing::Test {
29 protected:
30 MessageCenterSettingsControllerBaseTest()
31 : testing_profile_manager_(TestingBrowserProcess::GetGlobal()){};
33 ~MessageCenterSettingsControllerBaseTest() override{};
35 base::FilePath GetProfilePath(const std::string& base_name) {
36 return testing_profile_manager_.profile_manager()->user_data_dir()
37 .AppendASCII(base_name);
40 void SetUp() override { ASSERT_TRUE(testing_profile_manager_.SetUp()); }
42 virtual TestingProfile* CreateProfile(const std::string& name) {
43 return testing_profile_manager_.CreateTestingProfile(name);
46 void CreateController() {
47 controller_.reset(new MessageCenterSettingsController(
48 testing_profile_manager_.profile_info_cache()));
51 void ResetController() {
52 controller_.reset();
55 MessageCenterSettingsController* controller() { return controller_.get(); }
57 private:
58 content::TestBrowserThreadBundle thread_bundle_;
59 TestingProfileManager testing_profile_manager_;
60 scoped_ptr<MessageCenterSettingsController> controller_;
62 DISALLOW_COPY_AND_ASSIGN(MessageCenterSettingsControllerBaseTest);
65 #if defined(OS_CHROMEOS)
67 class MessageCenterSettingsControllerChromeOSTest
68 : public MessageCenterSettingsControllerBaseTest {
69 protected:
70 MessageCenterSettingsControllerChromeOSTest() {}
71 ~MessageCenterSettingsControllerChromeOSTest() override {}
73 void SetUp() override {
74 MessageCenterSettingsControllerBaseTest::SetUp();
76 // Initialize the UserManager singleton to a fresh FakeUserManager instance.
77 user_manager_enabler_.reset(new chromeos::ScopedUserManagerEnabler(
78 new chromeos::FakeChromeUserManager));
81 void TearDown() override {
82 ResetController();
83 MessageCenterSettingsControllerBaseTest::TearDown();
86 TestingProfile* CreateProfile(const std::string& name) override {
87 TestingProfile* profile =
88 MessageCenterSettingsControllerBaseTest::CreateProfile(name);
90 GetFakeUserManager()->AddUser(name);
91 GetFakeUserManager()->LoginUser(name);
92 return profile;
95 void SwitchActiveUser(const std::string& name) {
96 GetFakeUserManager()->SwitchActiveUser(name);
97 controller()->ActiveUserChanged(GetFakeUserManager()->GetActiveUser());
100 private:
101 chromeos::FakeChromeUserManager* GetFakeUserManager() {
102 return static_cast<chromeos::FakeChromeUserManager*>(
103 user_manager::UserManager::Get());
106 scoped_ptr<chromeos::ScopedUserManagerEnabler> user_manager_enabler_;
108 DISALLOW_COPY_AND_ASSIGN(MessageCenterSettingsControllerChromeOSTest);
111 typedef MessageCenterSettingsControllerChromeOSTest
112 MessageCenterSettingsControllerTest;
113 #else
114 typedef MessageCenterSettingsControllerBaseTest
115 MessageCenterSettingsControllerTest;
116 #endif // OS_CHROMEOS
118 #if !defined(OS_CHROMEOS)
119 TEST_F(MessageCenterSettingsControllerTest, NotifierGroups) {
120 CreateProfile("Profile-1");
121 CreateProfile("Profile-2");
122 CreateController();
124 EXPECT_EQ(controller()->GetNotifierGroupCount(), 2u);
126 EXPECT_EQ(controller()->GetNotifierGroupAt(0).name,
127 base::UTF8ToUTF16("Profile-1"));
128 EXPECT_EQ(controller()->GetNotifierGroupAt(0).index, 0u);
130 EXPECT_EQ(controller()->GetNotifierGroupAt(1).name,
131 base::UTF8ToUTF16("Profile-2"));
132 EXPECT_EQ(controller()->GetNotifierGroupAt(1).index, 1u);
134 EXPECT_EQ(controller()->GetActiveNotifierGroup().name,
135 base::UTF8ToUTF16("Profile-1"));
136 EXPECT_EQ(controller()->GetActiveNotifierGroup().index, 0u);
138 controller()->SwitchToNotifierGroup(1);
139 EXPECT_EQ(controller()->GetActiveNotifierGroup().name,
140 base::UTF8ToUTF16("Profile-2"));
141 EXPECT_EQ(controller()->GetActiveNotifierGroup().index, 1u);
143 controller()->SwitchToNotifierGroup(0);
144 EXPECT_EQ(controller()->GetActiveNotifierGroup().name,
145 base::UTF8ToUTF16("Profile-1"));
147 #else // !defined(OS_CHROMEOS)
148 TEST_F(MessageCenterSettingsControllerChromeOSTest, NotifierGroups) {
149 CreateProfile("Profile-1");
150 CreateProfile("Profile-2");
151 CreateController();
153 EXPECT_EQ(controller()->GetNotifierGroupCount(), 1u);
155 EXPECT_EQ(controller()->GetNotifierGroupAt(0).name,
156 base::UTF8ToUTF16("Profile-1"));
157 EXPECT_EQ(controller()->GetNotifierGroupAt(0).index, 0u);
159 SwitchActiveUser("Profile-2");
160 EXPECT_EQ(controller()->GetNotifierGroupCount(), 1u);
161 EXPECT_EQ(controller()->GetNotifierGroupAt(0).name,
162 base::UTF8ToUTF16("Profile-2"));
163 EXPECT_EQ(controller()->GetNotifierGroupAt(0).index, 1u);
165 SwitchActiveUser("Profile-1");
166 EXPECT_EQ(controller()->GetNotifierGroupCount(), 1u);
167 EXPECT_EQ(controller()->GetNotifierGroupAt(0).name,
168 base::UTF8ToUTF16("Profile-1"));
169 EXPECT_EQ(controller()->GetNotifierGroupAt(0).index, 0u);
171 // TODO(mukai): write a test case to reproduce the actual guest session scenario
172 // in ChromeOS -- no profiles in the profile_info_cache.
173 #endif // !defined(OS_CHROMEOS)
175 TEST_F(MessageCenterSettingsControllerTest, NotifierSortOrder) {
176 TestingProfile* profile = CreateProfile("Profile-1");
177 extensions::TestExtensionSystem* test_extension_system =
178 static_cast<extensions::TestExtensionSystem*>(
179 extensions::ExtensionSystem::Get(profile));
180 base::CommandLine command_line(base::CommandLine::NO_PROGRAM);
181 ExtensionService* extension_service =
182 test_extension_system->CreateExtensionService(
183 &command_line, base::FilePath() /* install_directory */,
184 false /* autoupdate_enabled*/);
186 extensions::ExtensionBuilder foo_app;
187 // Foo is an app with name Foo and should appear second.
188 const std::string kFooId = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
190 // Bar is an app with name Bar and should appear first.
191 const std::string kBarId = "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb";
193 // Baz is an app with name Baz and should not appear in the notifier list
194 // since it doesn't have notifications permission.
195 const std::string kBazId = "cccccccccccccccccccccccccccccccc";
197 foo_app.SetManifest(
198 extensions::DictionaryBuilder()
199 .Set("name", "Foo")
200 .Set("version", "1.0.0")
201 .Set("manifest_version", 2)
202 .Set("app", extensions::DictionaryBuilder().Set(
203 "background",
204 extensions::DictionaryBuilder().Set(
205 "scripts", extensions::ListBuilder().Append(
206 "background.js"))))
207 .Set("permissions",
208 extensions::ListBuilder().Append("notifications")));
209 foo_app.SetID(kFooId);
210 extension_service->AddExtension(foo_app.Build().get());
212 extensions::ExtensionBuilder bar_app;
213 bar_app.SetManifest(
214 extensions::DictionaryBuilder()
215 .Set("name", "Bar")
216 .Set("version", "1.0.0")
217 .Set("manifest_version", 2)
218 .Set("app", extensions::DictionaryBuilder().Set(
219 "background",
220 extensions::DictionaryBuilder().Set(
221 "scripts", extensions::ListBuilder().Append(
222 "background.js"))))
223 .Set("permissions",
224 extensions::ListBuilder().Append("notifications")));
225 bar_app.SetID(kBarId);
226 extension_service->AddExtension(bar_app.Build().get());
228 extensions::ExtensionBuilder baz_app;
229 baz_app.SetManifest(
230 extensions::DictionaryBuilder()
231 .Set("name", "baz")
232 .Set("version", "1.0.0")
233 .Set("manifest_version", 2)
234 .Set("app", extensions::DictionaryBuilder().Set(
235 "background",
236 extensions::DictionaryBuilder().Set(
237 "scripts", extensions::ListBuilder().Append(
238 "background.js")))));
239 baz_app.SetID(kBazId);
240 extension_service->AddExtension(baz_app.Build().get());
241 CreateController();
243 std::vector<message_center::Notifier*> notifiers;
244 controller()->GetNotifierList(&notifiers);
246 #if !defined(OS_CHROMEOS)
247 EXPECT_EQ(2u, notifiers.size());
248 #else
249 // ChromeOS always adds a system notifier to end of the list.
250 EXPECT_EQ(3u, notifiers.size());
251 EXPECT_EQ(ash::system_notifier::kNotifierScreenshot,
252 notifiers[2]->notifier_id.id);
253 #endif
255 EXPECT_EQ(kBarId, notifiers[0]->notifier_id.id);
256 EXPECT_EQ(kFooId, notifiers[1]->notifier_id.id);
258 STLDeleteElements(&notifiers);