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

Side by Side Diff: mojo/public/cpp/bindings/tests/sync_method_unittest.cc

Issue 1701423002: Mojo C++ bindings: support sync methods - part 1. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 10 months 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 unified diff | Download patch
« no previous file with comments | « mojo/public/cpp/bindings/tests/BUILD.gn ('k') | mojo/public/interfaces/bindings/tests/BUILD.gn » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "base/bind.h"
6 #include "base/logging.h"
7 #include "base/macros.h"
8 #include "base/message_loop/message_loop.h"
9 #include "base/run_loop.h"
10 #include "base/threading/thread.h"
11 #include "mojo/message_pump/message_pump_mojo.h"
12 #include "mojo/public/cpp/bindings/binding.h"
13 #include "mojo/public/interfaces/bindings/tests/test_sync_methods.mojom.h"
14 #include "testing/gtest/include/gtest/gtest.h"
15
16 namespace mojo {
17 namespace test {
18 namespace {
19
20 class SyncMethodTest : public testing::Test {
21 public:
22 SyncMethodTest() : loop_(common::MessagePumpMojo::Create()) {}
23 ~SyncMethodTest() override { loop_.RunUntilIdle(); }
24
25 private:
26 base::MessageLoop loop_;
27 };
28
29 class TestSyncImpl : public TestSync {
30 public:
31 TestSyncImpl(TestSyncRequest request) : binding_(this, std::move(request)) {}
32
33 void set_ping_notification(const Closure& closure) {
34 ping_notification_ = closure;
35 }
36
37 // TestSync implementation:
38 void Get(const GetCallback& callback) override { callback.Run(42); }
39 void Set(int32_t value, const SetCallback& callback) override {
40 callback.Run();
41 }
42 void Ping(const PingCallback& callback) override {
43 ping_notification_.Run();
44 callback.Run();
45 }
46 void Echo(int32_t value, const EchoCallback& callback) override {
47 callback.Run(value);
48 }
49
50 private:
51 Binding<TestSync> binding_;
52 Closure ping_notification_;
53
54 DISALLOW_COPY_AND_ASSIGN(TestSyncImpl);
55 };
56
57 class TestSyncServiceThread {
58 public:
59 TestSyncServiceThread()
60 : thread_("TestSyncServiceThread"), ping_called_(false) {
61 base::Thread::Options thread_options;
62 thread_options.message_pump_factory =
63 base::Bind(&common::MessagePumpMojo::Create);
64 thread_.StartWithOptions(thread_options);
65 }
66
67 void SetUp(TestSyncRequest request) {
68 CHECK(thread_.task_runner()->BelongsToCurrentThread());
69 impl_.reset(new TestSyncImpl(std::move(request)));
70 impl_->set_ping_notification([this]() {
71 base::AutoLock locker(lock_);
72 ping_called_ = true;
73 });
74 }
75
76 void TearDown() {
77 CHECK(thread_.task_runner()->BelongsToCurrentThread());
78 impl_.reset();
79 }
80
81 base::Thread* thread() { return &thread_; }
82 bool ping_called() const {
83 base::AutoLock locker(lock_);
84 return ping_called_;
85 }
86
87 private:
88 base::Thread thread_;
89
90 scoped_ptr<TestSyncImpl> impl_;
91
92 mutable base::Lock lock_;
93 bool ping_called_;
94
95 DISALLOW_COPY_AND_ASSIGN(TestSyncServiceThread);
96 };
97
98 TEST_F(SyncMethodTest, CallSyncMethodAsynchronously) {
99 TestSyncPtr ptr;
100 TestSyncImpl impl(GetProxy(&ptr));
101
102 base::RunLoop run_loop;
103 ptr->Echo(123, [&run_loop](int32_t result) {
104 EXPECT_EQ(123, result);
105 run_loop.Quit();
106 });
107 run_loop.Run();
108 }
109
110 TEST_F(SyncMethodTest, BasicSyncCalls) {
111 TestSyncPtr ptr;
112
113 TestSyncServiceThread service_thread;
114 service_thread.thread()->task_runner()->PostTask(
115 FROM_HERE, base::Bind(&TestSyncServiceThread::SetUp,
116 base::Unretained(&service_thread),
117 base::Passed(GetProxy(&ptr))));
118 ASSERT_TRUE(ptr->Ping());
119 ASSERT_TRUE(service_thread.ping_called());
120
121 int32_t output_value = -1;
122 ASSERT_TRUE(ptr->Echo(42, &output_value));
123 ASSERT_EQ(42, output_value);
124
125 base::RunLoop run_loop;
126 service_thread.thread()->task_runner()->PostTaskAndReply(
127 FROM_HERE, base::Bind(&TestSyncServiceThread::TearDown,
128 base::Unretained(&service_thread)),
129 run_loop.QuitClosure());
130 run_loop.Run();
131 }
132
133 } // namespace
134 } // namespace test
135 } // namespace mojo
OLDNEW
« no previous file with comments | « mojo/public/cpp/bindings/tests/BUILD.gn ('k') | mojo/public/interfaces/bindings/tests/BUILD.gn » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698