|
| 1 | +// Copyright 2026 The Cobalt Authors. All Rights Reserved. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +#include "base/run_loop.h" |
| 16 | +#include "base/test/metrics/histogram_tester.h" |
| 17 | +#include "base/threading/thread_restrictions.h" |
| 18 | +#include "cobalt/browser/global_features.h" |
| 19 | +#include "cobalt/browser/metrics/cobalt_metrics_service_client.h" |
| 20 | +#include "cobalt/browser/metrics/cobalt_metrics_services_manager_client.h" |
| 21 | +#include "cobalt/browser/switches.h" |
| 22 | +#include "cobalt/testing/browser_tests/browser/test_shell.h" |
| 23 | +#include "cobalt/testing/browser_tests/content_browser_test.h" |
| 24 | +#include "components/metrics/metrics_pref_names.h" |
| 25 | +#include "components/metrics_services_manager/metrics_services_manager.h" |
| 26 | +#include "components/prefs/pref_service.h" |
| 27 | +#include "content/public/test/browser_test.h" |
| 28 | +#include "content/public/test/browser_test_utils.h" |
| 29 | +#include "testing/gtest/include/gtest/gtest.h" |
| 30 | + |
| 31 | +namespace cobalt { |
| 32 | + |
| 33 | +class FontMetricsBrowserTest : public content::ContentBrowserTest { |
| 34 | + public: |
| 35 | + FontMetricsBrowserTest() = default; |
| 36 | + ~FontMetricsBrowserTest() override = default; |
| 37 | + |
| 38 | + void SetUpCommandLine(base::CommandLine* command_line) override { |
| 39 | + content::ContentBrowserTest::SetUpCommandLine(command_line); |
| 40 | + // Set a short interval for memory metrics to verify periodic recording. |
| 41 | + command_line->AppendSwitchASCII(switches::kMemoryMetricsInterval, "1"); |
| 42 | + } |
| 43 | +}; |
| 44 | + |
| 45 | +IN_PROC_BROWSER_TEST_F(FontMetricsBrowserTest, RecordsFontHistograms) { |
| 46 | + base::HistogramTester histogram_tester; |
| 47 | + |
| 48 | + base::ScopedAllowBlockingForTesting allow_blocking; |
| 49 | + auto* features = GlobalFeatures::GetInstance(); |
| 50 | + |
| 51 | + // Ensure metrics recording is started. |
| 52 | + features->metrics_services_manager()->UpdateUploadPermissions(true); |
| 53 | + |
| 54 | + // Load a page with various characters to exercise font caches and fallback in |
| 55 | + // Blink. |
| 56 | + std::string html_content = R"( |
| 57 | + <html> |
| 58 | + <body> |
| 59 | + <p>Standard Latin text.</p> |
| 60 | + <p>Emoji: 😀 😃 😄 😁 😆 😅 😂 🤣 🦩 🦒 🦚 🦝</p> |
| 61 | + <p>Rare CJK: 𠜎 𠜱 𠝹 𠱓 𠱸 𠲖 𠳏 𠳕</p> |
| 62 | + </body> |
| 63 | + </html> |
| 64 | + )"; |
| 65 | + |
| 66 | + GURL url("data:text/html;charset=utf-8," + html_content); |
| 67 | + ASSERT_TRUE(content::NavigateToURL(shell()->web_contents(), url)); |
| 68 | + |
| 69 | + // Trigger a memory dump manually for testing. |
| 70 | + auto* manager_client = features->metrics_services_manager_client(); |
| 71 | + ASSERT_TRUE(manager_client); |
| 72 | + auto* client = static_cast<CobaltMetricsServiceClient*>( |
| 73 | + manager_client->metrics_service_client()); |
| 74 | + ASSERT_TRUE(client); |
| 75 | + |
| 76 | + base::RunLoop run_loop; |
| 77 | + client->ScheduleRecordForTesting(run_loop.QuitClosure()); |
| 78 | + run_loop.Run(); |
| 79 | + |
| 80 | + base::RunLoop().RunUntilIdle(); |
| 81 | + |
| 82 | + // Check for memory histograms (Skia Glyph Cache). |
| 83 | + EXPECT_GE( |
| 84 | + histogram_tester.GetAllSamples("Memory.Browser.PrivateMemoryFootprint") |
| 85 | + .size(), |
| 86 | + 1u); |
| 87 | + |
| 88 | + EXPECT_GE( |
| 89 | + histogram_tester |
| 90 | + .GetAllSamples("Memory.Experimental.Browser2.Skia.Small.SkGlyphCache") |
| 91 | + .size(), |
| 92 | + 1u); |
| 93 | + |
| 94 | + EXPECT_GE(histogram_tester |
| 95 | + .GetAllSamples("Memory.Experimental.Browser2.Small.FontCaches") |
| 96 | + .size(), |
| 97 | + 1u); |
| 98 | +} |
| 99 | + |
| 100 | +} // namespace cobalt |
0 commit comments