Chromium Code Reviews| 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 #include "base/files/file_path.h" | 5 #include "base/files/file_path.h" |
| 6 #include "base/json/json_file_value_serializer.h" | 6 #include "base/json/json_file_value_serializer.h" |
| 7 #include "base/memory/ref_counted.h" | 7 #include "base/memory/ref_counted.h" |
| 8 #include "base/path_service.h" | 8 #include "base/path_service.h" |
| 9 #include "base/run_loop.h" | 9 #include "base/run_loop.h" |
| 10 #if defined(OS_WIN) | |
| 11 #include "base/string_util.h" | |
| 12 #endif | |
| 10 #include "base/values.h" | 13 #include "base/values.h" |
| 11 #include "chrome/browser/extensions/extension_service.h" | 14 #include "chrome/browser/extensions/extension_service.h" |
| 12 #include "chrome/browser/extensions/extension_service_unittest.h" | 15 #include "chrome/browser/extensions/extension_service_unittest.h" |
| 13 #include "chrome/browser/extensions/permissions_updater.h" | 16 #include "chrome/browser/extensions/permissions_updater.h" |
| 14 #include "chrome/common/chrome_notification_types.h" | 17 #include "chrome/common/chrome_notification_types.h" |
| 15 #include "chrome/common/chrome_paths.h" | 18 #include "chrome/common/chrome_paths.h" |
| 16 #include "chrome/common/extensions/extension.h" | 19 #include "chrome/common/extensions/extension.h" |
| 20 #include "chrome/common/extensions/extension_test_util.h" | |
| 17 #include "chrome/common/extensions/permissions/permission_set.h" | 21 #include "chrome/common/extensions/permissions/permission_set.h" |
| 18 #include "chrome/test/base/testing_profile.h" | 22 #include "chrome/test/base/testing_profile.h" |
| 19 #include "content/public/browser/notification_observer.h" | 23 #include "content/public/browser/notification_observer.h" |
| 20 #include "content/public/browser/notification_registrar.h" | 24 #include "content/public/browser/notification_registrar.h" |
| 21 #include "content/public/browser/notification_service.h" | 25 #include "content/public/browser/notification_service.h" |
| 22 #include "testing/gtest/include/gtest/gtest.h" | 26 #include "testing/gtest/include/gtest/gtest.h" |
| 23 | 27 |
| 28 using extension_test_util::LoadManifest; | |
| 29 | |
| 24 namespace extensions { | 30 namespace extensions { |
| 25 | 31 |
| 26 namespace { | 32 namespace { |
| 27 | 33 |
| 28 // A helper class that listens for NOTIFICATION_EXTENSION_PERMISSIONS_UPDATED. | 34 // A helper class that listens for NOTIFICATION_EXTENSION_PERMISSIONS_UPDATED. |
| 29 class PermissionsUpdaterListener : public content::NotificationObserver { | 35 class PermissionsUpdaterListener : public content::NotificationObserver { |
| 30 public: | 36 public: |
| 31 PermissionsUpdaterListener() | 37 PermissionsUpdaterListener() |
| 32 : received_notification_(false), waiting_(false) { | 38 : received_notification_(false), waiting_(false) { |
| 33 registrar_.Add(this, | 39 registrar_.Add(this, |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 80 bool waiting_; | 86 bool waiting_; |
| 81 content::NotificationRegistrar registrar_; | 87 content::NotificationRegistrar registrar_; |
| 82 scoped_refptr<const Extension> extension_; | 88 scoped_refptr<const Extension> extension_; |
| 83 scoped_refptr<const PermissionSet> permissions_; | 89 scoped_refptr<const PermissionSet> permissions_; |
| 84 UpdatedExtensionPermissionsInfo::Reason reason_; | 90 UpdatedExtensionPermissionsInfo::Reason reason_; |
| 85 }; | 91 }; |
| 86 | 92 |
| 87 class PermissionsUpdaterTest : public ExtensionServiceTestBase { | 93 class PermissionsUpdaterTest : public ExtensionServiceTestBase { |
| 88 }; | 94 }; |
| 89 | 95 |
| 90 scoped_refptr<Extension> LoadManifest(std::string* error) { | 96 scoped_refptr<Extension> LoadOurManifest() { |
| 91 base::FilePath path; | 97 base::FilePath path; |
| 92 PathService::Get(chrome::DIR_TEST_DATA, &path); | 98 path = path.AppendASCII("api_test") |
| 93 path = path.AppendASCII("extensions") | |
| 94 .AppendASCII("api_test") | |
| 95 .AppendASCII("permissions") | 99 .AppendASCII("permissions") |
| 96 .AppendASCII("optional") | 100 .AppendASCII("optional"); |
| 97 .AppendASCII("manifest.json"); | 101 #if defined(OS_WIN) |
|
Matt Perry
2013/04/25 19:59:21
Just use FilePath::AsUTF8Unsafe()
vabr (Chromium)
2013/04/26 09:58:52
Done.
| |
| 98 | 102 // In this test, the paths are in ASCII anyway. |
| 99 JSONFileValueSerializer serializer(path); | 103 const std::string path_string(WideToASCII(path.value())); |
| 100 scoped_ptr<Value> result(serializer.Deserialize(NULL, error)); | 104 #else |
| 101 if (!result) | 105 const std::string& path_string(path.value()); |
| 102 return NULL; | 106 #endif |
| 103 | 107 return LoadManifest( |
| 104 scoped_refptr<Extension> extension = Extension::Create( | 108 path_string, "manifest.json", Manifest::INTERNAL, Extension::NO_FLAGS); |
| 105 path.DirName(), Manifest::INTERNAL, | |
| 106 *static_cast<DictionaryValue*>(result.get()), Extension::NO_FLAGS, error); | |
| 107 return extension; | |
| 108 } | 109 } |
| 109 | 110 |
| 110 void AddPattern(URLPatternSet* extent, const std::string& pattern) { | 111 void AddPattern(URLPatternSet* extent, const std::string& pattern) { |
| 111 int schemes = URLPattern::SCHEME_ALL; | 112 int schemes = URLPattern::SCHEME_ALL; |
| 112 extent->AddPattern(URLPattern(schemes, pattern)); | 113 extent->AddPattern(URLPattern(schemes, pattern)); |
| 113 } | 114 } |
| 114 | 115 |
| 115 } // namespace | 116 } // namespace |
| 116 | 117 |
| 117 // Test that the PermissionUpdater can correctly add and remove active | 118 // Test that the PermissionUpdater can correctly add and remove active |
| 118 // permissions. This tests all of PermissionsUpdater's public methods because | 119 // permissions. This tests all of PermissionsUpdater's public methods because |
| 119 // GrantActivePermissions and UpdateActivePermissions are used by | 120 // GrantActivePermissions and UpdateActivePermissions are used by |
| 120 // AddPermissions. | 121 // AddPermissions. |
| 121 TEST_F(PermissionsUpdaterTest, AddAndRemovePermissions) { | 122 TEST_F(PermissionsUpdaterTest, AddAndRemovePermissions) { |
| 122 InitializeEmptyExtensionService(); | 123 InitializeEmptyExtensionService(); |
| 123 | 124 |
| 124 // Load the test extension. | 125 // Load the test extension. |
| 125 std::string error; | 126 scoped_refptr<Extension> extension = LoadOurManifest(); |
| 126 scoped_refptr<Extension> extension = LoadManifest(&error); | 127 ASSERT_TRUE(extension.get()); |
| 127 ASSERT_TRUE(error.empty()) << error; | |
| 128 | 128 |
| 129 APIPermissionSet default_apis; | 129 APIPermissionSet default_apis; |
| 130 default_apis.insert(APIPermission::kManagement); | 130 default_apis.insert(APIPermission::kManagement); |
| 131 URLPatternSet default_hosts; | 131 URLPatternSet default_hosts; |
| 132 AddPattern(&default_hosts, "http://a.com/*"); | 132 AddPattern(&default_hosts, "http://a.com/*"); |
| 133 scoped_refptr<PermissionSet> default_permissions = | 133 scoped_refptr<PermissionSet> default_permissions = |
| 134 new PermissionSet(default_apis, default_hosts, URLPatternSet()); | 134 new PermissionSet(default_apis, default_hosts, URLPatternSet()); |
| 135 | 135 |
| 136 // Make sure it loaded properly. | 136 // Make sure it loaded properly. |
| 137 scoped_refptr<const PermissionSet> permissions = | 137 scoped_refptr<const PermissionSet> permissions = |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 202 // Verify that the extension prefs hold the new active permissions and the | 202 // Verify that the extension prefs hold the new active permissions and the |
| 203 // same granted permissions. | 203 // same granted permissions. |
| 204 from_prefs = prefs->GetActivePermissions(extension->id()); | 204 from_prefs = prefs->GetActivePermissions(extension->id()); |
| 205 ASSERT_EQ(*active_permissions, *from_prefs); | 205 ASSERT_EQ(*active_permissions, *from_prefs); |
| 206 | 206 |
| 207 from_prefs = prefs->GetGrantedPermissions(extension->id()); | 207 from_prefs = prefs->GetGrantedPermissions(extension->id()); |
| 208 ASSERT_EQ(*granted_permissions, *from_prefs); | 208 ASSERT_EQ(*granted_permissions, *from_prefs); |
| 209 } | 209 } |
| 210 | 210 |
| 211 } // namespace extensions | 211 } // namespace extensions |
| OLD | NEW |