1 // Copyright 2022 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.
5 #include "base/run_loop.h"
6 #include "base/task/thread_pool/thread_pool_instance.h"
7 #include "build/build_config.h"
8 #include "chrome/browser/browser_process.h"
9 #include "chrome/browser/ui/browser.h"
10 #include "chrome/test/base/in_process_browser_test.h"
11 #include "chrome/test/base/ui_test_utils.h"
12 #include "components/metrics/content/subprocess_metrics_provider.h"
13 #include "content/public/test/browser_test.h"
14 #include "content/public/test/browser_test_utils.h"
15 #include "content/public/test/content_browser_test.h"
16 #include "services/network/public/cpp/features.h"
17 #include "testing/gmock/include/gmock/gmock.h"
19 class ChromeDataUseMeasurementBrowserTest : public InProcessBrowserTest {
21 void SetUpOnMainThread() override {
22 embedded_test_server()->ServeFilesFromSourceDirectory(
23 GetChromeTestDataDir());
24 ASSERT_TRUE(embedded_test_server()->Start());
27 size_t GetTotalDataUse() const {
28 return histogram_tester_.GetTotalSum("DataUse.BytesReceived3.Delegate") +
29 histogram_tester_.GetTotalSum("DataUse.BytesSent3.Delegate");
32 void RetryUntilUserInitiatedDataUsePrefHasEntry() {
34 base::ThreadPoolInstance::Get()->FlushForTesting();
35 base::RunLoop().RunUntilIdle();
36 content::FetchHistogramsFromChildProcesses();
37 metrics::SubprocessMetricsProvider::MergeHistogramDeltasForTesting();
38 } while (GetTotalDataUse() == 0);
42 base::HistogramTester histogram_tester_;
45 IN_PROC_BROWSER_TEST_F(ChromeDataUseMeasurementBrowserTest, DataUseRecorded) {
46 ASSERT_TRUE(ui_test_utils::NavigateToURL(
47 browser(), embedded_test_server()->GetURL("/title1.html")));
48 RetryUntilUserInitiatedDataUsePrefHasEntry();
50 EXPECT_GT(GetTotalDataUse(), 0u);