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

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

Issue 2445223003: [Extensions Bindings] Add more utility functions (Closed)
Patch Set: 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, |out_error| with the thrown error.
lazyboy 2016/10/25 20:09:07 nit: |out_error| contains the thrown error or sth.
Devlin 2016/10/25 20:55:52 Whoops! Done.
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 return result;
113 }
114
115 v8::Local<v8::Value> RunFunction(v8::Local<v8::Function> function,
116 v8::Local<v8::Context> context,
117 int argc,
118 v8::Local<v8::Value> argv[]) {
119 return RunFunction(function, context, v8::Undefined(context->GetIsolate()),
120 argc, argv);
121 }
122
123 v8::Local<v8::Value> RunFunctionOnGlobal(v8::Local<v8::Function> function,
124 v8::Local<v8::Context> context,
125 int argc,
126 v8::Local<v8::Value> argv[]) {
127 return RunFunction(function, context, context->Global(), argc, argv);
128 }
129
130 void RunFunctionOnGlobalAndIgnoreResult(v8::Local<v8::Function> function,
131 v8::Local<v8::Context> context,
132 int argc,
133 v8::Local<v8::Value> argv[]) {
134 RunFunction(function, context, context->Global(), argc, argv);
135 }
136
137 void RunFunctionAndExpectError(v8::Local<v8::Function> function,
138 v8::Local<v8::Context> context,
139 v8::Local<v8::Value> receiver,
140 int argc,
141 v8::Local<v8::Value> argv[],
142 const std::string& expected_error) {
143 std::string error;
144 v8::Local<v8::Value> result;
145 EXPECT_FALSE(RunFunctionImpl(function, context, receiver, argc, argv, &result,
146 &error));
147 EXPECT_EQ(expected_error, error);
lazyboy 2016/10/25 20:09:07 EXPECT_TRUE(result.isEmpty())?
Devlin 2016/10/25 20:55:52 Done.
148 }
149
150 void RunFunctionAndExpectError(v8::Local<v8::Function> function,
151 v8::Local<v8::Context> context,
152 int argc,
153 v8::Local<v8::Value> argv[],
154 const std::string& expected_error) {
155 RunFunctionAndExpectError(function, context,
156 v8::Undefined(context->GetIsolate()), argc, argv,
157 expected_error);
158 }
159
160 v8::Local<v8::Value> GetPropertyFromObject(v8::Local<v8::Object> object,
161 v8::Local<v8::Context> context,
162 base::StringPiece key) {
163 v8::Local<v8::Value> result;
164 EXPECT_TRUE(object->Get(context, gin::StringToV8(context->GetIsolate(), key))
165 .ToLocal(&result));
166 return result;
167 }
168
169 std::unique_ptr<base::Value> GetBaseValuePropertyFromObject(
170 v8::Local<v8::Object> object,
171 v8::Local<v8::Context> context,
172 base::StringPiece key) {
173 return V8ToBaseValue(GetPropertyFromObject(object, context, key), context);
174 }
175
70 } // namespace extensions 176 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698