IndexedDBFactory now ForceCloses databases.
[chromium-blink-merge.git] / content / browser / webui / web_ui_data_source_unittest.cc
blob4dc9c81767171b028a7f0bf6672a87b78fe6c3d9
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.
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"
12 namespace content {
13 namespace {
15 const int kDummyStringId = 123;
16 const int kDummyDefaultResourceId = 456;
17 const int kDummyResourceId = 789;
19 const char kDummyString[] = "foo";
20 const char kDummyDefaultResource[] = "<html>foo</html>";
21 const char kDummytResource[] = "<html>blah</html>";
23 class TestClient : public TestContentClient {
24 public:
25 TestClient() {}
26 virtual ~TestClient() {}
28 virtual base::string16 GetLocalizedString(int message_id) const OVERRIDE {
29 if (message_id == kDummyStringId)
30 return base::UTF8ToUTF16(kDummyString);
31 return base::string16();
35 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));
47 return bytes;
53 class WebUIDataSourceTest : public testing::Test {
54 public:
55 WebUIDataSourceTest() : result_data_(NULL) {}
56 virtual ~WebUIDataSourceTest() {}
57 WebUIDataSourceImpl* source() { return source_.get(); }
59 void StartDataRequest(const std::string& path) {
60 source_->StartDataRequest(
61 path,
62 0, 0,
63 base::Bind(&WebUIDataSourceTest::SendResult,
64 base::Unretained(this)));
67 std::string GetMimeType(const std::string& path) const {
68 return source_->GetMimeType(path);
71 scoped_refptr<base::RefCountedMemory> result_data_;
73 private:
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);
83 // Store response for later comparisons.
84 void SendResult(base::RefCountedMemory* data) {
85 result_data_ = data;
88 scoped_refptr<WebUIDataSourceImpl> source_;
89 TestClient client_;
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);
101 TEST_F(WebUIDataSourceTest, SomeStrings) {
102 source()->SetJsonPath("strings.js");
103 source()->AddString("planet", base::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);
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);
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);
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);
154 } // namespace content