Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(160)

Side by Side Diff: extensions/renderer/api_binding_test_util.cc

Issue 2445223003: [Extensions Bindings] Add more utility functions (Closed)
Patch Set: lazyboy's Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 "extensions/renderer/api_binding_test_util.h" 5 #include "extensions/renderer/api_binding_test_util.h"
6 6
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 "gin/converter.h" 12 #include "gin/converter.h"
13 #include "testing/gtest/include/gtest/gtest.h" 13 #include "testing/gtest/include/gtest/gtest.h"
14 14
15 namespace extensions { 15 namespace extensions {
16 16
17 namespace {
18
19 // Common call function implementation. Calls the given |function| with the
20 // specified |receiver| and arguments. If the call succeeds (doesn't throw an
21 // error), populates |out_value| with the returned result. If the call does
22 // throw, populates |out_error| with the thrown error.
23 // Returns true if the function runs without throwing an error.
24 bool RunFunctionImpl(v8::Local<v8::Function> function,
25 v8::Local<v8::Context> context,
26 v8::Local<v8::Value> receiver,
27 int argc,
28 v8::Local<v8::Value> argv[],
29 v8::Local<v8::Value>* out_value,
30 std::string* out_error) {
31 v8::TryCatch try_catch(context->GetIsolate());
32 v8::MaybeLocal<v8::Value> maybe_result =
33 function->Call(context, receiver, argc, argv);
34 if (try_catch.HasCaught()) {
35 *out_error = gin::V8ToString(try_catch.Message()->Get());
36 return false;
37 }
38 v8::Local<v8::Value> result;
39 if (!maybe_result.ToLocal(&result)) {
40 *out_error = "Could not convert result to v8::Local.";
41 return false;
42 }
43 *out_value = result;
44 return true;
45 }
46
47 } // namespace
48
17 std::string ReplaceSingleQuotes(base::StringPiece str) { 49 std::string ReplaceSingleQuotes(base::StringPiece str) {
18 std::string result; 50 std::string result;
19 base::ReplaceChars(str.as_string(), "'", "\"", &result); 51 base::ReplaceChars(str.as_string(), "'", "\"", &result);
20 return result; 52 return result;
21 } 53 }
22 54
23 std::unique_ptr<base::Value> ValueFromString(base::StringPiece str) { 55 std::unique_ptr<base::Value> ValueFromString(base::StringPiece str) {
24 std::unique_ptr<base::Value> value = 56 std::unique_ptr<base::Value> value =
25 base::JSONReader::Read(ReplaceSingleQuotes(str)); 57 base::JSONReader::Read(ReplaceSingleQuotes(str));
26 EXPECT_TRUE(value) << str; 58 EXPECT_TRUE(value) << str;
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
60 return function; 92 return function;
61 } 93 }
62 94
63 std::unique_ptr<base::Value> V8ToBaseValue(v8::Local<v8::Value> value, 95 std::unique_ptr<base::Value> V8ToBaseValue(v8::Local<v8::Value> value,
64 v8::Local<v8::Context> context) { 96 v8::Local<v8::Context> context) {
65 std::unique_ptr<content::V8ValueConverter> converter( 97 std::unique_ptr<content::V8ValueConverter> converter(
66 content::V8ValueConverter::create()); 98 content::V8ValueConverter::create());
67 return converter->FromV8Value(value, context); 99 return converter->FromV8Value(value, context);
68 } 100 }
69 101
102 v8::Local<v8::Value> RunFunction(v8::Local<v8::Function> function,
103 v8::Local<v8::Context> context,
104 v8::Local<v8::Value> receiver,
105 int argc,
106 v8::Local<v8::Value> argv[]) {
107 std::string error;
108 v8::Local<v8::Value> result;
109 EXPECT_TRUE(
110 RunFunctionImpl(function, context, receiver, argc, argv, &result, &error))
111 << error;
112 EXPECT_FALSE(result.IsEmpty());
113 return result;
114 }
115
116 v8::Local<v8::Value> RunFunction(v8::Local<v8::Function> function,
117 v8::Local<v8::Context> context,
118 int argc,
119 v8::Local<v8::Value> argv[]) {
120 return RunFunction(function, context, v8::Undefined(context->GetIsolate()),
121 argc, argv);
122 }
123
124 v8::Local<v8::Value> RunFunctionOnGlobal(v8::Local<v8::Function> function,
125 v8::Local<v8::Context> context,
126 int argc,
127 v8::Local<v8::Value> argv[]) {
128 return RunFunction(function, context, context->Global(), argc, argv);
129 }
130
131 void RunFunctionOnGlobalAndIgnoreResult(v8::Local<v8::Function> function,
132 v8::Local<v8::Context> context,
133 int argc,
134 v8::Local<v8::Value> argv[]) {
135 RunFunction(function, context, context->Global(), argc, argv);
136 }
137
138 void RunFunctionAndExpectError(v8::Local<v8::Function> function,
139 v8::Local<v8::Context> context,
140 v8::Local<v8::Value> receiver,
141 int argc,
142 v8::Local<v8::Value> argv[],
143 const std::string& expected_error) {
144 std::string error;
145 v8::Local<v8::Value> result;
146 EXPECT_FALSE(RunFunctionImpl(function, context, receiver, argc, argv, &result,
147 &error));
148 EXPECT_TRUE(result.IsEmpty());
149 EXPECT_EQ(expected_error, error);
150 }
151
152 void RunFunctionAndExpectError(v8::Local<v8::Function> function,
153 v8::Local<v8::Context> context,
154 int argc,
155 v8::Local<v8::Value> argv[],
156 const std::string& expected_error) {
157 RunFunctionAndExpectError(function, context,
158 v8::Undefined(context->GetIsolate()), argc, argv,
159 expected_error);
160 }
161
162 v8::Local<v8::Value> GetPropertyFromObject(v8::Local<v8::Object> object,
163 v8::Local<v8::Context> context,
164 base::StringPiece key) {
165 v8::Local<v8::Value> result;
166 EXPECT_TRUE(object->Get(context, gin::StringToV8(context->GetIsolate(), key))
167 .ToLocal(&result));
168 return result;
169 }
170
171 std::unique_ptr<base::Value> GetBaseValuePropertyFromObject(
172 v8::Local<v8::Object> object,
173 v8::Local<v8::Context> context,
174 base::StringPiece key) {
175 return V8ToBaseValue(GetPropertyFromObject(object, context, key), context);
176 }
177
70 } // namespace extensions 178 } // namespace extensions
OLDNEW
« no previous file with comments | « extensions/renderer/api_binding_test_util.h ('k') | extensions/renderer/api_binding_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698