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.
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"
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"
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
)));
41 ASSERT_TRUE(base::ReadFileToString(base::FilePath(path
), &data
));
42 ASSERT_STREQ("COW\n", data
.c_str());
47 class DetachChrome
: public StubChrome
{
49 DetachChrome() : quit_called_(false) {}
50 virtual ~DetachChrome() {}
52 // Overridden from Chrome:
53 virtual Status
Quit() OVERRIDE
{
63 TEST(SessionCommandsTest
, Quit
) {
64 DetachChrome
* chrome
= new DetachChrome();
65 Session
session("id", scoped_ptr
<Chrome
>(chrome
));
67 base::DictionaryValue params
;
68 scoped_ptr
<base::Value
> value
;
70 ASSERT_EQ(kOk
, ExecuteQuit(false, &session
, params
, &value
).code());
71 ASSERT_TRUE(chrome
->quit_called_
);
73 chrome
->quit_called_
= false;
74 ASSERT_EQ(kOk
, ExecuteQuit(true, &session
, params
, &value
).code());
75 ASSERT_TRUE(chrome
->quit_called_
);
78 TEST(SessionCommandsTest
, QuitWithDetach
) {
79 DetachChrome
* chrome
= new DetachChrome();
80 Session
session("id", scoped_ptr
<Chrome
>(chrome
));
81 session
.detach
= true;
83 base::DictionaryValue params
;
84 scoped_ptr
<base::Value
> value
;
86 ASSERT_EQ(kOk
, ExecuteQuit(true, &session
, params
, &value
).code());
87 ASSERT_FALSE(chrome
->quit_called_
);
89 ASSERT_EQ(kOk
, ExecuteQuit(false, &session
, params
, &value
).code());
90 ASSERT_TRUE(chrome
->quit_called_
);
95 class FailsToQuitChrome
: public StubChrome
{
97 FailsToQuitChrome() {}
98 virtual ~FailsToQuitChrome() {}
100 // Overridden from Chrome:
101 virtual Status
Quit() OVERRIDE
{
102 return Status(kUnknownError
);
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());
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
;
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
);
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
);
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
);
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
);
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
);
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
);