| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 <stdint.h> | 5 #include <stdint.h> |
| 6 #include <utility> |
| 6 | 7 |
| 7 #include "base/message_loop/message_loop.h" | 8 #include "base/message_loop/message_loop.h" |
| 8 #include "mojo/message_pump/message_pump_mojo.h" | 9 #include "mojo/message_pump/message_pump_mojo.h" |
| 9 #include "mojo/public/cpp/bindings/binding.h" | 10 #include "mojo/public/cpp/bindings/binding.h" |
| 10 #include "mojo/public/cpp/bindings/strong_binding.h" | 11 #include "mojo/public/cpp/bindings/strong_binding.h" |
| 11 #include "mojo/public/interfaces/bindings/tests/math_calculator.mojom.h" | 12 #include "mojo/public/interfaces/bindings/tests/math_calculator.mojom.h" |
| 12 #include "mojo/public/interfaces/bindings/tests/sample_interfaces.mojom.h" | 13 #include "mojo/public/interfaces/bindings/tests/sample_interfaces.mojom.h" |
| 13 #include "mojo/public/interfaces/bindings/tests/sample_service.mojom.h" | 14 #include "mojo/public/interfaces/bindings/tests/sample_service.mojom.h" |
| 14 #include "mojo/public/interfaces/bindings/tests/scoping.mojom.h" | 15 #include "mojo/public/interfaces/bindings/tests/scoping.mojom.h" |
| 15 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
| (...skipping 20 matching lines...) Expand all Loading... |
| 36 template <typename Method, typename Class> | 37 template <typename Method, typename Class> |
| 37 RunnableImpl<Method, Class> MakeRunnable(Method method, Class object) { | 38 RunnableImpl<Method, Class> MakeRunnable(Method method, Class object) { |
| 38 return RunnableImpl<Method, Class>(method, object); | 39 return RunnableImpl<Method, Class>(method, object); |
| 39 } | 40 } |
| 40 | 41 |
| 41 typedef mojo::Callback<void(double)> CalcCallback; | 42 typedef mojo::Callback<void(double)> CalcCallback; |
| 42 | 43 |
| 43 class MathCalculatorImpl : public math::Calculator { | 44 class MathCalculatorImpl : public math::Calculator { |
| 44 public: | 45 public: |
| 45 explicit MathCalculatorImpl(InterfaceRequest<math::Calculator> request) | 46 explicit MathCalculatorImpl(InterfaceRequest<math::Calculator> request) |
| 46 : total_(0.0), binding_(this, request.Pass()) {} | 47 : total_(0.0), binding_(this, std::move(request)) {} |
| 47 ~MathCalculatorImpl() override {} | 48 ~MathCalculatorImpl() override {} |
| 48 | 49 |
| 49 void CloseMessagePipe() { binding_.Close(); } | 50 void CloseMessagePipe() { binding_.Close(); } |
| 50 | 51 |
| 51 void WaitForIncomingMethodCall() { binding_.WaitForIncomingMethodCall(); } | 52 void WaitForIncomingMethodCall() { binding_.WaitForIncomingMethodCall(); } |
| 52 | 53 |
| 53 void Clear(const CalcCallback& callback) override { | 54 void Clear(const CalcCallback& callback) override { |
| 54 total_ = 0.0; | 55 total_ = 0.0; |
| 55 callback.Run(total_); | 56 callback.Run(total_); |
| 56 } | 57 } |
| 57 | 58 |
| 58 void Add(double value, const CalcCallback& callback) override { | 59 void Add(double value, const CalcCallback& callback) override { |
| 59 total_ += value; | 60 total_ += value; |
| 60 callback.Run(total_); | 61 callback.Run(total_); |
| 61 } | 62 } |
| 62 | 63 |
| 63 void Multiply(double value, const CalcCallback& callback) override { | 64 void Multiply(double value, const CalcCallback& callback) override { |
| 64 total_ *= value; | 65 total_ *= value; |
| 65 callback.Run(total_); | 66 callback.Run(total_); |
| 66 } | 67 } |
| 67 | 68 |
| 68 private: | 69 private: |
| 69 double total_; | 70 double total_; |
| 70 Binding<math::Calculator> binding_; | 71 Binding<math::Calculator> binding_; |
| 71 }; | 72 }; |
| 72 | 73 |
| 73 class MathCalculatorUI { | 74 class MathCalculatorUI { |
| 74 public: | 75 public: |
| 75 explicit MathCalculatorUI(math::CalculatorPtr calculator) | 76 explicit MathCalculatorUI(math::CalculatorPtr calculator) |
| 76 : calculator_(calculator.Pass()), | 77 : calculator_(std::move(calculator)), |
| 77 output_(0.0), | 78 output_(0.0), |
| 78 callback_(MakeRunnable(&MathCalculatorUI::Output, this)) {} | 79 callback_(MakeRunnable(&MathCalculatorUI::Output, this)) {} |
| 79 | 80 |
| 80 bool WaitForIncomingResponse() { | 81 bool WaitForIncomingResponse() { |
| 81 return calculator_.WaitForIncomingResponse(); | 82 return calculator_.WaitForIncomingResponse(); |
| 82 } | 83 } |
| 83 | 84 |
| 84 bool encountered_error() const { return calculator_.encountered_error(); } | 85 bool encountered_error() const { return calculator_.encountered_error(); } |
| 85 | 86 |
| 86 void Add(double value) { calculator_->Add(value, callback_); } | 87 void Add(double value) { calculator_->Add(value, callback_); } |
| (...skipping 10 matching lines...) Expand all Loading... |
| 97 void Output(double output) { output_ = output; } | 98 void Output(double output) { output_ = output; } |
| 98 | 99 |
| 99 math::CalculatorPtr calculator_; | 100 math::CalculatorPtr calculator_; |
| 100 double output_; | 101 double output_; |
| 101 Callback<void(double)> callback_; | 102 Callback<void(double)> callback_; |
| 102 }; | 103 }; |
| 103 | 104 |
| 104 class SelfDestructingMathCalculatorUI { | 105 class SelfDestructingMathCalculatorUI { |
| 105 public: | 106 public: |
| 106 explicit SelfDestructingMathCalculatorUI(math::CalculatorPtr calculator) | 107 explicit SelfDestructingMathCalculatorUI(math::CalculatorPtr calculator) |
| 107 : calculator_(calculator.Pass()), nesting_level_(0) { | 108 : calculator_(std::move(calculator)), nesting_level_(0) { |
| 108 ++num_instances_; | 109 ++num_instances_; |
| 109 } | 110 } |
| 110 | 111 |
| 111 void BeginTest(bool nested) { | 112 void BeginTest(bool nested) { |
| 112 nesting_level_ = nested ? 2 : 1; | 113 nesting_level_ = nested ? 2 : 1; |
| 113 calculator_->Add( | 114 calculator_->Add( |
| 114 1.0, MakeRunnable(&SelfDestructingMathCalculatorUI::Output, this)); | 115 1.0, MakeRunnable(&SelfDestructingMathCalculatorUI::Output, this)); |
| 115 } | 116 } |
| 116 | 117 |
| 117 static int num_instances() { return num_instances_; } | 118 static int num_instances() { return num_instances_; } |
| (...skipping 18 matching lines...) Expand all Loading... |
| 136 }; | 137 }; |
| 137 | 138 |
| 138 // static | 139 // static |
| 139 int SelfDestructingMathCalculatorUI::num_instances_ = 0; | 140 int SelfDestructingMathCalculatorUI::num_instances_ = 0; |
| 140 | 141 |
| 141 class ReentrantServiceImpl : public sample::Service { | 142 class ReentrantServiceImpl : public sample::Service { |
| 142 public: | 143 public: |
| 143 ~ReentrantServiceImpl() override {} | 144 ~ReentrantServiceImpl() override {} |
| 144 | 145 |
| 145 explicit ReentrantServiceImpl(InterfaceRequest<sample::Service> request) | 146 explicit ReentrantServiceImpl(InterfaceRequest<sample::Service> request) |
| 146 : call_depth_(0), max_call_depth_(0), binding_(this, request.Pass()) {} | 147 : call_depth_(0), |
| 148 max_call_depth_(0), |
| 149 binding_(this, std::move(request)) {} |
| 147 | 150 |
| 148 int max_call_depth() { return max_call_depth_; } | 151 int max_call_depth() { return max_call_depth_; } |
| 149 | 152 |
| 150 void Frobinate(sample::FooPtr foo, | 153 void Frobinate(sample::FooPtr foo, |
| 151 sample::Service::BazOptions baz, | 154 sample::Service::BazOptions baz, |
| 152 sample::PortPtr port, | 155 sample::PortPtr port, |
| 153 const sample::Service::FrobinateCallback& callback) override { | 156 const sample::Service::FrobinateCallback& callback) override { |
| 154 max_call_depth_ = std::max(++call_depth_, max_call_depth_); | 157 max_call_depth_ = std::max(++call_depth_, max_call_depth_); |
| 155 if (call_depth_ == 1) { | 158 if (call_depth_ == 1) { |
| 156 EXPECT_TRUE(binding_.WaitForIncomingMethodCall()); | 159 EXPECT_TRUE(binding_.WaitForIncomingMethodCall()); |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 200 EXPECT_FALSE(calc.is_bound()); | 203 EXPECT_FALSE(calc.is_bound()); |
| 201 MathCalculatorImpl calc_impl(GetProxy(&calc)); | 204 MathCalculatorImpl calc_impl(GetProxy(&calc)); |
| 202 EXPECT_TRUE(calc.is_bound()); | 205 EXPECT_TRUE(calc.is_bound()); |
| 203 } | 206 } |
| 204 | 207 |
| 205 TEST_F(InterfacePtrTest, EndToEnd) { | 208 TEST_F(InterfacePtrTest, EndToEnd) { |
| 206 math::CalculatorPtr calc; | 209 math::CalculatorPtr calc; |
| 207 MathCalculatorImpl calc_impl(GetProxy(&calc)); | 210 MathCalculatorImpl calc_impl(GetProxy(&calc)); |
| 208 | 211 |
| 209 // Suppose this is instantiated in a process that has pipe1_. | 212 // Suppose this is instantiated in a process that has pipe1_. |
| 210 MathCalculatorUI calculator_ui(calc.Pass()); | 213 MathCalculatorUI calculator_ui(std::move(calc)); |
| 211 | 214 |
| 212 calculator_ui.Add(2.0); | 215 calculator_ui.Add(2.0); |
| 213 calculator_ui.Multiply(5.0); | 216 calculator_ui.Multiply(5.0); |
| 214 | 217 |
| 215 PumpMessages(); | 218 PumpMessages(); |
| 216 | 219 |
| 217 EXPECT_EQ(10.0, calculator_ui.GetOutput()); | 220 EXPECT_EQ(10.0, calculator_ui.GetOutput()); |
| 218 } | 221 } |
| 219 | 222 |
| 220 TEST_F(InterfacePtrTest, EndToEnd_Synchronous) { | 223 TEST_F(InterfacePtrTest, EndToEnd_Synchronous) { |
| 221 math::CalculatorPtr calc; | 224 math::CalculatorPtr calc; |
| 222 MathCalculatorImpl calc_impl(GetProxy(&calc)); | 225 MathCalculatorImpl calc_impl(GetProxy(&calc)); |
| 223 | 226 |
| 224 // Suppose this is instantiated in a process that has pipe1_. | 227 // Suppose this is instantiated in a process that has pipe1_. |
| 225 MathCalculatorUI calculator_ui(calc.Pass()); | 228 MathCalculatorUI calculator_ui(std::move(calc)); |
| 226 | 229 |
| 227 EXPECT_EQ(0.0, calculator_ui.GetOutput()); | 230 EXPECT_EQ(0.0, calculator_ui.GetOutput()); |
| 228 | 231 |
| 229 calculator_ui.Add(2.0); | 232 calculator_ui.Add(2.0); |
| 230 EXPECT_EQ(0.0, calculator_ui.GetOutput()); | 233 EXPECT_EQ(0.0, calculator_ui.GetOutput()); |
| 231 calc_impl.WaitForIncomingMethodCall(); | 234 calc_impl.WaitForIncomingMethodCall(); |
| 232 calculator_ui.WaitForIncomingResponse(); | 235 calculator_ui.WaitForIncomingResponse(); |
| 233 EXPECT_EQ(2.0, calculator_ui.GetOutput()); | 236 EXPECT_EQ(2.0, calculator_ui.GetOutput()); |
| 234 | 237 |
| 235 calculator_ui.Multiply(5.0); | 238 calculator_ui.Multiply(5.0); |
| 236 EXPECT_EQ(2.0, calculator_ui.GetOutput()); | 239 EXPECT_EQ(2.0, calculator_ui.GetOutput()); |
| 237 calc_impl.WaitForIncomingMethodCall(); | 240 calc_impl.WaitForIncomingMethodCall(); |
| 238 calculator_ui.WaitForIncomingResponse(); | 241 calculator_ui.WaitForIncomingResponse(); |
| 239 EXPECT_EQ(10.0, calculator_ui.GetOutput()); | 242 EXPECT_EQ(10.0, calculator_ui.GetOutput()); |
| 240 } | 243 } |
| 241 | 244 |
| 242 TEST_F(InterfacePtrTest, Movable) { | 245 TEST_F(InterfacePtrTest, Movable) { |
| 243 math::CalculatorPtr a; | 246 math::CalculatorPtr a; |
| 244 math::CalculatorPtr b; | 247 math::CalculatorPtr b; |
| 245 MathCalculatorImpl calc_impl(GetProxy(&b)); | 248 MathCalculatorImpl calc_impl(GetProxy(&b)); |
| 246 | 249 |
| 247 EXPECT_TRUE(!a); | 250 EXPECT_TRUE(!a); |
| 248 EXPECT_FALSE(!b); | 251 EXPECT_FALSE(!b); |
| 249 | 252 |
| 250 a = b.Pass(); | 253 a = std::move(b); |
| 251 | 254 |
| 252 EXPECT_FALSE(!a); | 255 EXPECT_FALSE(!a); |
| 253 EXPECT_TRUE(!b); | 256 EXPECT_TRUE(!b); |
| 254 } | 257 } |
| 255 | 258 |
| 256 TEST_F(InterfacePtrTest, Resettable) { | 259 TEST_F(InterfacePtrTest, Resettable) { |
| 257 math::CalculatorPtr a; | 260 math::CalculatorPtr a; |
| 258 | 261 |
| 259 EXPECT_TRUE(!a); | 262 EXPECT_TRUE(!a); |
| 260 | 263 |
| 261 MessagePipe pipe; | 264 MessagePipe pipe; |
| 262 | 265 |
| 263 // Save this so we can test it later. | 266 // Save this so we can test it later. |
| 264 Handle handle = pipe.handle0.get(); | 267 Handle handle = pipe.handle0.get(); |
| 265 | 268 |
| 266 a = MakeProxy(InterfacePtrInfo<math::Calculator>(pipe.handle0.Pass(), 0u)); | 269 a = MakeProxy( |
| 270 InterfacePtrInfo<math::Calculator>(std::move(pipe.handle0), 0u)); |
| 267 | 271 |
| 268 EXPECT_FALSE(!a); | 272 EXPECT_FALSE(!a); |
| 269 | 273 |
| 270 a.reset(); | 274 a.reset(); |
| 271 | 275 |
| 272 EXPECT_TRUE(!a); | 276 EXPECT_TRUE(!a); |
| 273 EXPECT_FALSE(a.internal_state()->is_bound()); | 277 EXPECT_FALSE(a.internal_state()->is_bound()); |
| 274 | 278 |
| 275 // Test that handle was closed. | 279 // Test that handle was closed. |
| 276 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, CloseRaw(handle)); | 280 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, CloseRaw(handle)); |
| 277 } | 281 } |
| 278 | 282 |
| 279 TEST_F(InterfacePtrTest, BindInvalidHandle) { | 283 TEST_F(InterfacePtrTest, BindInvalidHandle) { |
| 280 math::CalculatorPtr ptr; | 284 math::CalculatorPtr ptr; |
| 281 EXPECT_FALSE(ptr.get()); | 285 EXPECT_FALSE(ptr.get()); |
| 282 EXPECT_FALSE(ptr); | 286 EXPECT_FALSE(ptr); |
| 283 | 287 |
| 284 ptr.Bind(InterfacePtrInfo<math::Calculator>()); | 288 ptr.Bind(InterfacePtrInfo<math::Calculator>()); |
| 285 EXPECT_FALSE(ptr.get()); | 289 EXPECT_FALSE(ptr.get()); |
| 286 EXPECT_FALSE(ptr); | 290 EXPECT_FALSE(ptr); |
| 287 } | 291 } |
| 288 | 292 |
| 289 TEST_F(InterfacePtrTest, EncounteredError) { | 293 TEST_F(InterfacePtrTest, EncounteredError) { |
| 290 math::CalculatorPtr proxy; | 294 math::CalculatorPtr proxy; |
| 291 MathCalculatorImpl calc_impl(GetProxy(&proxy)); | 295 MathCalculatorImpl calc_impl(GetProxy(&proxy)); |
| 292 | 296 |
| 293 MathCalculatorUI calculator_ui(proxy.Pass()); | 297 MathCalculatorUI calculator_ui(std::move(proxy)); |
| 294 | 298 |
| 295 calculator_ui.Add(2.0); | 299 calculator_ui.Add(2.0); |
| 296 PumpMessages(); | 300 PumpMessages(); |
| 297 EXPECT_EQ(2.0, calculator_ui.GetOutput()); | 301 EXPECT_EQ(2.0, calculator_ui.GetOutput()); |
| 298 EXPECT_FALSE(calculator_ui.encountered_error()); | 302 EXPECT_FALSE(calculator_ui.encountered_error()); |
| 299 | 303 |
| 300 calculator_ui.Multiply(5.0); | 304 calculator_ui.Multiply(5.0); |
| 301 EXPECT_FALSE(calculator_ui.encountered_error()); | 305 EXPECT_FALSE(calculator_ui.encountered_error()); |
| 302 | 306 |
| 303 // Close the server. | 307 // Close the server. |
| 304 calc_impl.CloseMessagePipe(); | 308 calc_impl.CloseMessagePipe(); |
| 305 | 309 |
| 306 // The state change isn't picked up locally yet. | 310 // The state change isn't picked up locally yet. |
| 307 EXPECT_FALSE(calculator_ui.encountered_error()); | 311 EXPECT_FALSE(calculator_ui.encountered_error()); |
| 308 | 312 |
| 309 PumpMessages(); | 313 PumpMessages(); |
| 310 | 314 |
| 311 // OK, now we see the error. | 315 // OK, now we see the error. |
| 312 EXPECT_TRUE(calculator_ui.encountered_error()); | 316 EXPECT_TRUE(calculator_ui.encountered_error()); |
| 313 } | 317 } |
| 314 | 318 |
| 315 TEST_F(InterfacePtrTest, EncounteredErrorCallback) { | 319 TEST_F(InterfacePtrTest, EncounteredErrorCallback) { |
| 316 math::CalculatorPtr proxy; | 320 math::CalculatorPtr proxy; |
| 317 MathCalculatorImpl calc_impl(GetProxy(&proxy)); | 321 MathCalculatorImpl calc_impl(GetProxy(&proxy)); |
| 318 | 322 |
| 319 bool encountered_error = false; | 323 bool encountered_error = false; |
| 320 proxy.set_connection_error_handler( | 324 proxy.set_connection_error_handler( |
| 321 [&encountered_error]() { encountered_error = true; }); | 325 [&encountered_error]() { encountered_error = true; }); |
| 322 | 326 |
| 323 MathCalculatorUI calculator_ui(proxy.Pass()); | 327 MathCalculatorUI calculator_ui(std::move(proxy)); |
| 324 | 328 |
| 325 calculator_ui.Add(2.0); | 329 calculator_ui.Add(2.0); |
| 326 PumpMessages(); | 330 PumpMessages(); |
| 327 EXPECT_EQ(2.0, calculator_ui.GetOutput()); | 331 EXPECT_EQ(2.0, calculator_ui.GetOutput()); |
| 328 EXPECT_FALSE(calculator_ui.encountered_error()); | 332 EXPECT_FALSE(calculator_ui.encountered_error()); |
| 329 | 333 |
| 330 calculator_ui.Multiply(5.0); | 334 calculator_ui.Multiply(5.0); |
| 331 EXPECT_FALSE(calculator_ui.encountered_error()); | 335 EXPECT_FALSE(calculator_ui.encountered_error()); |
| 332 | 336 |
| 333 // Close the server. | 337 // Close the server. |
| (...skipping 12 matching lines...) Expand all Loading... |
| 346 EXPECT_TRUE(encountered_error); | 350 EXPECT_TRUE(encountered_error); |
| 347 } | 351 } |
| 348 | 352 |
| 349 TEST_F(InterfacePtrTest, DestroyInterfacePtrOnMethodResponse) { | 353 TEST_F(InterfacePtrTest, DestroyInterfacePtrOnMethodResponse) { |
| 350 math::CalculatorPtr proxy; | 354 math::CalculatorPtr proxy; |
| 351 MathCalculatorImpl calc_impl(GetProxy(&proxy)); | 355 MathCalculatorImpl calc_impl(GetProxy(&proxy)); |
| 352 | 356 |
| 353 EXPECT_EQ(0, SelfDestructingMathCalculatorUI::num_instances()); | 357 EXPECT_EQ(0, SelfDestructingMathCalculatorUI::num_instances()); |
| 354 | 358 |
| 355 SelfDestructingMathCalculatorUI* impl = | 359 SelfDestructingMathCalculatorUI* impl = |
| 356 new SelfDestructingMathCalculatorUI(proxy.Pass()); | 360 new SelfDestructingMathCalculatorUI(std::move(proxy)); |
| 357 impl->BeginTest(false); | 361 impl->BeginTest(false); |
| 358 | 362 |
| 359 PumpMessages(); | 363 PumpMessages(); |
| 360 | 364 |
| 361 EXPECT_EQ(0, SelfDestructingMathCalculatorUI::num_instances()); | 365 EXPECT_EQ(0, SelfDestructingMathCalculatorUI::num_instances()); |
| 362 } | 366 } |
| 363 | 367 |
| 364 TEST_F(InterfacePtrTest, NestedDestroyInterfacePtrOnMethodResponse) { | 368 TEST_F(InterfacePtrTest, NestedDestroyInterfacePtrOnMethodResponse) { |
| 365 math::CalculatorPtr proxy; | 369 math::CalculatorPtr proxy; |
| 366 MathCalculatorImpl calc_impl(GetProxy(&proxy)); | 370 MathCalculatorImpl calc_impl(GetProxy(&proxy)); |
| 367 | 371 |
| 368 EXPECT_EQ(0, SelfDestructingMathCalculatorUI::num_instances()); | 372 EXPECT_EQ(0, SelfDestructingMathCalculatorUI::num_instances()); |
| 369 | 373 |
| 370 SelfDestructingMathCalculatorUI* impl = | 374 SelfDestructingMathCalculatorUI* impl = |
| 371 new SelfDestructingMathCalculatorUI(proxy.Pass()); | 375 new SelfDestructingMathCalculatorUI(std::move(proxy)); |
| 372 impl->BeginTest(true); | 376 impl->BeginTest(true); |
| 373 | 377 |
| 374 PumpMessages(); | 378 PumpMessages(); |
| 375 | 379 |
| 376 EXPECT_EQ(0, SelfDestructingMathCalculatorUI::num_instances()); | 380 EXPECT_EQ(0, SelfDestructingMathCalculatorUI::num_instances()); |
| 377 } | 381 } |
| 378 | 382 |
| 379 TEST_F(InterfacePtrTest, ReentrantWaitForIncomingMethodCall) { | 383 TEST_F(InterfacePtrTest, ReentrantWaitForIncomingMethodCall) { |
| 380 sample::ServicePtr proxy; | 384 sample::ServicePtr proxy; |
| 381 ReentrantServiceImpl impl(GetProxy(&proxy)); | 385 ReentrantServiceImpl impl(GetProxy(&proxy)); |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 437 EXPECT_EQ(456, impl.integer()); | 441 EXPECT_EQ(456, impl.integer()); |
| 438 } | 442 } |
| 439 | 443 |
| 440 class StrongMathCalculatorImpl : public math::Calculator { | 444 class StrongMathCalculatorImpl : public math::Calculator { |
| 441 public: | 445 public: |
| 442 StrongMathCalculatorImpl(ScopedMessagePipeHandle handle, | 446 StrongMathCalculatorImpl(ScopedMessagePipeHandle handle, |
| 443 bool* error_received, | 447 bool* error_received, |
| 444 bool* destroyed) | 448 bool* destroyed) |
| 445 : error_received_(error_received), | 449 : error_received_(error_received), |
| 446 destroyed_(destroyed), | 450 destroyed_(destroyed), |
| 447 binding_(this, handle.Pass()) { | 451 binding_(this, std::move(handle)) { |
| 448 binding_.set_connection_error_handler( | 452 binding_.set_connection_error_handler( |
| 449 [this]() { *error_received_ = true; }); | 453 [this]() { *error_received_ = true; }); |
| 450 } | 454 } |
| 451 ~StrongMathCalculatorImpl() override { *destroyed_ = true; } | 455 ~StrongMathCalculatorImpl() override { *destroyed_ = true; } |
| 452 | 456 |
| 453 // math::Calculator implementation. | 457 // math::Calculator implementation. |
| 454 void Clear(const CalcCallback& callback) override { callback.Run(total_); } | 458 void Clear(const CalcCallback& callback) override { callback.Run(total_); } |
| 455 | 459 |
| 456 void Add(double value, const CalcCallback& callback) override { | 460 void Add(double value, const CalcCallback& callback) override { |
| 457 total_ += value; | 461 total_ += value; |
| (...skipping 12 matching lines...) Expand all Loading... |
| 470 | 474 |
| 471 StrongBinding<math::Calculator> binding_; | 475 StrongBinding<math::Calculator> binding_; |
| 472 }; | 476 }; |
| 473 | 477 |
| 474 TEST(StrongConnectorTest, Math) { | 478 TEST(StrongConnectorTest, Math) { |
| 475 base::MessageLoop loop(common::MessagePumpMojo::Create()); | 479 base::MessageLoop loop(common::MessagePumpMojo::Create()); |
| 476 | 480 |
| 477 bool error_received = false; | 481 bool error_received = false; |
| 478 bool destroyed = false; | 482 bool destroyed = false; |
| 479 MessagePipe pipe; | 483 MessagePipe pipe; |
| 480 new StrongMathCalculatorImpl(pipe.handle0.Pass(), &error_received, | 484 new StrongMathCalculatorImpl(std::move(pipe.handle0), &error_received, |
| 481 &destroyed); | 485 &destroyed); |
| 482 | 486 |
| 483 math::CalculatorPtr calc; | 487 math::CalculatorPtr calc; |
| 484 calc.Bind(InterfacePtrInfo<math::Calculator>(pipe.handle1.Pass(), 0u)); | 488 calc.Bind(InterfacePtrInfo<math::Calculator>(std::move(pipe.handle1), 0u)); |
| 485 | 489 |
| 486 { | 490 { |
| 487 // Suppose this is instantiated in a process that has the other end of the | 491 // Suppose this is instantiated in a process that has the other end of the |
| 488 // message pipe. | 492 // message pipe. |
| 489 MathCalculatorUI calculator_ui(calc.Pass()); | 493 MathCalculatorUI calculator_ui(std::move(calc)); |
| 490 | 494 |
| 491 calculator_ui.Add(2.0); | 495 calculator_ui.Add(2.0); |
| 492 calculator_ui.Multiply(5.0); | 496 calculator_ui.Multiply(5.0); |
| 493 | 497 |
| 494 loop.RunUntilIdle(); | 498 loop.RunUntilIdle(); |
| 495 | 499 |
| 496 EXPECT_EQ(10.0, calculator_ui.GetOutput()); | 500 EXPECT_EQ(10.0, calculator_ui.GetOutput()); |
| 497 EXPECT_FALSE(error_received); | 501 EXPECT_FALSE(error_received); |
| 498 EXPECT_FALSE(destroyed); | 502 EXPECT_FALSE(destroyed); |
| 499 } | 503 } |
| 500 // Destroying calculator_ui should close the pipe and generate an error on the | 504 // Destroying calculator_ui should close the pipe and generate an error on the |
| 501 // other | 505 // other |
| 502 // end which will destroy the instance since it is strongly bound. | 506 // end which will destroy the instance since it is strongly bound. |
| 503 | 507 |
| 504 loop.RunUntilIdle(); | 508 loop.RunUntilIdle(); |
| 505 EXPECT_TRUE(error_received); | 509 EXPECT_TRUE(error_received); |
| 506 EXPECT_TRUE(destroyed); | 510 EXPECT_TRUE(destroyed); |
| 507 } | 511 } |
| 508 | 512 |
| 509 class WeakMathCalculatorImpl : public math::Calculator { | 513 class WeakMathCalculatorImpl : public math::Calculator { |
| 510 public: | 514 public: |
| 511 WeakMathCalculatorImpl(ScopedMessagePipeHandle handle, | 515 WeakMathCalculatorImpl(ScopedMessagePipeHandle handle, |
| 512 bool* error_received, | 516 bool* error_received, |
| 513 bool* destroyed) | 517 bool* destroyed) |
| 514 : error_received_(error_received), | 518 : error_received_(error_received), |
| 515 destroyed_(destroyed), | 519 destroyed_(destroyed), |
| 516 binding_(this, handle.Pass()) { | 520 binding_(this, std::move(handle)) { |
| 517 binding_.set_connection_error_handler( | 521 binding_.set_connection_error_handler( |
| 518 [this]() { *error_received_ = true; }); | 522 [this]() { *error_received_ = true; }); |
| 519 } | 523 } |
| 520 ~WeakMathCalculatorImpl() override { *destroyed_ = true; } | 524 ~WeakMathCalculatorImpl() override { *destroyed_ = true; } |
| 521 | 525 |
| 522 void Clear(const CalcCallback& callback) override { callback.Run(total_); } | 526 void Clear(const CalcCallback& callback) override { callback.Run(total_); } |
| 523 | 527 |
| 524 void Add(double value, const CalcCallback& callback) override { | 528 void Add(double value, const CalcCallback& callback) override { |
| 525 total_ += value; | 529 total_ += value; |
| 526 callback.Run(total_); | 530 callback.Run(total_); |
| (...skipping 11 matching lines...) Expand all Loading... |
| 538 | 542 |
| 539 Binding<math::Calculator> binding_; | 543 Binding<math::Calculator> binding_; |
| 540 }; | 544 }; |
| 541 | 545 |
| 542 TEST(WeakConnectorTest, Math) { | 546 TEST(WeakConnectorTest, Math) { |
| 543 base::MessageLoop loop(common::MessagePumpMojo::Create()); | 547 base::MessageLoop loop(common::MessagePumpMojo::Create()); |
| 544 | 548 |
| 545 bool error_received = false; | 549 bool error_received = false; |
| 546 bool destroyed = false; | 550 bool destroyed = false; |
| 547 MessagePipe pipe; | 551 MessagePipe pipe; |
| 548 WeakMathCalculatorImpl impl(pipe.handle0.Pass(), &error_received, &destroyed); | 552 WeakMathCalculatorImpl impl(std::move(pipe.handle0), &error_received, |
| 553 &destroyed); |
| 549 | 554 |
| 550 math::CalculatorPtr calc; | 555 math::CalculatorPtr calc; |
| 551 calc.Bind(InterfacePtrInfo<math::Calculator>(pipe.handle1.Pass(), 0u)); | 556 calc.Bind(InterfacePtrInfo<math::Calculator>(std::move(pipe.handle1), 0u)); |
| 552 | 557 |
| 553 { | 558 { |
| 554 // Suppose this is instantiated in a process that has the other end of the | 559 // Suppose this is instantiated in a process that has the other end of the |
| 555 // message pipe. | 560 // message pipe. |
| 556 MathCalculatorUI calculator_ui(calc.Pass()); | 561 MathCalculatorUI calculator_ui(std::move(calc)); |
| 557 | 562 |
| 558 calculator_ui.Add(2.0); | 563 calculator_ui.Add(2.0); |
| 559 calculator_ui.Multiply(5.0); | 564 calculator_ui.Multiply(5.0); |
| 560 | 565 |
| 561 loop.RunUntilIdle(); | 566 loop.RunUntilIdle(); |
| 562 | 567 |
| 563 EXPECT_EQ(10.0, calculator_ui.GetOutput()); | 568 EXPECT_EQ(10.0, calculator_ui.GetOutput()); |
| 564 EXPECT_FALSE(error_received); | 569 EXPECT_FALSE(error_received); |
| 565 EXPECT_FALSE(destroyed); | 570 EXPECT_FALSE(destroyed); |
| 566 // Destroying calculator_ui should close the pipe and generate an error on | 571 // Destroying calculator_ui should close the pipe and generate an error on |
| 567 // the other | 572 // the other |
| 568 // end which will destroy the instance since it is strongly bound. | 573 // end which will destroy the instance since it is strongly bound. |
| 569 } | 574 } |
| 570 | 575 |
| 571 loop.RunUntilIdle(); | 576 loop.RunUntilIdle(); |
| 572 EXPECT_TRUE(error_received); | 577 EXPECT_TRUE(error_received); |
| 573 EXPECT_FALSE(destroyed); | 578 EXPECT_FALSE(destroyed); |
| 574 } | 579 } |
| 575 | 580 |
| 576 class CImpl : public C { | 581 class CImpl : public C { |
| 577 public: | 582 public: |
| 578 CImpl(bool* d_called, InterfaceRequest<C> request) | 583 CImpl(bool* d_called, InterfaceRequest<C> request) |
| 579 : d_called_(d_called), | 584 : d_called_(d_called), binding_(this, std::move(request)) {} |
| 580 binding_(this, request.Pass()) {} | |
| 581 ~CImpl() override {} | 585 ~CImpl() override {} |
| 582 | 586 |
| 583 private: | 587 private: |
| 584 void D() override { | 588 void D() override { |
| 585 *d_called_ = true; | 589 *d_called_ = true; |
| 586 } | 590 } |
| 587 | 591 |
| 588 bool* d_called_; | 592 bool* d_called_; |
| 589 StrongBinding<C> binding_; | 593 StrongBinding<C> binding_; |
| 590 }; | 594 }; |
| 591 | 595 |
| 592 class BImpl : public B { | 596 class BImpl : public B { |
| 593 public: | 597 public: |
| 594 BImpl(bool* d_called, InterfaceRequest<B> request) | 598 BImpl(bool* d_called, InterfaceRequest<B> request) |
| 595 : d_called_(d_called), | 599 : d_called_(d_called), binding_(this, std::move(request)) {} |
| 596 binding_(this, request.Pass()) {} | |
| 597 ~BImpl() override {} | 600 ~BImpl() override {} |
| 598 | 601 |
| 599 private: | 602 private: |
| 600 void GetC(InterfaceRequest<C> c) override { | 603 void GetC(InterfaceRequest<C> c) override { |
| 601 new CImpl(d_called_, c.Pass()); | 604 new CImpl(d_called_, std::move(c)); |
| 602 } | 605 } |
| 603 | 606 |
| 604 bool* d_called_; | 607 bool* d_called_; |
| 605 StrongBinding<B> binding_; | 608 StrongBinding<B> binding_; |
| 606 }; | 609 }; |
| 607 | 610 |
| 608 class AImpl : public A { | 611 class AImpl : public A { |
| 609 public: | 612 public: |
| 610 explicit AImpl(InterfaceRequest<A> request) | 613 explicit AImpl(InterfaceRequest<A> request) |
| 611 : d_called_(false), | 614 : d_called_(false), binding_(this, std::move(request)) {} |
| 612 binding_(this, request.Pass()) {} | |
| 613 ~AImpl() override {} | 615 ~AImpl() override {} |
| 614 | 616 |
| 615 bool d_called() const { return d_called_; } | 617 bool d_called() const { return d_called_; } |
| 616 | 618 |
| 617 private: | 619 private: |
| 618 void GetB(InterfaceRequest<B> b) override { | 620 void GetB(InterfaceRequest<B> b) override { |
| 619 new BImpl(&d_called_, b.Pass()); | 621 new BImpl(&d_called_, std::move(b)); |
| 620 } | 622 } |
| 621 | 623 |
| 622 bool d_called_; | 624 bool d_called_; |
| 623 Binding<A> binding_; | 625 Binding<A> binding_; |
| 624 }; | 626 }; |
| 625 | 627 |
| 626 TEST_F(InterfacePtrTest, Scoping) { | 628 TEST_F(InterfacePtrTest, Scoping) { |
| 627 APtr a; | 629 APtr a; |
| 628 AImpl a_impl(GetProxy(&a)); | 630 AImpl a_impl(GetProxy(&a)); |
| 629 | 631 |
| (...skipping 10 matching lines...) Expand all Loading... |
| 640 // While B & C have fallen out of scope, the pipes will remain until they are | 642 // While B & C have fallen out of scope, the pipes will remain until they are |
| 641 // flushed. | 643 // flushed. |
| 642 EXPECT_FALSE(a_impl.d_called()); | 644 EXPECT_FALSE(a_impl.d_called()); |
| 643 PumpMessages(); | 645 PumpMessages(); |
| 644 EXPECT_TRUE(a_impl.d_called()); | 646 EXPECT_TRUE(a_impl.d_called()); |
| 645 } | 647 } |
| 646 | 648 |
| 647 } // namespace | 649 } // namespace |
| 648 } // namespace test | 650 } // namespace test |
| 649 } // namespace mojo | 651 } // namespace mojo |
| OLD | NEW |