extension_manifest_test.h revision 5821806d5e7f356e8fa4b058a389a808ea183019
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
3// found in the LICENSE file.
4
5#ifndef CHROME_COMMON_EXTENSIONS_MANIFEST_TESTS_EXTENSION_MANIFEST_TEST_H_
6#define CHROME_COMMON_EXTENSIONS_MANIFEST_TESTS_EXTENSION_MANIFEST_TEST_H_
7
8#include "base/values.h"
9#include "base/memory/scoped_ptr.h"
10#include "chrome/common/extensions/extension.h"
11#include "chrome/common/extensions/extension_manifest_constants.h"
12#include "chrome/common/extensions/features/feature.h"
13#include "testing/gtest/include/gtest/gtest.h"
14
15namespace errors = extension_manifest_errors;
16namespace keys = extension_manifest_keys;
17
18class ExtensionManifestTest : public testing::Test {
19 public:
20  ExtensionManifestTest();
21
22 protected:
23  // If filename is a relative path, LoadManifestFile will treat it relative to
24  // the appropriate test directory.
25  static DictionaryValue* LoadManifestFile(const std::string& filename,
26                                           std::string* error);
27
28  // Helper class that simplifies creating methods that take either a filename
29  // to a manifest or the manifest itself.
30  class Manifest {
31   public:
32    explicit Manifest(const char* name);
33    Manifest(DictionaryValue* manifest, const char* name);
34    // C++98 requires the copy constructor for a type to be visible if you
35    // take a const-ref of a temporary for that type.  Since Manifest
36    // contains a scoped_ptr, its implicit copy constructor is declared
37    // Manifest(Manifest&) according to spec 12.8.5.  This breaks the first
38    // requirement and thus you cannot use it with LoadAndExpectError() or
39    // LoadAndExpectSuccess() easily.
40    //
41    // To get around this spec pedantry, we declare the copy constructor
42    // explicitly.  It will never get invoked.
43    Manifest(const Manifest& m);
44
45    ~Manifest();
46
47    const std::string& name() const { return name_; };
48
49    DictionaryValue* GetManifest(std::string* error) const;
50
51   private:
52    const std::string name_;
53    mutable DictionaryValue* manifest_;
54    mutable scoped_ptr<DictionaryValue> manifest_holder_;
55  };
56
57  scoped_refptr<extensions::Extension> LoadExtension(
58      const Manifest& manifest,
59      std::string* error,
60      extensions::Extension::Location location =
61          extensions::Extension::INTERNAL,
62      int flags = extensions::Extension::NO_FLAGS);
63
64  scoped_refptr<extensions::Extension> LoadAndExpectSuccess(
65      const Manifest& manifest,
66      extensions::Extension::Location location =
67          extensions::Extension::INTERNAL,
68      int flags = extensions::Extension::NO_FLAGS);
69
70  scoped_refptr<extensions::Extension> LoadAndExpectSuccess(
71      char const* manifest_name,
72      extensions::Extension::Location location =
73          extensions::Extension::INTERNAL,
74      int flags = extensions::Extension::NO_FLAGS);
75
76  scoped_refptr<extensions::Extension> LoadAndExpectWarning(
77      const Manifest& manifest,
78      const std::string& expected_error,
79      extensions::Extension::Location location =
80          extensions::Extension::INTERNAL,
81      int flags = extensions::Extension::NO_FLAGS);
82
83  scoped_refptr<extensions::Extension> LoadAndExpectWarning(
84      char const* manifest_name,
85      const std::string& expected_error,
86      extensions::Extension::Location location =
87          extensions::Extension::INTERNAL,
88      int flags = extensions::Extension::NO_FLAGS);
89
90  void VerifyExpectedError(extensions::Extension* extension,
91                           const std::string& name,
92                           const std::string& error,
93                           const std::string& expected_error);
94
95  void LoadAndExpectError(char const* manifest_name,
96                          const std::string& expected_error,
97                          extensions::Extension::Location location =
98                              extensions::Extension::INTERNAL,
99                          int flags = extensions::Extension::NO_FLAGS);
100
101  void LoadAndExpectError(const Manifest& manifest,
102                          const std::string& expected_error,
103                          extensions::Extension::Location location =
104                              extensions::Extension::INTERNAL,
105                          int flags = extensions::Extension::NO_FLAGS);
106
107  void AddPattern(URLPatternSet* extent, const std::string& pattern);
108
109  // used to differentiate between calls to LoadAndExpectError,
110  // LoadAndExpectWarning and LoadAndExpectSuccess via function RunTestcases.
111  enum EXPECT_TYPE {
112    EXPECT_TYPE_ERROR,
113    EXPECT_TYPE_WARNING,
114    EXPECT_TYPE_SUCCESS
115  };
116
117  struct Testcase {
118    std::string manifest_filename_;
119    std::string expected_error_; // only used for ExpectedError tests
120    extensions::Extension::Location location_;
121    int flags_;
122
123    Testcase(std::string manifest_filename, std::string expected_error,
124        extensions::Extension::Location location, int flags);
125
126    Testcase(std::string manifest_filename, std::string expected_error);
127
128    explicit Testcase(std::string manifest_filename);
129
130    Testcase(std::string manifest_filename,
131             extensions::Extension::Location location,
132             int flags);
133  };
134
135  void RunTestcases(const Testcase* testcases, size_t num_testcases,
136      EXPECT_TYPE type);
137
138  bool enable_apps_;
139
140  // Force the manifest tests to run as though they are on trunk, since several
141  // tests rely on manifest features being available that aren't on
142  // stable/beta.
143  //
144  // These objects nest, so if a test wants to explicitly test the behaviour
145  // on stable or beta, declare it inside that test.
146  extensions::Feature::ScopedCurrentChannel current_channel_;
147};
148
149#endif  // CHROME_COMMON_EXTENSIONS_MANIFEST_TESTS_EXTENSION_MANIFEST_TEST_H_
150