extension_ui_unittest.cc revision 72a454cd3513ac24fbdd0e0cb9ad70b86a99b801
1// Copyright (c) 2006-2009 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 "base/path_service.h"
6#include "base/string_util.h"
7#include "chrome/browser/extensions/extensions_ui.h"
8#include "chrome/common/chrome_paths.h"
9#include "chrome/common/extensions/extension.h"
10#include "chrome/common/json_value_serializer.h"
11#include "testing/gtest/include/gtest/gtest.h"
12
13namespace {
14  static DictionaryValue* DeserializeJSONTestData(const FilePath& path,
15      std::string *error) {
16    Value* value;
17
18    JSONFileValueSerializer serializer(path);
19    value = serializer.Deserialize(NULL, error);
20
21    return static_cast<DictionaryValue*>(value);
22  }
23
24  static bool CompareExpectedAndActualOutput(
25      const FilePath& extension_path,
26      const std::vector<ExtensionPage>& pages,
27      const FilePath& expected_output_path) {
28    // TODO(rafaelw): Using the extension_path passed in above, causes this
29    // unit test to fail on linux. The Values come back valid, but the
30    // UserScript.path() values return "".
31#if defined(OS_WIN)
32    FilePath path(FILE_PATH_LITERAL("c:\\foo"));
33#elif defined(OS_POSIX)
34    FilePath path(FILE_PATH_LITERAL("/foo"));
35#endif
36    std::string error;
37
38    FilePath manifest_path = extension_path.Append(
39        Extension::kManifestFilename);
40    scoped_ptr<DictionaryValue> extension_data(DeserializeJSONTestData(
41        manifest_path, &error));
42    EXPECT_EQ("", error);
43
44    scoped_refptr<Extension> extension(Extension::Create(
45        path, Extension::INVALID, *extension_data, true, &error));
46    EXPECT_TRUE(extension.get());
47    EXPECT_EQ("", error);
48
49    scoped_ptr<DictionaryValue> expected_output_data(DeserializeJSONTestData(
50        expected_output_path, &error));
51    EXPECT_EQ("", error);
52
53    // Produce test output.
54    scoped_ptr<DictionaryValue> actual_output_data(
55        ExtensionsDOMHandler::CreateExtensionDetailValue(NULL, extension.get(),
56                                                         pages, true, false));
57
58    // Compare the outputs.
59    return expected_output_data->Equals(actual_output_data.get());
60  }
61}  // namespace
62
63TEST(ExtensionUITest, GenerateExtensionsJSONData) {
64  FilePath data_test_dir_path, extension_path, expected_output_path;
65  EXPECT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &data_test_dir_path));
66
67  // Test Extension1
68  extension_path = data_test_dir_path.AppendASCII("extensions")
69      .AppendASCII("good")
70      .AppendASCII("Extensions")
71      .AppendASCII("behllobkkfkfnphdnhnkndlbkcpglgmj")
72      .AppendASCII("1.0.0.0");
73
74  std::vector<ExtensionPage> pages;
75  pages.push_back(ExtensionPage(
76      GURL("chrome-extension://behllobkkfkfnphdnhnkndlbkcpglgmj/bar.html"),
77      42, 88, false));
78  pages.push_back(ExtensionPage(
79      GURL("chrome-extension://behllobkkfkfnphdnhnkndlbkcpglgmj/dog.html"),
80      0, 0, false));
81
82  expected_output_path = data_test_dir_path.AppendASCII("extensions")
83      .AppendASCII("ui")
84      .AppendASCII("create_extension_detail_value_expected_output")
85      .AppendASCII("good-extension1.json");
86
87  EXPECT_TRUE(CompareExpectedAndActualOutput(extension_path, pages,
88      expected_output_path)) << extension_path.value();
89
90#if !defined(OS_CHROMEOS)
91  // Test Extension2
92  extension_path = data_test_dir_path.AppendASCII("extensions")
93      .AppendASCII("good")
94      .AppendASCII("Extensions")
95      .AppendASCII("hpiknbiabeeppbpihjehijgoemciehgk")
96      .AppendASCII("2");
97
98  expected_output_path = data_test_dir_path.AppendASCII("extensions")
99      .AppendASCII("ui")
100      .AppendASCII("create_extension_detail_value_expected_output")
101      .AppendASCII("good-extension2.json");
102
103  // It's OK to have duplicate URLs, so long as the IDs are different.
104  pages[1].url = pages[0].url;
105
106  EXPECT_TRUE(CompareExpectedAndActualOutput(extension_path, pages,
107      expected_output_path)) << extension_path.value();
108#endif
109
110  // Test Extension3
111  extension_path = data_test_dir_path.AppendASCII("extensions")
112      .AppendASCII("good")
113      .AppendASCII("Extensions")
114      .AppendASCII("bjafgdebaacbbbecmhlhpofkepfkgcpa")
115      .AppendASCII("1.0");
116
117  expected_output_path = data_test_dir_path.AppendASCII("extensions")
118      .AppendASCII("ui")
119      .AppendASCII("create_extension_detail_value_expected_output")
120      .AppendASCII("good-extension3.json");
121
122  pages.clear();
123
124  EXPECT_TRUE(CompareExpectedAndActualOutput(extension_path, pages,
125      expected_output_path)) << extension_path.value();
126}
127