- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / test / chromedriver / chrome / geolocation_override_manager_unittest.cc
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.
4
5 #include <string>
6 #include <vector>
7
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"
15
16 namespace {
17
18 struct Command {
19   Command() {}
20   Command(const std::string& method, const base::DictionaryValue& params)
21       : method(method) {
22     this->params.MergeDictionary(&params);
23   }
24   Command(const Command& command) {
25     *this = command;
26   }
27   Command& operator=(const Command& command) {
28     method = command.method;
29     params.Clear();
30     params.MergeDictionary(&command.params);
31     return *this;
32   }
33   ~Command() {}
34
35   std::string method;
36   base::DictionaryValue params;
37 };
38
39 class RecorderDevToolsClient : public StubDevToolsClient {
40  public:
41   RecorderDevToolsClient() {}
42   virtual ~RecorderDevToolsClient() {}
43
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));
50     return Status(kOk);
51   }
52
53   std::vector<Command> commands_;
54 };
55
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);
66 }
67
68 }  // namespace
69
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));
78
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));
84 }
85
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());
91
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));
99 }
100
101 TEST(GeolocationOverrideManager, SendsCommandOnNavigation) {
102   RecorderDevToolsClient client;
103   GeolocationOverrideManager manager(&client);
104   base::DictionaryValue main_frame_params;
105   ASSERT_EQ(kOk,
106             manager.OnEvent(&client, "Page.frameNavigated", main_frame_params)
107                 .code());
108   ASSERT_EQ(0u, client.commands_.size());
109
110   Geoposition geoposition = {1, 2, 3};
111   manager.OverrideGeolocation(geoposition);
112   ASSERT_EQ(1u, client.commands_.size());
113   ASSERT_EQ(kOk,
114             manager.OnEvent(&client, "Page.frameNavigated", main_frame_params)
115                 .code());
116   ASSERT_EQ(2u, client.commands_.size());
117   ASSERT_NO_FATAL_FAILURE(
118       AssertGeolocationCommand(client.commands_[1], geoposition));
119
120   base::DictionaryValue sub_frame_params;
121   sub_frame_params.SetString("frame.parentId", "id");
122   ASSERT_EQ(
123       kOk,
124       manager.OnEvent(&client, "Page.frameNavigated", sub_frame_params).code());
125   ASSERT_EQ(2u, client.commands_.size());
126 }