blob: e70a780acd89375c90c7e050428b7d2ce2e8d2c4 [file] [log] [blame]
Alex Yangcc8ef632024-10-08 20:13:371// Copyright 2024 The Chromium Authors
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 "content/browser/webui/web_ui_impl.h"
6
7#include "content/browser/webui/url_data_manager_backend.h"
8#include "content/browser/webui/web_ui_data_source_impl.h"
9#include "content/public/browser/url_data_source.h"
10#include "content/public/test/browser_task_environment.h"
11#include "testing/gtest/include/gtest/gtest.h"
12#include "ui/base/webui/web_ui_util.h"
Alex Yangb31a03b2024-11-20 17:57:2713#include "url/origin.h"
Alex Yangcc8ef632024-10-08 20:13:3714
15namespace content {
16
17class WebUIImplTest : public testing::Test {
18 protected:
19 WebUIDataSourceImpl* CreateDataSource(const std::string& source_name) {
20 return new WebUIDataSourceImpl(source_name);
21 }
22 BrowserTaskEnvironment task_environment;
23};
24
25// The URLDataManagerBackend starts with two data source, "chrome://resources"
26// and "chrome-untrusted://resources". Currently, only "chrome://resources" is
27// supported.
28TEST_F(WebUIImplTest, LocalResourceLoaderConfigForDefaultDataSource) {
29 URLDataManagerBackend data_backend;
30 auto config =
31 WebUIImpl::GetLocalResourceLoaderConfigForTesting(&data_backend);
Alex Yangb31a03b2024-11-20 17:57:2732 url::Origin origin = url::Origin::Create(GURL("chrome://resources/"));
33 const auto& config_source = config->sources[origin];
34 ASSERT_TRUE(config_source);
Alex Yangcc8ef632024-10-08 20:13:3735 EXPECT_TRUE(config_source->headers.starts_with("HTTP/1.1 200 OK"));
36 EXPECT_EQ(config_source->should_replace_i18n_in_js, false);
37 EXPECT_GT(config_source->path_to_resource_id_map.size(), 0ul);
38 EXPECT_GT(config_source->replacement_strings.size(), 0ul);
39}
40
41// This test adds a data source with extra string replacements and resource
42// paths added to it and ensures this metadata is reflected in the resulting
43// Mojo struct.
44TEST_F(WebUIImplTest, LocalResourceLoaderConfigForCustomDataSource) {
45 URLDataManagerBackend data_backend;
46 auto* data_source = CreateDataSource("my-data-source");
47 data_source->AddString("a", "b");
48 data_source->AddResourcePath("path/to/resource", 42);
49 data_source->EnableReplaceI18nInJS();
50 data_backend.AddDataSource(data_source);
51 auto config =
52 WebUIImpl::GetLocalResourceLoaderConfigForTesting(&data_backend);
Alex Yangb31a03b2024-11-20 17:57:2753 url::Origin origin = url::Origin::Create(GURL("chrome://my-data-source"));
54 const auto& config_source = config->sources[origin];
55 ASSERT_TRUE(config_source);
Alex Yangcc8ef632024-10-08 20:13:3756 EXPECT_TRUE(config_source->headers.starts_with("HTTP/1.1 200 OK"));
57 EXPECT_EQ(config_source->should_replace_i18n_in_js, true);
58 EXPECT_EQ(config_source->path_to_resource_id_map["path/to/resource"], 42);
59 EXPECT_EQ(config_source->replacement_strings["a"], "b");
60}
61
62} // namespace content