| 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 #ifndef EXTENSIONS_COMMON_MANIFEST_TEST_H_ | 5 #ifndef CHROME_COMMON_EXTENSIONS_MANIFEST_TESTS_EXTENSION_MANIFEST_TEST_H_ |
| 6 #define EXTENSIONS_COMMON_MANIFEST_TEST_H_ | 6 #define CHROME_COMMON_EXTENSIONS_MANIFEST_TESTS_EXTENSION_MANIFEST_TEST_H_ |
| 7 | 7 |
| 8 #include "base/memory/ref_counted.h" | 8 #include "base/memory/ref_counted.h" |
| 9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
| 10 #include "base/values.h" | 10 #include "base/values.h" |
| 11 #include "chrome/common/extensions/features/feature_channel.h" |
| 11 #include "extensions/common/extension.h" | 12 #include "extensions/common/extension.h" |
| 12 #include "extensions/common/manifest.h" | 13 #include "extensions/common/manifest.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
| 14 | 15 |
| 15 namespace base { | 16 class ExtensionManifestTest : public testing::Test { |
| 16 class FilePath; | |
| 17 } | |
| 18 | |
| 19 namespace extensions { | |
| 20 | |
| 21 // Base class for tests that parse a manifest file. | |
| 22 class ManifestTest : public testing::Test { | |
| 23 public: | 17 public: |
| 24 ManifestTest(); | 18 ExtensionManifestTest(); |
| 25 virtual ~ManifestTest(); | |
| 26 | 19 |
| 27 protected: | 20 protected: |
| 28 // Helper class that simplifies creating methods that take either a filename | 21 // Helper class that simplifies creating methods that take either a filename |
| 29 // to a manifest or the manifest itself. | 22 // to a manifest or the manifest itself. |
| 30 class ManifestData { | 23 class Manifest { |
| 31 public: | 24 public: |
| 32 explicit ManifestData(const char* name); | 25 explicit Manifest(const char* name); |
| 33 ManifestData(base::DictionaryValue* manifest, const char* name); | 26 Manifest(base::DictionaryValue* manifest, const char* name); |
| 34 explicit ManifestData(scoped_ptr<base::DictionaryValue> manifest); | 27 explicit Manifest(scoped_ptr<base::DictionaryValue> manifest); |
| 35 // C++98 requires the copy constructor for a type to be visible if you | 28 // C++98 requires the copy constructor for a type to be visible if you |
| 36 // take a const-ref of a temporary for that type. Since Manifest | 29 // take a const-ref of a temporary for that type. Since Manifest |
| 37 // contains a scoped_ptr, its implicit copy constructor is declared | 30 // contains a scoped_ptr, its implicit copy constructor is declared |
| 38 // Manifest(Manifest&) according to spec 12.8.5. This breaks the first | 31 // Manifest(Manifest&) according to spec 12.8.5. This breaks the first |
| 39 // requirement and thus you cannot use it with LoadAndExpectError() or | 32 // requirement and thus you cannot use it with LoadAndExpectError() or |
| 40 // LoadAndExpectSuccess() easily. | 33 // LoadAndExpectSuccess() easily. |
| 41 // | 34 // |
| 42 // To get around this spec pedantry, we declare the copy constructor | 35 // To get around this spec pedantry, we declare the copy constructor |
| 43 // explicitly. It will never get invoked. | 36 // explicitly. It will never get invoked. |
| 44 ManifestData(const ManifestData& m); | 37 Manifest(const Manifest& m); |
| 45 | 38 |
| 46 ~ManifestData(); | 39 ~Manifest(); |
| 47 | 40 |
| 48 const std::string& name() const { return name_; }; | 41 const std::string& name() const { return name_; }; |
| 49 | 42 |
| 50 base::DictionaryValue* GetManifest(base::FilePath manifest_path, | 43 base::DictionaryValue* GetManifest(char const* test_data_dir, |
| 51 std::string* error) const; | 44 std::string* error) const; |
| 52 | 45 |
| 53 private: | 46 private: |
| 54 const std::string name_; | 47 const std::string name_; |
| 55 mutable base::DictionaryValue* manifest_; | 48 mutable base::DictionaryValue* manifest_; |
| 56 mutable scoped_ptr<base::DictionaryValue> manifest_holder_; | 49 mutable scoped_ptr<base::DictionaryValue> manifest_holder_; |
| 57 }; | 50 }; |
| 58 | 51 |
| 59 // Returns the path in which to find test manifest data files, for example | 52 // The subdirectory in which to find test data files. |
| 60 // extensions/test/data/manifest_tests. | 53 virtual char const* test_data_dir(); |
| 61 virtual base::FilePath GetTestDataDir(); | |
| 62 | 54 |
| 63 scoped_ptr<base::DictionaryValue> LoadManifest( | 55 scoped_ptr<base::DictionaryValue> LoadManifest( |
| 64 char const* manifest_name, | 56 char const* manifest_name, |
| 65 std::string* error); | 57 std::string* error); |
| 66 | 58 |
| 67 scoped_refptr<extensions::Extension> LoadExtension( | 59 scoped_refptr<extensions::Extension> LoadExtension( |
| 68 const ManifestData& manifest, | 60 const Manifest& manifest, |
| 69 std::string* error, | 61 std::string* error, |
| 70 extensions::Manifest::Location location = | 62 extensions::Manifest::Location location = |
| 71 extensions::Manifest::INTERNAL, | 63 extensions::Manifest::INTERNAL, |
| 72 int flags = extensions::Extension::NO_FLAGS); | 64 int flags = extensions::Extension::NO_FLAGS); |
| 73 | 65 |
| 74 scoped_refptr<extensions::Extension> LoadAndExpectSuccess( | 66 scoped_refptr<extensions::Extension> LoadAndExpectSuccess( |
| 75 const ManifestData& manifest, | 67 const Manifest& manifest, |
| 76 extensions::Manifest::Location location = | 68 extensions::Manifest::Location location = |
| 77 extensions::Manifest::INTERNAL, | 69 extensions::Manifest::INTERNAL, |
| 78 int flags = extensions::Extension::NO_FLAGS); | 70 int flags = extensions::Extension::NO_FLAGS); |
| 79 | 71 |
| 80 scoped_refptr<extensions::Extension> LoadAndExpectSuccess( | 72 scoped_refptr<extensions::Extension> LoadAndExpectSuccess( |
| 81 char const* manifest_name, | 73 char const* manifest_name, |
| 82 extensions::Manifest::Location location = | 74 extensions::Manifest::Location location = |
| 83 extensions::Manifest::INTERNAL, | 75 extensions::Manifest::INTERNAL, |
| 84 int flags = extensions::Extension::NO_FLAGS); | 76 int flags = extensions::Extension::NO_FLAGS); |
| 85 | 77 |
| 78 // Load and expect success from a manifest provided as a json string. Single |
| 79 // quotes will be replaced with double quotes for test readability. |
| 80 scoped_refptr<extensions::Extension> LoadFromStringAndExpectSuccess( |
| 81 char const* manifest_json); |
| 82 |
| 86 scoped_refptr<extensions::Extension> LoadAndExpectWarning( | 83 scoped_refptr<extensions::Extension> LoadAndExpectWarning( |
| 87 const ManifestData& manifest, | 84 const Manifest& manifest, |
| 88 const std::string& expected_error, | 85 const std::string& expected_error, |
| 89 extensions::Manifest::Location location = | 86 extensions::Manifest::Location location = |
| 90 extensions::Manifest::INTERNAL, | 87 extensions::Manifest::INTERNAL, |
| 91 int flags = extensions::Extension::NO_FLAGS); | 88 int flags = extensions::Extension::NO_FLAGS); |
| 92 | 89 |
| 93 scoped_refptr<extensions::Extension> LoadAndExpectWarning( | 90 scoped_refptr<extensions::Extension> LoadAndExpectWarning( |
| 94 char const* manifest_name, | 91 char const* manifest_name, |
| 95 const std::string& expected_error, | 92 const std::string& expected_error, |
| 96 extensions::Manifest::Location location = | 93 extensions::Manifest::Location location = |
| 97 extensions::Manifest::INTERNAL, | 94 extensions::Manifest::INTERNAL, |
| 98 int flags = extensions::Extension::NO_FLAGS); | 95 int flags = extensions::Extension::NO_FLAGS); |
| 99 | 96 |
| 100 void VerifyExpectedError(extensions::Extension* extension, | 97 void VerifyExpectedError(extensions::Extension* extension, |
| 101 const std::string& name, | 98 const std::string& name, |
| 102 const std::string& error, | 99 const std::string& error, |
| 103 const std::string& expected_error); | 100 const std::string& expected_error); |
| 104 | 101 |
| 105 void LoadAndExpectError(char const* manifest_name, | 102 void LoadAndExpectError(char const* manifest_name, |
| 106 const std::string& expected_error, | 103 const std::string& expected_error, |
| 107 extensions::Manifest::Location location = | 104 extensions::Manifest::Location location = |
| 108 extensions::Manifest::INTERNAL, | 105 extensions::Manifest::INTERNAL, |
| 109 int flags = extensions::Extension::NO_FLAGS); | 106 int flags = extensions::Extension::NO_FLAGS); |
| 110 | 107 |
| 111 void LoadAndExpectError(const ManifestData& manifest, | 108 void LoadAndExpectError(const Manifest& manifest, |
| 112 const std::string& expected_error, | 109 const std::string& expected_error, |
| 113 extensions::Manifest::Location location = | 110 extensions::Manifest::Location location = |
| 114 extensions::Manifest::INTERNAL, | 111 extensions::Manifest::INTERNAL, |
| 115 int flags = extensions::Extension::NO_FLAGS); | 112 int flags = extensions::Extension::NO_FLAGS); |
| 116 | 113 |
| 114 // Load and expect an error from a manifest provided as a json string. Single |
| 115 // quotes will be replaced with double quotes for test readability. |
| 116 void LoadFromStringAndExpectError(char const* manifest_json, |
| 117 const std::string& expected_error); |
| 118 |
| 117 void AddPattern(extensions::URLPatternSet* extent, | 119 void AddPattern(extensions::URLPatternSet* extent, |
| 118 const std::string& pattern); | 120 const std::string& pattern); |
| 119 | 121 |
| 120 // used to differentiate between calls to LoadAndExpectError, | 122 // used to differentiate between calls to LoadAndExpectError, |
| 121 // LoadAndExpectWarning and LoadAndExpectSuccess via function RunTestcases. | 123 // LoadAndExpectWarning and LoadAndExpectSuccess via function RunTestcases. |
| 122 enum ExpectType { | 124 enum ExpectType { |
| 123 EXPECT_TYPE_ERROR, | 125 EXPECT_TYPE_ERROR, |
| 124 EXPECT_TYPE_WARNING, | 126 EXPECT_TYPE_WARNING, |
| 125 EXPECT_TYPE_SUCCESS | 127 EXPECT_TYPE_SUCCESS |
| 126 }; | 128 }; |
| (...skipping 17 matching lines...) Expand all Loading... |
| 144 }; | 146 }; |
| 145 | 147 |
| 146 void RunTestcases(const Testcase* testcases, | 148 void RunTestcases(const Testcase* testcases, |
| 147 size_t num_testcases, | 149 size_t num_testcases, |
| 148 ExpectType type); | 150 ExpectType type); |
| 149 | 151 |
| 150 void RunTestcase(const Testcase& testcase, ExpectType type); | 152 void RunTestcase(const Testcase& testcase, ExpectType type); |
| 151 | 153 |
| 152 bool enable_apps_; | 154 bool enable_apps_; |
| 153 | 155 |
| 154 private: | 156 // Force the manifest tests to run as though they are on trunk, since several |
| 155 DISALLOW_COPY_AND_ASSIGN(ManifestTest); | 157 // tests rely on manifest features being available that aren't on |
| 158 // stable/beta. |
| 159 // |
| 160 // These objects nest, so if a test wants to explicitly test the behaviour |
| 161 // on stable or beta, declare it inside that test. |
| 162 extensions::ScopedCurrentChannel current_channel_; |
| 156 }; | 163 }; |
| 157 | 164 |
| 158 } // namespace extensions | 165 #endif // CHROME_COMMON_EXTENSIONS_MANIFEST_TESTS_EXTENSION_MANIFEST_TEST_H_ |
| 159 | |
| 160 #endif // EXTENSIONS_COMMON_MANIFEST_TEST_H_ | |
| OLD | NEW |