Chromium Code Reviews
Help | Chromium Project | Sign in
(273)

Side by Side Diff: chrome/browser/devtools/device/adb/adb_client_socket_browsertest.cc

Issue 257563003: DevTools: Unify ADB-related browser tests (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fixed clang compile Created 1 year, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | chrome/browser/devtools/device/adb/mock_adb_server.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/strings/string_number_conversions.h" 5 #include "chrome/browser/devtools/device/adb/mock_adb_server.h"
6 #include "chrome/browser/devtools/device/devtools_android_bridge.h" 6 #include "chrome/browser/devtools/device/devtools_android_bridge.h"
7 #include "chrome/browser/devtools/devtools_target_impl.h"
7 #include "chrome/browser/ui/browser.h" 8 #include "chrome/browser/ui/browser.h"
8 #include "chrome/test/base/in_process_browser_test.h" 9 #include "chrome/test/base/in_process_browser_test.h"
9 #include "content/public/browser/browser_thread.h" 10 #include "content/public/browser/browser_thread.h"
10 #include "content/public/test/browser_test.h"
11 #include "content/public/test/test_utils.h" 11 #include "content/public/test/test_utils.h"
12 #include "net/base/host_port_pair.h"
13 #include "net/base/io_buffer.h"
14 #include "net/base/ip_endpoint.h"
15 #include "net/base/net_errors.h"
16 #include "net/base/net_log.h"
17 #include "net/socket/stream_socket.h"
18 #include "net/socket/tcp_server_socket.h"
19 12
20 const char kOpenedUnixSocketsCommand[] = "shell:cat /proc/net/unix"; 13 using content::BrowserThread;
21 const char kDeviceModelCommand[] = "shell:getprop ro.product.model";
22 const char kDumpsysCommand[] = "shell:dumpsys window policy";
23 const char kListProcessesCommand[] = "shell:ps";
24 const char kInstalledChromePackagesCommand[] = "shell:pm list packages";
25 const char kDeviceModel[] = "Nexus 8";
26 14
27 const char kSampleOpenedUnixSocketsWithoutBrowsers[] = 15 static scoped_refptr<DevToolsAndroidBridge::RemoteBrowser>
28 "Num RefCount Protocol Flags Type St Inode Path\n" 16 FindBrowserByDisplayName(DevToolsAndroidBridge::RemoteBrowsers browsers,
29 "00000000: 00000004 00000000" 17 const std::string& name) {
30 " 00000000 0002 01 3328 /dev/socket/wpa_wlan0\n" 18 for (DevToolsAndroidBridge::RemoteBrowsers::iterator it = browsers.begin();
31 "00000000: 00000002 00000000" 19 it != browsers.end(); ++it)
32 " 00010000 0001 01 5394 /dev/socket/vold\n"; 20 if ((*it)->display_name() == name)
33 21 return *it;
34 const char kSampleDumpsys[] = 22 return NULL;
35 "WINDOW MANAGER POLICY STATE (dumpsys window policy)\r\n"
36 " mStable=(0,50)-(720,1184)\r\n";
37
38 const char kSampleListProcesses[] =
39 "USER PID PPID VSIZE RSS WCHAN PC NAME\n"
40 "root 1 0 688 508 ffffffff 00000000 S /init\n";
41
42 const char kSampleListPackages[] = "package:com.example.app";
43
44 static const int kBufferSize = 16*1024;
45 static const int kAdbPort = 5037;
46
47 static const int kAdbMessageHeaderSize = 4;
48
49 // This is single connection server which listens on specified port and
50 // simplifies asynchronous IO.
51 // To write custom server, extend this class and implement TryProcessData
52 // method which is invoked everytime data arrives. In case of successful data
53 // processing(e.g. enough data collected already to parse client reply/request)
54 // return amount of bytes processed to throw them away from buffer
55 // To send data, SendData method should be used. This method is non-blocking
56 // and appends data to be sent to internal buffer.
57 // Since all calls are non-blocking and no callbacks are given, internal
58 // overflows may occur in case too heavy traffic.
59 // In case of heavy traffic performance may suffer because of memcpy calls.
60 class SingleConnectionServer {
61 public:
62 SingleConnectionServer(net::IPEndPoint endpoint, int buffer_size);
63 virtual ~SingleConnectionServer();
64
65 protected:
66 virtual int TryProcessData(const char* data, int size) = 0;
67 void SendData(const char* data, int size);
68
69 private:
70 void AcceptConnection();
71 void OnAccepted(int result);
72
73 void ReadData();
74 void OnDataRead(int count);
75
76 void WriteData();
77 void OnDataWritten(int count);
78
79 private:
80 int bytes_to_write_;
81 scoped_ptr<net::TCPServerSocket> server_socket_;
82 scoped_ptr<net::StreamSocket> client_socket_;
83 scoped_refptr<net::GrowableIOBuffer> input_buffer_;
84 scoped_refptr<net::GrowableIOBuffer> output_buffer_;
85
86 DISALLOW_COPY_AND_ASSIGN(SingleConnectionServer);
87 };
88
89 SingleConnectionServer::SingleConnectionServer(net::IPEndPoint endpoint,
90 int buffer_size)
91 : bytes_to_write_(0) {
92 CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO));
93
94 input_buffer_ = new net::GrowableIOBuffer();
95 input_buffer_->SetCapacity(buffer_size);
96
97 output_buffer_ = new net::GrowableIOBuffer();
98
99 server_socket_.reset(new net::TCPServerSocket(NULL, net::NetLog::Source()));
100 server_socket_->Listen(endpoint, 1);
101
102 AcceptConnection();
103 } 23 }
104 24
105 SingleConnectionServer::~SingleConnectionServer() {
106 CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO));
107
108 server_socket_.reset();
109
110 if (client_socket_) {
111 client_socket_->Disconnect();
112 client_socket_.reset();
113 }
114 }
115
116 void SingleConnectionServer::SendData(const char* data, int size) {
117 CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO));
118
119 if ((output_buffer_->offset() + bytes_to_write_ + size) >
120 output_buffer_->capacity()) {
121 // If not enough space without relocation
122 if (output_buffer_->capacity() < (bytes_to_write_ + size)) {
123 // If even buffer is not enough
124 int new_size = std::max(output_buffer_->capacity() * 2, size * 2);
125 output_buffer_->SetCapacity(new_size);
126 }
127 memmove(output_buffer_->StartOfBuffer(),
128 output_buffer_->data(),
129 bytes_to_write_);
130 output_buffer_->set_offset(0);
131 }
132
133 memcpy(output_buffer_->data() + bytes_to_write_, data, size);
134 bytes_to_write_ += size;
135
136 if (bytes_to_write_ == size)
137 // If write loop wasn't yet started, then start it
138 WriteData();
139 }
140
141 void SingleConnectionServer::AcceptConnection() {
142 if (client_socket_) {
143 client_socket_->Disconnect();
144 client_socket_.reset();
145 }
146
147 int accept_result = server_socket_->Accept(&client_socket_,
148 base::Bind(&SingleConnectionServer::OnAccepted, base::Unretained(this)));
149
150 if (accept_result != net::ERR_IO_PENDING)
151 content::BrowserThread::PostTask(
152 content::BrowserThread::IO,
153 FROM_HERE,
154 base::Bind(&SingleConnectionServer::OnAccepted,
155 base::Unretained(this),
156 accept_result));
157 }
158
159 void SingleConnectionServer::OnAccepted(int result) {
160 ASSERT_EQ(result, 0); // Fails if the socket is already in use.
161 ReadData();
162 }
163
164 void SingleConnectionServer::ReadData() {
165 CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO));
166
167 if (input_buffer_->RemainingCapacity() == 0)
168 input_buffer_->SetCapacity(input_buffer_->capacity() * 2);
169
170 int read_result = client_socket_->Read(
171 input_buffer_.get(),
172 input_buffer_->RemainingCapacity(),
173 base::Bind(&SingleConnectionServer::OnDataRead, base::Unretained(this)));
174
175 if (read_result != net::ERR_IO_PENDING)
176 OnDataRead(read_result);
177 }
178
179 void SingleConnectionServer::OnDataRead(int count) {
180 if (count <= 0) {
181 AcceptConnection();
182 return;
183 }
184
185 input_buffer_->set_offset(input_buffer_->offset() + count);
186
187 int bytes_processed;
188
189 do {
190 char* data = input_buffer_->StartOfBuffer();
191 int data_size = input_buffer_->offset();
192
193 bytes_processed = TryProcessData(data, data_size);
194
195 if (bytes_processed) {
196 memmove(data, data + bytes_processed, data_size - bytes_processed);
197 input_buffer_->set_offset( data_size - bytes_processed);
198 }
199 } while (bytes_processed);
200
201 // Posting is needed not to enter deep recursion in case too synchronous IO
202 content::BrowserThread::PostTask(content::BrowserThread::IO, FROM_HERE,
203 base::Bind(&SingleConnectionServer::ReadData, base::Unretained(this)));
204 }
205
206 void SingleConnectionServer::WriteData() {
207 CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO));
208 CHECK_GE(output_buffer_->capacity(),
209 output_buffer_->offset() + bytes_to_write_) << "Overflow";
210
211 int write_result = client_socket_->Write(
212 output_buffer_,
213 bytes_to_write_,
214 base::Bind(&SingleConnectionServer::OnDataWritten,
215 base::Unretained(this)));
216 if (write_result != net::ERR_IO_PENDING)
217 OnDataWritten(write_result);
218 }
219
220 void SingleConnectionServer::OnDataWritten(int count) {
221 CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO));
222 if (count < 0) {
223 AcceptConnection();
224 return;
225 }
226
227 CHECK_GT(count, 0);
228 CHECK_GE(output_buffer_->capacity(),
229 output_buffer_->offset() + bytes_to_write_) << "Overflow";
230
231 bytes_to_write_ -= count;
232 output_buffer_->set_offset(output_buffer_->offset() + count);
233
234 if (bytes_to_write_ != 0)
235 // Posting is needed not to enter deep recursion in case too synchronous IO
236 content::BrowserThread::PostTask(content::BrowserThread::IO, FROM_HERE,
237 base::Bind(&SingleConnectionServer::WriteData, base::Unretained(this)));
238 }
239
240
241 class MockAdbServer: public SingleConnectionServer {
242 public:
243 MockAdbServer(net::IPEndPoint endpoint, int buffer_size)
244 : SingleConnectionServer(endpoint, buffer_size)
245 {}
246
247 virtual ~MockAdbServer() {}
248
249 private:
250 virtual int TryProcessData(const char* data, int size) OVERRIDE {
251 CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO));
252
253 if (size >= kAdbMessageHeaderSize) {
254 std::string message_header(data, kAdbMessageHeaderSize);
255 int message_size;
256
257 EXPECT_TRUE(base::HexStringToInt(message_header, &message_size));
258
259 if (size >= message_size + kAdbMessageHeaderSize) {
260 std::string message_body(data + kAdbMessageHeaderSize, message_size );
261
262 ProcessCommand(message_body);
263
264 return kAdbMessageHeaderSize + message_size;
265 }
266 }
267
268 return 0;
269 }
270
271 void ProcessCommand(const std::string& command) {
272 CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO));
273
274 if (command == "host:devices") {
275 SendResponse("01498B321301A00A\tdevice\n01498B2B0D01300E\toffline");
276 } else if (command == "host:transport:01498B321301A00A") {
277 SendResponse("");
278 } else if (command == kDeviceModelCommand) {
279 SendResponse(kDeviceModel);
280 } else if (command == kOpenedUnixSocketsCommand) {
281 SendResponse(kSampleOpenedUnixSocketsWithoutBrowsers);
282 } else if (command == kDumpsysCommand) {
283 SendResponse(kSampleDumpsys);
284 } else if (command == kListProcessesCommand) {
285 SendResponse(kSampleListProcesses);
286 } else if (command == kInstalledChromePackagesCommand) {
287 SendResponse(kSampleListPackages);
288 } else {
289 NOTREACHED() << "Unknown command - " << command;
290 }
291 }
292
293 void SendResponse(const std::string& response) {
294 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO));
295
296 std::stringstream response_stream;
297 response_stream << "OKAY";
298
299 int size = response.size();
300 if (size > 0) {
301 static const char kHexChars[] = "0123456789ABCDEF";
302 for (int i = 3; i >= 0; i--)
303 response_stream << kHexChars[ (size >> 4*i) & 0x0f ];
304 response_stream << response;
305 }
306
307 std::string response_data = response_stream.str();
308 SendData(response_data.c_str(), response_data.size());
309 }
310 };
311
312 class AdbClientSocketTest : public InProcessBrowserTest, 25 class AdbClientSocketTest : public InProcessBrowserTest,
313 public DevToolsAndroidBridge::DeviceListListener { 26 public DevToolsAndroidBridge::DeviceListListener {
314 27
315 public: 28 public:
316 void StartTest() { 29 void StartTest() {
317 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); 30 Profile* profile = browser()->profile();
318 31 android_bridge_ = DevToolsAndroidBridge::Factory::GetForProfile(profile);
319 content::BrowserThread::PostTaskAndReply( 32 AndroidDeviceManager::DeviceProviders device_providers;
320 content::BrowserThread::IO, 33 device_providers.push_back(AndroidDeviceManager::GetAdbDeviceProvider());
321 FROM_HERE, 34 android_bridge_->set_device_providers_for_test(device_providers);
322 base::Bind(&AdbClientSocketTest::StartMockAdbServer, 35 android_bridge_->AddDeviceListListener(this);
323 base::Unretained(this)),
324 base::Bind(&AdbClientSocketTest::AddListener,
325 base::Unretained(this)));
326 } 36 }
327 37
328 virtual void DeviceListChanged( 38 virtual void DeviceListChanged(
329 const DevToolsAndroidBridge::RemoteDevices& devices) OVERRIDE { 39 const DevToolsAndroidBridge::RemoteDevices& devices) OVERRIDE {
330 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); 40 devices_ = devices;
331 android_bridge_->RemoveDeviceListListener(this); 41 android_bridge_->RemoveDeviceListListener(this);
332 devices_ = devices; 42 base::MessageLoop::current()->Quit();
333 EndTest();
334 } 43 }
335 44
336 void CheckDevices() { 45 void CheckDevices() {
337 ASSERT_EQ(2U, devices_.size()); 46 ASSERT_EQ(2U, devices_.size());
338 47
339 scoped_refptr<DevToolsAndroidBridge::RemoteDevice> online_device_; 48 scoped_refptr<DevToolsAndroidBridge::RemoteDevice> connected =
340 scoped_refptr<DevToolsAndroidBridge::RemoteDevice> offline_device_; 49 devices_[0]->is_connected() ? devices_[0] : devices_[1];
341 50
342 for (DevToolsAndroidBridge::RemoteDevices::const_iterator it = 51 scoped_refptr<DevToolsAndroidBridge::RemoteDevice> not_connected =
343 devices_.begin(); it != devices_.end(); ++it) { 52 devices_[0]->is_connected() ? devices_[1] : devices_[0];
344 if ((*it)->serial() == "01498B321301A00A")
345 online_device_ = *it;
346 else if ((*it)->serial() == "01498B2B0D01300E")
347 offline_device_ = *it;
348 }
349 53
350 ASSERT_EQ(online_device_->serial(), "01498B321301A00A"); 54 ASSERT_TRUE(connected->is_connected());
351 ASSERT_TRUE(online_device_->is_connected()); 55 ASSERT_FALSE(not_connected->is_connected());
352 ASSERT_FALSE(offline_device_->is_connected());
353 56
354 ASSERT_EQ(online_device_->model(), kDeviceModel); 57 ASSERT_EQ(720, connected->screen_size().width());
355 ASSERT_EQ(online_device_->browsers().size(), 0U); 58 ASSERT_EQ(1184, connected->screen_size().height());
356 ASSERT_EQ(online_device_->screen_size(), gfx::Size(720, 1184)); 59
60 ASSERT_EQ("01498B321301A00A", connected->serial());
61 ASSERT_EQ("Nexus 6", connected->model());
62
63 ASSERT_EQ("01498B2B0D01300E", not_connected->serial());
64 ASSERT_EQ("Offline", not_connected->model());
65
66 const DevToolsAndroidBridge::RemoteBrowsers& browsers =
67 connected->browsers();
68 ASSERT_EQ(4U, browsers.size());
69
70 scoped_refptr<DevToolsAndroidBridge::RemoteBrowser> chrome =
71 FindBrowserByDisplayName(browsers, "Chrome");
72 ASSERT_TRUE(chrome);
73
74 scoped_refptr<DevToolsAndroidBridge::RemoteBrowser> chrome_beta =
75 FindBrowserByDisplayName(browsers, "Chrome Beta");
76 ASSERT_TRUE(chrome_beta);
77
78 scoped_refptr<DevToolsAndroidBridge::RemoteBrowser> chromium =
79 FindBrowserByDisplayName(browsers, "Chromium");
80 ASSERT_FALSE(chromium);
81
82 scoped_refptr<DevToolsAndroidBridge::RemoteBrowser> webview =
83 FindBrowserByDisplayName(browsers, "WebView in com.sample.feed");
84 ASSERT_TRUE(webview);
85
86 scoped_refptr<DevToolsAndroidBridge::RemoteBrowser> noprocess =
87 FindBrowserByDisplayName(browsers, "Noprocess");
88 ASSERT_TRUE(noprocess);
89
90 ASSERT_EQ("32.0.1679.0", chrome->version());
91 ASSERT_EQ("31.0.1599.0", chrome_beta->version());
92 ASSERT_EQ("4.0", webview->version());
93
94 std::vector<DevToolsTargetImpl*> chrome_pages =
95 chrome->CreatePageTargets();
96 std::vector<DevToolsTargetImpl*> chrome_beta_pages =
97 chrome_beta->CreatePageTargets();
98 std::vector<DevToolsTargetImpl*> webview_pages =
99 webview->CreatePageTargets();
100
101 ASSERT_EQ(1U, chrome_pages.size());
102 ASSERT_EQ(0U, chrome_beta_pages.size());
103 ASSERT_EQ(2U, webview_pages.size());
104
105 // Check that we have non-empty description for webview pages.
106 ASSERT_EQ(0U, chrome_pages[0]->GetDescription().size());
107 ASSERT_NE(0U, webview_pages[0]->GetDescription().size());
108 ASSERT_NE(0U, webview_pages[1]->GetDescription().size());
109
110 ASSERT_EQ(GURL("http://www.chromium.org/"), chrome_pages[0]->GetUrl());
111 ASSERT_EQ("The Chromium Projects", chrome_pages[0]->GetTitle());
112
113 STLDeleteElements(&chrome_pages);
114 STLDeleteElements(&webview_pages);
357 } 115 }
358 116
359 private: 117 private:
360 void EndTest() {
361 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
362 android_bridge_ = NULL;
363
364 content::BrowserThread::PostTaskAndReply(
365 content::BrowserThread::IO,
366 FROM_HERE,
367 base::Bind(&AdbClientSocketTest::StopMockAdbServer,
368 base::Unretained(this)),
369 base::Bind(&AdbClientSocketTest::StopMessageLoop,
370 base::Unretained(this)));
371 }
372
373 void StartMockAdbServer() {
374 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO));
375 net::IPAddressNumber address;
376 net::ParseIPLiteralToNumber("127.0.0.1", &address);
377 net::IPEndPoint endpoint(address, kAdbPort);
378
379 adb_server_.reset(new MockAdbServer(endpoint, kBufferSize));
380 }
381
382 void StopMockAdbServer() {
383 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO));
384 adb_server_.reset();
385 }
386
387 void StopMessageLoop() {
388 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
389 runner->Quit();
390 }
391
392 void AddListener() {
393 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
394 android_bridge_ = DevToolsAndroidBridge::Factory::GetForProfile(
395 browser()->profile());
396
397 AndroidDeviceManager::DeviceProviders device_providers;
398 device_providers.push_back(AndroidDeviceManager::GetAdbDeviceProvider());
399
400 android_bridge_->set_device_providers_for_test(device_providers);
401 android_bridge_->AddDeviceListListener(this);
402 }
403
404 public:
405 scoped_refptr<content::MessageLoopRunner> runner;
406
407 private:
408 scoped_ptr<MockAdbServer> adb_server_;
409 scoped_refptr<DevToolsAndroidBridge> android_bridge_; 118 scoped_refptr<DevToolsAndroidBridge> android_bridge_;
410 DevToolsAndroidBridge::RemoteDevices devices_; 119 DevToolsAndroidBridge::RemoteDevices devices_;
411 }; 120 };
412 121
413 IN_PROC_BROWSER_TEST_F(AdbClientSocketTest, TestAdbClientSocket) { 122 IN_PROC_BROWSER_TEST_F(AdbClientSocketTest, TestAdbClientSocket) {
414 CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); 123 StartMockAdbServer();
415 runner = new content::MessageLoopRunner;
416
417 StartTest(); 124 StartTest();
418 125 content::RunMessageLoop();
419 runner->Run();
420
421 CheckDevices(); 126 CheckDevices();
127 StopMockAdbServer();
422 } 128 }
OLDNEW
« no previous file with comments | « no previous file | chrome/browser/devtools/device/adb/mock_adb_server.h » ('j') | no next file with comments »

Powered by Google App Engine
RSS Feeds Recent Issues | This issue
This is Rietveld 1f9106d