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
3 // found in the LICENSE file.
4
5 #include <string>
6
7 #include "base/bind.h"
8 #include "base/callback.h"
9 #include "base/file_util.h"
10 #include "base/files/file_path.h"
11 #include "base/memory/linked_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"
16 #include "base/values.h"
17 #include "chrome/test/chromedriver/chrome/status.h"
18 #include "chrome/test/chromedriver/chrome/stub_chrome.h"
19 #include "chrome/test/chromedriver/commands.h"
20 #include "chrome/test/chromedriver/session.h"
21 #include "chrome/test/chromedriver/session_commands.h"
22 #include "testing/gtest/include/gtest/gtest.h"
23
TEST(SessionCommandTest,FileUpload)24 TEST(SessionCommandTest, FileUpload) {
25 Session session("id");
26 base::DictionaryValue params;
27 scoped_ptr<base::Value> value;
28 // Zip file entry that contains a single file with contents 'COW\n', base64
29 // encoded following RFC 1521.
30 const char* kBase64ZipEntry =
31 "UEsDBBQAAAAAAMROi0K/wAzGBAAAAAQAAAADAAAAbW9vQ09XClBLAQIUAxQAAAAAAMROi0K/"
32 "wAzG\nBAAAAAQAAAADAAAAAAAAAAAAAACggQAAAABtb29QSwUGAAAAAAEAAQAxAAAAJQAAAA"
33 "AA\n";
34 params.SetString("file", kBase64ZipEntry);
35 Status status = ExecuteUploadFile(&session, params, &value);
36 ASSERT_EQ(kOk, status.code()) << status.message();
37 base::FilePath::StringType path;
38 ASSERT_TRUE(value->GetAsString(&path));
39 ASSERT_TRUE(base::PathExists(base::FilePath(path)));
40 std::string data;
41 ASSERT_TRUE(base::ReadFileToString(base::FilePath(path), &data));
42 ASSERT_STREQ("COW\n", data.c_str());
43 }
44
45 namespace {
46
47 class DetachChrome : public StubChrome {
48 public:
DetachChrome()49 DetachChrome() : quit_called_(false) {}
~DetachChrome()50 virtual ~DetachChrome() {}
51
52 // Overridden from Chrome:
Quit()53 virtual Status Quit() OVERRIDE {
54 quit_called_ = true;
55 return Status(kOk);
56 }
57
58 bool quit_called_;
59 };
60
61 } // namespace
62
TEST(SessionCommandsTest,Quit)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
TEST(SessionCommandsTest,QuitWithDetach)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:
FailsToQuitChrome()97 FailsToQuitChrome() {}
~FailsToQuitChrome()98 virtual ~FailsToQuitChrome() {}
99
100 // Overridden from Chrome:
Quit()101 virtual Status Quit() OVERRIDE {
102 return Status(kUnknownError);
103 }
104 };
105
106 } // namespace
107
TEST(SessionCommandsTest,QuitFails)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 }
114
TEST(SessionCommandsTest,AutoReporting)115 TEST(SessionCommandsTest, AutoReporting) {
116 DetachChrome* chrome = new DetachChrome();
117 Session session("id", scoped_ptr<Chrome>(chrome));
118 base::DictionaryValue params;
119 scoped_ptr<base::Value> value;
120 StatusCode status_code;
121 bool enabled;
122
123 // autoreporting should be disabled by default
124 status_code = ExecuteIsAutoReporting(&session, params, &value).code();
125 ASSERT_EQ(kOk, status_code);
126 ASSERT_FALSE(session.auto_reporting_enabled);
127 ASSERT_TRUE(value.get()->GetAsBoolean(&enabled));
128 ASSERT_FALSE(enabled);
129
130 // an error should be given if the |enabled| parameter is not set
131 status_code = ExecuteSetAutoReporting(&session, params, &value).code();
132 ASSERT_EQ(kUnknownError, status_code);
133
134 // try to enable autoreporting
135 params.SetBoolean("enabled", true);
136 status_code = ExecuteSetAutoReporting(&session, params, &value).code();
137 ASSERT_EQ(kOk, status_code);
138 ASSERT_TRUE(session.auto_reporting_enabled);
139
140 // check that autoreporting was enabled successfully
141 status_code = ExecuteIsAutoReporting(&session, params, &value).code();
142 ASSERT_EQ(kOk, status_code);
143 ASSERT_TRUE(value.get()->GetAsBoolean(&enabled));
144 ASSERT_TRUE(enabled);
145
146 // try to disable autoreporting
147 params.SetBoolean("enabled", false);
148 status_code = ExecuteSetAutoReporting(&session, params, &value).code();
149 ASSERT_EQ(kOk, status_code);
150 ASSERT_FALSE(session.auto_reporting_enabled);
151
152 // check that autoreporting was disabled successfully
153 status_code = ExecuteIsAutoReporting(&session, params, &value).code();
154 ASSERT_EQ(kOk, status_code);
155 ASSERT_TRUE(value.get()->GetAsBoolean(&enabled));
156 ASSERT_FALSE(enabled);
157 }
158