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