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.
8 #include "base/compiler_specific.h"
9 #include "base/values.h"
10 #include "chrome/test/chromedriver/chrome/device_metrics.h"
11 #include "chrome/test/chromedriver/chrome/mobile_emulation_override_manager.h"
12 #include "chrome/test/chromedriver/chrome/status.h"
13 #include "chrome/test/chromedriver/chrome/stub_devtools_client.h"
14 #include "chrome/test/chromedriver/chrome/version.h"
15 #include "testing/gtest/include/gtest/gtest.h"
21 Command(const std::string& method, const base::DictionaryValue& params)
23 this->params.MergeDictionary(¶ms);
25 Command(const Command& command) {
28 Command& operator=(const Command& command) {
29 method = command.method;
31 params.MergeDictionary(&command.params);
37 base::DictionaryValue params;
40 class RecorderDevToolsClient : public StubDevToolsClient {
42 RecorderDevToolsClient() {}
43 virtual ~RecorderDevToolsClient() {}
45 // Overridden from StubDevToolsClient:
46 virtual Status SendCommandAndGetResult(
47 const std::string& method,
48 const base::DictionaryValue& params,
49 scoped_ptr<base::DictionaryValue>* result) OVERRIDE {
50 commands_.push_back(Command(method, params));
54 std::vector<Command> commands_;
57 void AssertDeviceMetricsCommand(const Command& command,
58 const DeviceMetrics& device_metrics) {
59 ASSERT_EQ("Page.setDeviceMetricsOverride", command.method);
61 double device_scale_factor, font_scale_factor;
62 bool mobile, fit_window, text_autosizing;
63 ASSERT_TRUE(command.params.GetInteger("width", &width));
64 ASSERT_TRUE(command.params.GetInteger("height", &height));
65 ASSERT_TRUE(command.params.GetDouble("deviceScaleFactor",
66 &device_scale_factor));
67 ASSERT_TRUE(command.params.GetBoolean("mobile", &mobile));
68 ASSERT_TRUE(command.params.GetBoolean("fitWindow", &fit_window));
69 ASSERT_TRUE(command.params.GetBoolean("textAutosizing", &text_autosizing));
70 ASSERT_TRUE(command.params.GetDouble("fontScaleFactor", &font_scale_factor));
71 ASSERT_EQ(device_metrics.width, width);
72 ASSERT_EQ(device_metrics.height, height);
73 ASSERT_EQ(device_metrics.device_scale_factor, device_scale_factor);
74 ASSERT_EQ(device_metrics.mobile, mobile);
75 ASSERT_EQ(device_metrics.fit_window, fit_window);
76 ASSERT_EQ(device_metrics.text_autosizing, text_autosizing);
77 ASSERT_EQ(device_metrics.font_scale_factor, font_scale_factor);
82 TEST(MobileEmulationOverrideManager, SendsCommandOnConnect) {
83 RecorderDevToolsClient client;
84 DeviceMetrics device_metrics(1, 2, 3.0);
85 BrowserInfo browser_info;
86 MobileEmulationOverrideManager manager(&client,
89 ASSERT_EQ(0u, client.commands_.size());
90 ASSERT_EQ(kOk, manager.OnConnected(&client).code());
92 ASSERT_EQ(1u, client.commands_.size());
93 ASSERT_EQ(kOk, manager.OnConnected(&client).code());
94 ASSERT_EQ(2u, client.commands_.size());
95 ASSERT_NO_FATAL_FAILURE(
96 AssertDeviceMetricsCommand(client.commands_[1], device_metrics));
99 TEST(MobileEmulationOverrideManager, SendsCommandOnNavigation) {
100 RecorderDevToolsClient client;
101 DeviceMetrics device_metrics(1, 2, 3.0);
102 BrowserInfo browser_info;
103 MobileEmulationOverrideManager manager(&client,
106 base::DictionaryValue main_frame_params;
108 manager.OnEvent(&client, "Page.frameNavigated", main_frame_params)
110 ASSERT_EQ(1u, client.commands_.size());
112 manager.OnEvent(&client, "Page.frameNavigated", main_frame_params)
114 ASSERT_EQ(2u, client.commands_.size());
115 ASSERT_NO_FATAL_FAILURE(
116 AssertDeviceMetricsCommand(client.commands_[1], device_metrics));
118 base::DictionaryValue sub_frame_params;
119 sub_frame_params.SetString("frame.parentId", "id");
122 manager.OnEvent(&client, "Page.frameNavigated", sub_frame_params).code());
123 ASSERT_EQ(2u, client.commands_.size());