OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/path_service.h" | 5 #include "base/path_service.h" |
6 #include "base/string_util.h" | 6 #include "base/string_util.h" |
7 #include "chrome/browser/extensions/extensions_ui.h" | 7 #include "chrome/browser/extensions/extensions_ui.h" |
8 #include "chrome/common/chrome_paths.h" | 8 #include "chrome/common/chrome_paths.h" |
9 #include "chrome/common/extensions/extension.h" | 9 #include "chrome/common/extensions/extension.h" |
10 #include "chrome/common/json_value_serializer.h" | 10 #include "chrome/common/json_value_serializer.h" |
11 #include "testing/gtest/include/gtest/gtest.h" | 11 #include "testing/gtest/include/gtest/gtest.h" |
12 | 12 |
13 namespace { | 13 namespace { |
14 static DictionaryValue* DeserializeJSONTestData(const FilePath& path, | 14 static DictionaryValue* DeserializeJSONTestData(const FilePath& path, |
15 std::string *error) { | 15 std::string *error) { |
16 Value* value; | 16 Value* value; |
17 | 17 |
18 JSONFileValueSerializer serializer(path); | 18 JSONFileValueSerializer serializer(path); |
19 value = serializer.Deserialize(NULL, error); | 19 value = serializer.Deserialize(NULL, error); |
20 | 20 |
21 return static_cast<DictionaryValue*>(value); | 21 return static_cast<DictionaryValue*>(value); |
22 } | 22 } |
23 | 23 |
24 static bool CompareExpectedAndActualOutput( | 24 static void CheckExpectedAndActualOutput( |
25 const FilePath& extension_path, | 25 const FilePath& extension_path, |
26 const std::vector<ExtensionPage>& pages, | 26 const std::vector<ExtensionPage>& pages, |
27 const FilePath& expected_output_path) { | 27 const FilePath& expected_output_path) { |
28 // TODO(rafaelw): Using the extension_path passed in above, causes this | 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 | 29 // unit test to fail on linux. The Values come back valid, but the |
30 // UserScript.path() values return "". | 30 // UserScript.path() values return "". |
31 #if defined(OS_WIN) | 31 #if defined(OS_WIN) |
32 FilePath path(FILE_PATH_LITERAL("c:\\foo")); | 32 FilePath path(FILE_PATH_LITERAL("c:\\foo")); |
33 #elif defined(OS_POSIX) | 33 #elif defined(OS_POSIX) |
34 FilePath path(FILE_PATH_LITERAL("/foo")); | 34 FilePath path(FILE_PATH_LITERAL("/foo")); |
(...skipping 14 matching lines...) Expand all Loading... |
49 | 49 |
50 scoped_ptr<DictionaryValue> expected_output_data(DeserializeJSONTestData( | 50 scoped_ptr<DictionaryValue> expected_output_data(DeserializeJSONTestData( |
51 expected_output_path, &error)); | 51 expected_output_path, &error)); |
52 EXPECT_EQ("", error); | 52 EXPECT_EQ("", error); |
53 | 53 |
54 // Produce test output. | 54 // Produce test output. |
55 scoped_ptr<DictionaryValue> actual_output_data( | 55 scoped_ptr<DictionaryValue> actual_output_data( |
56 ExtensionsDOMHandler::CreateExtensionDetailValue(NULL, extension.get(), | 56 ExtensionsDOMHandler::CreateExtensionDetailValue(NULL, extension.get(), |
57 pages, true, false)); | 57 pages, true, false)); |
58 | 58 |
59 // Compare the outputs. | 59 // If the comparison fails, it's easiest to understand why when comparing |
60 return expected_output_data->Equals(actual_output_data.get()); | 60 // the JSON serializations. |
| 61 std::string details = "Expected (" + |
| 62 expected_output_path.MaybeAsASCII() + "):\n"; |
| 63 std::string serialization = ""; |
| 64 JSONStringValueSerializer serializer(&serialization); |
| 65 serializer.set_pretty_print(true); |
| 66 serializer.Serialize(*expected_output_data); |
| 67 details += serialization; |
| 68 |
| 69 details += "\nActual (" + extension_path.MaybeAsASCII() + "):\n"; |
| 70 serialization.clear(); |
| 71 serializer.Serialize(*actual_output_data); |
| 72 details += serialization; |
| 73 |
| 74 EXPECT_TRUE(expected_output_data->Equals(actual_output_data.get())) |
| 75 << details; |
61 } | 76 } |
62 } // namespace | 77 } // namespace |
63 | 78 |
64 TEST(ExtensionUITest, GenerateExtensionsJSONData) { | 79 TEST(ExtensionUITest, GenerateExtensionsJSONData) { |
65 FilePath data_test_dir_path, extension_path, expected_output_path; | 80 FilePath data_test_dir_path, extension_path, expected_output_path; |
66 EXPECT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &data_test_dir_path)); | 81 EXPECT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &data_test_dir_path)); |
67 | 82 |
68 // Test Extension1 | 83 // Test Extension1 |
69 extension_path = data_test_dir_path.AppendASCII("extensions") | 84 extension_path = data_test_dir_path.AppendASCII("extensions") |
70 .AppendASCII("good") | 85 .AppendASCII("good") |
71 .AppendASCII("Extensions") | 86 .AppendASCII("Extensions") |
72 .AppendASCII("behllobkkfkfnphdnhnkndlbkcpglgmj") | 87 .AppendASCII("behllobkkfkfnphdnhnkndlbkcpglgmj") |
73 .AppendASCII("1.0.0.0"); | 88 .AppendASCII("1.0.0.0"); |
74 | 89 |
75 std::vector<ExtensionPage> pages; | 90 std::vector<ExtensionPage> pages; |
76 pages.push_back(ExtensionPage( | 91 pages.push_back(ExtensionPage( |
77 GURL("chrome-extension://behllobkkfkfnphdnhnkndlbkcpglgmj/bar.html"), | 92 GURL("chrome-extension://behllobkkfkfnphdnhnkndlbkcpglgmj/bar.html"), |
78 42, 88, false)); | 93 42, 88, false)); |
79 pages.push_back(ExtensionPage( | 94 pages.push_back(ExtensionPage( |
80 GURL("chrome-extension://behllobkkfkfnphdnhnkndlbkcpglgmj/dog.html"), | 95 GURL("chrome-extension://behllobkkfkfnphdnhnkndlbkcpglgmj/dog.html"), |
81 0, 0, false)); | 96 0, 0, false)); |
82 | 97 |
83 expected_output_path = data_test_dir_path.AppendASCII("extensions") | 98 expected_output_path = data_test_dir_path.AppendASCII("extensions") |
84 .AppendASCII("ui") | 99 .AppendASCII("ui") |
85 .AppendASCII("create_extension_detail_value_expected_output") | 100 .AppendASCII("create_extension_detail_value_expected_output") |
86 .AppendASCII("good-extension1.json"); | 101 .AppendASCII("good-extension1.json"); |
87 | 102 |
88 EXPECT_TRUE(CompareExpectedAndActualOutput(extension_path, pages, | 103 CheckExpectedAndActualOutput(extension_path, pages, expected_output_path); |
89 expected_output_path)) << extension_path.value(); | |
90 | 104 |
91 #if !defined(OS_CHROMEOS) | 105 #if !defined(OS_CHROMEOS) |
92 // Test Extension2 | 106 // Test Extension2 |
93 extension_path = data_test_dir_path.AppendASCII("extensions") | 107 extension_path = data_test_dir_path.AppendASCII("extensions") |
94 .AppendASCII("good") | 108 .AppendASCII("good") |
95 .AppendASCII("Extensions") | 109 .AppendASCII("Extensions") |
96 .AppendASCII("hpiknbiabeeppbpihjehijgoemciehgk") | 110 .AppendASCII("hpiknbiabeeppbpihjehijgoemciehgk") |
97 .AppendASCII("2"); | 111 .AppendASCII("2"); |
98 | 112 |
99 expected_output_path = data_test_dir_path.AppendASCII("extensions") | 113 expected_output_path = data_test_dir_path.AppendASCII("extensions") |
100 .AppendASCII("ui") | 114 .AppendASCII("ui") |
101 .AppendASCII("create_extension_detail_value_expected_output") | 115 .AppendASCII("create_extension_detail_value_expected_output") |
102 .AppendASCII("good-extension2.json"); | 116 .AppendASCII("good-extension2.json"); |
103 | 117 |
104 // It's OK to have duplicate URLs, so long as the IDs are different. | 118 // It's OK to have duplicate URLs, so long as the IDs are different. |
105 pages[1].url = pages[0].url; | 119 pages[1].url = pages[0].url; |
106 | 120 |
107 EXPECT_TRUE(CompareExpectedAndActualOutput(extension_path, pages, | 121 CheckExpectedAndActualOutput(extension_path, pages, expected_output_path); |
108 expected_output_path)) << extension_path.value(); | |
109 #endif | 122 #endif |
110 | 123 |
111 // Test Extension3 | 124 // Test Extension3 |
112 extension_path = data_test_dir_path.AppendASCII("extensions") | 125 extension_path = data_test_dir_path.AppendASCII("extensions") |
113 .AppendASCII("good") | 126 .AppendASCII("good") |
114 .AppendASCII("Extensions") | 127 .AppendASCII("Extensions") |
115 .AppendASCII("bjafgdebaacbbbecmhlhpofkepfkgcpa") | 128 .AppendASCII("bjafgdebaacbbbecmhlhpofkepfkgcpa") |
116 .AppendASCII("1.0"); | 129 .AppendASCII("1.0"); |
117 | 130 |
118 expected_output_path = data_test_dir_path.AppendASCII("extensions") | 131 expected_output_path = data_test_dir_path.AppendASCII("extensions") |
119 .AppendASCII("ui") | 132 .AppendASCII("ui") |
120 .AppendASCII("create_extension_detail_value_expected_output") | 133 .AppendASCII("create_extension_detail_value_expected_output") |
121 .AppendASCII("good-extension3.json"); | 134 .AppendASCII("good-extension3.json"); |
122 | 135 |
123 pages.clear(); | 136 pages.clear(); |
124 | 137 |
125 EXPECT_TRUE(CompareExpectedAndActualOutput(extension_path, pages, | 138 CheckExpectedAndActualOutput(extension_path, pages, expected_output_path); |
126 expected_output_path)) << extension_path.value(); | |
127 } | 139 } |
OLD | NEW |