OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <string> | 5 #include <string> |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/callback.h" | 8 #include "base/callback.h" |
9 #include "base/compiler_specific.h" | 9 #include "base/compiler_specific.h" |
10 #include "base/files/file_path.h" | 10 #include "base/files/file_path.h" |
11 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
| 12 #include "base/message_loop/message_loop.h" |
| 13 #include "base/run_loop.h" |
12 #include "base/synchronization/lock.h" | 14 #include "base/synchronization/lock.h" |
| 15 #include "base/threading/thread.h" |
13 #include "base/values.h" | 16 #include "base/values.h" |
14 #include "chrome/test/chromedriver/chrome/status.h" | 17 #include "chrome/test/chromedriver/chrome/status.h" |
15 #include "chrome/test/chromedriver/chrome/stub_chrome.h" | 18 #include "chrome/test/chromedriver/chrome/stub_chrome.h" |
16 #include "chrome/test/chromedriver/chrome/stub_web_view.h" | 19 #include "chrome/test/chromedriver/chrome/stub_web_view.h" |
17 #include "chrome/test/chromedriver/chrome/web_view.h" | 20 #include "chrome/test/chromedriver/chrome/web_view.h" |
18 #include "chrome/test/chromedriver/commands.h" | 21 #include "chrome/test/chromedriver/commands.h" |
19 #include "chrome/test/chromedriver/element_commands.h" | 22 #include "chrome/test/chromedriver/element_commands.h" |
20 #include "chrome/test/chromedriver/fake_session_accessor.h" | 23 #include "chrome/test/chromedriver/session.h" |
21 #include "chrome/test/chromedriver/session_commands.h" | 24 #include "chrome/test/chromedriver/session_commands.h" |
22 #include "chrome/test/chromedriver/window_commands.h" | 25 #include "chrome/test/chromedriver/window_commands.h" |
23 #include "testing/gtest/include/gtest/gtest.h" | 26 #include "testing/gtest/include/gtest/gtest.h" |
24 #include "third_party/webdriver/atoms.h" | 27 #include "third_party/webdriver/atoms.h" |
25 | 28 |
26 TEST(CommandsTest, GetStatus) { | 29 namespace { |
27 base::DictionaryValue params; | 30 |
28 scoped_ptr<base::Value> value; | 31 void OnGetStatus(const Status& status, |
29 std::string session_id; | 32 scoped_ptr<base::Value> value, |
30 ASSERT_EQ( | 33 const std::string& session_id) { |
31 kOk, ExecuteGetStatus(params, std::string(), &value, &session_id).code()); | 34 ASSERT_EQ(kOk, status.code()); |
32 base::DictionaryValue* dict; | 35 base::DictionaryValue* dict; |
33 ASSERT_TRUE(value->GetAsDictionary(&dict)); | 36 ASSERT_TRUE(value->GetAsDictionary(&dict)); |
34 base::Value* unused; | 37 base::Value* unused; |
35 ASSERT_TRUE(dict->Get("os.name", &unused)); | 38 ASSERT_TRUE(dict->Get("os.name", &unused)); |
36 ASSERT_TRUE(dict->Get("os.version", &unused)); | 39 ASSERT_TRUE(dict->Get("os.version", &unused)); |
37 ASSERT_TRUE(dict->Get("os.arch", &unused)); | 40 ASSERT_TRUE(dict->Get("os.arch", &unused)); |
38 ASSERT_TRUE(dict->Get("build.version", &unused)); | 41 ASSERT_TRUE(dict->Get("build.version", &unused)); |
39 } | 42 } |
40 | 43 |
| 44 } // namespace |
| 45 |
| 46 TEST(CommandsTest, GetStatus) { |
| 47 base::DictionaryValue params; |
| 48 ExecuteGetStatus(params, std::string(), base::Bind(&OnGetStatus)); |
| 49 } |
| 50 |
41 namespace { | 51 namespace { |
42 | 52 |
43 Status ExecuteStubQuit( | 53 void ExecuteStubQuit( |
44 int* count, | 54 int* count, |
45 const base::DictionaryValue& params, | 55 const base::DictionaryValue& params, |
46 const std::string& session_id, | 56 const std::string& session_id, |
47 scoped_ptr<base::Value>* value, | 57 const CommandCallback& callback) { |
48 std::string* out_session_id) { | |
49 if (*count == 0) { | 58 if (*count == 0) { |
50 EXPECT_STREQ("id", session_id.c_str()); | 59 EXPECT_STREQ("id", session_id.c_str()); |
51 } else { | 60 } else { |
52 EXPECT_STREQ("id2", session_id.c_str()); | 61 EXPECT_STREQ("id2", session_id.c_str()); |
53 } | 62 } |
54 (*count)++; | 63 (*count)++; |
55 return Status(kOk); | 64 callback.Run(Status(kOk), scoped_ptr<base::Value>(), session_id); |
| 65 } |
| 66 |
| 67 void OnQuitAll(const Status& status, |
| 68 scoped_ptr<base::Value> value, |
| 69 const std::string& session_id) { |
| 70 ASSERT_EQ(kOk, status.code()); |
| 71 ASSERT_FALSE(value.get()); |
56 } | 72 } |
57 | 73 |
58 } // namespace | 74 } // namespace |
59 | 75 |
60 TEST(CommandsTest, QuitAll) { | 76 TEST(CommandsTest, QuitAll) { |
61 SessionMap map; | 77 SessionThreadMap map; |
62 Session session("id"); | 78 Session session("id"); |
63 Session session2("id2"); | 79 Session session2("id2"); |
64 map.Set(session.id, | 80 map[session.id] = make_linked_ptr(new base::Thread("1")); |
65 scoped_refptr<SessionAccessor>(new FakeSessionAccessor(&session))); | 81 map[session2.id] = make_linked_ptr(new base::Thread("2")); |
66 map.Set(session2.id, | |
67 scoped_refptr<SessionAccessor>(new FakeSessionAccessor(&session2))); | |
68 | 82 |
69 int count = 0; | 83 int count = 0; |
70 Command cmd = base::Bind(&ExecuteStubQuit, &count); | 84 Command cmd = base::Bind(&ExecuteStubQuit, &count); |
71 base::DictionaryValue params; | 85 base::DictionaryValue params; |
72 scoped_ptr<base::Value> value; | 86 base::MessageLoop loop; |
73 std::string session_id; | 87 ExecuteQuitAll(cmd, &map, params, std::string(), base::Bind(&OnQuitAll)); |
74 Status status = | |
75 ExecuteQuitAll(cmd, &map, params, std::string(), &value, &session_id); | |
76 ASSERT_EQ(kOk, status.code()); | |
77 ASSERT_FALSE(value.get()); | |
78 ASSERT_EQ(2, count); | 88 ASSERT_EQ(2, count); |
79 } | 89 } |
80 | 90 |
81 TEST(CommandsTest, Quit) { | |
82 SessionMap map; | |
83 Session session("id", scoped_ptr<Chrome>(new StubChrome())); | |
84 scoped_refptr<FakeSessionAccessor> session_accessor( | |
85 new FakeSessionAccessor(&session)); | |
86 map.Set(session.id, session_accessor); | |
87 base::DictionaryValue params; | |
88 scoped_ptr<base::Value> value; | |
89 std::string out_session_id; | |
90 ASSERT_EQ(kOk, | |
91 ExecuteQuit(false, &map, params, session.id, &value, | |
92 &out_session_id).code()); | |
93 ASSERT_FALSE(map.Has(session.id)); | |
94 ASSERT_TRUE(session_accessor->IsSessionDeleted()); | |
95 ASSERT_FALSE(value.get()); | |
96 } | |
97 | |
98 namespace { | 91 namespace { |
99 | 92 |
100 class DetachChrome : public StubChrome { | 93 Status ExecuteSimpleCommand( |
101 public: | 94 const std::string& expected_id, |
102 DetachChrome() : quit_called(false) {} | 95 base::DictionaryValue* expected_params, |
103 virtual ~DetachChrome() {} | 96 base::Value* value, |
| 97 Session* session, |
| 98 const base::DictionaryValue& params, |
| 99 scoped_ptr<base::Value>* return_value) { |
| 100 EXPECT_EQ(expected_id, session->id); |
| 101 EXPECT_TRUE(expected_params->Equals(¶ms)); |
| 102 return_value->reset(value->DeepCopy()); |
| 103 session->quit = true; |
| 104 return Status(kOk); |
| 105 } |
104 | 106 |
105 bool IsQuitCalled() { | 107 void OnSimpleCommand(base::RunLoop* run_loop, |
106 return quit_called; | 108 const std::string& expected_session_id, |
107 } | 109 base::Value* expected_value, |
108 | 110 const Status& status, |
109 // Overridden from Chrome: | 111 scoped_ptr<base::Value> value, |
110 virtual Status Quit() OVERRIDE { | 112 const std::string& session_id) { |
111 quit_called = true; | 113 ASSERT_EQ(kOk, status.code()); |
112 return Status(kOk); | 114 ASSERT_TRUE(expected_value->Equals(value.get())); |
113 } | 115 ASSERT_EQ(expected_session_id, session_id); |
114 | 116 run_loop->Quit(); |
115 private: | 117 } |
116 bool quit_called; | |
117 }; | |
118 | 118 |
119 } // namespace | 119 } // namespace |
120 | 120 |
121 TEST(CommandsTest, QuitWhenDetach) { | 121 TEST(CommandsTest, ExecuteSessionCommand) { |
122 SessionMap map; | 122 SessionThreadMap map; |
123 DetachChrome* chrome = new DetachChrome(); | 123 linked_ptr<base::Thread> thread(new base::Thread("1")); |
124 Session session("id", scoped_ptr<Chrome>(chrome)); | 124 ASSERT_TRUE(thread->Start()); |
125 session.detach = true; | 125 std::string id("id"); |
| 126 thread->message_loop()->PostTask( |
| 127 FROM_HERE, |
| 128 base::Bind(&internal::CreateSessionOnSessionThreadForTesting, id)); |
| 129 map[id] = thread; |
126 | 130 |
127 scoped_refptr<FakeSessionAccessor> session_accessor( | |
128 new FakeSessionAccessor(&session)); | |
129 base::DictionaryValue params; | 131 base::DictionaryValue params; |
130 scoped_ptr<base::Value> value; | 132 params.SetInteger("param", 5); |
131 std::string out_session_id; | 133 base::FundamentalValue expected_value(6); |
| 134 SessionCommand cmd = base::Bind( |
| 135 &ExecuteSimpleCommand, id, ¶ms, &expected_value); |
132 | 136 |
133 map.Set(session.id, session_accessor); | 137 base::MessageLoop loop; |
134 ASSERT_EQ(kOk, | 138 base::RunLoop run_loop; |
135 ExecuteQuit(true, &map, params, session.id, &value, | 139 ExecuteSessionCommand( |
136 &out_session_id).code()); | 140 &map, |
137 ASSERT_FALSE(map.Has(session.id)); | 141 cmd, |
138 ASSERT_FALSE(value.get()); | 142 false, |
139 ASSERT_FALSE(chrome->IsQuitCalled()); | 143 params, |
140 | 144 id, |
141 map.Set(session.id, session_accessor); | 145 base::Bind(&OnSimpleCommand, &run_loop, id, &expected_value)); |
142 ASSERT_EQ(kOk, | 146 run_loop.Run(); |
143 ExecuteQuit(false, &map, params, session.id, &value, | |
144 &out_session_id).code()); | |
145 ASSERT_FALSE(map.Has(session.id)); | |
146 ASSERT_FALSE(value.get()); | |
147 ASSERT_TRUE(chrome->IsQuitCalled()); | |
148 } | 147 } |
149 | 148 |
150 namespace { | 149 namespace { |
151 | 150 |
152 class FailsToQuitChrome : public StubChrome { | 151 Status ShouldNotBeCalled( |
153 public: | 152 Session* session, |
154 FailsToQuitChrome() {} | 153 const base::DictionaryValue& params, |
155 virtual ~FailsToQuitChrome() {} | 154 scoped_ptr<base::Value>* value) { |
| 155 EXPECT_TRUE(false); |
| 156 return Status(kOk); |
| 157 } |
156 | 158 |
157 // Overridden from Chrome: | 159 void OnNoSuchSession(const Status& status, |
158 virtual Status Quit() OVERRIDE { | 160 scoped_ptr<base::Value> value, |
159 return Status(kUnknownError); | 161 const std::string& session_id) { |
160 } | 162 EXPECT_EQ(kNoSuchSession, status.code()); |
161 }; | 163 EXPECT_FALSE(value.get()); |
| 164 } |
| 165 |
| 166 void OnNoSuchSessionIsOk(const Status& status, |
| 167 scoped_ptr<base::Value> value, |
| 168 const std::string& session_id) { |
| 169 EXPECT_EQ(kOk, status.code()); |
| 170 EXPECT_FALSE(value.get()); |
| 171 } |
162 | 172 |
163 } // namespace | 173 } // namespace |
164 | 174 |
165 TEST(CommandsTest, QuitFails) { | 175 TEST(CommandsTest, ExecuteSessionCommandOnNoSuchSession) { |
166 SessionMap map; | 176 SessionThreadMap map; |
167 Session session("id", scoped_ptr<Chrome>(new FailsToQuitChrome())); | |
168 scoped_refptr<FakeSessionAccessor> session_accessor( | |
169 new FakeSessionAccessor(&session)); | |
170 map.Set(session.id, session_accessor); | |
171 base::DictionaryValue params; | 177 base::DictionaryValue params; |
172 scoped_ptr<base::Value> value; | 178 ExecuteSessionCommand(&map, |
173 std::string out_session_id; | 179 base::Bind(&ShouldNotBeCalled), |
174 ASSERT_EQ(kUnknownError, | 180 false, |
175 ExecuteQuit(false, &map, params, session.id, &value, | 181 params, |
176 &out_session_id).code()); | 182 "session", |
177 ASSERT_FALSE(map.Has(session.id)); | 183 base::Bind(&OnNoSuchSession)); |
178 ASSERT_TRUE(session_accessor->IsSessionDeleted()); | 184 } |
179 ASSERT_FALSE(value.get()); | 185 |
| 186 TEST(CommandsTest, ExecuteSessionCommandOnNoSuchSessionWhenItExpectsOk) { |
| 187 SessionThreadMap map; |
| 188 base::DictionaryValue params; |
| 189 ExecuteSessionCommand(&map, |
| 190 base::Bind(&ShouldNotBeCalled), |
| 191 true, |
| 192 params, |
| 193 "session", |
| 194 base::Bind(&OnNoSuchSessionIsOk)); |
180 } | 195 } |
181 | 196 |
182 namespace { | 197 namespace { |
| 198 |
| 199 void OnNoSuchSessionAndQuit(base::RunLoop* run_loop, |
| 200 const Status& status, |
| 201 scoped_ptr<base::Value> value, |
| 202 const std::string& session_id) { |
| 203 run_loop->Quit(); |
| 204 EXPECT_EQ(kNoSuchSession, status.code()); |
| 205 EXPECT_FALSE(value.get()); |
| 206 } |
| 207 |
| 208 } // namespace |
| 209 |
| 210 TEST(CommandsTest, ExecuteSessionCommandOnJustDeletedSession) { |
| 211 SessionThreadMap map; |
| 212 linked_ptr<base::Thread> thread(new base::Thread("1")); |
| 213 ASSERT_TRUE(thread->Start()); |
| 214 std::string id("id"); |
| 215 map[id] = thread; |
| 216 |
| 217 base::MessageLoop loop; |
| 218 base::RunLoop run_loop; |
| 219 ExecuteSessionCommand(&map, |
| 220 base::Bind(&ShouldNotBeCalled), |
| 221 false, |
| 222 base::DictionaryValue(), |
| 223 "session", |
| 224 base::Bind(&OnNoSuchSessionAndQuit, &run_loop)); |
| 225 run_loop.Run(); |
| 226 } |
| 227 |
| 228 namespace { |
183 | 229 |
184 enum TestScenario { | 230 enum TestScenario { |
185 kElementExistsQueryOnce = 0, | 231 kElementExistsQueryOnce = 0, |
186 kElementExistsQueryTwice, | 232 kElementExistsQueryTwice, |
187 kElementNotExistsQueryOnce, | 233 kElementNotExistsQueryOnce, |
188 kElementExistsTimeout | 234 kElementExistsTimeout |
189 }; | 235 }; |
190 | 236 |
191 class FindElementWebView : public StubWebView { | 237 class FindElementWebView : public StubWebView { |
192 public: | 238 public: |
(...skipping 296 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
489 scoped_ptr<base::Value> result; | 535 scoped_ptr<base::Value> result; |
490 ASSERT_EQ( | 536 ASSERT_EQ( |
491 kStaleElementReference, | 537 kStaleElementReference, |
492 ExecuteFindChildElement( | 538 ExecuteFindChildElement( |
493 1, &session, &web_view, element_id, params, &result).code()); | 539 1, &session, &web_view, element_id, params, &result).code()); |
494 ASSERT_EQ( | 540 ASSERT_EQ( |
495 kStaleElementReference, | 541 kStaleElementReference, |
496 ExecuteFindChildElements( | 542 ExecuteFindChildElements( |
497 1, &session, &web_view, element_id, params, &result).code()); | 543 1, &session, &web_view, element_id, params, &result).code()); |
498 } | 544 } |
OLD | NEW |