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
|
// 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 <string>
#include "base/bind.h"
#include "base/callback.h"
#include "base/memory/scoped_ptr.h"
#include "base/values.h"
#include "chrome/test/chromedriver/command_executor_impl.h"
#include "chrome/test/chromedriver/status.h"
#include "testing/gtest/include/gtest/gtest.h"
TEST(CommandExecutorImplTest, UnknownCommand) {
CommandExecutorImpl executor;
base::DictionaryValue empty_dict;
StatusCode status;
scoped_ptr<base::Value> value;
std::string session_id;
executor.ExecuteCommand("noSuchCommand", empty_dict, "session",
&status, &value, &session_id);
ASSERT_EQ(kUnknownCommand, status);
base::DictionaryValue* error;
ASSERT_TRUE(value->GetAsDictionary(&error));
std::string error_msg;
ASSERT_TRUE(error->GetString("message", &error_msg));
ASSERT_NE(std::string::npos, error_msg.find("noSuchCommand"));
ASSERT_STREQ("session", session_id.c_str());
}
namespace {
Status ExecuteSimpleCommand(
const base::DictionaryValue* expected_params,
const std::string& expected_session_id,
const base::DictionaryValue& params,
const std::string& session_id,
scoped_ptr<base::Value>* value,
std::string* out_session_id) {
EXPECT_EQ(expected_params, ¶ms);
EXPECT_STREQ(expected_session_id.c_str(), session_id.c_str());
value->reset(new base::StringValue("hi"));
*out_session_id = "out session id";
return Status(kOk);
}
} // namespace
TEST(CommandExecutorImplTest, SimpleCommand) {
CommandExecutorImpl executor;
base::DictionaryValue params;
std::string session_id("some id");
executor.command_map_.Set("simpleCommand",
base::Bind(&ExecuteSimpleCommand, ¶ms, session_id));
StatusCode status_code;
scoped_ptr<base::Value> value;
std::string out_session_id;
executor.ExecuteCommand("simpleCommand", params, session_id,
&status_code, &value,
&out_session_id);
ASSERT_EQ(kOk, status_code);
ASSERT_TRUE(value);
base::StringValue hi("hi");
ASSERT_TRUE(value->Equals(&hi));
ASSERT_STREQ("out session id", out_session_id.c_str());
}
namespace {
Status ExecuteSimpleCommand2(
const base::DictionaryValue& params,
const std::string& session_id,
scoped_ptr<base::Value>* out_value,
std::string* out_session_id) {
return Status(kOk);
}
} // namespace
TEST(CommandExecutorImplTest, CommandThatDoesntSetValueOrSessionId) {
CommandExecutorImpl executor;
executor.command_map_.Set(
"simpleCommand",
base::Bind(&ExecuteSimpleCommand2));
base::DictionaryValue params;
StatusCode status_code;
scoped_ptr<base::Value> value;
std::string session_id;
executor.ExecuteCommand("simpleCommand", params, "session",
&status_code, &value, &session_id);
ASSERT_TRUE(value->IsType(base::Value::TYPE_NULL));
ASSERT_STREQ("", session_id.c_str());
}
namespace {
Status ExecuteSimpleCommand3(
const base::DictionaryValue& params,
const std::string& session_id,
scoped_ptr<base::Value>* value,
std::string* out_session_id) {
value->reset(new base::StringValue("hi"));
return Status(kUnknownError);
}
} // namespace
TEST(CommandExecutorImplTest, CommandThatReturnsError) {
CommandExecutorImpl executor;
executor.command_map_.Set("simpleCommand",
base::Bind(&ExecuteSimpleCommand3));
base::DictionaryValue params;
StatusCode status_code;
scoped_ptr<base::Value> value;
std::string out_session_id;
executor.ExecuteCommand("simpleCommand", params, "",
&status_code, &value,
&out_session_id);
ASSERT_EQ(kUnknownError, status_code);
ASSERT_TRUE(value);
base::DictionaryValue* error;
ASSERT_TRUE(value->GetAsDictionary(&error));
std::string message;
ASSERT_TRUE(error->GetString("message", &message));
ASSERT_NE(std::string::npos, message.find("unknown error"));
}
|