-
Notifications
You must be signed in to change notification settings - Fork 68
/
Copy pathchrome_impl_unittest.cc
252 lines (220 loc) · 7.22 KB
/
chrome_impl_unittest.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <list>
#include <string>
#include "base/compiler_specific.h"
#include "base/memory/scoped_ptr.h"
#include "base/values.h"
#include "chrome/test/chromedriver/chrome_impl.h"
#include "chrome/test/chromedriver/devtools_client.h"
#include "chrome/test/chromedriver/status.h"
#include "testing/gtest/include/gtest/gtest.h"
TEST(ParsePagesInfo, Normal) {
std::list<std::string> urls;
Status status = internal::ParsePagesInfo(
"[{\"webSocketDebuggerUrl\": \"http://debugurl\"}]",
&urls);
ASSERT_TRUE(status.IsOk());
ASSERT_EQ(1u, urls.size());
ASSERT_EQ("http://debugurl", urls.front());
}
TEST(ParsePagesInfo, Multiple) {
std::list<std::string> urls;
Status status = internal::ParsePagesInfo(
"[{\"webSocketDebuggerUrl\": \"http://debugurl\"},"
" {\"webSocketDebuggerUrl\": \"http://debugurl2\"}]",
&urls);
ASSERT_TRUE(status.IsOk());
ASSERT_EQ(2u, urls.size());
ASSERT_EQ("http://debugurl", urls.front());
ASSERT_EQ("http://debugurl2", urls.back());
}
namespace {
void AssertFails(const std::string& data) {
std::list<std::string> urls;
Status status = internal::ParsePagesInfo(data, &urls);
ASSERT_FALSE(status.IsOk());
ASSERT_EQ(0u, urls.size());
}
} // namespace
TEST(ParsePagesInfo, InvalidJSON) {
AssertFails("[");
}
TEST(ParsePagesInfo, NonList) {
AssertFails("{}");
}
TEST(ParsePagesInfo, NonDictionary) {
AssertFails("[1]");
}
TEST(ParsePagesInfo, NoDebuggerUrl) {
AssertFails("[{\"hi\": 1}]");
}
TEST(ParsePagesInfo, InvalidDebuggerUrl) {
AssertFails("[{\"webSocketDebuggerUrl\": 1}]");
}
namespace {
class FakeDevToolsClient : public DevToolsClient {
public:
FakeDevToolsClient() : status_(kOk) {}
virtual ~FakeDevToolsClient() {}
void set_status(const Status& status) {
status_ = status;
}
void set_result(const base::DictionaryValue& result) {
result_.Clear();
result_.MergeDictionary(&result);
}
// Overridden from DevToolsClient:
virtual Status SendCommand(const std::string& method,
const base::DictionaryValue& params) OVERRIDE {
return SendCommandAndGetResult(method, params, NULL);
}
virtual Status SendCommandAndGetResult(
const std::string& method,
const base::DictionaryValue& params,
scoped_ptr<base::DictionaryValue>* result) OVERRIDE {
if (status_.IsError())
return status_;
result->reset(result_.DeepCopy());
return Status(kOk);
}
virtual void AddListener(DevToolsEventListener* listener) OVERRIDE {}
virtual Status HandleEventsUntil(
const ConditionalFunc& conditional_func) OVERRIDE {
return Status(kOk);
}
private:
Status status_;
base::DictionaryValue result_;
};
void AssertEvalFails(const base::DictionaryValue& command_result) {
scoped_ptr<base::DictionaryValue> result;
FakeDevToolsClient client;
client.set_result(command_result);
Status status = internal::EvaluateScript(&client, 0, "",
internal::ReturnByValue, &result);
ASSERT_EQ(kUnknownError, status.code());
ASSERT_FALSE(result);
}
} // namespace
TEST(EvaluateScript, CommandError) {
scoped_ptr<base::DictionaryValue> result;
FakeDevToolsClient client;
client.set_status(Status(kUnknownError));
Status status = internal::EvaluateScript(&client, 0, "",
internal::ReturnByValue, &result);
ASSERT_EQ(kUnknownError, status.code());
ASSERT_FALSE(result);
}
TEST(EvaluateScript, MissingWasThrown) {
base::DictionaryValue dict;
ASSERT_NO_FATAL_FAILURE(AssertEvalFails(dict));
}
TEST(EvaluateScript, MissingResult) {
base::DictionaryValue dict;
dict.SetBoolean("wasThrown", false);
ASSERT_NO_FATAL_FAILURE(AssertEvalFails(dict));
}
TEST(EvaluateScript, Throws) {
base::DictionaryValue dict;
dict.SetBoolean("wasThrown", true);
dict.SetString("result.type", "undefined");
ASSERT_NO_FATAL_FAILURE(AssertEvalFails(dict));
}
TEST(EvaluateScript, Ok) {
scoped_ptr<base::DictionaryValue> result;
base::DictionaryValue dict;
dict.SetBoolean("wasThrown", false);
dict.SetInteger("result.key", 100);
FakeDevToolsClient client;
client.set_result(dict);
ASSERT_TRUE(internal::EvaluateScript(
&client, 0, "", internal::ReturnByValue, &result).IsOk());
ASSERT_TRUE(result);
ASSERT_TRUE(result->HasKey("key"));
}
TEST(EvaluateScriptAndGetValue, MissingType) {
scoped_ptr<base::Value> result;
FakeDevToolsClient client;
base::DictionaryValue dict;
dict.SetBoolean("wasThrown", false);
dict.SetInteger("result.value", 1);
client.set_result(dict);
ASSERT_TRUE(internal::EvaluateScriptAndGetValue(
&client, 0, "", &result).IsError());
}
TEST(EvaluateScriptAndGetValue, Undefined) {
scoped_ptr<base::Value> result;
FakeDevToolsClient client;
base::DictionaryValue dict;
dict.SetBoolean("wasThrown", false);
dict.SetString("result.type", "undefined");
client.set_result(dict);
Status status = internal::EvaluateScriptAndGetValue(
&client, 0, "", &result);
ASSERT_EQ(kOk, status.code());
ASSERT_TRUE(result && result->IsType(base::Value::TYPE_NULL));
}
TEST(EvaluateScriptAndGetValue, Ok) {
scoped_ptr<base::Value> result;
FakeDevToolsClient client;
base::DictionaryValue dict;
dict.SetBoolean("wasThrown", false);
dict.SetString("result.type", "integer");
dict.SetInteger("result.value", 1);
client.set_result(dict);
Status status = internal::EvaluateScriptAndGetValue(
&client, 0, "", &result);
ASSERT_EQ(kOk, status.code());
int value;
ASSERT_TRUE(result && result->GetAsInteger(&value));
ASSERT_EQ(1, value);
}
TEST(EvaluateScriptAndGetObject, NoObject) {
FakeDevToolsClient client;
base::DictionaryValue dict;
dict.SetBoolean("wasThrown", false);
dict.SetString("result.type", "integer");
client.set_result(dict);
std::string object_id;
ASSERT_TRUE(internal::EvaluateScriptAndGetObject(
&client, 0, "", &object_id).IsError());
ASSERT_TRUE(object_id.empty());
}
TEST(EvaluateScriptAndGetObject, Ok) {
FakeDevToolsClient client;
base::DictionaryValue dict;
dict.SetBoolean("wasThrown", false);
dict.SetString("result.objectId", "id");
client.set_result(dict);
std::string object_id;
ASSERT_TRUE(internal::EvaluateScriptAndGetObject(
&client, 0, "", &object_id).IsOk());
ASSERT_STREQ("id", object_id.c_str());
}
TEST(ParseCallFunctionResult, NotDict) {
scoped_ptr<base::Value> result;
base::FundamentalValue value(1);
ASSERT_NE(kOk, internal::ParseCallFunctionResult(value, &result).code());
}
TEST(ParseCallFunctionResult, Ok) {
scoped_ptr<base::Value> result;
base::DictionaryValue dict;
dict.SetInteger("status", 0);
dict.SetInteger("value", 1);
Status status = internal::ParseCallFunctionResult(dict, &result);
ASSERT_EQ(kOk, status.code());
int value;
ASSERT_TRUE(result && result->GetAsInteger(&value));
ASSERT_EQ(1, value);
}
TEST(ParseCallFunctionResult, ScriptError) {
scoped_ptr<base::Value> result;
base::DictionaryValue dict;
dict.SetInteger("status", 1);
dict.SetInteger("value", 1);
Status status = internal::ParseCallFunctionResult(dict, &result);
ASSERT_EQ(1, status.code());
ASSERT_FALSE(result);
}