793f8341eaed69d217fc2068e5ccbf84e87dd782
[platform/framework/web/crosswalk.git] / src / chrome / test / chromedriver / chrome / mobile_emulation_override_manager.cc
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
3 // found in the LICENSE file.
4
5 #include "chrome/test/chromedriver/chrome/mobile_emulation_override_manager.h"
6
7 #include "base/values.h"
8 #include "chrome/test/chromedriver/chrome/browser_info.h"
9 #include "chrome/test/chromedriver/chrome/device_metrics.h"
10 #include "chrome/test/chromedriver/chrome/devtools_client.h"
11 #include "chrome/test/chromedriver/chrome/status.h"
12
13 MobileEmulationOverrideManager::MobileEmulationOverrideManager(
14     DevToolsClient* client,
15     const DeviceMetrics* device_metrics,
16     const BrowserInfo* browser_info)
17     : client_(client),
18       overridden_device_metrics_(device_metrics),
19       browser_info_(browser_info) {
20   if (overridden_device_metrics_)
21     client_->AddListener(this);
22 }
23
24 MobileEmulationOverrideManager::~MobileEmulationOverrideManager() {
25 }
26
27 Status MobileEmulationOverrideManager::OnConnected(DevToolsClient* client) {
28   return ApplyOverrideIfNeeded();
29 }
30
31 Status MobileEmulationOverrideManager::OnEvent(
32     DevToolsClient* client,
33     const std::string& method,
34     const base::DictionaryValue& params) {
35   if (method == "Page.frameNavigated") {
36     const base::Value* unused_value;
37     if (!params.Get("frame.parentId", &unused_value))
38       return ApplyOverrideIfNeeded();
39   }
40   return Status(kOk);
41 }
42
43 Status MobileEmulationOverrideManager::ApplyOverrideIfNeeded() {
44   if (overridden_device_metrics_ == NULL)
45     return Status(kOk);
46
47   // Old revisions of Blink expect a parameter named |emulateViewport| but in
48   // Blink revision 177367 (Chromium revision 281046, build number 2081) this
49   // was renamed to |mobile|.
50   std::string mobile_param_name = "mobile";
51   if (browser_info_->browser_name == "chrome") {
52     if (browser_info_->build_no <= 2081)
53       mobile_param_name = "emulateViewport";
54   } else {
55     if (browser_info_->blink_revision < 177367)
56       mobile_param_name = "emulateViewport";
57   }
58
59   base::DictionaryValue params;
60   params.SetInteger("width", overridden_device_metrics_->width);
61   params.SetInteger("height", overridden_device_metrics_->height);
62   params.SetDouble("deviceScaleFactor",
63                    overridden_device_metrics_->device_scale_factor);
64   params.SetBoolean(mobile_param_name, overridden_device_metrics_->mobile);
65   params.SetBoolean("fitWindow", overridden_device_metrics_->fit_window);
66   params.SetBoolean("textAutosizing",
67                     overridden_device_metrics_->text_autosizing);
68   params.SetDouble("fontScaleFactor",
69                    overridden_device_metrics_->font_scale_factor);
70   Status status = client_->SendCommand("Page.setDeviceMetricsOverride", params);
71   if (status.IsError())
72     return status;
73
74   // Always emulate touch.
75   base::DictionaryValue emulate_touch_params;
76   emulate_touch_params.SetBoolean("enabled", true);
77   return client_->SendCommand(
78       "Page.setTouchEmulationEnabled", emulate_touch_params);
79 }