OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include <list> | |
6 #include <string> | |
7 | |
8 #include "base/bind.h" | |
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" | |
22 | |
23 namespace { | |
24 | |
25 void ExpectExecuteError(const std::string& command) { | |
26 std::string response; | |
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)); | |
32 int status; | |
33 ASSERT_TRUE(dict->GetInteger("status", &status)); | |
34 EXPECT_EQ(kUnknownError, status); | |
35 } | |
36 | |
37 class DummyExecutor : public CommandExecutor { | |
38 public: | |
39 virtual ~DummyExecutor() {} | |
40 | |
41 virtual void Init() OVERRIDE {} | |
42 virtual void ExecuteCommand(const std::string& name, | |
43 const base::DictionaryValue& params, | |
44 const std::string& session_id, | |
45 StatusCode* status, | |
46 scoped_ptr<base::Value>* value, | |
47 std::string* out_session_id) OVERRIDE {} | |
48 }; | |
49 | |
50 | |
51 struct ExpectedCommand { | |
52 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) | |
59 : name(name), | |
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); | |
65 } | |
66 | |
67 ~ExpectedCommand() {} | |
68 | |
69 std::string name; | |
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; | |
75 }; | |
76 | |
77 class ExecutorMock : public CommandExecutor { | |
78 public: | |
79 virtual ~ExecutorMock() { | |
80 EXPECT_TRUE(DidSatisfyExpectations()); | |
81 } | |
82 | |
83 virtual void Init() OVERRIDE {} | |
84 | |
85 virtual void ExecuteCommand(const std::string& name, | |
86 const base::DictionaryValue& params, | |
87 const std::string& session_id, | |
88 StatusCode* status, | |
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()); | |
99 } | |
100 | |
101 void Expect(scoped_ptr<ExpectedCommand> expected) { | |
102 expectations_.push_back(expected.release()); | |
103 } | |
104 | |
105 bool DidSatisfyExpectations() const { | |
106 return expectations_.empty(); | |
107 } | |
108 | |
109 private: | |
110 ScopedVector<ExpectedCommand> expectations_; | |
111 }; | |
112 | |
113 } // namespace | |
114 | |
115 TEST(ChromeDriver, InvalidCommands) { | |
116 Init(scoped_ptr<CommandExecutor>(new DummyExecutor())); | |
117 ExpectExecuteError("hi[]"); | |
118 ExpectExecuteError("[]"); | |
119 ExpectExecuteError( | |
120 "{\"parameters\": {}, \"sessionId\": \"\"}"); | |
121 ExpectExecuteError( | |
122 "{\"name\": 1, \"parameters\": {}, \"sessionId\": \"\"}"); | |
123 ExpectExecuteError( | |
124 "{\"name\": \"\", \"sessionId\": \"\"}"); | |
125 ExpectExecuteError( | |
126 "{\"name\": \"\", \"parameters\": 1, \"sessionId\": \"\"}"); | |
127 ExpectExecuteError( | |
128 "{\"name\": \"\", \"parameters\": {}}"); | |
129 ExpectExecuteError( | |
130 "{\"name\": \"\", \"parameters\": {}, \"sessionId\": 1}"); | |
131 Shutdown(); | |
132 } | |
133 | |
134 TEST(ChromeDriver, ExecuteCommand) { | |
135 scoped_ptr<ExecutorMock> scoped_mock(new ExecutorMock()); | |
136 ExecutorMock* mock = scoped_mock.get(); | |
137 Init(scoped_mock.PassAs<CommandExecutor>()); | |
138 { | |
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"))); | |
144 } | |
145 std::string response; | |
146 ExecuteCommand("{\"name\": \"name\", " | |
147 " \"parameters\": {\"param\": 100}, " | |
148 " \"sessionId\": \"id\"}", | |
149 &response); | |
150 ASSERT_TRUE(mock->DidSatisfyExpectations()); | |
151 { | |
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)); | |
156 int status; | |
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); | |
162 } | |
163 { | |
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()))); | |
168 } | |
169 Shutdown(); | |
170 } | |
OLD | NEW |