1 // Copyright (c) 2013 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/geolocation_override_manager.h"
11 #include "chrome/test/chromedriver/chrome/geoposition.h"
12 #include "chrome/test/chromedriver/chrome/status.h"
13 #include "chrome/test/chromedriver/chrome/stub_devtools_client.h"
14 #include "testing/gtest/include/gtest/gtest.h"
20 Command(const std::string& method, const base::DictionaryValue& params)
22 this->params.MergeDictionary(¶ms);
24 Command(const Command& command) {
27 Command& operator=(const Command& command) {
28 method = command.method;
30 params.MergeDictionary(&command.params);
36 base::DictionaryValue params;
39 class RecorderDevToolsClient : public StubDevToolsClient {
41 RecorderDevToolsClient() {}
42 virtual ~RecorderDevToolsClient() {}
44 // Overridden from StubDevToolsClient:
45 virtual Status SendCommandAndGetResult(
46 const std::string& method,
47 const base::DictionaryValue& params,
48 scoped_ptr<base::DictionaryValue>* result) OVERRIDE {
49 commands_.push_back(Command(method, params));
53 std::vector<Command> commands_;
56 void AssertGeolocationCommand(const Command& command,
57 const Geoposition& geoposition) {
58 ASSERT_EQ("Page.setGeolocationOverride", command.method);
59 double latitude, longitude, accuracy;
60 ASSERT_TRUE(command.params.GetDouble("latitude", &latitude));
61 ASSERT_TRUE(command.params.GetDouble("longitude", &longitude));
62 ASSERT_TRUE(command.params.GetDouble("accuracy", &accuracy));
63 ASSERT_EQ(geoposition.latitude, latitude);
64 ASSERT_EQ(geoposition.longitude, longitude);
65 ASSERT_EQ(geoposition.accuracy, accuracy);
70 TEST(GeolocationOverrideManager, OverrideSendsCommand) {
71 RecorderDevToolsClient client;
72 GeolocationOverrideManager manager(&client);
73 Geoposition geoposition = {1, 2, 3};
74 manager.OverrideGeolocation(geoposition);
75 ASSERT_EQ(1u, client.commands_.size());
76 ASSERT_NO_FATAL_FAILURE(
77 AssertGeolocationCommand(client.commands_[0], geoposition));
79 geoposition.latitude = 5;
80 manager.OverrideGeolocation(geoposition);
81 ASSERT_EQ(2u, client.commands_.size());
82 ASSERT_NO_FATAL_FAILURE(
83 AssertGeolocationCommand(client.commands_[1], geoposition));
86 TEST(GeolocationOverrideManager, SendsCommandOnConnect) {
87 RecorderDevToolsClient client;
88 GeolocationOverrideManager manager(&client);
89 ASSERT_EQ(0u, client.commands_.size());
90 ASSERT_EQ(kOk, manager.OnConnected(&client).code());
92 Geoposition geoposition = {1, 2, 3};
93 manager.OverrideGeolocation(geoposition);
94 ASSERT_EQ(1u, client.commands_.size());
95 ASSERT_EQ(kOk, manager.OnConnected(&client).code());
96 ASSERT_EQ(2u, client.commands_.size());
97 ASSERT_NO_FATAL_FAILURE(
98 AssertGeolocationCommand(client.commands_[1], geoposition));
101 TEST(GeolocationOverrideManager, SendsCommandOnNavigation) {
102 RecorderDevToolsClient client;
103 GeolocationOverrideManager manager(&client);
104 base::DictionaryValue main_frame_params;
106 manager.OnEvent(&client, "Page.frameNavigated", main_frame_params)
108 ASSERT_EQ(0u, client.commands_.size());
110 Geoposition geoposition = {1, 2, 3};
111 manager.OverrideGeolocation(geoposition);
112 ASSERT_EQ(1u, client.commands_.size());
114 manager.OnEvent(&client, "Page.frameNavigated", main_frame_params)
116 ASSERT_EQ(2u, client.commands_.size());
117 ASSERT_NO_FATAL_FAILURE(
118 AssertGeolocationCommand(client.commands_[1], geoposition));
120 base::DictionaryValue sub_frame_params;
121 sub_frame_params.SetString("frame.parentId", "id");
124 manager.OnEvent(&client, "Page.frameNavigated", sub_frame_params).code());
125 ASSERT_EQ(2u, client.commands_.size());