Apply _RELATIVE relocations ahead of others.
[chromium-blink-merge.git] / content / browser / webui / web_ui_data_source_unittest.cc
blobc5b469860a281de24339918ddba33ffbbd83b317
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/public/test/test_browser_thread_bundle.h"
10 #include "content/test/test_content_client.h"
11 #include "testing/gtest/include/gtest/gtest.h"
13 namespace content {
14 namespace {
16 const int kDummyStringId = 123;
17 const int kDummyDefaultResourceId = 456;
18 const int kDummyResourceId = 789;
20 const char kDummyString[] = "foo";
21 const char kDummyDefaultResource[] = "<html>foo</html>";
22 const char kDummytResource[] = "<html>blah</html>";
24 class TestClient : public TestContentClient {
25 public:
26 TestClient() {}
27 ~TestClient() override {}
29 base::string16 GetLocalizedString(int message_id) const override {
30 if (message_id == kDummyStringId)
31 return base::UTF8ToUTF16(kDummyString);
32 return base::string16();
36 base::RefCountedStaticMemory* GetDataResourceBytes(
37 int resource_id) const override {
38 base::RefCountedStaticMemory* bytes = NULL;
39 if (resource_id == kDummyDefaultResourceId) {
40 bytes = new base::RefCountedStaticMemory(
41 kDummyDefaultResource, arraysize(kDummyDefaultResource));
42 } else if (resource_id == kDummyResourceId) {
43 bytes = new base::RefCountedStaticMemory(
44 kDummytResource, arraysize(kDummytResource));
46 return bytes;
52 class WebUIDataSourceTest : public testing::Test {
53 public:
54 WebUIDataSourceTest() : result_data_(NULL) {}
55 ~WebUIDataSourceTest() override {}
56 WebUIDataSourceImpl* source() { return source_.get(); }
58 void StartDataRequest(const std::string& path) {
59 source_->StartDataRequest(
60 path,
61 0, 0,
62 base::Bind(&WebUIDataSourceTest::SendResult,
63 base::Unretained(this)));
66 std::string GetMimeType(const std::string& path) const {
67 return source_->GetMimeType(path);
70 scoped_refptr<base::RefCountedMemory> result_data_;
72 private:
73 void SetUp() override {
74 SetContentClient(&client_);
75 WebUIDataSource* source = WebUIDataSourceImpl::Create("host");
76 WebUIDataSourceImpl* source_impl = static_cast<WebUIDataSourceImpl*>(
77 source);
78 source_impl->disable_set_font_strings_for_testing();
79 source_ = make_scoped_refptr(source_impl);
82 // Store response for later comparisons.
83 void SendResult(base::RefCountedMemory* data) {
84 result_data_ = data;
87 TestBrowserThreadBundle thread_bundle_;
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(result_data_->front_as<char>(), result_data_->size());
96 EXPECT_NE(result.find("loadTimeData.data = {"), std::string::npos);
97 EXPECT_NE(result.find("};"), std::string::npos);
100 TEST_F(WebUIDataSourceTest, SomeStrings) {
101 source()->SetJsonPath("strings.js");
102 source()->AddString("planet", base::ASCIIToUTF16("pluto"));
103 source()->AddLocalizedString("button", kDummyStringId);
104 StartDataRequest("strings.js");
105 std::string result(result_data_->front_as<char>(), result_data_->size());
106 EXPECT_NE(result.find("\"planet\":\"pluto\""), std::string::npos);
107 EXPECT_NE(result.find("\"button\":\"foo\""), std::string::npos);
110 TEST_F(WebUIDataSourceTest, DefaultResource) {
111 source()->SetDefaultResource(kDummyDefaultResourceId);
112 StartDataRequest("foobar" );
113 std::string result(result_data_->front_as<char>(), result_data_->size());
114 EXPECT_NE(result.find(kDummyDefaultResource), std::string::npos);
115 StartDataRequest("strings.js");
116 result = std::string(result_data_->front_as<char>(), result_data_->size());
117 EXPECT_NE(result.find(kDummyDefaultResource), std::string::npos);
120 TEST_F(WebUIDataSourceTest, NamedResource) {
121 source()->SetDefaultResource(kDummyDefaultResourceId);
122 source()->AddResourcePath("foobar", kDummyResourceId);
123 StartDataRequest("foobar");
124 std::string result(result_data_->front_as<char>(), result_data_->size());
125 EXPECT_NE(result.find(kDummytResource), std::string::npos);
126 StartDataRequest("strings.js");
127 result = std::string(result_data_->front_as<char>(), result_data_->size());
128 EXPECT_NE(result.find(kDummyDefaultResource), std::string::npos);
131 TEST_F(WebUIDataSourceTest, MimeType) {
132 const char* html = "text/html";
133 const char* js = "application/javascript";
134 EXPECT_EQ(GetMimeType(std::string()), html);
135 EXPECT_EQ(GetMimeType("foo"), html);
136 EXPECT_EQ(GetMimeType("foo.html"), html);
137 EXPECT_EQ(GetMimeType(".js"), js);
138 EXPECT_EQ(GetMimeType("foo.js"), js);
139 EXPECT_EQ(GetMimeType("js"), html);
140 EXPECT_EQ(GetMimeType("foojs"), html);
141 EXPECT_EQ(GetMimeType("foo.jsp"), html);
144 } // namespace content