Enable PPAPI tests for non-SFI mode.
[chromium-blink-merge.git] / chrome / test / chromedriver / session_commands_unittest.cc
blobe33439b9f18bbcd65edbc60625be878d424da00d
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/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"
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 virtual ~DetachChrome() {}
52 // Overridden from Chrome:
53 virtual 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 virtual ~FailsToQuitChrome() {}
100 // Overridden from Chrome:
101 virtual Status Quit() OVERRIDE {
102 return Status(kUnknownError);
106 } // namespace
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;
121 bool enabled;
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);