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