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

Side by Side Diff: third_party/WebKit/Source/core/mojo/tests/JsToCppTest.cpp

Issue 2734783008: Moves mojo_js_integration_tests into blink. (Closed)
Patch Set: all tests pass Created 3 years, 6 months 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 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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 <stddef.h> 5 #include "bindings/core/v8/ScriptController.h"
6 #include <stdint.h> 6 #include "bindings/core/v8/ScriptSourceCode.h"
7 #include "bindings/core/v8/V8BindingForCore.h"
8 #include "bindings/core/v8/V8ScriptRunner.h"
9 #include "core/frame/Settings.h"
10 #include "core/mojo/MojoHandle.h"
11 #include "core/testing/DummyPageHolder.h"
12 #include "mojo/public/cpp/bindings/binding.h"
13 #include "mojo/public/cpp/system/wait.h"
14 #include "platform/testing/UnitTestHelpers.h"
15 #include "testing/gtest/include/gtest/gtest.h"
16 #include "third_party/WebKit/Source/core/mojo/tests/JsToCpp.mojom-blink.h"
7 17
8 #include <string> 18 namespace blink {
9 #include <utility> 19 namespace {
10
11 #include "base/at_exit.h"
12 #include "base/files/file_path.h"
13 #include "base/files/file_util.h"
14 #include "base/macros.h"
15 #include "base/run_loop.h"
16 #include "base/strings/utf_string_conversions.h"
17 #include "base/test/scoped_task_environment.h"
18 #include "base/threading/thread_task_runner_handle.h"
19 #include "gin/array_buffer.h"
20 #include "gin/public/isolate_holder.h"
21 #include "gin/v8_initializer.h"
22 #include "mojo/common/data_pipe_utils.h"
23 #include "mojo/edk/js/mojo_runner_delegate.h"
24 #include "mojo/edk/js/tests/js_to_cpp.mojom.h"
25 #include "mojo/public/cpp/bindings/binding.h"
26 #include "mojo/public/cpp/bindings/lib/validation_errors.h"
27 #include "mojo/public/cpp/system/core.h"
28 #include "mojo/public/cpp/system/wait.h"
29 #include "testing/gtest/include/gtest/gtest.h"
30
31 namespace mojo {
32 namespace edk {
33 namespace js {
34 20
35 // Global value updated by some checks to prevent compilers from optimizing 21 // Global value updated by some checks to prevent compilers from optimizing
36 // reads out of existence. 22 // reads out of existence.
37 uint32_t g_waste_accumulator = 0; 23 uint32_t g_waste_accumulator = 0;
38 24
39 namespace {
40
41 // Negative numbers with different values in each byte, the last of 25 // Negative numbers with different values in each byte, the last of
42 // which can survive promotion to double and back. 26 // which can survive promotion to double and back.
43 const int8_t kExpectedInt8Value = -65; 27 const int8_t kExpectedInt8Value = -65;
44 const int16_t kExpectedInt16Value = -16961; 28 const int16_t kExpectedInt16Value = -16961;
45 const int32_t kExpectedInt32Value = -1145258561; 29 const int32_t kExpectedInt32Value = -1145258561;
46 const int64_t kExpectedInt64Value = -77263311946305LL; 30 const int64_t kExpectedInt64Value = -77263311946305LL;
47 31
48 // Positive numbers with different values in each byte, the last of 32 // Positive numbers with different values in each byte, the last of
49 // which can survive promotion to double and back. 33 // which can survive promotion to double and back.
50 const uint8_t kExpectedUInt8Value = 65; 34 const uint8_t kExpectedUInt8Value = 65;
51 const uint16_t kExpectedUInt16Value = 16961; 35 const uint16_t kExpectedUInt16Value = 16961;
52 const uint32_t kExpectedUInt32Value = 1145258561; 36 const uint32_t kExpectedUInt32Value = 1145258561;
53 const uint64_t kExpectedUInt64Value = 77263311946305LL; 37 const uint64_t kExpectedUInt64Value = 77263311946305LL;
54 38
55 // Double/float values, including special case constants. 39 // Double/float values, including special case constants.
56 const double kExpectedDoubleVal = 3.14159265358979323846; 40 const double kExpectedDoubleVal = 3.14159265358979323846;
57 const double kExpectedDoubleInf = std::numeric_limits<double>::infinity(); 41 const double kExpectedDoubleInf = std::numeric_limits<double>::infinity();
58 const double kExpectedDoubleNan = std::numeric_limits<double>::quiet_NaN(); 42 const double kExpectedDoubleNan = std::numeric_limits<double>::quiet_NaN();
59 const float kExpectedFloatVal = static_cast<float>(kExpectedDoubleVal); 43 const float kExpectedFloatVal = static_cast<float>(kExpectedDoubleVal);
60 const float kExpectedFloatInf = std::numeric_limits<float>::infinity(); 44 const float kExpectedFloatInf = std::numeric_limits<float>::infinity();
61 const float kExpectedFloatNan = std::numeric_limits<float>::quiet_NaN(); 45 const float kExpectedFloatNan = std::numeric_limits<float>::quiet_NaN();
62 46
63 // NaN has the property that it is not equal to itself. 47 // NaN has the property that it is not equal to itself.
64 #define EXPECT_NAN(x) EXPECT_NE(x, x) 48 #define EXPECT_NAN(x) EXPECT_NE(x, x)
65 49
66 void CheckDataPipe(ScopedDataPipeConsumerHandle data_pipe_handle) { 50 String MojoBindingsScriptPath() {
67 std::string buffer; 51 String filepath = testing::ExecutableDir();
68 bool result = common::BlockingCopyToString(std::move(data_pipe_handle), 52 filepath.append("/gen/mojo/public/js/mojo_bindings.js");
yzshen1 2017/06/05 17:33:26 Just to double check: is this accessible in isolat
alokp 2017/06/05 18:53:29 I am not sure. I am hoping that we include everyth
69 &buffer); 53 return filepath;
70 EXPECT_TRUE(result);
71 EXPECT_EQ(64u, buffer.size());
72 for (int i = 0; i < 64; ++i) {
73 EXPECT_EQ(i, buffer[i]);
74 }
75 } 54 }
76 55
77 void CheckMessagePipe(MessagePipeHandle message_pipe_handle) { 56 String TestBindingsScriptPath() {
57 String filepath = testing::ExecutableDir();
58 filepath.append(
59 "/gen/third_party/WebKit/Source/core/mojo/tests/JsToCpp.mojom.js");
60 return filepath;
61 }
62
63 String TestScriptPath() {
64 String filepath = testing::BlinkRootDir();
65 filepath.append("/Source/core/mojo/tests/JsToCppTest.js");
66 return filepath;
67 }
68
69 void CheckDataPipe(mojo::DataPipeConsumerHandle data_pipe_handle) {
70 const void* buffer = nullptr;
71 unsigned num_bytes = 0;
72 MojoResult result = BeginReadDataRaw(data_pipe_handle, &buffer, &num_bytes,
yzshen1 2017/06/05 17:33:26 - Do we need to wait just like what line 85 does?
alokp 2017/06/05 18:53:29 Done.
73 MOJO_READ_DATA_FLAG_NONE);
74 EXPECT_EQ(MOJO_RESULT_OK, result);
75 EXPECT_EQ(64u, num_bytes);
76 for (unsigned i = 0; i < num_bytes; ++i) {
77 EXPECT_EQ(i, static_cast<unsigned>(static_cast<const char*>(buffer)[i]));
78 }
79 EndReadDataRaw(data_pipe_handle, num_bytes);
80 }
81
82 void CheckMessagePipe(mojo::MessagePipeHandle message_pipe_handle) {
78 unsigned char buffer[100]; 83 unsigned char buffer[100];
79 uint32_t buffer_size = static_cast<uint32_t>(sizeof(buffer)); 84 uint32_t buffer_size = static_cast<uint32_t>(sizeof(buffer));
80 MojoResult result = Wait(message_pipe_handle, MOJO_HANDLE_SIGNAL_READABLE); 85 MojoResult result = Wait(message_pipe_handle, MOJO_HANDLE_SIGNAL_READABLE);
81 EXPECT_EQ(MOJO_RESULT_OK, result); 86 EXPECT_EQ(MOJO_RESULT_OK, result);
82 result = ReadMessageRaw( 87 result = ReadMessageRaw(message_pipe_handle, buffer, &buffer_size, 0, 0, 0);
83 message_pipe_handle, buffer, &buffer_size, 0, 0, 0);
84 EXPECT_EQ(MOJO_RESULT_OK, result); 88 EXPECT_EQ(MOJO_RESULT_OK, result);
85 EXPECT_EQ(64u, buffer_size); 89 EXPECT_EQ(64u, buffer_size);
86 for (int i = 0; i < 64; ++i) { 90 for (int i = 0; i < 64; ++i) {
87 EXPECT_EQ(255 - i, buffer[i]); 91 EXPECT_EQ(255 - i, buffer[i]);
88 } 92 }
89 } 93 }
90 94
91 js_to_cpp::EchoArgsPtr BuildSampleEchoArgs() { 95 js_to_cpp::blink::EchoArgsPtr BuildSampleEchoArgs() {
92 js_to_cpp::EchoArgsPtr args(js_to_cpp::EchoArgs::New()); 96 auto args = js_to_cpp::blink::EchoArgs::New();
93 args->si64 = kExpectedInt64Value; 97 args->si64 = kExpectedInt64Value;
94 args->si32 = kExpectedInt32Value; 98 args->si32 = kExpectedInt32Value;
95 args->si16 = kExpectedInt16Value; 99 args->si16 = kExpectedInt16Value;
96 args->si8 = kExpectedInt8Value; 100 args->si8 = kExpectedInt8Value;
97 args->ui64 = kExpectedUInt64Value; 101 args->ui64 = kExpectedUInt64Value;
98 args->ui32 = kExpectedUInt32Value; 102 args->ui32 = kExpectedUInt32Value;
99 args->ui16 = kExpectedUInt16Value; 103 args->ui16 = kExpectedUInt16Value;
100 args->ui8 = kExpectedUInt8Value; 104 args->ui8 = kExpectedUInt8Value;
101 args->float_val = kExpectedFloatVal; 105 args->float_val = kExpectedFloatVal;
102 args->float_inf = kExpectedFloatInf; 106 args->float_inf = kExpectedFloatInf;
103 args->float_nan = kExpectedFloatNan; 107 args->float_nan = kExpectedFloatNan;
104 args->double_val = kExpectedDoubleVal; 108 args->double_val = kExpectedDoubleVal;
105 args->double_inf = kExpectedDoubleInf; 109 args->double_inf = kExpectedDoubleInf;
106 args->double_nan = kExpectedDoubleNan; 110 args->double_nan = kExpectedDoubleNan;
107 args->name.emplace("coming"); 111 args->name = "coming";
108 args->string_array.emplace(3); 112 args->string_array.emplace(3);
109 (*args->string_array)[0] = "one"; 113 (*args->string_array)[0] = "one";
110 (*args->string_array)[1] = "two"; 114 (*args->string_array)[1] = "two";
111 (*args->string_array)[2] = "three"; 115 (*args->string_array)[2] = "three";
112 return args; 116 return args;
113 } 117 }
114 118
115 void CheckSampleEchoArgs(js_to_cpp::EchoArgsPtr arg) { 119 void CheckSampleEchoArgs(const js_to_cpp::blink::EchoArgsPtr& arg) {
116 EXPECT_EQ(kExpectedInt64Value, arg->si64); 120 EXPECT_EQ(kExpectedInt64Value, arg->si64);
117 EXPECT_EQ(kExpectedInt32Value, arg->si32); 121 EXPECT_EQ(kExpectedInt32Value, arg->si32);
118 EXPECT_EQ(kExpectedInt16Value, arg->si16); 122 EXPECT_EQ(kExpectedInt16Value, arg->si16);
119 EXPECT_EQ(kExpectedInt8Value, arg->si8); 123 EXPECT_EQ(kExpectedInt8Value, arg->si8);
120 EXPECT_EQ(kExpectedUInt64Value, arg->ui64); 124 EXPECT_EQ(kExpectedUInt64Value, arg->ui64);
121 EXPECT_EQ(kExpectedUInt32Value, arg->ui32); 125 EXPECT_EQ(kExpectedUInt32Value, arg->ui32);
122 EXPECT_EQ(kExpectedUInt16Value, arg->ui16); 126 EXPECT_EQ(kExpectedUInt16Value, arg->ui16);
123 EXPECT_EQ(kExpectedUInt8Value, arg->ui8); 127 EXPECT_EQ(kExpectedUInt8Value, arg->ui8);
124 EXPECT_EQ(kExpectedFloatVal, arg->float_val); 128 EXPECT_EQ(kExpectedFloatVal, arg->float_val);
125 EXPECT_EQ(kExpectedFloatInf, arg->float_inf); 129 EXPECT_EQ(kExpectedFloatInf, arg->float_inf);
126 EXPECT_NAN(arg->float_nan); 130 EXPECT_NAN(arg->float_nan);
127 EXPECT_EQ(kExpectedDoubleVal, arg->double_val); 131 EXPECT_EQ(kExpectedDoubleVal, arg->double_val);
128 EXPECT_EQ(kExpectedDoubleInf, arg->double_inf); 132 EXPECT_EQ(kExpectedDoubleInf, arg->double_inf);
129 EXPECT_NAN(arg->double_nan); 133 EXPECT_NAN(arg->double_nan);
130 EXPECT_EQ(std::string("coming"), *arg->name); 134 EXPECT_EQ(String("coming"), arg->name);
131 EXPECT_EQ(std::string("one"), (*arg->string_array)[0]); 135 EXPECT_EQ(String("one"), (*arg->string_array)[0]);
132 EXPECT_EQ(std::string("two"), (*arg->string_array)[1]); 136 EXPECT_EQ(String("two"), (*arg->string_array)[1]);
133 EXPECT_EQ(std::string("three"), (*arg->string_array)[2]); 137 EXPECT_EQ(String("three"), (*arg->string_array)[2]);
134 CheckDataPipe(std::move(arg->data_handle)); 138 CheckDataPipe(arg->data_handle.get());
135 CheckMessagePipe(arg->message_handle.get()); 139 CheckMessagePipe(arg->message_handle.get());
136 } 140 }
137 141
138 void CheckSampleEchoArgsList(const js_to_cpp::EchoArgsListPtr& list) { 142 void CheckSampleEchoArgsList(const js_to_cpp::blink::EchoArgsListPtr& list) {
139 if (list.is_null()) 143 if (list.is_null())
140 return; 144 return;
141 CheckSampleEchoArgs(std::move(list->item)); 145 CheckSampleEchoArgs(list->item);
142 CheckSampleEchoArgsList(list->next); 146 CheckSampleEchoArgsList(list->next);
143 } 147 }
144 148
145 // More forgiving checks are needed in the face of potentially corrupt 149 // More forgiving checks are needed in the face of potentially corrupt
146 // messages. The values don't matter so long as all accesses are within 150 // messages. The values don't matter so long as all accesses are within
147 // bounds. 151 // bounds.
148 void CheckCorruptedString(const std::string& arg) { 152 void CheckCorruptedString(const String& arg) {
149 for (size_t i = 0; i < arg.size(); ++i) 153 for (size_t i = 0; i < arg.length(); ++i)
150 g_waste_accumulator += arg[i]; 154 g_waste_accumulator += arg[i];
151 } 155 }
152 156
153 void CheckCorruptedString(const base::Optional<std::string>& arg) { 157 void CheckCorruptedStringArray(const Optional<Vector<String>>& string_array) {
154 if (!arg)
155 return;
156 CheckCorruptedString(*arg);
157 }
158
159 void CheckCorruptedStringArray(
160 const base::Optional<std::vector<std::string>>& string_array) {
161 if (!string_array) 158 if (!string_array)
162 return; 159 return;
163 for (size_t i = 0; i < string_array->size(); ++i) 160 for (size_t i = 0; i < string_array->size(); ++i)
164 CheckCorruptedString((*string_array)[i]); 161 CheckCorruptedString((*string_array)[i]);
165 } 162 }
166 163
167 void CheckCorruptedDataPipe(MojoHandle data_pipe_handle) { 164 void CheckCorruptedDataPipe(mojo::DataPipeConsumerHandle data_pipe_handle) {
168 unsigned char buffer[100]; 165 unsigned char buffer[100];
169 uint32_t buffer_size = static_cast<uint32_t>(sizeof(buffer)); 166 uint32_t buffer_size = static_cast<uint32_t>(sizeof(buffer));
170 MojoResult result = MojoReadData( 167 MojoResult result = ReadDataRaw(data_pipe_handle, buffer, &buffer_size,
171 data_pipe_handle, buffer, &buffer_size, MOJO_READ_DATA_FLAG_NONE); 168 MOJO_READ_DATA_FLAG_NONE);
172 if (result != MOJO_RESULT_OK) 169 if (result != MOJO_RESULT_OK)
173 return; 170 return;
174 for (uint32_t i = 0; i < buffer_size; ++i) 171 for (uint32_t i = 0; i < buffer_size; ++i)
175 g_waste_accumulator += buffer[i]; 172 g_waste_accumulator += buffer[i];
176 } 173 }
177 174
178 void CheckCorruptedMessagePipe(MojoHandle message_pipe_handle) { 175 void CheckCorruptedMessagePipe(mojo::MessagePipeHandle message_pipe_handle) {
179 unsigned char buffer[100]; 176 unsigned char buffer[100];
180 uint32_t buffer_size = static_cast<uint32_t>(sizeof(buffer)); 177 uint32_t buffer_size = static_cast<uint32_t>(sizeof(buffer));
181 MojoResult result = MojoReadMessage( 178 MojoResult result =
182 message_pipe_handle, buffer, &buffer_size, 0, 0, 0); 179 ReadMessageRaw(message_pipe_handle, buffer, &buffer_size, 0, 0, 0);
183 if (result != MOJO_RESULT_OK) 180 if (result != MOJO_RESULT_OK)
184 return; 181 return;
185 for (uint32_t i = 0; i < buffer_size; ++i) 182 for (uint32_t i = 0; i < buffer_size; ++i)
186 g_waste_accumulator += buffer[i]; 183 g_waste_accumulator += buffer[i];
187 } 184 }
188 185
189 void CheckCorruptedEchoArgs(const js_to_cpp::EchoArgsPtr& arg) { 186 void CheckCorruptedEchoArgs(const js_to_cpp::blink::EchoArgsPtr& arg) {
190 if (arg.is_null()) 187 if (arg.is_null())
191 return; 188 return;
192 CheckCorruptedString(arg->name); 189 CheckCorruptedString(arg->name);
193 CheckCorruptedStringArray(arg->string_array); 190 CheckCorruptedStringArray(arg->string_array);
194 if (arg->data_handle.is_valid()) 191 if (arg->data_handle.is_valid())
195 CheckCorruptedDataPipe(arg->data_handle.get().value()); 192 CheckCorruptedDataPipe(arg->data_handle.get());
196 if (arg->message_handle.is_valid()) 193 if (arg->message_handle.is_valid())
197 CheckCorruptedMessagePipe(arg->message_handle.get().value()); 194 CheckCorruptedMessagePipe(arg->message_handle.get());
198 } 195 }
199 196
200 void CheckCorruptedEchoArgsList(const js_to_cpp::EchoArgsListPtr& list) { 197 void CheckCorruptedEchoArgsList(const js_to_cpp::blink::EchoArgsListPtr& list) {
201 if (list.is_null()) 198 if (list.is_null())
202 return; 199 return;
203 CheckCorruptedEchoArgs(list->item); 200 CheckCorruptedEchoArgs(list->item);
204 CheckCorruptedEchoArgsList(list->next); 201 CheckCorruptedEchoArgsList(list->next);
205 } 202 }
206 203
207 // Base Provider implementation class. It's expected that tests subclass and 204 // Base Provider implementation class. It's expected that tests subclass and
208 // override the appropriate Provider functions. When test is done quit the 205 // override the appropriate Provider functions. When test is done quit the
209 // run_loop(). 206 // run_loop().
210 class CppSideConnection : public js_to_cpp::CppSide { 207 class CppSideConnection : public js_to_cpp::blink::CppSide {
211 public: 208 public:
212 CppSideConnection() 209 CppSideConnection()
213 : run_loop_(nullptr), 210 : js_side_(nullptr), mishandled_messages_(0), binding_(this) {}
yzshen1 2017/06/05 17:33:25 nit: js_side_ init is not necessary.
alokp 2017/06/05 18:53:29 Done.
214 js_side_(nullptr),
215 mishandled_messages_(0),
216 binding_(this) {}
217 ~CppSideConnection() override {} 211 ~CppSideConnection() override {}
218 212
219 void set_run_loop(base::RunLoop* run_loop) { run_loop_ = run_loop; } 213 void set_js_side(js_to_cpp::blink::JsSidePtr js_side) {
220 base::RunLoop* run_loop() { return run_loop_; } 214 js_side_ = std::move(js_side);
215 }
216 js_to_cpp::blink::JsSide* js_side() { return js_side_.get(); }
221 217
222 void set_js_side(js_to_cpp::JsSide* js_side) { js_side_ = js_side; } 218 void Bind(mojo::InterfaceRequest<js_to_cpp::blink::CppSide> request) {
223 js_to_cpp::JsSide* js_side() { return js_side_; }
224
225 void Bind(InterfaceRequest<js_to_cpp::CppSide> request) {
226 binding_.Bind(std::move(request)); 219 binding_.Bind(std::move(request));
227 // Keep the pipe open even after validation errors. 220 // Keep the pipe open even after validation errors.
228 binding_.EnableTestingMode(); 221 binding_.EnableTestingMode();
229 } 222 }
230 223
231 // js_to_cpp::CppSide: 224 // js_to_cpp::CppSide:
232 void StartTest() override { NOTREACHED(); } 225 void StartTest() override { NOTREACHED(); }
233 226
234 void TestFinished() override { NOTREACHED(); } 227 void TestFinished() override { NOTREACHED(); }
235 228
236 void PingResponse() override { mishandled_messages_ += 1; } 229 void PingResponse() override { mishandled_messages_ += 1; }
237 230
238 void EchoResponse(js_to_cpp::EchoArgsListPtr list) override { 231 void EchoResponse(js_to_cpp::blink::EchoArgsListPtr list) override {
239 mishandled_messages_ += 1; 232 mishandled_messages_ += 1;
240 } 233 }
241 234
242 void BitFlipResponse( 235 void BitFlipResponse(
243 js_to_cpp::EchoArgsListPtr list, 236 js_to_cpp::blink::EchoArgsListPtr list,
244 js_to_cpp::ForTestingAssociatedPtrInfo not_used) override { 237 js_to_cpp::blink::ForTestingAssociatedPtrInfo not_used) override {
245 mishandled_messages_ += 1; 238 mishandled_messages_ += 1;
246 } 239 }
247 240
248 void BackPointerResponse(js_to_cpp::EchoArgsListPtr list) override { 241 void BackPointerResponse(js_to_cpp::blink::EchoArgsListPtr list) override {
249 mishandled_messages_ += 1; 242 mishandled_messages_ += 1;
250 } 243 }
251 244
252 protected: 245 protected:
253 base::RunLoop* run_loop_; 246 js_to_cpp::blink::JsSidePtr js_side_;
254 js_to_cpp::JsSide* js_side_;
255 int mishandled_messages_; 247 int mishandled_messages_;
256 mojo::Binding<js_to_cpp::CppSide> binding_; 248 mojo::Binding<js_to_cpp::blink::CppSide> binding_;
257
258 private:
259 DISALLOW_COPY_AND_ASSIGN(CppSideConnection);
260 }; 249 };
261 250
262 // Trivial test to verify a message sent from JS is received. 251 // Trivial test to verify a message sent from JS is received.
263 class PingCppSideConnection : public CppSideConnection { 252 class PingCppSideConnection : public CppSideConnection {
264 public: 253 public:
265 PingCppSideConnection() : got_message_(false) {} 254 PingCppSideConnection() : got_message_(false) {}
266 ~PingCppSideConnection() override {} 255 ~PingCppSideConnection() override {}
267 256
268 // js_to_cpp::CppSide: 257 // js_to_cpp::CppSide:
269 void StartTest() override { js_side_->Ping(); } 258 void StartTest() override { js_side_->Ping(); }
270 259
271 void PingResponse() override { 260 void PingResponse() override {
272 got_message_ = true; 261 got_message_ = true;
273 run_loop()->Quit(); 262 testing::ExitRunLoop();
274 } 263 }
275 264
276 bool DidSucceed() { 265 bool DidSucceed() { return got_message_ && !mishandled_messages_; }
277 return got_message_ && !mishandled_messages_;
278 }
279 266
280 private: 267 private:
281 bool got_message_; 268 bool got_message_;
282 DISALLOW_COPY_AND_ASSIGN(PingCppSideConnection);
283 }; 269 };
284 270
285 // Test that parameters are passed with correct values. 271 // Test that parameters are passed with correct values.
286 class EchoCppSideConnection : public CppSideConnection { 272 class EchoCppSideConnection : public CppSideConnection {
287 public: 273 public:
288 EchoCppSideConnection() : 274 EchoCppSideConnection() : message_count_(0), termination_seen_(false) {}
289 message_count_(0),
290 termination_seen_(false) {
291 }
292 ~EchoCppSideConnection() override {} 275 ~EchoCppSideConnection() override {}
293 276
294 // js_to_cpp::CppSide: 277 // js_to_cpp::CppSide:
295 void StartTest() override { 278 void StartTest() override {
296 js_side_->Echo(kExpectedMessageCount, BuildSampleEchoArgs()); 279 js_side_->Echo(kExpectedMessageCount, BuildSampleEchoArgs());
297 } 280 }
298 281
299 void EchoResponse(js_to_cpp::EchoArgsListPtr list) override { 282 void EchoResponse(js_to_cpp::blink::EchoArgsListPtr list) override {
300 const js_to_cpp::EchoArgsPtr& special_arg = list->item;
301 message_count_ += 1; 283 message_count_ += 1;
284
285 const js_to_cpp::blink::EchoArgsPtr& special_arg = list->item;
302 EXPECT_EQ(-1, special_arg->si64); 286 EXPECT_EQ(-1, special_arg->si64);
303 EXPECT_EQ(-1, special_arg->si32); 287 EXPECT_EQ(-1, special_arg->si32);
304 EXPECT_EQ(-1, special_arg->si16); 288 EXPECT_EQ(-1, special_arg->si16);
305 EXPECT_EQ(-1, special_arg->si8); 289 EXPECT_EQ(-1, special_arg->si8);
306 EXPECT_EQ(std::string("going"), *special_arg->name); 290 EXPECT_EQ(String("going"), special_arg->name);
291 CheckDataPipe(special_arg->data_handle.get());
292 CheckMessagePipe(special_arg->message_handle.get());
293
307 CheckSampleEchoArgsList(list->next); 294 CheckSampleEchoArgsList(list->next);
308 } 295 }
309 296
310 void TestFinished() override { 297 void TestFinished() override {
311 termination_seen_ = true; 298 termination_seen_ = true;
312 run_loop()->Quit(); 299 testing::ExitRunLoop();
313 } 300 }
314 301
315 bool DidSucceed() { 302 bool DidSucceed() {
316 return termination_seen_ && 303 return termination_seen_ && !mishandled_messages_ &&
317 !mishandled_messages_ && 304 message_count_ == kExpectedMessageCount;
318 message_count_ == kExpectedMessageCount;
319 } 305 }
320 306
321 private: 307 private:
322 static const int kExpectedMessageCount = 10; 308 static const int kExpectedMessageCount = 10;
323 int message_count_; 309 int message_count_;
324 bool termination_seen_; 310 bool termination_seen_;
325 DISALLOW_COPY_AND_ASSIGN(EchoCppSideConnection);
326 }; 311 };
327 312
328 // Test that corrupted messages don't wreak havoc. 313 // Test that corrupted messages don't wreak havoc.
329 class BitFlipCppSideConnection : public CppSideConnection { 314 class BitFlipCppSideConnection : public CppSideConnection {
330 public: 315 public:
331 BitFlipCppSideConnection() : termination_seen_(false) {} 316 BitFlipCppSideConnection() : termination_seen_(false) {}
332 ~BitFlipCppSideConnection() override {} 317 ~BitFlipCppSideConnection() override {}
333 318
334 // js_to_cpp::CppSide: 319 // js_to_cpp::CppSide:
335 void StartTest() override { js_side_->BitFlip(BuildSampleEchoArgs()); } 320 void StartTest() override { js_side_->BitFlip(BuildSampleEchoArgs()); }
336 321
337 void BitFlipResponse( 322 void BitFlipResponse(
338 js_to_cpp::EchoArgsListPtr list, 323 js_to_cpp::blink::EchoArgsListPtr list,
339 js_to_cpp::ForTestingAssociatedPtrInfo not_used) override { 324 js_to_cpp::blink::ForTestingAssociatedPtrInfo not_used) override {
340 CheckCorruptedEchoArgsList(list); 325 CheckCorruptedEchoArgsList(list);
341 } 326 }
342 327
343 void TestFinished() override { 328 void TestFinished() override {
344 termination_seen_ = true; 329 termination_seen_ = true;
345 run_loop()->Quit(); 330 testing::ExitRunLoop();
346 } 331 }
347 332
348 bool DidSucceed() { 333 bool DidSucceed() { return termination_seen_; }
349 return termination_seen_;
350 }
351 334
352 private: 335 private:
353 bool termination_seen_; 336 bool termination_seen_;
354 DISALLOW_COPY_AND_ASSIGN(BitFlipCppSideConnection);
355 }; 337 };
356 338
357 // Test that severely random messages don't wreak havoc. 339 // Test that severely random messages don't wreak havoc.
358 class BackPointerCppSideConnection : public CppSideConnection { 340 class BackPointerCppSideConnection : public CppSideConnection {
359 public: 341 public:
360 BackPointerCppSideConnection() : termination_seen_(false) {} 342 BackPointerCppSideConnection() : termination_seen_(false) {}
361 ~BackPointerCppSideConnection() override {} 343 ~BackPointerCppSideConnection() override {}
362 344
363 // js_to_cpp::CppSide: 345 // js_to_cpp::CppSide:
364 void StartTest() override { js_side_->BackPointer(BuildSampleEchoArgs()); } 346 void StartTest() override { js_side_->BackPointer(BuildSampleEchoArgs()); }
365 347
366 void BackPointerResponse(js_to_cpp::EchoArgsListPtr list) override { 348 void BackPointerResponse(js_to_cpp::blink::EchoArgsListPtr list) override {
367 CheckCorruptedEchoArgsList(list); 349 CheckCorruptedEchoArgsList(list);
368 } 350 }
369 351
370 void TestFinished() override { 352 void TestFinished() override {
371 termination_seen_ = true; 353 termination_seen_ = true;
372 run_loop()->Quit(); 354 testing::ExitRunLoop();
373 } 355 }
374 356
375 bool DidSucceed() { 357 bool DidSucceed() { return termination_seen_; }
376 return termination_seen_; 358
359 private:
360 bool termination_seen_;
361 };
362
363 class JsToCppTest : public ::testing::Test {
364 public:
365 JsToCppTest() : page_holder_(DummyPageHolder::Create()) {
366 page_holder_->GetPage().GetSettings().SetScriptEnabled(true);
jbroman 2017/06/06 20:14:08 V8TestingScope will take out some of the boilerpla
alokp 2017/06/06 23:52:23 Done.
367 }
368
369 void RunTest(CppSideConnection* cpp_side) {
370 js_to_cpp::blink::CppSidePtr cpp_side_ptr;
371 cpp_side->Bind(MakeRequest(&cpp_side_ptr));
372
373 js_to_cpp::blink::JsSidePtr js_side_ptr;
374 auto js_side_request = MakeRequest(&js_side_ptr);
375 js_side_ptr->SetCppSide(std::move(cpp_side_ptr));
376 cpp_side->set_js_side(std::move(js_side_ptr));
377
378 ScriptState* script_state =
379 ToScriptStateForMainWorld(&page_holder_->GetFrame());
380 ASSERT_TRUE(script_state);
381
382 ScriptState::Scope scope(script_state);
383 ExecuteScript(MojoBindingsScriptPath());
384 ExecuteScript(TestBindingsScriptPath());
385
386 v8::Local<v8::Value> start_fn = ExecuteScript(TestScriptPath());
387 ASSERT_FALSE(start_fn.IsEmpty());
388 ASSERT_TRUE(start_fn->IsFunction());
389 v8::Local<v8::Object> global_proxy = script_state->GetContext()->Global();
390 v8::Local<v8::Value> args[1] = {
391 ToV8(MojoHandle::Create(
392 mojo::ScopedHandle::From(js_side_request.PassMessagePipe())),
393 global_proxy, script_state->GetIsolate())};
394 V8ScriptRunner::CallFunction(start_fn.As<v8::Function>(),
395 &page_holder_->GetDocument(), global_proxy, 1,
396 args, script_state->GetIsolate());
397 testing::EnterRunLoop();
398 }
399
400 protected:
401 v8::Local<v8::Value> ExecuteScript(const String& script_path) {
402 RefPtr<SharedBuffer> script_src = testing::ReadFromFile(script_path);
403 return page_holder_->GetFrame()
404 .GetScriptController()
405 .ExecuteScriptInMainWorldAndReturnValue(
406 ScriptSourceCode(String(script_src->Data(), script_src->size())));
377 } 407 }
378 408
379 private: 409 private:
380 bool termination_seen_; 410 std::unique_ptr<DummyPageHolder> page_holder_;
381 DISALLOW_COPY_AND_ASSIGN(BackPointerCppSideConnection);
382 };
383
384 } // namespace
385
386 class JsToCppTest : public testing::Test {
387 public:
388 JsToCppTest() {}
389
390 void RunTest(const std::string& test, CppSideConnection* cpp_side) {
391 cpp_side->set_run_loop(&run_loop_);
392
393 js_to_cpp::JsSidePtr js_side;
394 auto js_side_proxy = MakeRequest(&js_side);
395
396 cpp_side->set_js_side(js_side.get());
397 js_to_cpp::CppSidePtr cpp_side_ptr;
398 cpp_side->Bind(MakeRequest(&cpp_side_ptr));
399
400 js_side->SetCppSide(std::move(cpp_side_ptr));
401
402 #ifdef V8_USE_EXTERNAL_STARTUP_DATA
403 gin::V8Initializer::LoadV8Snapshot();
404 gin::V8Initializer::LoadV8Natives();
405 #endif
406
407 gin::IsolateHolder::Initialize(gin::IsolateHolder::kStrictMode,
408 gin::IsolateHolder::kStableV8Extras,
409 gin::ArrayBufferAllocator::SharedInstance());
410 gin::IsolateHolder instance(base::ThreadTaskRunnerHandle::Get());
411 MojoRunnerDelegate delegate;
412 gin::ShellRunner runner(&delegate, instance.isolate());
413 delegate.Start(&runner, js_side_proxy.PassMessagePipe().release().value(),
414 test);
415
416 run_loop_.Run();
417 }
418
419 private:
420 base::ShadowingAtExitManager at_exit_;
421 base::test::ScopedTaskEnvironment scoped_task_environment_;
422 base::RunLoop run_loop_;
423
424 DISALLOW_COPY_AND_ASSIGN(JsToCppTest);
425 }; 411 };
426 412
427 TEST_F(JsToCppTest, Ping) { 413 TEST_F(JsToCppTest, Ping) {
428 PingCppSideConnection cpp_side_connection; 414 PingCppSideConnection cpp_side_connection;
429 RunTest("mojo/edk/js/tests/js_to_cpp_tests", &cpp_side_connection); 415 RunTest(&cpp_side_connection);
430 EXPECT_TRUE(cpp_side_connection.DidSucceed()); 416 EXPECT_TRUE(cpp_side_connection.DidSucceed());
431 } 417 }
432 418
433 TEST_F(JsToCppTest, Echo) { 419 TEST_F(JsToCppTest, Echo) {
434 EchoCppSideConnection cpp_side_connection; 420 EchoCppSideConnection cpp_side_connection;
435 RunTest("mojo/edk/js/tests/js_to_cpp_tests", &cpp_side_connection); 421 RunTest(&cpp_side_connection);
436 EXPECT_TRUE(cpp_side_connection.DidSucceed()); 422 EXPECT_TRUE(cpp_side_connection.DidSucceed());
437 } 423 }
438 424
439 TEST_F(JsToCppTest, BitFlip) { 425 TEST_F(JsToCppTest, BitFlip) {
440 // These tests generate a lot of expected validation errors. Suppress logging. 426 // These tests generate a lot of expected validation errors. Suppress logging.
441 mojo::internal::ScopedSuppressValidationErrorLoggingForTests log_suppression; 427 mojo::internal::ScopedSuppressValidationErrorLoggingForTests log_suppression;
442 428
443 BitFlipCppSideConnection cpp_side_connection; 429 BitFlipCppSideConnection cpp_side_connection;
444 RunTest("mojo/edk/js/tests/js_to_cpp_tests", &cpp_side_connection); 430 RunTest(&cpp_side_connection);
445 EXPECT_TRUE(cpp_side_connection.DidSucceed()); 431 EXPECT_TRUE(cpp_side_connection.DidSucceed());
446 } 432 }
447 433
448 TEST_F(JsToCppTest, BackPointer) { 434 TEST_F(JsToCppTest, BackPointer) {
449 // These tests generate a lot of expected validation errors. Suppress logging. 435 // These tests generate a lot of expected validation errors. Suppress logging.
450 mojo::internal::ScopedSuppressValidationErrorLoggingForTests log_suppression; 436 mojo::internal::ScopedSuppressValidationErrorLoggingForTests log_suppression;
451 437
452 BackPointerCppSideConnection cpp_side_connection; 438 BackPointerCppSideConnection cpp_side_connection;
453 RunTest("mojo/edk/js/tests/js_to_cpp_tests", &cpp_side_connection); 439 RunTest(&cpp_side_connection);
454 EXPECT_TRUE(cpp_side_connection.DidSucceed()); 440 EXPECT_TRUE(cpp_side_connection.DidSucceed());
455 } 441 }
456 442
457 } // namespace js 443 } // namespace
458 } // namespace edk 444 } // namespace blink
459 } // namespace mojo
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698