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

Side by Side Diff: chrome/test/chromedriver/chrome/mobile_emulation_override_manager.cc

Issue 2785083002: Use devtools to set user agent in chromedriver (Closed)
Patch Set: Fix New Tab test Created 3 years, 8 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
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 "chrome/test/chromedriver/chrome/mobile_emulation_override_manager.h" 5 #include "chrome/test/chromedriver/chrome/mobile_emulation_override_manager.h"
6 6
7 #include "base/values.h" 7 #include "base/values.h"
8 #include "chrome/test/chromedriver/chrome/device_metrics.h" 8 #include "chrome/test/chromedriver/chrome/device_metrics.h"
9 #include "chrome/test/chromedriver/chrome/devtools_client.h" 9 #include "chrome/test/chromedriver/chrome/devtools_client.h"
10 #include "chrome/test/chromedriver/chrome/status.h" 10 #include "chrome/test/chromedriver/chrome/status.h"
11 11
12 MobileEmulationOverrideManager::MobileEmulationOverrideManager( 12 MobileEmulationOverrideManager::MobileEmulationOverrideManager(
13 DevToolsClient* client, 13 DevToolsClient* client,
14 const DeviceMetrics* device_metrics) 14 const DeviceMetrics* device_metrics,
15 std::string user_agent)
15 : client_(client), 16 : client_(client),
16 overridden_device_metrics_(device_metrics) { 17 overridden_device_metrics_(device_metrics),
17 if (overridden_device_metrics_) 18 overridden_user_agent_(user_agent) {
19 if (overridden_device_metrics_ || !overridden_user_agent_.empty())
18 client_->AddListener(this); 20 client_->AddListener(this);
19 } 21 }
20 22
21 MobileEmulationOverrideManager::~MobileEmulationOverrideManager() { 23 MobileEmulationOverrideManager::~MobileEmulationOverrideManager() {
22 } 24 }
23 25
24 Status MobileEmulationOverrideManager::OnConnected(DevToolsClient* client) { 26 Status MobileEmulationOverrideManager::OnConnected(DevToolsClient* client) {
25 return ApplyOverrideIfNeeded(); 27 return ApplyOverrideIfNeeded();
26 } 28 }
27 29
28 Status MobileEmulationOverrideManager::OnEvent( 30 Status MobileEmulationOverrideManager::OnEvent(
29 DevToolsClient* client, 31 DevToolsClient* client,
30 const std::string& method, 32 const std::string& method,
31 const base::DictionaryValue& params) { 33 const base::DictionaryValue& params) {
32 if (method == "Page.frameNavigated") { 34 if (method == "Page.frameNavigated") {
33 const base::Value* unused_value; 35 const base::Value* unused_value;
34 if (!params.Get("frame.parentId", &unused_value)) 36 if (!params.Get("frame.parentId", &unused_value))
35 return ApplyOverrideIfNeeded(); 37 return ApplyOverrideIfNeeded();
36 } 38 }
37 return Status(kOk); 39 return Status(kOk);
38 } 40 }
39 41
40 bool MobileEmulationOverrideManager::IsEmulatingTouch() { 42 bool MobileEmulationOverrideManager::IsEmulatingTouch() {
41 return overridden_device_metrics_ && overridden_device_metrics_->touch; 43 return overridden_device_metrics_ && overridden_device_metrics_->touch;
42 } 44 }
43 45
44 Status MobileEmulationOverrideManager::ApplyOverrideIfNeeded() { 46 Status MobileEmulationOverrideManager::ApplyOverrideIfNeeded() {
47 if (!overridden_user_agent_.empty()) {
48 base::DictionaryValue net_enable_params, user_agent_params;
49 net_enable_params.SetInteger("maxTotalBufferSize", 0);
50 net_enable_params.SetInteger("maxResourceBufferSize", 0);
51 Status status = client_->SendCommand("Network.enable", net_enable_params);
52
53 user_agent_params.SetString("userAgent", overridden_user_agent_);
54 status =
55 client_->SendCommand("Network.setUserAgentOverride", user_agent_params);
56 if (status.IsError())
57 return status;
58 }
59
45 if (overridden_device_metrics_ == NULL) 60 if (overridden_device_metrics_ == NULL)
46 return Status(kOk); 61 return Status(kOk);
47 62
48 base::DictionaryValue params; 63 base::DictionaryValue params;
49 params.SetInteger("width", overridden_device_metrics_->width); 64 params.SetInteger("width", overridden_device_metrics_->width);
50 params.SetInteger("height", overridden_device_metrics_->height); 65 params.SetInteger("height", overridden_device_metrics_->height);
51 params.SetDouble("deviceScaleFactor", 66 params.SetDouble("deviceScaleFactor",
52 overridden_device_metrics_->device_scale_factor); 67 overridden_device_metrics_->device_scale_factor);
53 params.SetBoolean("mobile", overridden_device_metrics_->mobile); 68 params.SetBoolean("mobile", overridden_device_metrics_->mobile);
54 params.SetBoolean("fitWindow", overridden_device_metrics_->fit_window); 69 params.SetBoolean("fitWindow", overridden_device_metrics_->fit_window);
55 params.SetBoolean("textAutosizing", 70 params.SetBoolean("textAutosizing",
56 overridden_device_metrics_->text_autosizing); 71 overridden_device_metrics_->text_autosizing);
57 params.SetDouble("fontScaleFactor", 72 params.SetDouble("fontScaleFactor",
58 overridden_device_metrics_->font_scale_factor); 73 overridden_device_metrics_->font_scale_factor);
59 Status status = client_->SendCommand("Page.setDeviceMetricsOverride", params); 74 Status status = client_->SendCommand("Page.setDeviceMetricsOverride", params);
60 if (status.IsError()) 75 if (status.IsError())
61 return status; 76 return status;
62 77
63 if (overridden_device_metrics_->touch) { 78 if (overridden_device_metrics_->touch) {
64 base::DictionaryValue emulate_touch_params; 79 base::DictionaryValue emulate_touch_params;
65 emulate_touch_params.SetBoolean("enabled", true); 80 emulate_touch_params.SetBoolean("enabled", true);
66 status = client_->SendCommand("Emulation.setTouchEmulationEnabled", 81 status = client_->SendCommand("Emulation.setTouchEmulationEnabled",
67 emulate_touch_params); 82 emulate_touch_params);
68 } 83 }
69 84
70 return Status(kOk); 85 return Status(kOk);
71 } 86 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698