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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
|
// Copyright (c) 2012 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 "base/bind.h"
#include "base/memory/ref_counted_memory.h"
#include "base/utf_string_conversions.h"
#include "content/browser/webui/web_ui_data_source_impl.h"
#include "content/test/test_content_client.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace content {
namespace {
const int kDummyStringId = 123;
const int kDummyDefaultResourceId = 456;
const int kDummyResourceId = 789;
const char kDummyString[] = "foo";
const char kDummyDefaultResource[] = "<html>foo</html>";
const char kDummytResource[] = "<html>blah</html>";
class TestClient : public TestContentClient {
public:
TestClient() {}
virtual ~TestClient() {}
virtual string16 GetLocalizedString(int message_id) const OVERRIDE {
if (message_id == kDummyStringId)
return UTF8ToUTF16(kDummyString);
return string16();
}
virtual base::RefCountedStaticMemory* GetDataResourceBytes(
int resource_id) const OVERRIDE {
base::RefCountedStaticMemory* bytes = NULL;
if (resource_id == kDummyDefaultResourceId) {
bytes = new base::RefCountedStaticMemory(
reinterpret_cast<const unsigned char*>(kDummyDefaultResource),
arraysize(kDummyDefaultResource));
} else if (resource_id == kDummyResourceId) {
bytes = new base::RefCountedStaticMemory(
reinterpret_cast<const unsigned char*>(kDummytResource),
arraysize(kDummytResource));
}
return bytes;
}
};
}
class WebUIDataSourceTest : public testing::Test {
public:
WebUIDataSourceTest() : result_data_(NULL) {}
virtual ~WebUIDataSourceTest() {}
WebUIDataSourceImpl* source() { return source_.get(); }
void StartDataRequest(const std::string& path) {
source_->StartDataRequest(
path,
false,
base::Bind(&WebUIDataSourceTest::SendResult,
base::Unretained(this)));
}
std::string GetMimeType(const std::string& path) const {
return source_->GetMimeType(path);
}
scoped_refptr<base::RefCountedMemory> result_data_;
private:
virtual void SetUp() {
SetContentClient(&client_);
WebUIDataSource* source = WebUIDataSourceImpl::Create("host");
WebUIDataSourceImpl* source_impl = static_cast<WebUIDataSourceImpl*>(
source);
source_impl->disable_set_font_strings_for_testing();
source_ = make_scoped_refptr(source_impl);
}
// Store response for later comparisons.
void SendResult(base::RefCountedMemory* data) {
result_data_ = data;
}
scoped_refptr<WebUIDataSourceImpl> source_;
TestClient client_;
};
TEST_F(WebUIDataSourceTest, EmptyStrings) {
source()->SetJsonPath("strings.js");
StartDataRequest("strings.js");
std::string result(reinterpret_cast<const char*>(
result_data_->front()), result_data_->size());
EXPECT_NE(result.find("var templateData = {"), std::string::npos);
EXPECT_NE(result.find("};"), std::string::npos);
}
TEST_F(WebUIDataSourceTest, SomeStrings) {
source()->SetJsonPath("strings.js");
source()->AddString("planet", ASCIIToUTF16("pluto"));
source()->AddLocalizedString("button", kDummyStringId);
StartDataRequest("strings.js");
std::string result(reinterpret_cast<const char*>(
result_data_->front()), result_data_->size());
EXPECT_NE(result.find("\"planet\":\"pluto\""), std::string::npos);
EXPECT_NE(result.find("\"button\":\"foo\""), std::string::npos);
}
TEST_F(WebUIDataSourceTest, DefaultResource) {
source()->SetDefaultResource(kDummyDefaultResourceId);
StartDataRequest("foobar" );
std::string result(
reinterpret_cast<const char*>(result_data_->front()),
result_data_->size());
EXPECT_NE(result.find(kDummyDefaultResource), std::string::npos);
StartDataRequest("strings.js");
result = std::string(
reinterpret_cast<const char*>(result_data_->front()),
result_data_->size());
EXPECT_NE(result.find(kDummyDefaultResource), std::string::npos);
}
TEST_F(WebUIDataSourceTest, NamedResource) {
source()->SetDefaultResource(kDummyDefaultResourceId);
source()->AddResourcePath("foobar", kDummyResourceId);
StartDataRequest("foobar");
std::string result(
reinterpret_cast<const char*>(result_data_->front()),
result_data_->size());
EXPECT_NE(result.find(kDummytResource), std::string::npos);
StartDataRequest("strings.js");
result = std::string(
reinterpret_cast<const char*>(result_data_->front()),
result_data_->size());
EXPECT_NE(result.find(kDummyDefaultResource), std::string::npos);
}
TEST_F(WebUIDataSourceTest, MimeType) {
const char* html = "text/html";
const char* js = "application/javascript";
EXPECT_EQ(GetMimeType(""), html);
EXPECT_EQ(GetMimeType("foo"), html);
EXPECT_EQ(GetMimeType("foo.html"), html);
EXPECT_EQ(GetMimeType(".js"), js);
EXPECT_EQ(GetMimeType("foo.js"), js);
EXPECT_EQ(GetMimeType("js"), html);
EXPECT_EQ(GetMimeType("foojs"), html);
EXPECT_EQ(GetMimeType("foo.jsp"), html);
}
} // namespace content
|