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" |
11 #include "mojo/apps/js/mojo_runner_delegate.h" | 11 #include "mojo/apps/js/mojo_runner_delegate.h" |
| 12 #include "mojo/apps/js/test/js_to_cpp.mojom.h" |
12 #include "mojo/common/common_type_converters.h" | 13 #include "mojo/common/common_type_converters.h" |
13 #include "mojo/common/test/test_utils.h" | 14 #include "mojo/common/test/test_utils.h" |
| 15 #include "mojo/public/cpp/bindings/allocation_scope.h" |
14 #include "mojo/public/cpp/bindings/remote_ptr.h" | 16 #include "mojo/public/cpp/bindings/remote_ptr.h" |
15 #include "mojo/public/cpp/environment/environment.h" | 17 #include "mojo/public/cpp/environment/environment.h" |
16 #include "mojo/public/cpp/system/core.h" | 18 #include "mojo/public/cpp/system/core.h" |
17 #include "mojo/public/cpp/system/macros.h" | 19 #include "mojo/public/cpp/system/macros.h" |
18 #include "mojo/public/interfaces/bindings/tests/sample_interfaces.mojom.h" | |
19 #include "testing/gtest/include/gtest/gtest.h" | 20 #include "testing/gtest/include/gtest/gtest.h" |
20 | 21 |
21 namespace mojo { | 22 namespace mojo { |
22 namespace js { | 23 namespace js { |
23 namespace { | 24 namespace { |
24 | 25 |
| 26 // Negative numbers with different values in each byte, the last of |
| 27 // which can survive promotion to double and back. |
| 28 const int8 kExpectedInt8Value = -65; |
| 29 const int16 kExpectedInt16Value = -16961; |
| 30 const int32 kExpectedInt32Value = -1145258561; |
| 31 const int64 kExpectedInt64Value = -77263311946305LL; |
| 32 |
| 33 // Positive numbers with different values in each byte, the last of |
| 34 // which can survive promotion to double and back. |
| 35 const uint8 kExpectedUInt8Value = 65; |
| 36 const uint16 kExpectedUInt16Value = 16961; |
| 37 const uint32 kExpectedUInt32Value = 1145258561; |
| 38 const uint64 kExpectedUInt64Value = 77263311946305LL; |
| 39 |
| 40 // Double/float values, including special case constants. |
| 41 const double kExpectedDoubleVal = 3.14159265358979323846; |
| 42 const double kExpectedDoubleInf = std::numeric_limits<double>::infinity(); |
| 43 const double kExpectedDoubleNan = std::numeric_limits<double>::quiet_NaN(); |
| 44 const float kExpectedFloatVal = static_cast<float>(kExpectedDoubleVal); |
| 45 const float kExpectedFloatInf = std::numeric_limits<float>::infinity(); |
| 46 const float kExpectedFloatNan = std::numeric_limits<float>::quiet_NaN(); |
| 47 |
| 48 // NaN has the property that it is not equal to itself. |
| 49 #define EXPECT_NAN(x) EXPECT_NE(x, x) |
| 50 |
| 51 bool IsRunningOnIsolatedBot() { |
| 52 // TODO(yzshen): Remove this check once isolated tests are supported on the |
| 53 // Chromium waterfall. (http://crbug.com/351214) |
| 54 const base::FilePath test_file_path( |
| 55 test::GetFilePathForJSResource( |
| 56 "mojo/public/interfaces/bindings/tests/sample_interfaces.mojom")); |
| 57 if (!base::PathExists(test_file_path)) { |
| 58 LOG(WARNING) << "Mojom binding files don't exist. Skipping the test."; |
| 59 return true; |
| 60 } |
| 61 return false; |
| 62 } |
| 63 |
25 // Base Provider implementation class. It's expected that tests subclass and | 64 // Base Provider implementation class. It's expected that tests subclass and |
26 // override the appropriate Provider functions. When test is done quit the | 65 // override the appropriate Provider functions. When test is done quit the |
27 // run_loop(). | 66 // run_loop(). |
28 class ProviderConnection : public sample::Provider { | 67 class CppSideConnection : public js_to_cpp::CppSide { |
29 public: | 68 public: |
30 ProviderConnection() : run_loop_(NULL), client_(NULL) { | 69 CppSideConnection() : run_loop_(NULL), client_(NULL) { |
31 } | 70 } |
32 virtual ~ProviderConnection() {} | 71 virtual ~CppSideConnection() {} |
33 | 72 |
34 void set_run_loop(base::RunLoop* run_loop) { run_loop_ = run_loop; } | 73 void set_run_loop(base::RunLoop* run_loop) { run_loop_ = run_loop; } |
35 base::RunLoop* run_loop() { return run_loop_; } | 74 base::RunLoop* run_loop() { return run_loop_; } |
36 | 75 |
37 void set_client(sample::ProviderClient* client) { client_ = client; } | 76 void set_client(js_to_cpp::JsSide* client) { client_ = client; } |
38 sample::ProviderClient* client() { return client_; } | 77 js_to_cpp::JsSide* client() { return client_; } |
39 | 78 |
40 // sample::Provider: | 79 // js_to_cpp::CppSide: |
41 virtual void EchoString(const String& a, | 80 virtual void StartTest() OVERRIDE { |
42 const Callback<void(String)>& callback) OVERRIDE { | |
43 NOTREACHED(); | 81 NOTREACHED(); |
44 } | 82 } |
45 virtual void EchoStrings( | 83 |
46 const String& a, | 84 virtual void PingResponse() OVERRIDE { |
47 const String& b, | |
48 const Callback<void(String, String)>& callback) OVERRIDE { | |
49 NOTREACHED(); | 85 NOTREACHED(); |
50 } | 86 } |
51 virtual void EchoMessagePipeHandle( | 87 |
52 ScopedMessagePipeHandle a, | 88 virtual void EchoResponse(const js_to_cpp::EchoArgs& arg1, |
53 const Callback<void(ScopedMessagePipeHandle)>& callback) OVERRIDE { | 89 const js_to_cpp::EchoArgs& arg2) OVERRIDE { |
54 NOTREACHED(); | 90 NOTREACHED(); |
55 } | 91 } |
56 virtual void EchoEnum(sample::Enum a, | 92 |
57 const Callback<void(sample::Enum)>& callback) | 93 protected: |
58 OVERRIDE { | 94 base::RunLoop* run_loop_; |
59 NOTREACHED(); | 95 js_to_cpp::JsSide* client_; |
60 } | |
61 | 96 |
62 private: | 97 private: |
63 base::RunLoop* run_loop_; | 98 Environment environment; |
64 sample::ProviderClient* client_; | 99 DISALLOW_COPY_AND_ASSIGN(CppSideConnection); |
65 | |
66 DISALLOW_COPY_AND_ASSIGN(ProviderConnection); | |
67 }; | 100 }; |
68 | 101 |
| 102 // Trivial test to verify a message sent from JS is received. |
| 103 class PingCppSideConnection : public CppSideConnection { |
| 104 public: |
| 105 explicit PingCppSideConnection() : got_message_(false) {} |
| 106 virtual ~PingCppSideConnection() {} |
| 107 |
| 108 // js_to_cpp::CppSide: |
| 109 virtual void StartTest() OVERRIDE { |
| 110 client_->Ping(); |
| 111 } |
| 112 |
| 113 virtual void PingResponse() OVERRIDE { |
| 114 got_message_ = true; |
| 115 run_loop()->Quit(); |
| 116 } |
| 117 |
| 118 bool DidSucceed() { |
| 119 return got_message_; |
| 120 } |
| 121 |
| 122 private: |
| 123 bool got_message_; |
| 124 DISALLOW_COPY_AND_ASSIGN(PingCppSideConnection); |
| 125 }; |
| 126 |
| 127 // Test that parameters are passed with correct values. |
| 128 class EchoCppSideConnection : public CppSideConnection { |
| 129 public: |
| 130 explicit EchoCppSideConnection() : message_count_(0) {} |
| 131 virtual ~EchoCppSideConnection() {} |
| 132 |
| 133 // js_to_cpp::CppSide: |
| 134 virtual void StartTest() OVERRIDE { |
| 135 AllocationScope scope; |
| 136 js_to_cpp::EchoArgs::Builder builder; |
| 137 builder.set_si64(kExpectedInt64Value); |
| 138 builder.set_si32(kExpectedInt32Value); |
| 139 builder.set_si16(kExpectedInt16Value); |
| 140 builder.set_si8(kExpectedInt8Value); |
| 141 builder.set_ui64(kExpectedUInt64Value); |
| 142 builder.set_ui32(kExpectedUInt32Value); |
| 143 builder.set_ui16(kExpectedUInt16Value); |
| 144 builder.set_ui8(kExpectedUInt8Value); |
| 145 builder.set_float_val(kExpectedFloatVal); |
| 146 builder.set_float_inf(kExpectedFloatInf); |
| 147 builder.set_float_nan(kExpectedFloatNan); |
| 148 builder.set_double_val(kExpectedDoubleVal); |
| 149 builder.set_double_inf(kExpectedDoubleInf); |
| 150 builder.set_double_nan(kExpectedDoubleNan); |
| 151 builder.set_name("coming"); |
| 152 mojo::Array<mojo::String>::Builder string_array(3); |
| 153 string_array[0] = "one"; |
| 154 string_array[1] = "two"; |
| 155 string_array[2] = "three"; |
| 156 builder.set_string_array(string_array.Finish()); |
| 157 client_->Echo(builder.Finish()); |
| 158 } |
| 159 |
| 160 virtual void EchoResponse(const js_to_cpp::EchoArgs& arg1, |
| 161 const js_to_cpp::EchoArgs& arg2) OVERRIDE { |
| 162 EXPECT_EQ(kExpectedInt64Value, arg1.si64()); |
| 163 EXPECT_EQ(kExpectedInt32Value, arg1.si32()); |
| 164 EXPECT_EQ(kExpectedInt16Value, arg1.si16()); |
| 165 EXPECT_EQ(kExpectedInt8Value, arg1.si8()); |
| 166 EXPECT_EQ(kExpectedUInt64Value, arg1.ui64()); |
| 167 EXPECT_EQ(kExpectedUInt32Value, arg1.ui32()); |
| 168 EXPECT_EQ(kExpectedUInt16Value, arg1.ui16()); |
| 169 EXPECT_EQ(kExpectedUInt8Value, arg1.ui8()); |
| 170 EXPECT_EQ(kExpectedFloatVal, arg1.float_val()); |
| 171 EXPECT_EQ(kExpectedFloatInf, arg1.float_inf()); |
| 172 EXPECT_NAN(arg1.float_nan()); |
| 173 EXPECT_EQ(kExpectedDoubleVal, arg1.double_val()); |
| 174 EXPECT_EQ(kExpectedDoubleInf, arg1.double_inf()); |
| 175 EXPECT_NAN(arg1.double_nan()); |
| 176 EXPECT_EQ(std::string("coming"), arg1.name().To<std::string>()); |
| 177 EXPECT_EQ(std::string("one"), arg1.string_array()[0].To<std::string>()); |
| 178 EXPECT_EQ(std::string("two"), arg1.string_array()[1].To<std::string>()); |
| 179 EXPECT_EQ(std::string("three"), arg1.string_array()[2].To<std::string>()); |
| 180 |
| 181 EXPECT_EQ(-1, arg2.si64()); |
| 182 EXPECT_EQ(-1, arg2.si32()); |
| 183 EXPECT_EQ(-1, arg2.si16()); |
| 184 EXPECT_EQ(-1, arg2.si8()); |
| 185 EXPECT_EQ(std::string("going"), arg2.name().To<std::string>()); |
| 186 |
| 187 message_count_ += 1; |
| 188 if (message_count_ == kExpectedMessageCount) |
| 189 run_loop_->Quit(); |
| 190 } |
| 191 |
| 192 bool DidSucceed() { |
| 193 return message_count_ == kExpectedMessageCount; |
| 194 } |
| 195 |
| 196 private: |
| 197 static const int kExpectedMessageCount = 100; |
| 198 int message_count_; |
| 199 DISALLOW_COPY_AND_ASSIGN(EchoCppSideConnection); |
| 200 }; |
| 201 |
69 class JsToCppTest : public testing::Test { | 202 class JsToCppTest : public testing::Test { |
70 public: | 203 public: |
71 JsToCppTest() {} | 204 JsToCppTest() {} |
72 | 205 |
73 void RunTest(const std::string& test, ProviderConnection* provider) { | 206 void RunTest(const std::string& test, CppSideConnection* cpp_side) { |
74 provider->set_run_loop(&run_loop_); | 207 cpp_side->set_run_loop(&run_loop_); |
75 InterfacePipe<sample::Provider, sample::ProviderClient> pipe; | 208 InterfacePipe<js_to_cpp::CppSide, js_to_cpp::JsSide> pipe; |
76 RemotePtr<sample::ProviderClient> provider_client; | 209 RemotePtr<js_to_cpp::JsSide> js_side; |
77 provider_client.reset(pipe.handle_to_peer.Pass(), provider); | 210 js_side.reset(pipe.handle_to_peer.Pass(), cpp_side); |
78 | 211 cpp_side->set_client(js_side.get()); |
79 provider->set_client(provider_client.get()); | |
80 | 212 |
81 gin::IsolateHolder instance(gin::IsolateHolder::kStrictMode); | 213 gin::IsolateHolder instance(gin::IsolateHolder::kStrictMode); |
82 apps::MojoRunnerDelegate delegate; | 214 apps::MojoRunnerDelegate delegate; |
83 gin::ShellRunner runner(&delegate, instance.isolate()); | 215 gin::ShellRunner runner(&delegate, instance.isolate()); |
84 delegate.Start(&runner, pipe.handle_to_self.release().value(), | 216 delegate.Start(&runner, pipe.handle_to_self.release().value(), |
85 test); | 217 test); |
86 | 218 |
87 run_loop_.Run(); | 219 run_loop_.Run(); |
88 } | 220 } |
89 | 221 |
90 private: | 222 private: |
91 Environment environment; | |
92 base::MessageLoop loop; | 223 base::MessageLoop loop; |
93 base::RunLoop run_loop_; | 224 base::RunLoop run_loop_; |
94 | 225 |
95 DISALLOW_COPY_AND_ASSIGN(JsToCppTest); | 226 DISALLOW_COPY_AND_ASSIGN(JsToCppTest); |
96 }; | 227 }; |
97 | 228 |
98 // Trivial test to verify a message sent from JS is received. | 229 TEST_F(JsToCppTest, Ping) { |
99 class FromJsProviderConnection : public ProviderConnection { | 230 if (IsRunningOnIsolatedBot()) |
100 public: | 231 return; |
101 explicit FromJsProviderConnection() {} | |
102 virtual ~FromJsProviderConnection() { | |
103 } | |
104 | 232 |
105 const base::string16& echo_string() const { return echo_string_; } | 233 PingCppSideConnection cpp_side_connection; |
| 234 RunTest("mojo/apps/js/test/js_to_cpp_unittest", &cpp_side_connection); |
| 235 EXPECT_TRUE(cpp_side_connection.DidSucceed()); |
| 236 } |
106 | 237 |
107 // Provider: | 238 TEST_F(JsToCppTest, Echo) { |
108 virtual void EchoString(const String& a, | 239 if (IsRunningOnIsolatedBot()) |
109 const Callback<void(String)>& callback) OVERRIDE { | 240 return; |
110 echo_string_ = a.To<base::string16>(); | |
111 run_loop()->Quit(); | |
112 } | |
113 | 241 |
114 private: | 242 EchoCppSideConnection cpp_side_connection; |
115 base::string16 echo_string_; | 243 RunTest("mojo/apps/js/test/js_to_cpp_unittest", &cpp_side_connection); |
116 | 244 EXPECT_TRUE(cpp_side_connection.DidSucceed()); |
117 DISALLOW_COPY_AND_ASSIGN(FromJsProviderConnection); | |
118 }; | |
119 | |
120 TEST_F(JsToCppTest, FromJS) { | |
121 // TODO(yzshen): Remove this check once isolated tests are supported on the | |
122 // Chromium waterfall. (http://crbug.com/351214) | |
123 const base::FilePath test_file_path( | |
124 test::GetFilePathForJSResource( | |
125 "mojo/public/interfaces/bindings/tests/sample_interfaces.mojom")); | |
126 if (!base::PathExists(test_file_path)) { | |
127 LOG(WARNING) << "Mojom binding files don't exist. Skipping the test."; | |
128 return; | |
129 } | |
130 | |
131 FromJsProviderConnection provider; | |
132 RunTest("mojo/apps/js/test/js_to_cpp_unittest", &provider); | |
133 EXPECT_EQ("message", base::UTF16ToASCII(provider.echo_string())); | |
134 } | 245 } |
135 | 246 |
136 } // namespace | 247 } // namespace |
137 } // namespace js | 248 } // namespace js |
138 } // namespace mojo | 249 } // namespace mojo |
OLD | NEW |