1 // Copyright (c) 2012 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.
4
5 #include "base/bind.h"
6 #include "base/memory/ref_counted_memory.h"
7 #include "base/strings/utf_string_conversions.h"
8 #include "content/browser/webui/web_ui_data_source_impl.h"
9 #include "content/test/test_content_client.h"
10 #include "testing/gtest/include/gtest/gtest.h"
11
12 namespace content {
13 namespace {
14
15 const int kDummyStringId = 123;
16 const int kDummyDefaultResourceId = 456;
17 const int kDummyResourceId = 789;
18
19 const char kDummyString[] = "foo";
20 const char kDummyDefaultResource[] = "<html>foo</html>";
21 const char kDummytResource[] = "<html>blah</html>";
22
23 class TestClient : public TestContentClient {
24 public:
TestClient()25 TestClient() {}
~TestClient()26 virtual ~TestClient() {}
27
GetLocalizedString(int message_id) const28 virtual base::string16 GetLocalizedString(int message_id) const OVERRIDE {
29 if (message_id == kDummyStringId)
30 return UTF8ToUTF16(kDummyString);
31 return base::string16();
32
33 }
34
GetDataResourceBytes(int resource_id) const35 virtual base::RefCountedStaticMemory* GetDataResourceBytes(
36 int resource_id) const OVERRIDE {
37 base::RefCountedStaticMemory* bytes = NULL;
38 if (resource_id == kDummyDefaultResourceId) {
39 bytes = new base::RefCountedStaticMemory(
40 reinterpret_cast<const unsigned char*>(kDummyDefaultResource),
41 arraysize(kDummyDefaultResource));
42 } else if (resource_id == kDummyResourceId) {
43 bytes = new base::RefCountedStaticMemory(
44 reinterpret_cast<const unsigned char*>(kDummytResource),
45 arraysize(kDummytResource));
46 }
47 return bytes;
48 }
49 };
50
51 }
52
53 class WebUIDataSourceTest : public testing::Test {
54 public:
WebUIDataSourceTest()55 WebUIDataSourceTest() : result_data_(NULL) {}
~WebUIDataSourceTest()56 virtual ~WebUIDataSourceTest() {}
source()57 WebUIDataSourceImpl* source() { return source_.get(); }
58
StartDataRequest(const std::string & path)59 void StartDataRequest(const std::string& path) {
60 source_->StartDataRequest(
61 path,
62 0, 0,
63 base::Bind(&WebUIDataSourceTest::SendResult,
64 base::Unretained(this)));
65 }
66
GetMimeType(const std::string & path) const67 std::string GetMimeType(const std::string& path) const {
68 return source_->GetMimeType(path);
69 }
70
71 scoped_refptr<base::RefCountedMemory> result_data_;
72
73 private:
SetUp()74 virtual void SetUp() {
75 SetContentClient(&client_);
76 WebUIDataSource* source = WebUIDataSourceImpl::Create("host");
77 WebUIDataSourceImpl* source_impl = static_cast<WebUIDataSourceImpl*>(
78 source);
79 source_impl->disable_set_font_strings_for_testing();
80 source_ = make_scoped_refptr(source_impl);
81 }
82
83 // Store response for later comparisons.
SendResult(base::RefCountedMemory * data)84 void SendResult(base::RefCountedMemory* data) {
85 result_data_ = data;
86 }
87
88 scoped_refptr<WebUIDataSourceImpl> source_;
89 TestClient client_;
90 };
91
TEST_F(WebUIDataSourceTest,EmptyStrings)92 TEST_F(WebUIDataSourceTest, EmptyStrings) {
93 source()->SetJsonPath("strings.js");
94 StartDataRequest("strings.js");
95 std::string result(reinterpret_cast<const char*>(
96 result_data_->front()), result_data_->size());
97 EXPECT_NE(result.find("var templateData = {"), std::string::npos);
98 EXPECT_NE(result.find("};"), std::string::npos);
99 }
100
TEST_F(WebUIDataSourceTest,SomeStrings)101 TEST_F(WebUIDataSourceTest, SomeStrings) {
102 source()->SetJsonPath("strings.js");
103 source()->AddString("planet", ASCIIToUTF16("pluto"));
104 source()->AddLocalizedString("button", kDummyStringId);
105 StartDataRequest("strings.js");
106 std::string result(reinterpret_cast<const char*>(
107 result_data_->front()), result_data_->size());
108 EXPECT_NE(result.find("\"planet\":\"pluto\""), std::string::npos);
109 EXPECT_NE(result.find("\"button\":\"foo\""), std::string::npos);
110 }
111
TEST_F(WebUIDataSourceTest,DefaultResource)112 TEST_F(WebUIDataSourceTest, DefaultResource) {
113 source()->SetDefaultResource(kDummyDefaultResourceId);
114 StartDataRequest("foobar" );
115 std::string result(
116 reinterpret_cast<const char*>(result_data_->front()),
117 result_data_->size());
118 EXPECT_NE(result.find(kDummyDefaultResource), std::string::npos);
119 StartDataRequest("strings.js");
120 result = std::string(
121 reinterpret_cast<const char*>(result_data_->front()),
122 result_data_->size());
123 EXPECT_NE(result.find(kDummyDefaultResource), std::string::npos);
124 }
125
TEST_F(WebUIDataSourceTest,NamedResource)126 TEST_F(WebUIDataSourceTest, NamedResource) {
127 source()->SetDefaultResource(kDummyDefaultResourceId);
128 source()->AddResourcePath("foobar", kDummyResourceId);
129 StartDataRequest("foobar");
130 std::string result(
131 reinterpret_cast<const char*>(result_data_->front()),
132 result_data_->size());
133 EXPECT_NE(result.find(kDummytResource), std::string::npos);
134 StartDataRequest("strings.js");
135 result = std::string(
136 reinterpret_cast<const char*>(result_data_->front()),
137 result_data_->size());
138 EXPECT_NE(result.find(kDummyDefaultResource), std::string::npos);
139 }
140
TEST_F(WebUIDataSourceTest,MimeType)141 TEST_F(WebUIDataSourceTest, MimeType) {
142 const char* html = "text/html";
143 const char* js = "application/javascript";
144 EXPECT_EQ(GetMimeType(std::string()), html);
145 EXPECT_EQ(GetMimeType("foo"), html);
146 EXPECT_EQ(GetMimeType("foo.html"), html);
147 EXPECT_EQ(GetMimeType(".js"), js);
148 EXPECT_EQ(GetMimeType("foo.js"), js);
149 EXPECT_EQ(GetMimeType("js"), html);
150 EXPECT_EQ(GetMimeType("foojs"), html);
151 EXPECT_EQ(GetMimeType("foo.jsp"), html);
152 }
153
154 } // namespace content
155