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

Side by Side Diff: content/browser/browser_associated_interface_unittest.cc

Issue 2164783005: Adds BrowserAssociatedInterface (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@early-channel-setup
Patch Set: no CONTENT_EXPORT Created 4 years, 4 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 | « no previous file | content/browser/renderer_host/render_process_host_impl.cc » ('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 <memory>
6 #include <string>
7
8 #include "base/memory/ref_counted.h"
9 #include "base/message_loop/message_loop.h"
10 #include "base/pickle.h"
11 #include "base/run_loop.h"
12 #include "base/single_thread_task_runner.h"
13 #include "base/strings/stringprintf.h"
14 #include "base/threading/thread.h"
15 #include "content/public/browser/browser_associated_interface.h"
16 #include "content/public/browser/browser_message_filter.h"
17 #include "content/public/browser/browser_thread.h"
18 #include "content/public/test/test_browser_thread_bundle.h"
19 #include "content/test/test_browser_associated_interfaces.mojom.h"
20 #include "ipc/ipc_channel_factory.h"
21 #include "ipc/ipc_channel_mojo.h"
22 #include "ipc/ipc_channel_proxy.h"
23 #include "ipc/ipc_listener.h"
24 #include "ipc/ipc_message.h"
25 #include "mojo/public/cpp/system/message_pipe.h"
26 #include "testing/gtest/include/gtest/gtest.h"
27
28 namespace content {
29
30 const int kNumTestMessages = 100;
31
32 class BrowserAssociatedInterfaceTest : public testing::Test {
33 public:
34 static void AddFilterToChannel(BrowserMessageFilter* filter,
35 IPC::ChannelProxy* channel) {
36 filter->RegisterAssociatedInterfaces(channel);
37 channel->AddFilter(filter->GetFilter());
38 }
39 };
40
41 class ProxyRunner : public IPC::Listener {
42 public:
43 ProxyRunner(mojo::ScopedMessagePipeHandle pipe,
44 bool for_server,
45 scoped_refptr<base::SingleThreadTaskRunner> ipc_task_runner) {
46 std::unique_ptr<IPC::ChannelFactory> factory;
47 if (for_server) {
48 factory = IPC::ChannelMojo::CreateServerFactory(
49 std::move(pipe), ipc_task_runner);
50 } else {
51 factory = IPC::ChannelMojo::CreateClientFactory(
52 std::move(pipe), ipc_task_runner);
53 }
54 channel_ = IPC::ChannelProxy::Create(
55 std::move(factory), this, ipc_task_runner);
56 }
57
58 void ShutDown() { channel_.reset(); }
59
60 IPC::ChannelProxy* channel() { return channel_.get(); }
61
62 private:
63 // IPC::Listener:
64 bool OnMessageReceived(const IPC::Message& message) override { return false; }
65
66 std::unique_ptr<IPC::ChannelProxy> channel_;
67 };
68
69 class TestDriverMessageFilter
70 : public BrowserMessageFilter,
71 public BrowserAssociatedInterface<
72 mojom::BrowserAssociatedInterfaceTestDriver>,
73 public mojom::BrowserAssociatedInterfaceTestDriver {
74 public:
75 TestDriverMessageFilter()
76 : BrowserMessageFilter(0),
77 BrowserAssociatedInterface<mojom::BrowserAssociatedInterfaceTestDriver>(
78 this, this) {
79 }
80
81 private:
82 ~TestDriverMessageFilter() override {}
83
84 // BrowserMessageFilter:
85 bool OnMessageReceived(const IPC::Message& message) override {
86 std::string actual_string;
87 base::PickleIterator iter(message);
88 EXPECT_TRUE(iter.ReadString(&actual_string));
89 EXPECT_EQ(next_expected_string_, actual_string);
90 message_count_++;
91 return true;
92 }
93
94 // mojom::BrowserAssociatedInterfaceTestDriver:
95 void ExpectString(const std::string& expected) override {
96 next_expected_string_ = expected;
97 }
98
99 void RequestQuit(const RequestQuitCallback& callback) override {
100 EXPECT_EQ(kNumTestMessages, message_count_);
101 callback.Run();
102 base::MessageLoop::current()->QuitWhenIdle();
103 }
104
105 std::string next_expected_string_;
106 int message_count_ = 0;
107 };
108
109 class TestClientRunner {
110 public:
111 explicit TestClientRunner(mojo::ScopedMessagePipeHandle pipe)
112 : client_thread_("Test client") {
113 client_thread_.Start();
114 client_thread_.task_runner()->PostTask(
115 FROM_HERE, base::Bind(&RunTestClient, base::Passed(&pipe)));
116 }
117
118 ~TestClientRunner() {
119 client_thread_.Stop();
120 base::RunLoop().RunUntilIdle();
121 }
122
123 private:
124 static void RunTestClient(mojo::ScopedMessagePipeHandle pipe) {
125 base::Thread io_thread("Client IO thread");
126 io_thread.StartWithOptions(
127 base::Thread::Options(base::MessageLoop::TYPE_IO, 0));
128 ProxyRunner proxy(std::move(pipe), false, io_thread.task_runner());
129
130 mojom::BrowserAssociatedInterfaceTestDriverAssociatedPtr driver;
131 proxy.channel()->GetRemoteAssociatedInterface(&driver);
132
133 for (int i = 0; i < kNumTestMessages; ++i) {
134 std::string next_message = base::StringPrintf("test %d", i);
135 driver->ExpectString(next_message);
136
137 std::unique_ptr<IPC::Message> message(new IPC::Message);
138 message->WriteString(next_message);
139 proxy.channel()->Send(message.release());
140 }
141
142 driver->RequestQuit(base::MessageLoop::QuitWhenIdleClosure());
143
144 base::MessageLoop::ScopedNestableTaskAllower allow_nested_loops(
145 base::MessageLoop::current());
146 base::RunLoop().Run();
147
148 proxy.ShutDown();
149 io_thread.Stop();
150 base::RunLoop().RunUntilIdle();
151 }
152
153 base::Thread client_thread_;
154 };
155
156 TEST_F(BrowserAssociatedInterfaceTest, Basic) {
157 TestBrowserThreadBundle browser_threads_;
158 mojo::MessagePipe pipe;
159 ProxyRunner proxy(std::move(pipe.handle0), true,
160 BrowserThread::GetTaskRunnerForThread(BrowserThread::IO));
161 AddFilterToChannel(new TestDriverMessageFilter, proxy.channel());
162
163 TestClientRunner client(std::move(pipe.handle1));
164 base::RunLoop().Run();
165
166 proxy.ShutDown();
167 base::RunLoop().RunUntilIdle();
168 }
169
170 } // namespace content
OLDNEW
« no previous file with comments | « no previous file | content/browser/renderer_host/render_process_host_impl.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698