OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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/bind.h" | 5 #include "base/bind.h" |
6 #include "base/guid.h" | 6 #include "base/guid.h" |
7 #include "base/json/json_reader.h" | 7 #include "base/json/json_reader.h" |
8 #include "base/json/json_writer.h" | 8 #include "base/json/json_writer.h" |
9 #include "base/strings/string_util.h" | 9 #include "base/strings/string_util.h" |
10 #include "base/values.h" | 10 #include "base/values.h" |
11 #include "content/public/child/v8_value_converter.h" | 11 #include "content/public/child/v8_value_converter.h" |
12 #include "extensions/renderer/api_binding_test_util.h" | 12 #include "extensions/renderer/api_binding_test_util.h" |
13 #include "extensions/renderer/api_request_handler.h" | 13 #include "extensions/renderer/api_request_handler.h" |
14 #include "gin/converter.h" | 14 #include "gin/converter.h" |
15 #include "gin/public/context_holder.h" | 15 #include "gin/public/context_holder.h" |
16 #include "gin/public/isolate_holder.h" | 16 #include "gin/public/isolate_holder.h" |
17 #include "gin/test/v8_test.h" | 17 #include "gin/test/v8_test.h" |
18 #include "gin/try_catch.h" | 18 #include "gin/try_catch.h" |
19 #include "testing/gmock/include/gmock/gmock.h" | 19 #include "testing/gmock/include/gmock/gmock.h" |
20 | 20 |
21 namespace extensions { | 21 namespace extensions { |
22 | 22 |
23 namespace { | 23 namespace { |
24 | 24 |
25 const char kEchoArgs[] = | 25 const char kEchoArgs[] = |
26 "(function() { this.result = Array.from(arguments); })"; | 26 "(function() { this.result = Array.from(arguments); })"; |
27 | 27 |
28 std::unique_ptr<base::Value> GetResultFromContext( | |
29 v8::Isolate* isolate, | |
30 v8::Local<v8::Context> context) { | |
31 v8::Context::Scope context_scope(context); | |
32 v8::Local<v8::Value> res; | |
33 EXPECT_TRUE(context->Global() | |
34 ->Get(context, gin::StringToV8(isolate, "result")) | |
35 .ToLocal(&res)); | |
36 return V8ToBaseValue(res, context); | |
37 } | |
38 | |
39 } // namespace | 28 } // namespace |
40 | 29 |
41 class APIRequestHandlerTest : public gin::V8Test { | 30 class APIRequestHandlerTest : public gin::V8Test { |
42 public: | 31 public: |
43 // Runs the given |function|. | 32 // Runs the given |function|. |
44 void RunJS(v8::Local<v8::Function> function, | 33 void RunJS(v8::Local<v8::Function> function, |
45 v8::Local<v8::Context> context, | 34 v8::Local<v8::Context> context, |
46 int argc, | 35 int argc, |
47 v8::Local<v8::Value> argv[]) { | 36 v8::Local<v8::Value> argv[]) { |
48 EXPECT_FALSE( | 37 RunFunctionOnGlobal(function, context, argc, argv); |
49 function->Call(context, context->Global(), argc, argv).IsEmpty()); | |
50 did_run_js_ = true; | 38 did_run_js_ = true; |
51 } | 39 } |
52 | 40 |
53 protected: | 41 protected: |
54 APIRequestHandlerTest() {} | 42 APIRequestHandlerTest() {} |
55 ~APIRequestHandlerTest() override {} | 43 ~APIRequestHandlerTest() override {} |
56 | 44 |
57 bool did_run_js() const { return did_run_js_; } | 45 bool did_run_js() const { return did_run_js_; } |
58 | 46 |
59 private: | 47 private: |
(...skipping 24 matching lines...) Expand all Loading... |
84 testing::UnorderedElementsAre(request_id)); | 72 testing::UnorderedElementsAre(request_id)); |
85 | 73 |
86 const char kArguments[] = "['foo',1,{'prop1':'bar'}]"; | 74 const char kArguments[] = "['foo',1,{'prop1':'bar'}]"; |
87 std::unique_ptr<base::ListValue> response_arguments = | 75 std::unique_ptr<base::ListValue> response_arguments = |
88 ListValueFromString(kArguments); | 76 ListValueFromString(kArguments); |
89 ASSERT_TRUE(response_arguments); | 77 ASSERT_TRUE(response_arguments); |
90 request_handler.CompleteRequest(request_id, *response_arguments); | 78 request_handler.CompleteRequest(request_id, *response_arguments); |
91 | 79 |
92 EXPECT_TRUE(did_run_js()); | 80 EXPECT_TRUE(did_run_js()); |
93 std::unique_ptr<base::Value> result_value = | 81 std::unique_ptr<base::Value> result_value = |
94 GetResultFromContext(isolate, context); | 82 GetBaseValuePropertyFromObject(context->Global(), context, "result"); |
95 ASSERT_TRUE(result_value); | 83 ASSERT_TRUE(result_value); |
96 EXPECT_EQ(ReplaceSingleQuotes(kArguments), ValueToString(*result_value)); | 84 EXPECT_EQ(ReplaceSingleQuotes(kArguments), ValueToString(*result_value)); |
97 | 85 |
98 EXPECT_TRUE(request_handler.GetPendingRequestIdsForTesting().empty()); | 86 EXPECT_TRUE(request_handler.GetPendingRequestIdsForTesting().empty()); |
99 } | 87 } |
100 | 88 |
101 // Tests that trying to run non-existent or invalided requests is a no-op. | 89 // Tests that trying to run non-existent or invalided requests is a no-op. |
102 TEST_F(APIRequestHandlerTest, InvalidRequestsTest) { | 90 TEST_F(APIRequestHandlerTest, InvalidRequestsTest) { |
103 v8::Isolate* isolate = instance_->isolate(); | 91 v8::Isolate* isolate = instance_->isolate(); |
104 v8::HandleScope handle_scope(instance_->isolate()); | 92 v8::HandleScope handle_scope(instance_->isolate()); |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
160 std::unique_ptr<base::ListValue> response_a = | 148 std::unique_ptr<base::ListValue> response_a = |
161 ListValueFromString("['response_a:']"); | 149 ListValueFromString("['response_a:']"); |
162 ASSERT_TRUE(response_a); | 150 ASSERT_TRUE(response_a); |
163 | 151 |
164 request_handler.CompleteRequest(request_a, *response_a); | 152 request_handler.CompleteRequest(request_a, *response_a); |
165 EXPECT_TRUE(did_run_js()); | 153 EXPECT_TRUE(did_run_js()); |
166 EXPECT_THAT(request_handler.GetPendingRequestIdsForTesting(), | 154 EXPECT_THAT(request_handler.GetPendingRequestIdsForTesting(), |
167 testing::UnorderedElementsAre(request_b)); | 155 testing::UnorderedElementsAre(request_b)); |
168 | 156 |
169 std::unique_ptr<base::Value> result_a = | 157 std::unique_ptr<base::Value> result_a = |
170 GetResultFromContext(isolate, context_a); | 158 GetBaseValuePropertyFromObject(context_a->Global(), context_a, "result"); |
171 ASSERT_TRUE(result_a); | 159 ASSERT_TRUE(result_a); |
172 EXPECT_EQ(ReplaceSingleQuotes("'response_a:alpha'"), | 160 EXPECT_EQ(ReplaceSingleQuotes("'response_a:alpha'"), |
173 ValueToString(*result_a)); | 161 ValueToString(*result_a)); |
174 | 162 |
175 std::unique_ptr<base::ListValue> response_b = | 163 std::unique_ptr<base::ListValue> response_b = |
176 ListValueFromString("['response_b:']"); | 164 ListValueFromString("['response_b:']"); |
177 ASSERT_TRUE(response_b); | 165 ASSERT_TRUE(response_b); |
178 | 166 |
179 request_handler.CompleteRequest(request_b, *response_b); | 167 request_handler.CompleteRequest(request_b, *response_b); |
180 EXPECT_TRUE(request_handler.GetPendingRequestIdsForTesting().empty()); | 168 EXPECT_TRUE(request_handler.GetPendingRequestIdsForTesting().empty()); |
181 | 169 |
182 std::unique_ptr<base::Value> result_b = | 170 std::unique_ptr<base::Value> result_b = |
183 GetResultFromContext(isolate, context_b); | 171 GetBaseValuePropertyFromObject(context_b->Global(), context_b, "result"); |
184 ASSERT_TRUE(result_b); | 172 ASSERT_TRUE(result_b); |
185 EXPECT_EQ(ReplaceSingleQuotes("'response_b:beta'"), ValueToString(*result_b)); | 173 EXPECT_EQ(ReplaceSingleQuotes("'response_b:beta'"), ValueToString(*result_b)); |
186 } | 174 } |
187 | 175 |
188 } // namespace extensions | 176 } // namespace extensions |
OLD | NEW |