| 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" |
| 11 | 11 |
| 12 #include "base/command_line.h" | 12 #include "base/command_line.h" |
| 13 #include "base/file_path.h" | 13 #include "base/file_path.h" |
| 14 #include "base/file_util.h" | 14 #include "base/file_util.h" |
| 15 #include "base/memory/scoped_ptr.h" | 15 #include "base/memory/scoped_ptr.h" |
| 16 #include "base/message_loop.h" | 16 #include "base/message_loop.h" |
| 17 #include "base/stl_util.h" | 17 #include "base/stl_util.h" |
| 18 #include "chrome/browser/extensions/extension_service.h" | 18 #include "chrome/browser/extensions/extension_service.h" |
| 19 #include "chrome/browser/extensions/extension_service_unittest.h" | 19 #include "chrome/browser/extensions/extension_service_unittest.h" |
| 20 #include "chrome/browser/extensions/permissions_updater.h" | 20 #include "chrome/browser/extensions/permissions_updater.h" |
| 21 #include "chrome/common/extensions/extension.h" | 21 #include "chrome/common/extensions/extension.h" |
| 22 #include "chrome/common/extensions/extension_manifest_constants.h" | 22 #include "chrome/common/extensions/extension_manifest_constants.h" |
| 23 #include "chrome/common/extensions/permissions/api_permission.h" |
| 23 #include "chrome/common/extensions/permissions/permission_set.h" | 24 #include "chrome/common/extensions/permissions/permission_set.h" |
| 24 #include "chrome/test/base/testing_profile.h" | 25 #include "chrome/test/base/testing_profile.h" |
| 25 #include "content/public/browser/notification_registrar.h" | 26 #include "content/public/browser/notification_registrar.h" |
| 26 #include "content/public/browser/notification_types.h" | 27 #include "content/public/browser/notification_types.h" |
| 27 #include "content/public/test/test_browser_thread.h" | 28 #include "content/public/test/test_browser_thread.h" |
| 28 #include "testing/gtest/include/gtest/gtest.h" | 29 #include "testing/gtest/include/gtest/gtest.h" |
| 29 | 30 |
| 30 // This value is used to seed the PRNG at the beginning of a sequence of | 31 // This value is used to seed the PRNG at the beginning of a sequence of |
| 31 // operations to produce a repeatable sequence. | 32 // operations to produce a repeatable sequence. |
| 32 #define RANDOM_SEED (0x33F7A7A7) | 33 #define RANDOM_SEED (0x33F7A7A7) |
| 33 | 34 |
| 35 using extensions::APIPermission; |
| 34 using extensions::Extension; | 36 using extensions::Extension; |
| 35 | 37 |
| 36 // For ExtensionService interface when it requires a path that is not used. | 38 // For ExtensionService interface when it requires a path that is not used. |
| 37 FilePath bogus_file_path() { | 39 FilePath bogus_file_path() { |
| 38 return FilePath(FILE_PATH_LITERAL("//foobar_nonexistent")); | 40 return FilePath(FILE_PATH_LITERAL("//foobar_nonexistent")); |
| 39 } | 41 } |
| 40 | 42 |
| 41 class BackgroundApplicationListModelTest : public ExtensionServiceTestBase { | 43 class BackgroundApplicationListModelTest : public ExtensionServiceTestBase { |
| 42 public: | 44 public: |
| 43 BackgroundApplicationListModelTest() {} | 45 BackgroundApplicationListModelTest() {} |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 90 return output.str(); | 92 return output.str(); |
| 91 } | 93 } |
| 92 | 94 |
| 93 void AddBackgroundPermission(ExtensionService* service, | 95 void AddBackgroundPermission(ExtensionService* service, |
| 94 Extension* extension) { | 96 Extension* extension) { |
| 95 if (BackgroundApplicationListModel::IsBackgroundApp(*extension, | 97 if (BackgroundApplicationListModel::IsBackgroundApp(*extension, |
| 96 service->profile())) { | 98 service->profile())) { |
| 97 return; | 99 return; |
| 98 } | 100 } |
| 99 | 101 |
| 100 static scoped_refptr<Extension> temporary = | 102 scoped_refptr<Extension> temporary = |
| 101 CreateExtension(GenerateUniqueExtensionName(), true); | 103 CreateExtension(GenerateUniqueExtensionName(), true); |
| 102 scoped_refptr<const extensions::PermissionSet> permissions = | 104 scoped_refptr<const extensions::PermissionSet> permissions = |
| 103 temporary->GetActivePermissions(); | 105 temporary->GetActivePermissions(); |
| 104 extensions::PermissionsUpdater(service->profile()).AddPermissions( | 106 extensions::PermissionsUpdater(service->profile()).AddPermissions( |
| 105 extension, permissions.get()); | 107 extension, permissions.get()); |
| 106 } | 108 } |
| 107 | 109 |
| 108 void RemoveBackgroundPermission(ExtensionService* service, | 110 void RemoveBackgroundPermission(ExtensionService* service, |
| 109 Extension* extension) { | 111 Extension* extension) { |
| 110 if (!BackgroundApplicationListModel::IsBackgroundApp(*extension, | 112 if (!BackgroundApplicationListModel::IsBackgroundApp(*extension, |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 189 ExtensionService* service = profile_->GetExtensionService(); | 191 ExtensionService* service = profile_->GetExtensionService(); |
| 190 ASSERT_TRUE(service); | 192 ASSERT_TRUE(service); |
| 191 ASSERT_TRUE(service->is_ready()); | 193 ASSERT_TRUE(service->is_ready()); |
| 192 ASSERT_TRUE(service->extensions()); | 194 ASSERT_TRUE(service->extensions()); |
| 193 ASSERT_TRUE(service->extensions()->is_empty()); | 195 ASSERT_TRUE(service->extensions()->is_empty()); |
| 194 scoped_ptr<BackgroundApplicationListModel> model( | 196 scoped_ptr<BackgroundApplicationListModel> model( |
| 195 new BackgroundApplicationListModel(profile_.get())); | 197 new BackgroundApplicationListModel(profile_.get())); |
| 196 ASSERT_EQ(0U, model->size()); | 198 ASSERT_EQ(0U, model->size()); |
| 197 | 199 |
| 198 scoped_refptr<Extension> ext = CreateExtension("extension", false); | 200 scoped_refptr<Extension> ext = CreateExtension("extension", false); |
| 201 ASSERT_FALSE(ext->HasAPIPermission(APIPermission::kBackground)); |
| 199 scoped_refptr<Extension> bgapp = CreateExtension("application", true); | 202 scoped_refptr<Extension> bgapp = CreateExtension("application", true); |
| 203 ASSERT_TRUE(bgapp->HasAPIPermission(APIPermission::kBackground)); |
| 200 ASSERT_TRUE(service->extensions() != NULL); | 204 ASSERT_TRUE(service->extensions() != NULL); |
| 201 ASSERT_EQ(0U, service->extensions()->size()); | 205 ASSERT_EQ(0U, service->extensions()->size()); |
| 202 ASSERT_EQ(0U, model->size()); | 206 ASSERT_EQ(0U, model->size()); |
| 203 | 207 |
| 204 // Add one (non-background) extension and one background application | 208 // Add one (non-background) extension and one background application |
| 205 ASSERT_FALSE(IsBackgroundApp(*ext)); | 209 ASSERT_FALSE(IsBackgroundApp(*ext)); |
| 206 service->AddExtension(ext); | 210 service->AddExtension(ext); |
| 207 ASSERT_EQ(1U, service->extensions()->size()); | 211 ASSERT_EQ(1U, service->extensions()->size()); |
| 208 ASSERT_EQ(0U, model->size()); | 212 ASSERT_EQ(0U, model->size()); |
| 209 ASSERT_TRUE(IsBackgroundApp(*bgapp)); | 213 ASSERT_TRUE(IsBackgroundApp(*bgapp)); |
| 210 service->AddExtension(bgapp); | 214 service->AddExtension(bgapp); |
| 211 ASSERT_EQ(2U, service->extensions()->size()); | 215 ASSERT_EQ(2U, service->extensions()->size()); |
| 212 ASSERT_EQ(1U, model->size()); | 216 ASSERT_EQ(1U, model->size()); |
| 213 | 217 |
| 214 // Change permissions back and forth | 218 // Change permissions back and forth |
| 215 AddBackgroundPermission(service, ext.get()); | 219 AddBackgroundPermission(service, ext.get()); |
| 220 ASSERT_TRUE(ext->HasAPIPermission(APIPermission::kBackground)); |
| 216 ASSERT_EQ(2U, service->extensions()->size()); | 221 ASSERT_EQ(2U, service->extensions()->size()); |
| 217 ASSERT_EQ(2U, model->size()); | 222 ASSERT_EQ(2U, model->size()); |
| 218 RemoveBackgroundPermission(service, bgapp.get()); | 223 RemoveBackgroundPermission(service, bgapp.get()); |
| 224 ASSERT_FALSE(bgapp->HasAPIPermission(APIPermission::kBackground)); |
| 219 ASSERT_EQ(2U, service->extensions()->size()); | 225 ASSERT_EQ(2U, service->extensions()->size()); |
| 220 ASSERT_EQ(1U, model->size()); | 226 ASSERT_EQ(1U, model->size()); |
| 221 RemoveBackgroundPermission(service, ext.get()); | 227 RemoveBackgroundPermission(service, ext.get()); |
| 228 ASSERT_FALSE(ext->HasAPIPermission(APIPermission::kBackground)); |
| 222 ASSERT_EQ(2U, service->extensions()->size()); | 229 ASSERT_EQ(2U, service->extensions()->size()); |
| 223 ASSERT_EQ(0U, model->size()); | 230 ASSERT_EQ(0U, model->size()); |
| 224 AddBackgroundPermission(service, bgapp.get()); | 231 AddBackgroundPermission(service, bgapp.get()); |
| 232 ASSERT_TRUE(bgapp->HasAPIPermission(APIPermission::kBackground)); |
| 225 ASSERT_EQ(2U, service->extensions()->size()); | 233 ASSERT_EQ(2U, service->extensions()->size()); |
| 226 ASSERT_EQ(1U, model->size()); | 234 ASSERT_EQ(1U, model->size()); |
| 227 } | 235 } |
| 228 | 236 |
| 229 typedef std::set<scoped_refptr<Extension> > ExtensionCollection; | 237 typedef std::set<scoped_refptr<Extension> > ExtensionCollection; |
| 230 | 238 |
| 231 namespace { | 239 namespace { |
| 232 void AddExtension(ExtensionService* service, | 240 void AddExtension(ExtensionService* service, |
| 233 ExtensionCollection* extensions, | 241 ExtensionCollection* extensions, |
| 234 BackgroundApplicationListModel* model, | 242 BackgroundApplicationListModel* model, |
| (...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 353 break; | 361 break; |
| 354 case 2: | 362 case 2: |
| 355 TogglePermission(service, &extensions, model.get(), &expected, &count); | 363 TogglePermission(service, &extensions, model.get(), &expected, &count); |
| 356 break; | 364 break; |
| 357 default: | 365 default: |
| 358 NOTREACHED(); | 366 NOTREACHED(); |
| 359 break; | 367 break; |
| 360 } | 368 } |
| 361 } | 369 } |
| 362 } | 370 } |
| OLD | NEW |