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