| OLD | NEW |
| 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 "base/file_util.h" | 5 #include "base/file_util.h" |
| 6 #include "base/files/file_path.h" | 6 #include "base/files/file_path.h" |
| 7 #include "base/message_loop/message_loop.h" | 7 #include "base/message_loop/message_loop.h" |
| 8 #include "base/run_loop.h" | 8 #include "base/run_loop.h" |
| 9 #include "base/strings/utf_string_conversions.h" | 9 #include "base/strings/utf_string_conversions.h" |
| 10 #include "gin/public/isolate_holder.h" | 10 #include "gin/public/isolate_holder.h" |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 58 const base::FilePath test_file_path( | 58 const base::FilePath test_file_path( |
| 59 test::GetFilePathForJSResource( | 59 test::GetFilePathForJSResource( |
| 60 "mojo/public/interfaces/bindings/tests/sample_interfaces.mojom")); | 60 "mojo/public/interfaces/bindings/tests/sample_interfaces.mojom")); |
| 61 if (!base::PathExists(test_file_path)) { | 61 if (!base::PathExists(test_file_path)) { |
| 62 LOG(WARNING) << "Mojom binding files don't exist. Skipping the test."; | 62 LOG(WARNING) << "Mojom binding files don't exist. Skipping the test."; |
| 63 return true; | 63 return true; |
| 64 } | 64 } |
| 65 return false; | 65 return false; |
| 66 } | 66 } |
| 67 | 67 |
| 68 // NOTE: Callers will need to have established an AllocationScope, or you're | |
| 69 // gonna have a bad time. | |
| 70 js_to_cpp::EchoArgs BuildSampleEchoArgs() { | |
| 71 js_to_cpp::EchoArgs::Builder builder; | |
| 72 builder.set_si64(kExpectedInt64Value); | |
| 73 builder.set_si32(kExpectedInt32Value); | |
| 74 builder.set_si16(kExpectedInt16Value); | |
| 75 builder.set_si8(kExpectedInt8Value); | |
| 76 builder.set_ui64(kExpectedUInt64Value); | |
| 77 builder.set_ui32(kExpectedUInt32Value); | |
| 78 builder.set_ui16(kExpectedUInt16Value); | |
| 79 builder.set_ui8(kExpectedUInt8Value); | |
| 80 builder.set_float_val(kExpectedFloatVal); | |
| 81 builder.set_float_inf(kExpectedFloatInf); | |
| 82 builder.set_float_nan(kExpectedFloatNan); | |
| 83 builder.set_double_val(kExpectedDoubleVal); | |
| 84 builder.set_double_inf(kExpectedDoubleInf); | |
| 85 builder.set_double_nan(kExpectedDoubleNan); | |
| 86 builder.set_name("coming"); | |
| 87 mojo::Array<mojo::String>::Builder string_array(3); | |
| 88 string_array[0] = "one"; | |
| 89 string_array[1] = "two"; | |
| 90 string_array[2] = "three"; | |
| 91 builder.set_string_array(string_array.Finish()); | |
| 92 return builder.Finish(); | |
| 93 } | |
| 94 | |
| 95 void CheckSampleEchoArgs(const js_to_cpp::EchoArgs& arg) { | |
| 96 EXPECT_EQ(kExpectedInt64Value, arg.si64()); | |
| 97 EXPECT_EQ(kExpectedInt32Value, arg.si32()); | |
| 98 EXPECT_EQ(kExpectedInt16Value, arg.si16()); | |
| 99 EXPECT_EQ(kExpectedInt8Value, arg.si8()); | |
| 100 EXPECT_EQ(kExpectedUInt64Value, arg.ui64()); | |
| 101 EXPECT_EQ(kExpectedUInt32Value, arg.ui32()); | |
| 102 EXPECT_EQ(kExpectedUInt16Value, arg.ui16()); | |
| 103 EXPECT_EQ(kExpectedUInt8Value, arg.ui8()); | |
| 104 EXPECT_EQ(kExpectedFloatVal, arg.float_val()); | |
| 105 EXPECT_EQ(kExpectedFloatInf, arg.float_inf()); | |
| 106 EXPECT_NAN(arg.float_nan()); | |
| 107 EXPECT_EQ(kExpectedDoubleVal, arg.double_val()); | |
| 108 EXPECT_EQ(kExpectedDoubleInf, arg.double_inf()); | |
| 109 EXPECT_NAN(arg.double_nan()); | |
| 110 EXPECT_EQ(std::string("coming"), arg.name().To<std::string>()); | |
| 111 EXPECT_EQ(std::string("one"), arg.string_array()[0].To<std::string>()); | |
| 112 EXPECT_EQ(std::string("two"), arg.string_array()[1].To<std::string>()); | |
| 113 EXPECT_EQ(std::string("three"), arg.string_array()[2].To<std::string>()); | |
| 114 } | |
| 115 | |
| 116 void CheckDataPipe(MojoHandle data_pipe_handle) { | 68 void CheckDataPipe(MojoHandle data_pipe_handle) { |
| 117 char buffer[100]; | 69 char buffer[100]; |
| 118 uint32_t buffer_size = static_cast<uint32_t>(sizeof(buffer)); | 70 uint32_t buffer_size = static_cast<uint32_t>(sizeof(buffer)); |
| 119 MojoResult result = MojoReadData( | 71 MojoResult result = MojoReadData( |
| 120 data_pipe_handle, buffer, &buffer_size, MOJO_READ_DATA_FLAG_NONE); | 72 data_pipe_handle, buffer, &buffer_size, MOJO_READ_DATA_FLAG_NONE); |
| 121 EXPECT_EQ(MOJO_RESULT_OK, result); | 73 EXPECT_EQ(MOJO_RESULT_OK, result); |
| 122 EXPECT_EQ(64u, buffer_size); | 74 EXPECT_EQ(64u, buffer_size); |
| 123 for (int i = 0; i < 64; ++i) { | 75 for (int i = 0; i < 64; ++i) { |
| 124 EXPECT_EQ(i, buffer[i]); | 76 EXPECT_EQ(i, buffer[i]); |
| 125 } | 77 } |
| 126 } | 78 } |
| 127 | 79 |
| 80 // NOTE: Callers will need to have established an AllocationScope, or you're |
| 81 // gonna have a bad time. |
| 82 js_to_cpp::EchoArgs BuildSampleEchoArgs() { |
| 83 js_to_cpp::EchoArgs::Builder builder; |
| 84 builder.set_si64(kExpectedInt64Value); |
| 85 builder.set_si32(kExpectedInt32Value); |
| 86 builder.set_si16(kExpectedInt16Value); |
| 87 builder.set_si8(kExpectedInt8Value); |
| 88 builder.set_ui64(kExpectedUInt64Value); |
| 89 builder.set_ui32(kExpectedUInt32Value); |
| 90 builder.set_ui16(kExpectedUInt16Value); |
| 91 builder.set_ui8(kExpectedUInt8Value); |
| 92 builder.set_float_val(kExpectedFloatVal); |
| 93 builder.set_float_inf(kExpectedFloatInf); |
| 94 builder.set_float_nan(kExpectedFloatNan); |
| 95 builder.set_double_val(kExpectedDoubleVal); |
| 96 builder.set_double_inf(kExpectedDoubleInf); |
| 97 builder.set_double_nan(kExpectedDoubleNan); |
| 98 builder.set_name("coming"); |
| 99 mojo::Array<mojo::String>::Builder string_array(3); |
| 100 string_array[0] = "one"; |
| 101 string_array[1] = "two"; |
| 102 string_array[2] = "three"; |
| 103 builder.set_string_array(string_array.Finish()); |
| 104 return builder.Finish(); |
| 105 } |
| 106 |
| 107 void CheckSampleEchoArgs(const js_to_cpp::EchoArgs& arg) { |
| 108 EXPECT_EQ(kExpectedInt64Value, arg.si64()); |
| 109 EXPECT_EQ(kExpectedInt32Value, arg.si32()); |
| 110 EXPECT_EQ(kExpectedInt16Value, arg.si16()); |
| 111 EXPECT_EQ(kExpectedInt8Value, arg.si8()); |
| 112 EXPECT_EQ(kExpectedUInt64Value, arg.ui64()); |
| 113 EXPECT_EQ(kExpectedUInt32Value, arg.ui32()); |
| 114 EXPECT_EQ(kExpectedUInt16Value, arg.ui16()); |
| 115 EXPECT_EQ(kExpectedUInt8Value, arg.ui8()); |
| 116 EXPECT_EQ(kExpectedFloatVal, arg.float_val()); |
| 117 EXPECT_EQ(kExpectedFloatInf, arg.float_inf()); |
| 118 EXPECT_NAN(arg.float_nan()); |
| 119 EXPECT_EQ(kExpectedDoubleVal, arg.double_val()); |
| 120 EXPECT_EQ(kExpectedDoubleInf, arg.double_inf()); |
| 121 EXPECT_NAN(arg.double_nan()); |
| 122 EXPECT_EQ(std::string("coming"), arg.name().To<std::string>()); |
| 123 EXPECT_EQ(std::string("one"), arg.string_array()[0].To<std::string>()); |
| 124 EXPECT_EQ(std::string("two"), arg.string_array()[1].To<std::string>()); |
| 125 EXPECT_EQ(std::string("three"), arg.string_array()[2].To<std::string>()); |
| 126 CheckDataPipe(arg.data_handle().get().value()); |
| 127 } |
| 128 |
| 129 js_to_cpp::EchoArgsList BuildSampleEchoArgsList() { |
| 130 js_to_cpp::EchoArgsList::Builder builder; |
| 131 builder.set_item(BuildSampleEchoArgs()); |
| 132 return builder.Finish(); |
| 133 } |
| 134 |
| 135 void CheckSampleEchoArgsList(const js_to_cpp::EchoArgsList& list) { |
| 136 if (list.is_null()) |
| 137 return; |
| 138 CheckSampleEchoArgs(list.item()); |
| 139 CheckSampleEchoArgsList(list.next()); |
| 140 } |
| 141 |
| 128 void CheckCorruptedString(const mojo::String& arg) { | 142 void CheckCorruptedString(const mojo::String& arg) { |
| 129 // The values don't matter so long as all accesses are within bounds. | 143 // The values don't matter so long as all accesses are within bounds. |
| 130 if (arg.is_null()) | 144 if (arg.is_null()) |
| 131 return; | 145 return; |
| 132 for (size_t i = 0; i < arg.size(); ++i) | 146 for (size_t i = 0; i < arg.size(); ++i) |
| 133 g_waste_accumulator += arg[i]; | 147 g_waste_accumulator += arg[i]; |
| 134 } | 148 } |
| 135 | 149 |
| 136 void CheckCorruptedStringArray(const mojo::Array<mojo::String>& string_array) { | 150 void CheckCorruptedStringArray(const mojo::Array<mojo::String>& string_array) { |
| 137 if (string_array.is_null()) | 151 if (string_array.is_null()) |
| (...skipping 23 matching lines...) Expand all Loading... |
| 161 } | 175 } |
| 162 | 176 |
| 163 virtual void TestFinished() OVERRIDE { | 177 virtual void TestFinished() OVERRIDE { |
| 164 NOTREACHED(); | 178 NOTREACHED(); |
| 165 } | 179 } |
| 166 | 180 |
| 167 virtual void PingResponse() OVERRIDE { | 181 virtual void PingResponse() OVERRIDE { |
| 168 NOTREACHED(); | 182 NOTREACHED(); |
| 169 } | 183 } |
| 170 | 184 |
| 171 virtual void EchoResponse(const js_to_cpp::EchoArgs& arg1, | 185 virtual void EchoResponse(const js_to_cpp::EchoArgsList& list) OVERRIDE { |
| 172 const js_to_cpp::EchoArgs& arg2) OVERRIDE { | |
| 173 NOTREACHED(); | 186 NOTREACHED(); |
| 174 } | 187 } |
| 175 | 188 |
| 176 virtual void BitFlipResponse(const js_to_cpp::EchoArgs& arg1) OVERRIDE { | 189 virtual void BitFlipResponse(const js_to_cpp::EchoArgs& arg1) OVERRIDE { |
| 177 NOTREACHED(); | 190 NOTREACHED(); |
| 178 } | 191 } |
| 179 | 192 |
| 180 protected: | 193 protected: |
| 181 base::RunLoop* run_loop_; | 194 base::RunLoop* run_loop_; |
| 182 js_to_cpp::JsSide* js_side_; | 195 js_to_cpp::JsSide* js_side_; |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 215 public: | 228 public: |
| 216 explicit EchoCppSideConnection() : | 229 explicit EchoCppSideConnection() : |
| 217 message_count_(0), | 230 message_count_(0), |
| 218 termination_seen_(false) { | 231 termination_seen_(false) { |
| 219 } | 232 } |
| 220 virtual ~EchoCppSideConnection() {} | 233 virtual ~EchoCppSideConnection() {} |
| 221 | 234 |
| 222 // js_to_cpp::CppSide: | 235 // js_to_cpp::CppSide: |
| 223 virtual void StartTest() OVERRIDE { | 236 virtual void StartTest() OVERRIDE { |
| 224 AllocationScope scope; | 237 AllocationScope scope; |
| 225 js_side_->Echo(kExpectedMessageCount, BuildSampleEchoArgs()); | 238 js_side_->Echo(kExpectedMessageCount, BuildSampleEchoArgsList()); |
| 226 } | 239 } |
| 227 | 240 |
| 228 virtual void EchoResponse(const js_to_cpp::EchoArgs& arg1, | 241 virtual void EchoResponse(const js_to_cpp::EchoArgsList& list) OVERRIDE { |
| 229 const js_to_cpp::EchoArgs& arg2) OVERRIDE { | 242 const js_to_cpp::EchoArgs& special_arg = list.item(); |
| 230 message_count_ += 1; | 243 message_count_ += 1; |
| 231 CheckSampleEchoArgs(arg1); | 244 EXPECT_EQ(-1, special_arg.si64()); |
| 232 EXPECT_EQ(-1, arg2.si64()); | 245 EXPECT_EQ(-1, special_arg.si32()); |
| 233 EXPECT_EQ(-1, arg2.si32()); | 246 EXPECT_EQ(-1, special_arg.si16()); |
| 234 EXPECT_EQ(-1, arg2.si16()); | 247 EXPECT_EQ(-1, special_arg.si8()); |
| 235 EXPECT_EQ(-1, arg2.si8()); | 248 EXPECT_EQ(std::string("going"), special_arg.name().To<std::string>()); |
| 236 EXPECT_EQ(std::string("going"), arg2.name().To<std::string>()); | 249 CheckSampleEchoArgsList(list.next()); |
| 237 CheckDataPipe(arg2.data_handle().get().value()); | |
| 238 } | 250 } |
| 239 | 251 |
| 240 virtual void TestFinished() OVERRIDE { | 252 virtual void TestFinished() OVERRIDE { |
| 241 termination_seen_ = true; | 253 termination_seen_ = true; |
| 242 run_loop()->Quit(); | 254 run_loop()->Quit(); |
| 243 } | 255 } |
| 244 | 256 |
| 245 bool DidSucceed() { | 257 bool DidSucceed() { |
| 246 return termination_seen_ && message_count_ == kExpectedMessageCount; | 258 return termination_seen_ && message_count_ == kExpectedMessageCount; |
| 247 } | 259 } |
| (...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 346 if (IsRunningOnIsolatedBot()) | 358 if (IsRunningOnIsolatedBot()) |
| 347 return; | 359 return; |
| 348 | 360 |
| 349 BitFlipCppSideConnection cpp_side_connection; | 361 BitFlipCppSideConnection cpp_side_connection; |
| 350 RunTest("mojo/apps/js/test/js_to_cpp_unittest", &cpp_side_connection); | 362 RunTest("mojo/apps/js/test/js_to_cpp_unittest", &cpp_side_connection); |
| 351 EXPECT_TRUE(cpp_side_connection.DidSucceed()); | 363 EXPECT_TRUE(cpp_side_connection.DidSucceed()); |
| 352 } | 364 } |
| 353 | 365 |
| 354 } // namespace js | 366 } // namespace js |
| 355 } // namespace mojo | 367 } // namespace mojo |
| OLD | NEW |