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