OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // TODO(rickcam): Bug 73183: Add unit tests for image loading | 5 // TODO(rickcam): Bug 73183: Add unit tests for image loading |
6 | 6 |
7 #include <cstdlib> | 7 #include <cstdlib> |
8 #include <set> | 8 #include <set> |
9 | 9 |
10 #include "chrome/browser/background/background_application_list_model.h" | 10 #include "chrome/browser/background/background_application_list_model.h" |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
62 } | 62 } |
63 }; | 63 }; |
64 | 64 |
65 enum PushMessagingOption { | 65 enum PushMessagingOption { |
66 NO_PUSH_MESSAGING, | 66 NO_PUSH_MESSAGING, |
67 PUSH_MESSAGING_PERMISSION, | 67 PUSH_MESSAGING_PERMISSION, |
68 PUSH_MESSAGING_BUT_NOT_BACKGROUND | 68 PUSH_MESSAGING_BUT_NOT_BACKGROUND |
69 }; | 69 }; |
70 | 70 |
71 // Returns a barebones test Extension object with the specified |name|. The | 71 // Returns a barebones test Extension object with the specified |name|. The |
72 // returned extension will include background permission iff | 72 // returned extension will include background permission if |
73 // |background_permission| is true and pushMessaging permission if requested | 73 // |background_permission| is true. |
74 // by |push_messaging| value. Also the extension may have a specific id set | 74 static scoped_refptr<Extension> CreateExtension( |
75 // to test the case when it has a pushMessaging permission but is not | |
76 // considered a background app based on a whitelist. | |
77 static scoped_refptr<Extension> CreateExtensionBase( | |
78 const std::string& name, | 75 const std::string& name, |
79 bool background_permission, | 76 bool background_permission) { |
80 PushMessagingOption push_messaging) { | |
81 base::DictionaryValue manifest; | 77 base::DictionaryValue manifest; |
82 manifest.SetString(extensions::manifest_keys::kVersion, "1.0.0.0"); | 78 manifest.SetString(extensions::manifest_keys::kVersion, "1.0.0.0"); |
83 manifest.SetString(extensions::manifest_keys::kName, name); | 79 manifest.SetString(extensions::manifest_keys::kName, name); |
84 base::ListValue* permissions = new base::ListValue(); | 80 base::ListValue* permissions = new base::ListValue(); |
85 manifest.Set(extensions::manifest_keys::kPermissions, permissions); | 81 manifest.Set(extensions::manifest_keys::kPermissions, permissions); |
86 if (background_permission) { | 82 if (background_permission) { |
87 permissions->Append(new base::StringValue("background")); | 83 permissions->Append(new base::StringValue("background")); |
88 } | 84 } |
89 if (push_messaging == PUSH_MESSAGING_PERMISSION || | |
90 push_messaging == PUSH_MESSAGING_BUT_NOT_BACKGROUND) { | |
91 permissions->Append(new base::StringValue("pushMessaging")); | |
92 } | |
93 | 85 |
94 std::string error; | 86 std::string error; |
95 scoped_refptr<Extension> extension; | 87 scoped_refptr<Extension> extension; |
96 | 88 |
97 // There is a whitelist for extensions that have pushMessaging permission but | 89 extension = Extension::Create( |
98 // are not considered a background app. Create a test extension with a known | 90 bogus_file_pathname(name), |
99 // test id if needed. | 91 extensions::Manifest::INVALID_LOCATION, |
100 if (push_messaging == PUSH_MESSAGING_BUT_NOT_BACKGROUND) { | 92 manifest, |
101 extension = Extension::Create( | 93 Extension::NO_FLAGS, |
102 bogus_file_pathname(name), | 94 &error); |
103 extensions::Manifest::INVALID_LOCATION, | |
104 manifest, | |
105 Extension::NO_FLAGS, | |
106 "aaaabbbbccccddddeeeeffffgggghhhh", | |
107 &error); | |
108 } else { | |
109 extension = Extension::Create( | |
110 bogus_file_pathname(name), | |
111 extensions::Manifest::INVALID_LOCATION, | |
112 manifest, | |
113 Extension::NO_FLAGS, | |
114 &error); | |
115 } | |
116 | 95 |
117 // Cannot ASSERT_* here because that attempts an illegitimate return. | 96 // Cannot ASSERT_* here because that attempts an illegitimate return. |
118 // Cannot EXPECT_NE here because that assumes non-pointers unlike EXPECT_EQ | 97 // Cannot EXPECT_NE here because that assumes non-pointers unlike EXPECT_EQ |
119 EXPECT_TRUE(extension.get() != NULL) << error; | 98 EXPECT_TRUE(extension.get() != NULL) << error; |
120 return extension; | 99 return extension; |
121 } | 100 } |
122 | 101 |
123 static scoped_refptr<Extension> CreateExtension(const std::string& name, | |
124 bool background_permission) { | |
125 return CreateExtensionBase(name, background_permission, NO_PUSH_MESSAGING); | |
126 } | |
127 | |
128 namespace { | 102 namespace { |
129 std::string GenerateUniqueExtensionName() { | 103 std::string GenerateUniqueExtensionName() { |
130 static int uniqueness = 0; | 104 static int uniqueness = 0; |
131 std::ostringstream output; | 105 std::ostringstream output; |
132 output << "Unique Named Extension " << uniqueness; | 106 output << "Unique Named Extension " << uniqueness; |
133 ++uniqueness; | 107 ++uniqueness; |
134 return output.str(); | 108 return output.str(); |
135 } | 109 } |
136 | 110 |
137 void AddBackgroundPermission(ExtensionService* service, | 111 void AddBackgroundPermission(ExtensionService* service, |
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
247 ASSERT_EQ(1U, registry()->enabled_extensions().size()); | 221 ASSERT_EQ(1U, registry()->enabled_extensions().size()); |
248 ASSERT_EQ(0U, model->size()); | 222 ASSERT_EQ(0U, model->size()); |
249 ASSERT_FALSE(IsBackgroundApp(*ext3.get())); | 223 ASSERT_FALSE(IsBackgroundApp(*ext3.get())); |
250 service()->UninstallExtension(ext3->id(), | 224 service()->UninstallExtension(ext3->id(), |
251 extensions::UNINSTALL_REASON_FOR_TESTING, | 225 extensions::UNINSTALL_REASON_FOR_TESTING, |
252 base::Bind(&base::DoNothing), NULL); | 226 base::Bind(&base::DoNothing), NULL); |
253 ASSERT_EQ(0U, registry()->enabled_extensions().size()); | 227 ASSERT_EQ(0U, registry()->enabled_extensions().size()); |
254 ASSERT_EQ(0U, model->size()); | 228 ASSERT_EQ(0U, model->size()); |
255 } | 229 } |
256 | 230 |
257 // Verifies that pushMessaging also triggers background detection, except | |
258 // when extension is in a whitelist. | |
259 TEST_F(BackgroundApplicationListModelTest, PushMessagingTest) { | |
260 InitializeAndLoadEmptyExtensionService(); | |
261 ASSERT_TRUE(service()->is_ready()); | |
262 ASSERT_TRUE(registry()->enabled_extensions().is_empty()); | |
263 scoped_ptr<BackgroundApplicationListModel> model( | |
264 new BackgroundApplicationListModel(profile_.get())); | |
265 ASSERT_EQ(0U, model->size()); | |
266 | |
267 scoped_refptr<Extension> ext1 = CreateExtension("alpha", false); | |
268 scoped_refptr<Extension> ext2 = | |
269 CreateExtensionBase("charlie", false, PUSH_MESSAGING_BUT_NOT_BACKGROUND); | |
270 scoped_refptr<Extension> bgapp1 = | |
271 CreateExtensionBase("bravo", false, PUSH_MESSAGING_PERMISSION); | |
272 scoped_refptr<Extension> bgapp2 = | |
273 CreateExtensionBase("delta", true, PUSH_MESSAGING_PERMISSION); | |
274 scoped_refptr<Extension> bgapp3 = | |
275 CreateExtensionBase("echo", true, PUSH_MESSAGING_BUT_NOT_BACKGROUND); | |
276 ASSERT_EQ(0U, registry()->enabled_extensions().size()); | |
277 ASSERT_EQ(0U, model->size()); | |
278 | |
279 // Add alternating Extensions and Background Apps | |
280 ASSERT_FALSE(IsBackgroundApp(*ext1.get())); | |
281 service()->AddExtension(ext1.get()); | |
282 ASSERT_EQ(1U, registry()->enabled_extensions().size()); | |
283 ASSERT_EQ(0U, model->size()); | |
284 ASSERT_TRUE(IsBackgroundApp(*bgapp1.get())); | |
285 service()->AddExtension(bgapp1.get()); | |
286 ASSERT_EQ(2U, registry()->enabled_extensions().size()); | |
287 ASSERT_EQ(1U, model->size()); | |
288 ASSERT_FALSE(IsBackgroundApp(*ext2.get())); | |
289 service()->AddExtension(ext2.get()); | |
290 ASSERT_EQ(3U, registry()->enabled_extensions().size()); | |
291 ASSERT_EQ(1U, model->size()); | |
292 ASSERT_TRUE(IsBackgroundApp(*bgapp2.get())); | |
293 service()->AddExtension(bgapp2.get()); | |
294 ASSERT_EQ(4U, registry()->enabled_extensions().size()); | |
295 ASSERT_EQ(2U, model->size()); | |
296 // Need to remove ext2 because it uses same id as bgapp3. | |
297 ASSERT_FALSE(IsBackgroundApp(*ext2.get())); | |
298 service()->UninstallExtension(ext2->id(), | |
299 extensions::UNINSTALL_REASON_FOR_TESTING, | |
300 base::Bind(&base::DoNothing), NULL); | |
301 ASSERT_EQ(3U, registry()->enabled_extensions().size()); | |
302 ASSERT_EQ(2U, model->size()); | |
303 ASSERT_TRUE(IsBackgroundApp(*bgapp3.get())); | |
304 service()->AddExtension(bgapp3.get()); | |
305 ASSERT_EQ(4U, registry()->enabled_extensions().size()); | |
306 ASSERT_EQ(3U, model->size()); | |
307 | |
308 // Remove in FIFO order. | |
309 ASSERT_FALSE(IsBackgroundApp(*ext1.get())); | |
310 service()->UninstallExtension(ext1->id(), | |
311 extensions::UNINSTALL_REASON_FOR_TESTING, | |
312 base::Bind(&base::DoNothing), NULL); | |
313 ASSERT_EQ(3U, registry()->enabled_extensions().size()); | |
314 ASSERT_EQ(3U, model->size()); | |
315 ASSERT_TRUE(IsBackgroundApp(*bgapp1.get())); | |
316 service()->UninstallExtension(bgapp1->id(), | |
317 extensions::UNINSTALL_REASON_FOR_TESTING, | |
318 base::Bind(&base::DoNothing), NULL); | |
319 ASSERT_EQ(2U, registry()->enabled_extensions().size()); | |
320 ASSERT_EQ(2U, model->size()); | |
321 ASSERT_TRUE(IsBackgroundApp(*bgapp2.get())); | |
322 service()->UninstallExtension(bgapp2->id(), | |
323 extensions::UNINSTALL_REASON_FOR_TESTING, | |
324 base::Bind(&base::DoNothing), NULL); | |
325 ASSERT_EQ(1U, registry()->enabled_extensions().size()); | |
326 ASSERT_EQ(1U, model->size()); | |
327 ASSERT_TRUE(IsBackgroundApp(*bgapp3.get())); | |
328 service()->UninstallExtension(bgapp3->id(), | |
329 extensions::UNINSTALL_REASON_FOR_TESTING, | |
330 base::Bind(&base::DoNothing), NULL); | |
331 ASSERT_EQ(0U, registry()->enabled_extensions().size()); | |
332 ASSERT_EQ(0U, model->size()); | |
333 } | |
334 | |
335 // Verifies that an ephemeral app cannot trigger background mode. | 231 // Verifies that an ephemeral app cannot trigger background mode. |
336 TEST_F(BackgroundApplicationListModelTest, EphemeralAppTest) { | 232 TEST_F(BackgroundApplicationListModelTest, EphemeralAppTest) { |
337 InitializeAndLoadEmptyExtensionService(); | 233 InitializeAndLoadEmptyExtensionService(); |
338 ASSERT_TRUE(service()->is_ready()); | 234 ASSERT_TRUE(service()->is_ready()); |
339 ASSERT_TRUE(registry()->enabled_extensions().is_empty()); | 235 ASSERT_TRUE(registry()->enabled_extensions().is_empty()); |
340 scoped_ptr<BackgroundApplicationListModel> model( | 236 scoped_ptr<BackgroundApplicationListModel> model( |
341 new BackgroundApplicationListModel(profile_.get())); | 237 new BackgroundApplicationListModel(profile_.get())); |
342 ASSERT_EQ(0U, model->size()); | 238 ASSERT_EQ(0U, model->size()); |
343 | 239 |
344 scoped_refptr<Extension> installed = | |
345 CreateExtensionBase("installed", false, PUSH_MESSAGING_PERMISSION); | |
346 scoped_refptr<Extension> ephemeral = | |
347 CreateExtensionBase("ephemeral", false, PUSH_MESSAGING_PERMISSION); | |
348 scoped_refptr<Extension> background = CreateExtension("background", true); | 240 scoped_refptr<Extension> background = CreateExtension("background", true); |
349 | 241 |
350 // Installed app with push messaging permissions can trigger background mode. | |
351 ASSERT_TRUE(IsBackgroundApp(*installed.get())); | |
352 service()->AddExtension(installed.get()); | |
353 ASSERT_EQ(1U, registry()->enabled_extensions().size()); | |
354 ASSERT_EQ(1U, model->size()); | |
355 // An ephemeral app with push messaging permissions should not trigger | |
356 // background mode. | |
357 AddEphemeralApp(ephemeral.get(), service()); | |
358 ASSERT_FALSE(IsBackgroundApp(*ephemeral.get())); | |
359 ASSERT_EQ(2U, registry()->enabled_extensions().size()); | |
360 ASSERT_EQ(1U, model->size()); | |
361 // An ephemeral app with the background permission should not trigger | 242 // An ephemeral app with the background permission should not trigger |
362 // background mode. | 243 // background mode. |
363 AddEphemeralApp(background.get(), service()); | 244 AddEphemeralApp(background.get(), service()); |
364 ASSERT_FALSE(IsBackgroundApp(*background.get())); | 245 ASSERT_FALSE(IsBackgroundApp(*background.get())); |
365 ASSERT_EQ(3U, registry()->enabled_extensions().size()); | 246 ASSERT_EQ(1U, registry()->enabled_extensions().size()); |
366 ASSERT_EQ(1U, model->size()); | 247 ASSERT_EQ(0U, model->size()); |
367 | 248 |
368 // If the ephemeral app becomes promoted to an installed app, it can now | 249 // If the ephemeral app becomes promoted to an installed app, it can now |
369 // trigger background mode. | 250 // trigger background mode. |
370 service()->PromoteEphemeralApp(ephemeral.get(), false /*from sync*/); | 251 service()->PromoteEphemeralApp(background.get(), false /*from sync*/); |
371 ASSERT_TRUE(IsBackgroundApp(*ephemeral.get())); | 252 ASSERT_TRUE(IsBackgroundApp(*background.get())); |
372 ASSERT_EQ(3U, registry()->enabled_extensions().size()); | 253 ASSERT_EQ(1U, registry()->enabled_extensions().size()); |
373 ASSERT_EQ(2U, model->size()); | 254 ASSERT_EQ(1U, model->size()); |
374 } | 255 } |
375 | 256 |
376 // With minimal test logic, verifies behavior with dynamic permissions. | 257 // With minimal test logic, verifies behavior with dynamic permissions. |
377 TEST_F(BackgroundApplicationListModelTest, AddRemovePermissionsTest) { | 258 TEST_F(BackgroundApplicationListModelTest, AddRemovePermissionsTest) { |
378 InitializeAndLoadEmptyExtensionService(); | 259 InitializeAndLoadEmptyExtensionService(); |
379 ASSERT_TRUE(service()->is_ready()); | 260 ASSERT_TRUE(service()->is_ready()); |
380 ASSERT_TRUE(registry()->enabled_extensions().is_empty()); | 261 ASSERT_TRUE(registry()->enabled_extensions().is_empty()); |
381 scoped_ptr<BackgroundApplicationListModel> model( | 262 scoped_ptr<BackgroundApplicationListModel> model( |
382 new BackgroundApplicationListModel(profile_.get())); | 263 new BackgroundApplicationListModel(profile_.get())); |
383 ASSERT_EQ(0U, model->size()); | 264 ASSERT_EQ(0U, model->size()); |
(...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
554 case 2: | 435 case 2: |
555 TogglePermission(service(), &extensions, model.get(), &expected, | 436 TogglePermission(service(), &extensions, model.get(), &expected, |
556 &count); | 437 &count); |
557 break; | 438 break; |
558 default: | 439 default: |
559 NOTREACHED(); | 440 NOTREACHED(); |
560 break; | 441 break; |
561 } | 442 } |
562 } | 443 } |
563 } | 444 } |
OLD | NEW |