Alex Yang | cc8ef63 | 2024-10-08 20:13:37 | [diff] [blame] | 1 | // 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 Yang | b31a03b | 2024-11-20 17:57:27 | [diff] [blame] | 13 | #include "url/origin.h" |
Alex Yang | cc8ef63 | 2024-10-08 20:13:37 | [diff] [blame] | 14 | |
| 15 | namespace content { |
| 16 | |
| 17 | class 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. |
| 28 | TEST_F(WebUIImplTest, LocalResourceLoaderConfigForDefaultDataSource) { |
| 29 | URLDataManagerBackend data_backend; |
| 30 | auto config = |
| 31 | WebUIImpl::GetLocalResourceLoaderConfigForTesting(&data_backend); |
Alex Yang | b31a03b | 2024-11-20 17:57:27 | [diff] [blame] | 32 | url::Origin origin = url::Origin::Create(GURL("chrome://resources/")); |
| 33 | const auto& config_source = config->sources[origin]; |
| 34 | ASSERT_TRUE(config_source); |
Alex Yang | cc8ef63 | 2024-10-08 20:13:37 | [diff] [blame] | 35 | 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. |
| 44 | TEST_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 Yang | b31a03b | 2024-11-20 17:57:27 | [diff] [blame] | 53 | 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 Yang | cc8ef63 | 2024-10-08 20:13:37 | [diff] [blame] | 56 | 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 |