OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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/file_util.h" | 5 #include "base/file_util.h" |
6 #include "base/path_service.h" | 6 #include "base/path_service.h" |
7 #include "base/string_util.h" | 7 #include "base/string_util.h" |
8 #include "chrome/common/chrome_paths.h" | 8 #include "chrome/common/chrome_paths.h" |
9 #include "chrome/test/base/v8_unit_test.h" | 9 #include "chrome/test/base/v8_unit_test.h" |
10 #include "grit/renderer_resources.h" | 10 #include "grit/renderer_resources.h" |
11 #include "testing/gtest/include/gtest/gtest.h" | 11 #include "testing/gtest/include/gtest/gtest.h" |
12 #include "ui/base/resource/resource_bundle.h" | 12 #include "ui/base/resource/resource_bundle.h" |
13 | 13 |
14 static const char kJsonSchema[] = "json_schema.js"; | 14 static const char kJsonSchema[] = "json_schema.js"; |
15 static const char kJsonSchemaTest[] = "json_schema_test.js"; | 15 static const char kJsonSchemaTest[] = "json_schema_test.js"; |
16 | 16 |
17 class JsonSchemaTest : public V8UnitTest { | 17 class JsonSchemaTest : public V8UnitTest { |
18 public: | 18 public: |
19 JsonSchemaTest() {} | 19 JsonSchemaTest() {} |
20 | 20 |
21 virtual void SetUp() { | 21 virtual void SetUp() { |
22 V8UnitTest::SetUp(); | 22 V8UnitTest::SetUp(); |
23 | 23 |
24 // Add the json schema code to the context. | 24 // Add the json schema code to the context. |
25 std::string code = ResourceBundle::GetSharedInstance().GetRawDataResource( | 25 std::string code = ResourceBundle::GetSharedInstance().GetRawDataResource( |
26 IDR_JSON_SCHEMA_JS).as_string(); | 26 IDR_JSON_SCHEMA_JS).as_string(); |
27 | 27 |
28 // This is a nasty hack, but it is easier to test the code if we don't use | 28 // json_schema.js expects to have requireNative() defined. |
29 // it as a v8 extension. So replace the only bit that relies on that with a | 29 ExecuteScriptInContext( |
30 // more easily testable implementation. | 30 "function requireNative(id) {" |
31 ReplaceFirstSubstringAfterOffset(&code, 0, | 31 " return {" |
32 "native function GetChromeHidden();", | 32 " GetChromeHidden: function() { return {}; }," |
33 "function GetChromeHidden() {\n" | 33 " };" |
34 " if (!this.chromeHidden) this.chromeHidden = {};\n" | 34 "}", |
35 " return this.chromeHidden;\n" | 35 "test-code"); |
36 "}"); | |
37 ExecuteScriptInContext(code, kJsonSchema); | 36 ExecuteScriptInContext(code, kJsonSchema); |
38 | 37 |
39 // Add the test functions to the context. | 38 // Add the test functions to the context. |
40 FilePath test_js_file_path; | 39 FilePath test_js_file_path; |
41 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_js_file_path)); | 40 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_js_file_path)); |
42 test_js_file_path = test_js_file_path.AppendASCII("extensions"); | 41 test_js_file_path = test_js_file_path.AppendASCII("extensions"); |
43 test_js_file_path = test_js_file_path.AppendASCII(kJsonSchemaTest); | 42 test_js_file_path = test_js_file_path.AppendASCII(kJsonSchemaTest); |
44 std::string test_js; | 43 std::string test_js; |
45 ASSERT_TRUE(file_util::ReadFileToString(test_js_file_path, &test_js)); | 44 ASSERT_TRUE(file_util::ReadFileToString(test_js_file_path, &test_js)); |
46 ExecuteScriptInContext(test_js, kJsonSchemaTest); | 45 ExecuteScriptInContext(test_js, kJsonSchemaTest); |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
99 TestFunction("testGetAllTypesForSchema"); | 98 TestFunction("testGetAllTypesForSchema"); |
100 } | 99 } |
101 | 100 |
102 TEST_F(JsonSchemaTest, TestIsValidSchemaType) { | 101 TEST_F(JsonSchemaTest, TestIsValidSchemaType) { |
103 TestFunction("testIsValidSchemaType"); | 102 TestFunction("testIsValidSchemaType"); |
104 } | 103 } |
105 | 104 |
106 TEST_F(JsonSchemaTest, TestCheckSchemaOverlap) { | 105 TEST_F(JsonSchemaTest, TestCheckSchemaOverlap) { |
107 TestFunction("testCheckSchemaOverlap"); | 106 TestFunction("testCheckSchemaOverlap"); |
108 } | 107 } |
OLD | NEW |