OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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/file_util.h" | 9 #include "base/file_util.h" |
10 #include "base/files/file_path.h" | 10 #include "base/files/file_path.h" |
| 11 #include "base/memory/linked_ptr.h" |
11 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
| 13 #include "base/message_loop/message_loop.h" |
| 14 #include "base/run_loop.h" |
| 15 #include "base/threading/thread.h" |
12 #include "base/values.h" | 16 #include "base/values.h" |
13 #include "chrome/test/chromedriver/chrome/status.h" | 17 #include "chrome/test/chromedriver/chrome/status.h" |
14 #include "chrome/test/chromedriver/chrome/stub_chrome.h" | 18 #include "chrome/test/chromedriver/chrome/stub_chrome.h" |
15 #include "chrome/test/chromedriver/fake_session_accessor.h" | 19 #include "chrome/test/chromedriver/commands.h" |
16 #include "chrome/test/chromedriver/session.h" | 20 #include "chrome/test/chromedriver/session.h" |
17 #include "chrome/test/chromedriver/session_commands.h" | 21 #include "chrome/test/chromedriver/session_commands.h" |
18 #include "chrome/test/chromedriver/session_map.h" | |
19 #include "testing/gtest/include/gtest/gtest.h" | 22 #include "testing/gtest/include/gtest/gtest.h" |
20 | 23 |
21 namespace { | |
22 | |
23 Status ExecuteSimpleCommand( | |
24 Session* expected_session, | |
25 base::DictionaryValue* expected_params, | |
26 base::Value* value, | |
27 Session* session, | |
28 const base::DictionaryValue& params, | |
29 scoped_ptr<base::Value>* return_value) { | |
30 EXPECT_EQ(expected_session, session); | |
31 EXPECT_TRUE(expected_params->Equals(¶ms)); | |
32 return_value->reset(value->DeepCopy()); | |
33 return Status(kOk); | |
34 } | |
35 | |
36 } // namespace | |
37 | |
38 TEST(SessionCommandTest, SimpleCommand) { | |
39 SessionMap map; | |
40 Session session("session", scoped_ptr<Chrome>(new StubChrome())); | |
41 ASSERT_TRUE(session.thread.Start()); | |
42 scoped_refptr<SessionAccessor> accessor(new FakeSessionAccessor(&session)); | |
43 map.Set(session.id, accessor); | |
44 | |
45 base::DictionaryValue params; | |
46 params.SetInteger("param", 5); | |
47 base::FundamentalValue expected_value(6); | |
48 SessionCommand cmd = base::Bind( | |
49 &ExecuteSimpleCommand, &session, ¶ms, &expected_value); | |
50 | |
51 scoped_ptr<base::Value> value; | |
52 std::string session_id; | |
53 Status status = ExecuteSessionCommand( | |
54 &map, cmd, params, | |
55 session.id, &value, &session_id); | |
56 ASSERT_EQ(kOk, status.code()); | |
57 ASSERT_TRUE(expected_value.Equals(value.get())); | |
58 ASSERT_STREQ(session.id.c_str(), session_id.c_str()); | |
59 } | |
60 | |
61 namespace { | |
62 | |
63 Status ShouldNotBeCalled( | |
64 Session* session, | |
65 const base::DictionaryValue& params, | |
66 scoped_ptr<base::Value>* value) { | |
67 EXPECT_TRUE(false); | |
68 return Status(kOk); | |
69 } | |
70 | |
71 } // namespace | |
72 | |
73 TEST(SessionCommandTest, NoSuchSession) { | |
74 SessionMap map; | |
75 base::DictionaryValue params; | |
76 scoped_ptr<base::Value> value; | |
77 std::string session_id; | |
78 Status status = ExecuteSessionCommand( | |
79 &map, base::Bind(&ShouldNotBeCalled), params, | |
80 "session", &value, &session_id); | |
81 ASSERT_EQ(kNoSuchSession, status.code()); | |
82 ASSERT_FALSE(value.get()); | |
83 ASSERT_STREQ("session", session_id.c_str()); | |
84 } | |
85 | |
86 TEST(SessionCommandTest, SessionDeletedWhileWaiting) { | |
87 SessionMap map; | |
88 scoped_refptr<SessionAccessor> accessor(new FakeSessionAccessor(NULL)); | |
89 map.Set("session", accessor); | |
90 | |
91 base::DictionaryValue params; | |
92 scoped_ptr<base::Value> value; | |
93 std::string session_id; | |
94 Status status = ExecuteSessionCommand( | |
95 &map, base::Bind(&ShouldNotBeCalled), params, | |
96 "session", &value, &session_id); | |
97 ASSERT_EQ(kNoSuchSession, status.code()); | |
98 ASSERT_FALSE(value.get()); | |
99 ASSERT_STREQ("session", session_id.c_str()); | |
100 } | |
101 | |
102 TEST(SessionCommandTest, FileUpload) { | 24 TEST(SessionCommandTest, FileUpload) { |
103 Session session("id"); | 25 Session session("id"); |
104 base::DictionaryValue params; | 26 base::DictionaryValue params; |
105 scoped_ptr<base::Value> value; | 27 scoped_ptr<base::Value> value; |
106 // Zip file entry that contains a single file with contents 'COW\n', base64 | 28 // Zip file entry that contains a single file with contents 'COW\n', base64 |
107 // encoded following RFC 1521. | 29 // encoded following RFC 1521. |
108 const char* kBase64ZipEntry = | 30 const char* kBase64ZipEntry = |
109 "UEsDBBQAAAAAAMROi0K/wAzGBAAAAAQAAAADAAAAbW9vQ09XClBLAQIUAxQAAAAAAMROi0K/" | 31 "UEsDBBQAAAAAAMROi0K/wAzGBAAAAAQAAAADAAAAbW9vQ09XClBLAQIUAxQAAAAAAMROi0K/" |
110 "wAzG\nBAAAAAQAAAADAAAAAAAAAAAAAACggQAAAABtb29QSwUGAAAAAAEAAQAxAAAAJQAAAA" | 32 "wAzG\nBAAAAAQAAAADAAAAAAAAAAAAAACggQAAAABtb29QSwUGAAAAAAEAAQAxAAAAJQAAAA" |
111 "AA\n"; | 33 "AA\n"; |
112 params.SetString("file", kBase64ZipEntry); | 34 params.SetString("file", kBase64ZipEntry); |
113 Status status = ExecuteUploadFile(&session, params, &value); | 35 Status status = ExecuteUploadFile(&session, params, &value); |
114 ASSERT_EQ(kOk, status.code()) << status.message(); | 36 ASSERT_EQ(kOk, status.code()) << status.message(); |
115 base::FilePath::StringType path; | 37 base::FilePath::StringType path; |
116 ASSERT_TRUE(value->GetAsString(&path)); | 38 ASSERT_TRUE(value->GetAsString(&path)); |
117 ASSERT_TRUE(base::PathExists(base::FilePath(path))); | 39 ASSERT_TRUE(base::PathExists(base::FilePath(path))); |
118 std::string data; | 40 std::string data; |
119 ASSERT_TRUE(file_util::ReadFileToString(base::FilePath(path), &data)); | 41 ASSERT_TRUE(file_util::ReadFileToString(base::FilePath(path), &data)); |
120 ASSERT_STREQ("COW\n", data.c_str()); | 42 ASSERT_STREQ("COW\n", data.c_str()); |
121 } | 43 } |
| 44 |
| 45 namespace { |
| 46 |
| 47 class DetachChrome : public StubChrome { |
| 48 public: |
| 49 DetachChrome() : quit_called_(false) {} |
| 50 virtual ~DetachChrome() {} |
| 51 |
| 52 // Overridden from Chrome: |
| 53 virtual Status Quit() OVERRIDE { |
| 54 quit_called_ = true; |
| 55 return Status(kOk); |
| 56 } |
| 57 |
| 58 bool quit_called_; |
| 59 }; |
| 60 |
| 61 } // namespace |
| 62 |
| 63 TEST(SessionCommandsTest, Quit) { |
| 64 DetachChrome* chrome = new DetachChrome(); |
| 65 Session session("id", scoped_ptr<Chrome>(chrome)); |
| 66 |
| 67 base::DictionaryValue params; |
| 68 scoped_ptr<base::Value> value; |
| 69 |
| 70 ASSERT_EQ(kOk, ExecuteQuit(false, &session, params, &value).code()); |
| 71 ASSERT_TRUE(chrome->quit_called_); |
| 72 |
| 73 chrome->quit_called_ = false; |
| 74 ASSERT_EQ(kOk, ExecuteQuit(true, &session, params, &value).code()); |
| 75 ASSERT_TRUE(chrome->quit_called_); |
| 76 } |
| 77 |
| 78 TEST(SessionCommandsTest, QuitWithDetach) { |
| 79 DetachChrome* chrome = new DetachChrome(); |
| 80 Session session("id", scoped_ptr<Chrome>(chrome)); |
| 81 session.detach = true; |
| 82 |
| 83 base::DictionaryValue params; |
| 84 scoped_ptr<base::Value> value; |
| 85 |
| 86 ASSERT_EQ(kOk, ExecuteQuit(true, &session, params, &value).code()); |
| 87 ASSERT_FALSE(chrome->quit_called_); |
| 88 |
| 89 ASSERT_EQ(kOk, ExecuteQuit(false, &session, params, &value).code()); |
| 90 ASSERT_TRUE(chrome->quit_called_); |
| 91 } |
| 92 |
| 93 namespace { |
| 94 |
| 95 class FailsToQuitChrome : public StubChrome { |
| 96 public: |
| 97 FailsToQuitChrome() {} |
| 98 virtual ~FailsToQuitChrome() {} |
| 99 |
| 100 // Overridden from Chrome: |
| 101 virtual Status Quit() OVERRIDE { |
| 102 return Status(kUnknownError); |
| 103 } |
| 104 }; |
| 105 |
| 106 } // namespace |
| 107 |
| 108 TEST(SessionCommandsTest, QuitFails) { |
| 109 Session session("id", scoped_ptr<Chrome>(new FailsToQuitChrome())); |
| 110 base::DictionaryValue params; |
| 111 scoped_ptr<base::Value> value; |
| 112 ASSERT_EQ(kUnknownError, ExecuteQuit(false, &session, params, &value).code()); |
| 113 } |
OLD | NEW |