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/common/common_type_converters.h" | 12 #include "mojo/common/common_type_converters.h" |
13 #include "mojo/common/test/test_utils.h" | 13 #include "mojo/common/test/test_utils.h" |
14 #include "mojo/public/cpp/bindings/allocation_scope.h" | |
14 #include "mojo/public/cpp/bindings/remote_ptr.h" | 15 #include "mojo/public/cpp/bindings/remote_ptr.h" |
15 #include "mojo/public/cpp/environment/environment.h" | 16 #include "mojo/public/cpp/environment/environment.h" |
16 #include "mojo/public/cpp/system/core.h" | 17 #include "mojo/public/cpp/system/core.h" |
17 #include "mojo/public/cpp/system/macros.h" | 18 #include "mojo/public/cpp/system/macros.h" |
18 #include "mojo/public/interfaces/bindings/tests/sample_interfaces.mojom.h" | 19 #include "mojo/public/interfaces/bindings/tests/js_to_cpp.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 bool got_message = false; | |
27 int message_count = 0; | |
sky
2014/05/01 03:20:38
Can you scope these to JsToCppTest (or the subclas
Tom Sepez
2014/05/01 17:48:26
Moved to PingCppSideConnection / EchoCppSideConnec
| |
28 | |
29 const int kExpectedMessageCount = 100; | |
30 | |
31 // Negative numbers with different values in each byte, the last of | |
32 // which can survive promotion to double and back. | |
33 const int8 kExpectedInt8Value = -65; | |
34 const int16 kExpectedInt16Value = -16961; | |
35 const int32 kExpectedInt32Value = -1145258561; | |
36 const int64 kExpectedInt64Value = -77263311946305LL; | |
37 | |
38 // Positive numbers with different values in each byte, the last of | |
39 // which can survive promotion to double and back. | |
40 const uint8 kExpectedUInt8Value = 65; | |
41 const uint16 kExpectedUInt16Value = 16961; | |
42 const uint32 kExpectedUInt32Value = 1145258561; | |
43 const uint64 kExpectedUInt64Value = 77263311946305LL; | |
44 | |
45 // Double/float values, including special case constants. | |
46 const double kExpectedDoubleVal = 3.14159265358979323846; | |
47 const double kExpectedDoubleInf = std::numeric_limits<double>::infinity(); | |
48 const double kExpectedDoubleNan = std::numeric_limits<double>::quiet_NaN(); | |
49 const float kExpectedFloatVal = static_cast<float>(kExpectedDoubleVal); | |
50 const float kExpectedFloatInf = std::numeric_limits<float>::infinity(); | |
51 const float kExpectedFloatNan = std::numeric_limits<float>::quiet_NaN(); | |
52 | |
53 // NaN has the property that it is not equal to itself. | |
54 #define EXPECT_NAN(x) EXPECT_NE(x, x) | |
55 | |
56 bool IsRunningOnIsolatedBot() { | |
57 // TODO(yzshen): Remove this check once isolated tests are supported on the | |
58 // Chromium waterfall. (http://crbug.com/351214) | |
59 const base::FilePath test_file_path( | |
60 test::GetFilePathForJSResource( | |
61 "mojo/public/interfaces/bindings/tests/sample_interfaces.mojom")); | |
62 if (!base::PathExists(test_file_path)) { | |
63 LOG(WARNING) << "Mojom binding files don't exist. Skipping the test."; | |
64 return true; | |
65 } | |
66 return false; | |
67 } | |
68 | |
25 // Base Provider implementation class. It's expected that tests subclass and | 69 // Base Provider implementation class. It's expected that tests subclass and |
26 // override the appropriate Provider functions. When test is done quit the | 70 // override the appropriate Provider functions. When test is done quit the |
27 // run_loop(). | 71 // run_loop(). |
28 class ProviderConnection : public sample::Provider { | 72 class CppSideConnection : public js_to_cpp::CppSide { |
29 public: | 73 public: |
30 ProviderConnection() : run_loop_(NULL), client_(NULL) { | 74 CppSideConnection() : run_loop_(NULL), client_(NULL) { |
31 } | 75 } |
32 virtual ~ProviderConnection() {} | 76 virtual ~CppSideConnection() {} |
33 | 77 |
34 void set_run_loop(base::RunLoop* run_loop) { run_loop_ = run_loop; } | 78 void set_run_loop(base::RunLoop* run_loop) { run_loop_ = run_loop; } |
35 base::RunLoop* run_loop() { return run_loop_; } | 79 base::RunLoop* run_loop() { return run_loop_; } |
36 | 80 |
37 void set_client(sample::ProviderClient* client) { client_ = client; } | 81 void set_client(js_to_cpp::JsSide* client) { client_ = client; } |
38 sample::ProviderClient* client() { return client_; } | 82 js_to_cpp::JsSide* client() { return client_; } |
39 | 83 |
40 // sample::Provider: | 84 // js_to_cpp::CppSide: |
41 virtual void EchoString(const String& a, | 85 virtual void StartTest() OVERRIDE { |
42 const Callback<void(String)>& callback) OVERRIDE { | |
43 NOTREACHED(); | |
44 } | |
45 virtual void EchoStrings( | |
46 const String& a, | |
47 const String& b, | |
48 const Callback<void(String, String)>& callback) OVERRIDE { | |
49 NOTREACHED(); | |
50 } | |
51 virtual void EchoMessagePipeHandle( | |
52 ScopedMessagePipeHandle a, | |
53 const Callback<void(ScopedMessagePipeHandle)>& callback) OVERRIDE { | |
54 NOTREACHED(); | |
55 } | |
56 virtual void EchoEnum(sample::Enum a, | |
57 const Callback<void(sample::Enum)>& callback) | |
58 OVERRIDE { | |
59 NOTREACHED(); | 86 NOTREACHED(); |
60 } | 87 } |
61 | 88 |
89 virtual void PingResponse() OVERRIDE { | |
90 NOTREACHED(); | |
91 } | |
92 | |
93 virtual void EchoResponse(const js_to_cpp::EchoArgs& arg1, | |
94 const js_to_cpp::EchoArgs& arg2) OVERRIDE { | |
95 NOTREACHED(); | |
96 } | |
97 | |
98 protected: | |
99 base::RunLoop* run_loop_; | |
100 js_to_cpp::JsSide* client_; | |
101 | |
62 private: | 102 private: |
63 base::RunLoop* run_loop_; | 103 Environment environment; |
64 sample::ProviderClient* client_; | 104 DISALLOW_COPY_AND_ASSIGN(CppSideConnection); |
105 }; | |
65 | 106 |
66 DISALLOW_COPY_AND_ASSIGN(ProviderConnection); | 107 // Trivial test to verify a message sent from JS is received. |
108 class PingCppSideConnection : public CppSideConnection { | |
109 public: | |
110 explicit PingCppSideConnection() {} | |
111 virtual ~PingCppSideConnection() {} | |
112 | |
113 // js_to_cpp::CppSide: | |
114 virtual void StartTest() OVERRIDE { | |
115 client_->Ping(); | |
116 } | |
117 | |
118 virtual void PingResponse() OVERRIDE { | |
119 got_message = true; | |
120 run_loop()->Quit(); | |
121 } | |
122 | |
123 private: | |
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() {} | |
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 private: | |
193 DISALLOW_COPY_AND_ASSIGN(EchoCppSideConnection); | |
67 }; | 194 }; |
68 | 195 |
69 class JsToCppTest : public testing::Test { | 196 class JsToCppTest : public testing::Test { |
70 public: | 197 public: |
71 JsToCppTest() {} | 198 JsToCppTest() {} |
72 | 199 |
73 void RunTest(const std::string& test, ProviderConnection* provider) { | 200 void RunTest(const std::string& test, CppSideConnection* cpp_side) { |
74 provider->set_run_loop(&run_loop_); | 201 cpp_side->set_run_loop(&run_loop_); |
75 InterfacePipe<sample::Provider, sample::ProviderClient> pipe; | 202 InterfacePipe<js_to_cpp::CppSide, js_to_cpp::JsSide> pipe; |
76 RemotePtr<sample::ProviderClient> provider_client; | 203 RemotePtr<js_to_cpp::JsSide> js_side; |
77 provider_client.reset(pipe.handle_to_peer.Pass(), provider); | 204 js_side.reset(pipe.handle_to_peer.Pass(), cpp_side); |
78 | 205 cpp_side->set_client(js_side.get()); |
79 provider->set_client(provider_client.get()); | |
80 | 206 |
81 gin::IsolateHolder instance(gin::IsolateHolder::kStrictMode); | 207 gin::IsolateHolder instance(gin::IsolateHolder::kStrictMode); |
82 apps::MojoRunnerDelegate delegate; | 208 apps::MojoRunnerDelegate delegate; |
83 gin::ShellRunner runner(&delegate, instance.isolate()); | 209 gin::ShellRunner runner(&delegate, instance.isolate()); |
84 delegate.Start(&runner, pipe.handle_to_self.release().value(), | 210 delegate.Start(&runner, pipe.handle_to_self.release().value(), |
85 test); | 211 test); |
86 | 212 |
87 run_loop_.Run(); | 213 run_loop_.Run(); |
88 } | 214 } |
89 | 215 |
90 private: | 216 private: |
91 Environment environment; | |
92 base::MessageLoop loop; | 217 base::MessageLoop loop; |
93 base::RunLoop run_loop_; | 218 base::RunLoop run_loop_; |
94 | 219 |
95 DISALLOW_COPY_AND_ASSIGN(JsToCppTest); | 220 DISALLOW_COPY_AND_ASSIGN(JsToCppTest); |
96 }; | 221 }; |
97 | 222 |
98 // Trivial test to verify a message sent from JS is received. | 223 TEST_F(JsToCppTest, Ping) { |
99 class FromJsProviderConnection : public ProviderConnection { | 224 if (IsRunningOnIsolatedBot()) |
100 public: | 225 return; |
101 explicit FromJsProviderConnection() {} | 226 got_message = false; |
102 virtual ~FromJsProviderConnection() { | 227 PingCppSideConnection cpp_side_connection; |
103 } | 228 RunTest("mojo/apps/js/test/js_to_cpp_unittest", &cpp_side_connection); |
229 EXPECT_TRUE(got_message); | |
230 } | |
104 | 231 |
105 const base::string16& echo_string() const { return echo_string_; } | 232 TEST_F(JsToCppTest, Echo) { |
233 if (IsRunningOnIsolatedBot()) | |
234 return; | |
106 | 235 |
107 // Provider: | 236 message_count = 0; |
108 virtual void EchoString(const String& a, | 237 EchoCppSideConnection cpp_side_connection; |
109 const Callback<void(String)>& callback) OVERRIDE { | 238 RunTest("mojo/apps/js/test/js_to_cpp_unittest", &cpp_side_connection); |
110 echo_string_ = a.To<base::string16>(); | 239 EXPECT_EQ(kExpectedMessageCount, message_count); |
111 run_loop()->Quit(); | |
112 } | |
113 | |
114 private: | |
115 base::string16 echo_string_; | |
116 | |
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 } | 240 } |
135 | 241 |
136 } // namespace | 242 } // namespace |
137 } // namespace js | 243 } // namespace js |
138 } // namespace mojo | 244 } // namespace mojo |
OLD | NEW |