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

Side by Side Diff: mojo/public/cpp/system/tests/watcher_unittest.cc

Issue 1768443004: Revert of [mojo-bindings] Use Watch API instead of MessagePumpMojo (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 9 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/system/tests/BUILD.gn ('k') | mojo/public/cpp/system/watcher.h » ('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/callback.h"
7 #include "base/memory/scoped_ptr.h"
8 #include "base/message_loop/message_loop.h"
9 #include "base/run_loop.h"
10 #include "base/thread_task_runner_handle.h"
11 #include "mojo/public/c/system/types.h"
12 #include "mojo/public/cpp/system/message_pipe.h"
13 #include "mojo/public/cpp/system/watcher.h"
14 #include "testing/gtest/include/gtest/gtest.h"
15
16 namespace mojo {
17 namespace {
18
19 template <typename Handler>
20 void RunResultHandler(Handler f, MojoResult result) { f(result); }
21
22 template <typename Handler>
23 Watcher::ReadyCallback OnReady(Handler f) {
24 return base::Bind(&RunResultHandler<Handler>, f);
25 }
26
27 Watcher::ReadyCallback NotReached() {
28 return OnReady([] (MojoResult) { NOTREACHED(); });
29 }
30
31 class WatcherTest : public testing::Test {
32 public:
33 WatcherTest() {}
34 ~WatcherTest() override {}
35
36 void SetUp() override {
37 message_loop_.reset(new base::MessageLoop);
38 }
39
40 void TearDown() override {
41 message_loop_.reset();
42 }
43
44 protected:
45 scoped_ptr<base::MessageLoop> message_loop_;
46 };
47
48 TEST_F(WatcherTest, WatchBasic) {
49 ScopedMessagePipeHandle a, b;
50 CreateMessagePipe(nullptr, &a, &b);
51
52 bool notified = false;
53 base::RunLoop run_loop;
54 Watcher b_watcher;
55 EXPECT_EQ(MOJO_RESULT_OK,
56 b_watcher.Start(b.get(), MOJO_HANDLE_SIGNAL_READABLE,
57 OnReady([&] (MojoResult result) {
58 EXPECT_EQ(MOJO_RESULT_OK, result);
59 notified = true;
60 run_loop.Quit();
61 })));
62 EXPECT_TRUE(b_watcher.IsWatching());
63
64 EXPECT_EQ(MOJO_RESULT_OK, WriteMessageRaw(a.get(), "hello", 5, nullptr, 0,
65 MOJO_WRITE_MESSAGE_FLAG_NONE));
66 run_loop.Run();
67 EXPECT_TRUE(notified);
68
69 b_watcher.Cancel();
70 }
71
72 TEST_F(WatcherTest, WatchUnsatisfiable) {
73 ScopedMessagePipeHandle a, b;
74 CreateMessagePipe(nullptr, &a, &b);
75 a.reset();
76
77 Watcher b_watcher;
78 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION,
79 b_watcher.Start(b.get(), MOJO_HANDLE_SIGNAL_READABLE,
80 NotReached()));
81 EXPECT_FALSE(b_watcher.IsWatching());
82 }
83
84 TEST_F(WatcherTest, WatchInvalidHandle) {
85 ScopedMessagePipeHandle a, b;
86 CreateMessagePipe(nullptr, &a, &b);
87 a.reset();
88 b.reset();
89
90 Watcher b_watcher;
91 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT,
92 b_watcher.Start(b.get(), MOJO_HANDLE_SIGNAL_READABLE,
93 NotReached()));
94 EXPECT_FALSE(b_watcher.IsWatching());
95 }
96
97 TEST_F(WatcherTest, Cancel) {
98 ScopedMessagePipeHandle a, b;
99 CreateMessagePipe(nullptr, &a, &b);
100
101 base::RunLoop run_loop;
102 Watcher b_watcher;
103 EXPECT_EQ(MOJO_RESULT_OK,
104 b_watcher.Start(b.get(), MOJO_HANDLE_SIGNAL_READABLE,
105 NotReached()));
106 EXPECT_TRUE(b_watcher.IsWatching());
107 b_watcher.Cancel();
108 EXPECT_FALSE(b_watcher.IsWatching());
109
110 // This should never trigger the watcher.
111 EXPECT_EQ(MOJO_RESULT_OK, WriteMessageRaw(a.get(), "hello", 5, nullptr, 0,
112 MOJO_WRITE_MESSAGE_FLAG_NONE));
113
114 base::ThreadTaskRunnerHandle::Get()->PostTask(
115 FROM_HERE, run_loop.QuitClosure());
116 run_loop.Run();
117 }
118
119 TEST_F(WatcherTest, CancelOnClose) {
120 ScopedMessagePipeHandle a, b;
121 CreateMessagePipe(nullptr, &a, &b);
122
123 base::RunLoop run_loop;
124 Watcher b_watcher;
125 EXPECT_EQ(MOJO_RESULT_OK,
126 b_watcher.Start(b.get(), MOJO_HANDLE_SIGNAL_READABLE,
127 OnReady([&] (MojoResult result) {
128 EXPECT_EQ(MOJO_RESULT_CANCELLED, result);
129 run_loop.Quit();
130 })));
131 EXPECT_TRUE(b_watcher.IsWatching());
132
133 // This should trigger the watcher above.
134 b.reset();
135
136 run_loop.Run();
137
138 EXPECT_FALSE(b_watcher.IsWatching());
139 }
140
141 TEST_F(WatcherTest, CancelOnDestruction) {
142 ScopedMessagePipeHandle a, b;
143 CreateMessagePipe(nullptr, &a, &b);
144 base::RunLoop run_loop;
145 {
146 Watcher b_watcher;
147 EXPECT_EQ(MOJO_RESULT_OK,
148 b_watcher.Start(b.get(), MOJO_HANDLE_SIGNAL_READABLE,
149 NotReached()));
150 EXPECT_TRUE(b_watcher.IsWatching());
151
152 // |b_watcher| should be cancelled when it goes out of scope.
153 }
154
155 // This should never trigger the watcher above.
156 EXPECT_EQ(MOJO_RESULT_OK, WriteMessageRaw(a.get(), "hello", 5, nullptr, 0,
157 MOJO_WRITE_MESSAGE_FLAG_NONE));
158 base::ThreadTaskRunnerHandle::Get()->PostTask(
159 FROM_HERE, run_loop.QuitClosure());
160 run_loop.Run();
161 }
162
163 TEST_F(WatcherTest, NotifyOnMessageLoopDestruction) {
164 ScopedMessagePipeHandle a, b;
165 CreateMessagePipe(nullptr, &a, &b);
166
167 bool notified = false;
168 Watcher b_watcher;
169 EXPECT_EQ(MOJO_RESULT_OK,
170 b_watcher.Start(b.get(), MOJO_HANDLE_SIGNAL_READABLE,
171 OnReady([&] (MojoResult result) {
172 EXPECT_EQ(MOJO_RESULT_ABORTED, result);
173 notified = true;
174 })));
175 EXPECT_TRUE(b_watcher.IsWatching());
176
177 message_loop_.reset();
178
179 EXPECT_TRUE(notified);
180
181 EXPECT_TRUE(b_watcher.IsWatching());
182 b_watcher.Cancel();
183 }
184
185 } // namespace
186 } // namespace mojo
OLDNEW
« no previous file with comments | « mojo/public/cpp/system/tests/BUILD.gn ('k') | mojo/public/cpp/system/watcher.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698