1 // Copyright (c) 2012 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.
9 #include "base/compiler_specific.h"
10 #include "base/json/json_reader.h"
11 #include "base/location.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/memory/scoped_vector.h"
14 #include "base/message_loop.h"
15 #include "base/synchronization/waitable_event.h"
16 #include "base/threading/thread.h"
17 #include "base/values.h"
18 #include "chrome/test/chromedriver/chrome/status.h"
19 #include "chrome/test/chromedriver/chromedriver.h"
20 #include "chrome/test/chromedriver/command_executor.h"
21 #include "testing/gtest/include/gtest/gtest.h"
25 void ExpectExecuteError(const std::string
& command
) {
27 ExecuteCommand(command
, &response
);
28 scoped_ptr
<base::Value
> value(base::JSONReader::Read(response
));
29 ASSERT_TRUE(value
.get());
30 base::DictionaryValue
* dict
;
31 ASSERT_TRUE(value
->GetAsDictionary(&dict
));
33 ASSERT_TRUE(dict
->GetInteger("status", &status
));
34 EXPECT_EQ(kUnknownError
, status
);
37 class DummyExecutor
: public CommandExecutor
{
39 virtual ~DummyExecutor() {}
41 virtual void Init() OVERRIDE
{}
42 virtual void ExecuteCommand(const std::string
& name
,
43 const base::DictionaryValue
& params
,
44 const std::string
& session_id
,
46 scoped_ptr
<base::Value
>* value
,
47 std::string
* out_session_id
) OVERRIDE
{}
51 struct ExpectedCommand
{
53 const std::string
& name
,
54 const base::DictionaryValue
& in_params
,
55 const std::string
& session_id
,
56 StatusCode return_status
,
57 scoped_ptr
<base::Value
> return_value
,
58 const std::string
& return_session_id
)
60 session_id(session_id
),
61 return_status(return_status
),
62 return_value(return_value
.Pass()),
63 return_session_id(return_session_id
) {
64 params
.MergeDictionary(&in_params
);
70 base::DictionaryValue params
;
71 std::string session_id
;
72 StatusCode return_status
;
73 scoped_ptr
<base::Value
> return_value
;
74 std::string return_session_id
;
77 class ExecutorMock
: public CommandExecutor
{
79 virtual ~ExecutorMock() {
80 EXPECT_TRUE(DidSatisfyExpectations());
83 virtual void Init() OVERRIDE
{}
85 virtual void ExecuteCommand(const std::string
& name
,
86 const base::DictionaryValue
& params
,
87 const std::string
& session_id
,
89 scoped_ptr
<base::Value
>* value
,
90 std::string
* out_session_id
) OVERRIDE
{
91 ASSERT_TRUE(expectations_
.size());
92 ASSERT_STREQ(expectations_
[0]->name
.c_str(), name
.c_str());
93 ASSERT_TRUE(expectations_
[0]->params
.Equals(¶ms
));
94 ASSERT_STREQ(expectations_
[0]->session_id
.c_str(), session_id
.c_str());
95 *status
= expectations_
[0]->return_status
;
96 value
->reset(expectations_
[0]->return_value
.release());
97 *out_session_id
= expectations_
[0]->return_session_id
;
98 expectations_
.erase(expectations_
.begin());
101 void Expect(scoped_ptr
<ExpectedCommand
> expected
) {
102 expectations_
.push_back(expected
.release());
105 bool DidSatisfyExpectations() const {
106 return expectations_
.empty();
110 ScopedVector
<ExpectedCommand
> expectations_
;
115 TEST(ChromeDriver
, InvalidCommands
) {
116 Init(scoped_ptr
<CommandExecutor
>(new DummyExecutor()));
117 ExpectExecuteError("hi[]");
118 ExpectExecuteError("[]");
120 "{\"parameters\": {}, \"sessionId\": \"\"}");
122 "{\"name\": 1, \"parameters\": {}, \"sessionId\": \"\"}");
124 "{\"name\": \"\", \"sessionId\": \"\"}");
126 "{\"name\": \"\", \"parameters\": 1, \"sessionId\": \"\"}");
128 "{\"name\": \"\", \"parameters\": {}}");
130 "{\"name\": \"\", \"parameters\": {}, \"sessionId\": 1}");
134 TEST(ChromeDriver
, ExecuteCommand
) {
135 scoped_ptr
<ExecutorMock
> scoped_mock(new ExecutorMock());
136 ExecutorMock
* mock
= scoped_mock
.get();
137 Init(scoped_mock
.PassAs
<CommandExecutor
>());
139 base::DictionaryValue params
;
140 params
.SetInteger("param", 100);
141 scoped_ptr
<base::Value
> value(new base::StringValue("stuff"));
142 mock
->Expect(scoped_ptr
<ExpectedCommand
>(new ExpectedCommand(
143 "name", params
, "id", kOk
, value
.Pass(), "session_id")));
145 std::string response
;
146 ExecuteCommand("{\"name\": \"name\", "
147 " \"parameters\": {\"param\": 100}, "
148 " \"sessionId\": \"id\"}",
150 ASSERT_TRUE(mock
->DidSatisfyExpectations());
152 scoped_ptr
<base::Value
> value(base::JSONReader::Read(response
));
153 ASSERT_TRUE(value
.get());
154 base::DictionaryValue
* dict
;
155 ASSERT_TRUE(value
->GetAsDictionary(&dict
));
157 ASSERT_TRUE(dict
->GetInteger("status", &status
));
158 std::string value_str
;
159 ASSERT_TRUE(dict
->GetString("value", &value_str
));
160 EXPECT_STREQ("stuff", value_str
.c_str());
161 EXPECT_EQ(kOk
, status
);
164 base::DictionaryValue params
;
165 scoped_ptr
<base::Value
> value(base::Value::CreateNullValue());
166 mock
->Expect(scoped_ptr
<ExpectedCommand
>(new ExpectedCommand(
167 "quitAll", params
, std::string(), kOk
, value
.Pass(), std::string())));