Skip to content

Commit 069fcfe

Browse files
authored
cobalt/test: Ensure pref directories exist and skip clean shutdown steps (youtube#9293)
This fixes a DCHECK failure in CleanExitBeacon during browser tests. - Ensure parent directories for experiment and metrics configs exist before creating JsonPrefStore, allowing it to initialize synchronously. - Call CleanExitBeacon::SkipCleanShutdownStepsForTesting() in ContentBrowserTest::SetUp() to prevent unnecessary clean shutdown checks and updates during tests. Test: `$out_dir/bin/run_cobalt_browsertests --gtest_filter=FontMetricsBrowserTest.*` Bug: 431884442
1 parent e111d72 commit 069fcfe

File tree

3 files changed

+25
-6
lines changed

3 files changed

+25
-6
lines changed

cobalt/browser/global_features.cc

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "cobalt/browser/global_features.h"
1616

1717
#include "base/feature_list.h"
18+
#include "base/files/file_util.h"
1819
#include "base/no_destructor.h"
1920
#include "base/path_service.h"
2021
#include "base/time/time.h"
@@ -93,9 +94,8 @@ void GlobalFeatures::CreateExperimentConfig() {
9394

9495
RegisterPrefs(pref_registry.get());
9596

96-
base::FilePath path;
97-
CHECK(base::PathService::Get(base::DIR_CACHE, &path));
98-
path = path.Append(kExperimentConfigFilename);
97+
base::FilePath path =
98+
GetPrefFilePath(kExperimentConfigFilename, "experiment config");
9999

100100
PrefServiceFactory pref_service_factory;
101101
pref_service_factory.set_user_prefs(
@@ -127,9 +127,8 @@ void GlobalFeatures::CreateMetricsLocalState() {
127127
// call, etc., this is the setting that's overridden).
128128
pref_registry->RegisterBooleanPref(metrics::prefs::kMetricsReportingEnabled,
129129
false);
130-
base::FilePath path;
131-
CHECK(base::PathService::Get(base::DIR_CACHE, &path));
132-
path = path.Append(kMetricsConfigFilename);
130+
base::FilePath path =
131+
GetPrefFilePath(kMetricsConfigFilename, "metrics config");
133132

134133
PrefServiceFactory pref_service_factory;
135134
// TODO(b/397929564): Investigate using a Chrome's memory-mapped file store
@@ -155,6 +154,19 @@ void GlobalFeatures::InitializeActiveConfigData() {
155154
: kExperimentConfigActiveConfigData);
156155
}
157156

157+
base::FilePath GlobalFeatures::GetPrefFilePath(
158+
const base::FilePath::CharType filename[],
159+
const char* label) {
160+
base::FilePath path;
161+
CHECK(base::PathService::Get(base::DIR_CACHE, &path));
162+
path = path.Append(filename);
163+
164+
CHECK(base::CreateDirectory(path.DirName()))
165+
<< "Failed to create directory for " << label << ": "
166+
<< path.DirName().value();
167+
return path;
168+
}
169+
158170
void GlobalFeatures::Shutdown() {
159171
metrics::MetricsService* metrics = metrics_service();
160172
if (metrics) {

cobalt/browser/global_features.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,11 @@ class GlobalFeatures {
9393
// Modified config data should only apply to the next app life cycle.
9494
void InitializeActiveConfigData();
9595

96+
// Construct a FilePath for a pref file and ensure its parent directory
97+
// exists.
98+
base::FilePath GetPrefFilePath(const base::FilePath::CharType filename[],
99+
const char* label);
100+
96101
std::unique_ptr<base::FeatureList::Accessor> accessor_;
97102

98103
// Finch config/state.

cobalt/testing/browser_tests/content_browser_test.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include "cobalt/shell/common/shell_switches.h"
3030
#include "cobalt/testing/browser_tests/browser/test_shell.h"
3131
#include "cobalt/testing/browser_tests/content_browser_test_content_browser_client.h"
32+
#include "components/metrics/clean_exit_beacon.h"
3233
#include "content/browser/renderer_host/render_frame_host_impl.h"
3334
#include "content/public/browser/render_process_host.h"
3435
#include "content/public/common/content_paths.h"
@@ -99,6 +100,7 @@ void ContentBrowserTest::SetUpCommandLine(base::CommandLine* command_line) {
99100
}
100101

101102
void ContentBrowserTest::SetUp() {
103+
metrics::CleanExitBeacon::SkipCleanShutdownStepsForTesting();
102104
base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
103105
SetUpCommandLine(command_line);
104106

0 commit comments

Comments
 (0)