extension_ui_unittest.cc revision 3345a6884c488ff3a535c2c9acdd33d74b37e311
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    Extension extension(path);
37    std::string error;
38
39    FilePath manifest_path = extension_path.Append(
40        Extension::kManifestFilename);
41    scoped_ptr<DictionaryValue> extension_data(DeserializeJSONTestData(
42        manifest_path, &error));
43    EXPECT_EQ("", error);
44    EXPECT_TRUE(extension.InitFromValue(*extension_data, true, &error));
45    EXPECT_EQ("", error);
46
47    scoped_ptr<DictionaryValue> expected_output_data(DeserializeJSONTestData(
48        expected_output_path, &error));
49    EXPECT_EQ("", error);
50
51    // Produce test output.
52    scoped_ptr<DictionaryValue> actual_output_data(
53        ExtensionsDOMHandler::CreateExtensionDetailValue(NULL, &extension,
54                                                         pages, true));
55
56    // Compare the outputs.
57    return expected_output_data->Equals(actual_output_data.get());
58  }
59}  // namespace
60
61TEST(ExtensionUITest, GenerateExtensionsJSONData) {
62  FilePath data_test_dir_path, extension_path, expected_output_path;
63  EXPECT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &data_test_dir_path));
64
65  // Test Extension1
66  extension_path = data_test_dir_path.AppendASCII("extensions")
67      .AppendASCII("good")
68      .AppendASCII("Extensions")
69      .AppendASCII("behllobkkfkfnphdnhnkndlbkcpglgmj")
70      .AppendASCII("1.0.0.0");
71
72  std::vector<ExtensionPage> pages;
73  pages.push_back(ExtensionPage(
74      GURL("chrome-extension://behllobkkfkfnphdnhnkndlbkcpglgmj/bar.html"),
75      42, 88, false));
76  pages.push_back(ExtensionPage(
77      GURL("chrome-extension://behllobkkfkfnphdnhnkndlbkcpglgmj/dog.html"),
78      0, 0, false));
79
80  expected_output_path = data_test_dir_path.AppendASCII("extensions")
81      .AppendASCII("ui")
82      .AppendASCII("create_extension_detail_value_expected_output")
83      .AppendASCII("good-extension1.json");
84
85  EXPECT_TRUE(CompareExpectedAndActualOutput(extension_path, pages,
86      expected_output_path)) << extension_path.value();
87
88#if !defined(OS_CHROMEOS)
89  // Test Extension2
90  extension_path = data_test_dir_path.AppendASCII("extensions")
91      .AppendASCII("good")
92      .AppendASCII("Extensions")
93      .AppendASCII("hpiknbiabeeppbpihjehijgoemciehgk")
94      .AppendASCII("2");
95
96  expected_output_path = data_test_dir_path.AppendASCII("extensions")
97      .AppendASCII("ui")
98      .AppendASCII("create_extension_detail_value_expected_output")
99      .AppendASCII("good-extension2.json");
100
101  // It's OK to have duplicate URLs, so long as the IDs are different.
102  pages[1].url = pages[0].url;
103
104  EXPECT_TRUE(CompareExpectedAndActualOutput(extension_path, pages,
105      expected_output_path)) << extension_path.value();
106#endif
107
108  // Test Extension3
109  extension_path = data_test_dir_path.AppendASCII("extensions")
110      .AppendASCII("good")
111      .AppendASCII("Extensions")
112      .AppendASCII("bjafgdebaacbbbecmhlhpofkepfkgcpa")
113      .AppendASCII("1.0");
114
115  expected_output_path = data_test_dir_path.AppendASCII("extensions")
116      .AppendASCII("ui")
117      .AppendASCII("create_extension_detail_value_expected_output")
118      .AppendASCII("good-extension3.json");
119
120  pages.clear();
121
122  EXPECT_TRUE(CompareExpectedAndActualOutput(extension_path, pages,
123      expected_output_path)) << extension_path.value();
124}
125