Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1747)

Unified Diff: mojo/public/cpp/bindings/tests/interface_ptr_unittest.cc

Issue 708053005: Add simple strong and weak connectors (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « mojo/public/cpp/bindings/strong_connector.h ('k') | mojo/public/cpp/bindings/weak_connector.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: mojo/public/cpp/bindings/tests/interface_ptr_unittest.cc
diff --git a/mojo/public/cpp/bindings/tests/interface_ptr_unittest.cc b/mojo/public/cpp/bindings/tests/interface_ptr_unittest.cc
index 0cd56462555c046ab7ae28ce7fa16f1995524a4b..da0d681ef291890289a2fc384dcb27712d2226cf 100644
--- a/mojo/public/cpp/bindings/tests/interface_ptr_unittest.cc
+++ b/mojo/public/cpp/bindings/tests/interface_ptr_unittest.cc
@@ -3,6 +3,8 @@
// found in the LICENSE file.
#include "mojo/public/cpp/bindings/error_handler.h"
+#include "mojo/public/cpp/bindings/strong_connector.h"
+#include "mojo/public/cpp/bindings/weak_connector.h"
#include "mojo/public/cpp/environment/environment.h"
#include "mojo/public/cpp/utility/run_loop.h"
#include "mojo/public/interfaces/bindings/tests/math_calculator.mojom.h"
@@ -351,6 +353,148 @@ TEST_F(InterfacePtrTest, ReentrantWaitForIncomingMethodCall) {
EXPECT_EQ(2, impl->max_call_depth());
}
+class StrongMathCalculatorImpl : public math::Calculator, public ErrorHandler {
+ public:
+ StrongMathCalculatorImpl(ScopedMessagePipeHandle handle,
+ bool* error_received,
+ bool* destroyed)
+ : error_received_(error_received),
+ destroyed_(destroyed),
+ connector_(this, handle.Pass()) {
+ connector_.set_error_handler(this);
+ }
+ ~StrongMathCalculatorImpl() override { *destroyed_ = true; }
+
+ // math::Calculator implementation.
+ void Clear() override { connector_.client()->Output(total_); }
+
+ void Add(double value) override {
+ total_ += value;
+ connector_.client()->Output(total_);
+ }
+
+ void Multiply(double value) override {
+ total_ *= value;
+ connector_.client()->Output(total_);
+ }
+
+ // ErrorHandler implementation.
+ void OnConnectionError() override { *error_received_ = true; }
+
+ private:
+ double total_ = 0.0;
+ bool* error_received_;
+ bool* destroyed_;
+
+ StrongConnector<math::Calculator> connector_;
+};
+
+TEST(StrongConnectorTest, Math) {
+ Environment env;
+ RunLoop loop;
+
+ bool error_received = false;
+ bool destroyed = false;
+ MessagePipe pipe;
+ new StrongMathCalculatorImpl(
+ pipe.handle0.Pass(), &error_received, &destroyed);
+
+ math::CalculatorPtr calc;
+ calc.Bind(pipe.handle1.Pass());
+
+ {
+ // Suppose this is instantiated in a process that has the other end of the
+ // message pipe.
+ MathCalculatorUIImpl calculator_ui(calc.Pass());
+
+ calculator_ui.Add(2.0);
+ calculator_ui.Multiply(5.0);
+
+ loop.RunUntilIdle();
+
+ EXPECT_EQ(10.0, calculator_ui.GetOutput());
+ EXPECT_FALSE(error_received);
+ EXPECT_FALSE(destroyed);
+ }
+ // Destroying calculator_ui should close the pipe and generate an error on the
+ // other
+ // end which will destroy the instance since it is strongly bound.
+
+ loop.RunUntilIdle();
+ EXPECT_TRUE(error_received);
+ EXPECT_TRUE(destroyed);
+}
+
+class WeakMathCalculatorImpl : public math::Calculator, public ErrorHandler {
+ public:
+ WeakMathCalculatorImpl(ScopedMessagePipeHandle handle,
+ bool* error_received,
+ bool* destroyed)
+ : error_received_(error_received),
+ destroyed_(destroyed),
+ connector_(this, handle.Pass()) {
+ connector_.set_error_handler(this);
+ }
+ ~WeakMathCalculatorImpl() override { *destroyed_ = true; }
+
+ void Clear() override { connector_.client()->Output(total_); }
+
+ void Add(double value) override {
+ total_ += value;
+ connector_.client()->Output(total_);
+ }
+
+ void Multiply(double value) override {
+ total_ *= value;
+ connector_.client()->Output(total_);
+ }
+
+ // ErrorHandler implementation.
+ void OnConnectionError() override { *error_received_ = true; }
+
+ private:
+ double total_ = 0.0;
+ bool* error_received_;
+ bool* destroyed_;
+
+ WeakConnector<math::Calculator> connector_;
+};
+
+TEST(WeakConnectorTest, Math) {
+ Environment env;
+ RunLoop loop;
+
+ bool error_received = false;
+ bool destroyed = false;
+ MessagePipe pipe;
+ WeakMathCalculatorImpl impl(pipe.handle0.Pass(), &error_received, &destroyed);
+
+ math::CalculatorPtr calc;
+ calc.Bind(pipe.handle1.Pass());
+
+ {
+ // Suppose this is instantiated in a process that has the other end of the
+ // message pipe.
+ MathCalculatorUIImpl calculator_ui(calc.Pass());
+
+ calculator_ui.Add(2.0);
+ calculator_ui.Multiply(5.0);
+
+ loop.RunUntilIdle();
+
+ EXPECT_EQ(10.0, calculator_ui.GetOutput());
+ EXPECT_FALSE(error_received);
+ EXPECT_FALSE(destroyed);
+ // Destroying calculator_ui should close the pipe and generate an error on
+ // the other
+ // end which will destroy the instance since it is strongly bound.
+ }
+
+ loop.RunUntilIdle();
+ EXPECT_TRUE(error_received);
+ EXPECT_FALSE(destroyed);
+}
+
} // namespace
} // namespace test
} // namespace mojo
« no previous file with comments | « mojo/public/cpp/bindings/strong_connector.h ('k') | mojo/public/cpp/bindings/weak_connector.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698