Roll src/third_party/WebKit bf18a82:a9cee16 (svn 185297:185304)
[chromium-blink-merge.git] / chrome / test / chromedriver / session_commands_unittest.cc
blobcc62b79bcd2abe0cc296c87e4c1176856b985098
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.
5 #include <string>
7 #include "base/bind.h"
8 #include "base/callback.h"
9 #include "base/files/file_path.h"
10 #include "base/files/file_util.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"
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());
45 namespace {
47 class DetachChrome : public StubChrome {
48 public:
49 DetachChrome() : quit_called_(false) {}
50 ~DetachChrome() override {}
52 // Overridden from Chrome:
53 Status Quit() override {
54 quit_called_ = true;
55 return Status(kOk);
58 bool quit_called_;
61 } // namespace
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_);
93 namespace {
95 class FailsToQuitChrome : public StubChrome {
96 public:
97 FailsToQuitChrome() {}
98 ~FailsToQuitChrome() override {}
100 // Overridden from Chrome:
101 Status Quit() override { return Status(kUnknownError); }
104 } // namespace
106 TEST(SessionCommandsTest, QuitFails) {
107 Session session("id", scoped_ptr<Chrome>(new FailsToQuitChrome()));
108 base::DictionaryValue params;
109 scoped_ptr<base::Value> value;
110 ASSERT_EQ(kUnknownError, ExecuteQuit(false, &session, params, &value).code());
113 TEST(SessionCommandsTest, AutoReporting) {
114 DetachChrome* chrome = new DetachChrome();
115 Session session("id", scoped_ptr<Chrome>(chrome));
116 base::DictionaryValue params;
117 scoped_ptr<base::Value> value;
118 StatusCode status_code;
119 bool enabled;
121 // autoreporting should be disabled by default
122 status_code = ExecuteIsAutoReporting(&session, params, &value).code();
123 ASSERT_EQ(kOk, status_code);
124 ASSERT_FALSE(session.auto_reporting_enabled);
125 ASSERT_TRUE(value.get()->GetAsBoolean(&enabled));
126 ASSERT_FALSE(enabled);
128 // an error should be given if the |enabled| parameter is not set
129 status_code = ExecuteSetAutoReporting(&session, params, &value).code();
130 ASSERT_EQ(kUnknownError, status_code);
132 // try to enable autoreporting
133 params.SetBoolean("enabled", true);
134 status_code = ExecuteSetAutoReporting(&session, params, &value).code();
135 ASSERT_EQ(kOk, status_code);
136 ASSERT_TRUE(session.auto_reporting_enabled);
138 // check that autoreporting was enabled successfully
139 status_code = ExecuteIsAutoReporting(&session, params, &value).code();
140 ASSERT_EQ(kOk, status_code);
141 ASSERT_TRUE(value.get()->GetAsBoolean(&enabled));
142 ASSERT_TRUE(enabled);
144 // try to disable autoreporting
145 params.SetBoolean("enabled", false);
146 status_code = ExecuteSetAutoReporting(&session, params, &value).code();
147 ASSERT_EQ(kOk, status_code);
148 ASSERT_FALSE(session.auto_reporting_enabled);
150 // check that autoreporting was disabled successfully
151 status_code = ExecuteIsAutoReporting(&session, params, &value).code();
152 ASSERT_EQ(kOk, status_code);
153 ASSERT_TRUE(value.get()->GetAsBoolean(&enabled));
154 ASSERT_FALSE(enabled);