Skip to content

Commit 102fa36

Browse files
Implement ShellBrowserTestMainParts and consolidate shell_test_switch… (#8500)
…es.h Relocate shell_test_switches from cobalt/shell/common to cobalt/testing/browser_tests/common. Introduce ShellBrowserTestMainParts to encapsulate browser-test-specific BrowserMainParts logic, centralizing the toolkit initialization override for web tests. Move feature overrides for web tests from CobaltContentBrowserClient to ShellContentBrowserTestClient. These changes improve codebase organization by isolating test-specific infrastructure. Test: Build devel and verified CUJ locally. Bug: 446892499
1 parent 6f01687 commit 102fa36

16 files changed

Lines changed: 76 additions & 109 deletions

cobalt/browser/cobalt_content_browser_client.cc

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,6 @@
7373
#include "cobalt/android/browser_jni_headers/CobaltContentBrowserClient_jni.h"
7474
#endif // BUILDFLAG(IS_ANDROID)
7575

76-
#if defined(RUN_BROWSER_TESTS)
77-
#include "cobalt/shell/common/shell_test_switches.h" // nogncheck
78-
#endif // defined(RUN_BROWSER_TESTS)
79-
8076
namespace cobalt {
8177

8278
namespace {
@@ -509,20 +505,6 @@ void CobaltContentBrowserClient::CreateFeatureListAndFieldTrials() {
509505
std::vector<base::FeatureList::FeatureOverrideInfo> feature_overrides =
510506
content::GetSwitchDependentFeatureOverrides(command_line);
511507

512-
#if defined(RUN_BROWSER_TESTS)
513-
// Overrides for --run-web-tests.
514-
if (switches::IsRunWebTestsSwitchPresent()) {
515-
// Disable artificial timeouts for PNA-only preflights in warning-only mode
516-
// for web tests. We do not exercise this behavior with web tests as it is
517-
// intended to be a temporary rollout stage, and the short timeout causes
518-
// flakiness when the test server takes just a tad too long to respond.
519-
feature_overrides.emplace_back(
520-
std::cref(
521-
network::features::kPrivateNetworkAccessPreflightShortTimeout),
522-
base::FeatureList::OVERRIDE_DISABLE_FEATURE);
523-
}
524-
#endif // defined(RUN_BROWSER_TESTS)
525-
526508
feature_list->InitFromCommandLine(
527509
command_line.GetSwitchValueASCII(::switches::kEnableFeatures),
528510
command_line.GetSwitchValueASCII(::switches::kDisableFeatures));

cobalt/shell/BUILD.gn

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -252,10 +252,7 @@ static_library("cobalt_shell_lib") {
252252
if (run_browser_tests) {
253253
defines += [ "RUN_BROWSER_TESTS=1" ]
254254

255-
public_deps += [
256-
":shell_test_switches_lib",
257-
"//content/test:test_support",
258-
]
255+
public_deps += [ "//content/test:test_support" ]
259256
}
260257

261258
if (shell_use_toolkit_views) {
@@ -302,15 +299,6 @@ static_library("cobalt_shell_lib") {
302299
}
303300
}
304301

305-
source_set("shell_test_switches_lib") {
306-
testonly = true
307-
sources = [
308-
"common/shell_test_switches.cc",
309-
"common/shell_test_switches.h",
310-
]
311-
deps = [ "//base:base" ]
312-
}
313-
314302
# This component provides a ContentMainDelegate for Content Shell and derived
315303
# applications. This delegate is the central place that creates interfaces for
316304
# each type of process (browser, renderer, etc). This implementation of

cobalt/shell/browser/shell_browser_main_parts.cc

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,6 @@
7272
#include "ui/linux/linux_ui_factory.h" // nogncheck
7373
#endif
7474

75-
#if defined(RUN_BROWSER_TESTS)
76-
#include "cobalt/shell/common/shell_test_switches.h" // nogncheck
77-
#endif // defined(RUN_BROWSER_TESTS)
78-
7975
#if BUILDFLAG(IS_STARBOARD)
8076
#include "cobalt/shell/common/device_authentication.h"
8177
#endif
@@ -157,12 +153,6 @@ void ShellBrowserMainParts::InitializeMessageLoopContext() {
157153
}
158154

159155
void ShellBrowserMainParts::ToolkitInitialized() {
160-
#if defined(RUN_BROWSER_TESTS)
161-
if (switches::IsRunWebTestsSwitchPresent()) {
162-
return;
163-
}
164-
#endif // defined(RUN_BROWSER_TESTS)
165-
166156
#if BUILDFLAG(IS_LINUX)
167157
ui::LinuxUi::SetInstance(ui::GetDefaultLinuxUi());
168158
#endif

cobalt/shell/common/shell_test_switches.h

Lines changed: 0 additions & 38 deletions
This file was deleted.

cobalt/testing/browser_tests/BUILD.gn

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,8 @@ static_library("cobalt_shell_test_lib") {
399399
"app/shell_main_test_delegate.h",
400400
"browser/shell_browser_test_context.cc",
401401
"browser/shell_browser_test_context.h",
402+
"browser/shell_browser_test_main_parts.cc",
403+
"browser/shell_browser_test_main_parts.h",
402404
"browser/shell_content_browser_test_client.cc",
403405
"browser/shell_content_browser_test_client.h",
404406
"browser/test_shell.cc",
@@ -450,6 +452,8 @@ source_set("cobalt_browsertests_support") {
450452
"common/main_frame_counter_test_impl.h",
451453
"common/power_monitor_test_impl.cc",
452454
"common/power_monitor_test_impl.h",
455+
"common/shell_test_switches.cc",
456+
"common/shell_test_switches.h",
453457
"renderer/render_frame_test_helper.cc",
454458
"renderer/render_frame_test_helper.h",
455459
"renderer/shell_render_frame_observer.cc",
@@ -459,7 +463,6 @@ source_set("cobalt_browsertests_support") {
459463
public_deps = [
460464
":cobalt_browsertests_support_mojom",
461465
":shell_controller_mojom",
462-
"//cobalt/shell:shell_test_switches_lib",
463466
"//components/custom_handlers:test_support",
464467
"//components/metrics:test_support",
465468
"//components/services/storage/test_api",

cobalt/testing/browser_tests/app/shell_main_test_delegate.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
#include <iostream>
1818

1919
#include "base/command_line.h"
20-
#include "cobalt/shell/common/shell_test_switches.h"
2120
#include "cobalt/testing/browser_tests/common/shell_content_test_client.h"
21+
#include "cobalt/testing/browser_tests/common/shell_test_switches.h"
2222
#include "cobalt/testing/browser_tests/utility/shell_content_utility_client.h"
2323

2424
namespace content {
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Copyright 2025 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 "cobalt/testing/browser_tests/browser/shell_browser_test_main_parts.h"
16+
17+
#include "build/build_config.h"
18+
#include "cobalt/testing/browser_tests/common/shell_test_switches.h"
19+
20+
#if BUILDFLAG(IS_LINUX)
21+
#include "ui/linux/linux_ui.h" // nogncheck
22+
#include "ui/linux/linux_ui_factory.h" // nogncheck
23+
#endif
24+
25+
namespace content {
26+
27+
ShellBrowserTestMainParts::ShellBrowserTestMainParts() = default;
28+
29+
ShellBrowserTestMainParts::~ShellBrowserTestMainParts() = default;
30+
31+
void ShellBrowserTestMainParts::ToolkitInitialized() {
32+
if (switches::IsRunWebTestsSwitchPresent()) {
33+
return;
34+
}
35+
36+
#if BUILDFLAG(IS_LINUX)
37+
ui::LinuxUi::SetInstance(ui::GetDefaultLinuxUi());
38+
#endif
39+
}
40+
41+
} // namespace content

cobalt/shell/common/shell_test_switches.cc renamed to cobalt/testing/browser_tests/browser/shell_browser_test_main_parts.h

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,22 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
#include "cobalt/shell/common/shell_test_switches.h"
15+
#ifndef COBALT_TESTING_BROWSER_TESTS_BROWSER_SHELL_BROWSER_TEST_MAIN_PARTS_H_
16+
#define COBALT_TESTING_BROWSER_TESTS_BROWSER_SHELL_BROWSER_TEST_MAIN_PARTS_H_
1617

17-
#include "base/command_line.h"
18+
#include "cobalt/shell/browser/shell_browser_main_parts.h"
1819

19-
namespace switches {
20+
namespace content {
2021

21-
const char kExposeInternalsForTesting[] = "expose-internals-for-testing";
22+
class ShellBrowserTestMainParts : public ShellBrowserMainParts {
23+
public:
24+
ShellBrowserTestMainParts();
25+
~ShellBrowserTestMainParts() override;
2226

23-
const char kRunWebTests[] = "run-web-tests";
27+
// content::BrowserMainParts overrides.
28+
void ToolkitInitialized() override;
29+
};
2430

25-
bool IsRunWebTestsSwitchPresent() {
26-
return base::CommandLine::ForCurrentProcess()->HasSwitch(
27-
switches::kRunWebTests);
28-
}
31+
} // namespace content
2932

30-
} // namespace switches
33+
#endif // COBALT_TESTING_BROWSER_TESTS_BROWSER_SHELL_BROWSER_TEST_MAIN_PARTS_H_

cobalt/testing/browser_tests/browser/shell_content_browser_test_client.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -191,8 +191,8 @@ void ShellContentBrowserTestClient::AppendExtraCommandLineSwitches(
191191
ShellContentBrowserClient::AppendExtraCommandLineSwitches(command_line,
192192
child_process_id);
193193
static const char* kForwardTestSwitches[] = {
194-
test_switches::kExposeInternalsForTesting,
195-
test_switches::kRunWebTests,
194+
switches::kExposeInternalsForTesting,
195+
switches::kRunWebTests,
196196
};
197197

198198
command_line->CopySwitchesFrom(*base::CommandLine::ForCurrentProcess(),
@@ -266,7 +266,7 @@ void ShellContentBrowserTestClient::SetUpFieldTrials() {
266266
// Overrides for content/shell switches.
267267

268268
// Overrides for --run-web-tests.
269-
if (test_switches::IsRunWebTestsSwitchPresent()) {
269+
if (switches::IsRunWebTestsSwitchPresent()) {
270270
// Disable artificial timeouts for PNA-only preflights in warning-only mode
271271
// for web tests. We do not exercise this behavior with web tests as it is
272272
// intended to be a temporary rollout stage, and the short timeout causes

cobalt/testing/browser_tests/browser/test_shell.cc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ TestShell* TestShell::CreateShell(std::unique_ptr<WebContents> web_contents,
4646
// Note: Do not make RenderFrameHost or RenderViewHost specific state changes
4747
// here, because they will be forgotten after a cross-process navigation. Use
4848
// RenderFrameCreated or RenderViewCreated instead.
49-
if (test_switches::IsRunWebTestsSwitchPresent()) {
49+
if (switches::IsRunWebTestsSwitchPresent()) {
5050
raw_web_contents->GetMutableRendererPrefs()->use_custom_colors = false;
5151
raw_web_contents->SyncRendererPrefs();
5252
}
@@ -151,14 +151,14 @@ bool TestShell::DidAddMessageToConsole(
151151
const std::u16string& message,
152152
int32_t line_no,
153153
const std::u16string& source_id) {
154-
return test_switches::IsRunWebTestsSwitchPresent();
154+
return switches::IsRunWebTestsSwitchPresent();
155155
}
156156

157157
PictureInPictureResult TestShell::EnterPictureInPicture(
158158
WebContents* web_contents) {
159159
// During tests, returning success to pretend the window was created and allow
160160
// tests to run accordingly.
161-
if (test_switches::IsRunWebTestsSwitchPresent()) {
161+
if (switches::IsRunWebTestsSwitchPresent()) {
162162
return PictureInPictureResult::kSuccess;
163163
}
164164
return PictureInPictureResult::kNotSupported;
@@ -168,7 +168,7 @@ void TestShell::SetContentsBounds(WebContents* source,
168168
const gfx::Rect& bounds) {
169169
DCHECK(source == web_contents()); // There's only one WebContents per Shell.
170170

171-
if (test_switches::IsRunWebTestsSwitchPresent()) {
171+
if (switches::IsRunWebTestsSwitchPresent()) {
172172
// Note that chrome drops these requests on normal windows.
173173
// TODO(danakj): The position is dropped here but we use the size. Web tests
174174
// can't move the window in headless mode anyways, but maybe we should be

0 commit comments

Comments
 (0)