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#include "chrome/common/extensions/extension_test_util.h"
6
7#include "base/files/file_path.h"
8#include "base/json/json_file_value_serializer.h"
9#include "base/path_service.h"
10#include "base/values.h"
11#include "chrome/common/chrome_paths.h"
12#include "extensions/common/extension.h"
13#include "extensions/common/manifest_constants.h"
14#include "testing/gtest/include/gtest/gtest.h"
15
16using extensions::Extension;
17using extensions::Manifest;
18
19namespace extension_test_util {
20
21scoped_refptr<Extension> LoadManifestUnchecked(const std::string& dir,
22                                               const std::string& test_file,
23                                               Manifest::Location location,
24                                               int extra_flags,
25                                               const std::string& id,
26                                               std::string* error) {
27  base::FilePath path;
28  PathService::Get(chrome::DIR_TEST_DATA, &path);
29  path = path.AppendASCII("extensions")
30             .AppendASCII(dir)
31             .AppendASCII(test_file);
32
33  JSONFileValueSerializer serializer(path);
34  scoped_ptr<base::Value> result(serializer.Deserialize(NULL, error));
35  if (!result)
36    return NULL;
37  const base::DictionaryValue* dict;
38  CHECK(result->GetAsDictionary(&dict));
39
40  scoped_refptr<Extension> extension = Extension::Create(
41      path.DirName(), location, *dict, extra_flags, id, error);
42  return extension;
43}
44
45scoped_refptr<Extension> LoadManifestUnchecked(const std::string& dir,
46                                               const std::string& test_file,
47                                               Manifest::Location location,
48                                               int extra_flags,
49                                               std::string* error) {
50  return LoadManifestUnchecked(
51      dir, test_file, location, extra_flags, std::string(), error);
52}
53
54scoped_refptr<Extension> LoadManifest(const std::string& dir,
55                                      const std::string& test_file,
56                                      Manifest::Location location,
57                                      int extra_flags) {
58  std::string error;
59  scoped_refptr<Extension> extension =
60      LoadManifestUnchecked(dir, test_file, location, extra_flags, &error);
61
62  EXPECT_TRUE(extension.get()) << test_file << ":" << error;
63  return extension;
64}
65
66scoped_refptr<Extension> LoadManifest(const std::string& dir,
67                                      const std::string& test_file,
68                                      int extra_flags) {
69  return LoadManifest(dir, test_file, Manifest::INVALID_LOCATION, extra_flags);
70}
71
72scoped_refptr<Extension> LoadManifestStrict(const std::string& dir,
73                                            const std::string& test_file) {
74  return LoadManifest(dir, test_file, Extension::NO_FLAGS);
75}
76
77scoped_refptr<Extension> LoadManifest(const std::string& dir,
78                                      const std::string& test_file) {
79  return LoadManifest(dir, test_file, Extension::NO_FLAGS);
80}
81
82}  // namespace extension_test_util
83