Skip to content

Commit c949c7d

Browse files
Separate test logic from aura and provide production implementation (#6976)
This PR extracts test-specific logic from Aura by guarding them under `RUN_BROWSER_TESTS` preprocessor, and provides production logics for the production conditions. This PR also removes `testonly` flag from group("cobalt_shell_lib_deps") , and put all test dependencies to source_set("browsertests_sources") after the last remaining test-specific logic is separated from Aura. Test: as Aura is only used in Linux, I built and ran CUJ on linux debug and linux gold. Bug: 437981348
1 parent fb82fe8 commit c949c7d

6 files changed

Lines changed: 160 additions & 16 deletions

File tree

cobalt/shell/BUILD.gn

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,7 @@ source_set("android_shell_descriptors") {
4343
}
4444

4545
group("cobalt_shell_lib_deps") {
46-
testonly = true
47-
visibility = [
48-
"//cobalt/shell:*",
49-
"//cobalt/testing:*",
50-
]
46+
visibility = [ "//cobalt/shell:*" ]
5147

5248
public_deps = [
5349
":android_shell_descriptors",
@@ -131,7 +127,6 @@ group("cobalt_shell_lib_deps") {
131127
if (use_aura) {
132128
public_deps += [
133129
"//ui/aura",
134-
"//ui/aura:test_support",
135130
"//ui/events",
136131
"//ui/wm",
137132
]
@@ -141,9 +136,7 @@ group("cobalt_shell_lib_deps") {
141136
"//ui/color:color_headers",
142137
"//ui/native_theme",
143138
"//ui/resources",
144-
"//ui/views:test_support",
145139
"//ui/views/controls/webview",
146-
"//ui/wm:test_support",
147140
]
148141
}
149142
}
@@ -201,6 +194,17 @@ source_set("browsertests_sources") {
201194
"//services/test/echo:lib",
202195
"//services/test/echo/public/mojom",
203196
]
197+
198+
if (use_aura) {
199+
public_deps += [ "//ui/aura:test_support" ]
200+
201+
if (shell_use_toolkit_views) {
202+
public_deps += [
203+
"//ui/views:test_support",
204+
"//ui/wm:test_support",
205+
]
206+
}
207+
}
204208
}
205209

206210
static_library("cobalt_shell_lib") {
@@ -302,6 +306,8 @@ static_library("cobalt_shell_lib") {
302306
if (use_aura) {
303307
if (shell_use_toolkit_views) {
304308
sources += [
309+
"browser/cobalt_views_delegate.cc",
310+
"browser/cobalt_views_delegate.h",
305311
"browser/shell_platform_delegate_views.cc",
306312
"browser/shell_web_contents_view_delegate_views.cc",
307313
]
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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/shell/browser/cobalt_views_delegate.h"
16+
17+
#include "build/build_config.h"
18+
#include "ui/views/buildflags.h"
19+
#include "ui/views/widget/native_widget_aura.h"
20+
21+
#if BUILDFLAG(ENABLE_DESKTOP_AURA)
22+
#include "ui/views/widget/desktop_aura/desktop_native_widget_aura.h"
23+
#endif
24+
25+
namespace views {
26+
27+
CobaltViewsDelegate::CobaltViewsDelegate() = default;
28+
29+
CobaltViewsDelegate::~CobaltViewsDelegate() = default;
30+
31+
void CobaltViewsDelegate::OnBeforeWidgetInit(
32+
Widget::InitParams* params,
33+
internal::NativeWidgetDelegate* delegate) {
34+
#if BUILDFLAG(ENABLE_DESKTOP_AURA)
35+
// If we already have a native_widget, we don't have to try to come
36+
// up with one.
37+
if (params->native_widget) {
38+
return;
39+
}
40+
41+
if (params->parent && params->type != views::Widget::InitParams::TYPE_MENU &&
42+
params->type != views::Widget::InitParams::TYPE_TOOLTIP) {
43+
params->native_widget = new views::NativeWidgetAura(delegate);
44+
} else {
45+
params->native_widget = new views::DesktopNativeWidgetAura(delegate);
46+
}
47+
#endif
48+
}
49+
50+
} // namespace views
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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+
#ifndef COBALT_SHELL_BROWSER_COBALT_VIEWS_DELEGATE_H_
16+
#define COBALT_SHELL_BROWSER_COBALT_VIEWS_DELEGATE_H_
17+
18+
#include <memory>
19+
20+
#include "base/memory/raw_ptr.h"
21+
#include "build/build_config.h"
22+
#include "ui/views/layout/layout_provider.h"
23+
#include "ui/views/views_delegate.h"
24+
25+
namespace views {
26+
27+
class CobaltViewsDelegate : public ViewsDelegate {
28+
public:
29+
CobaltViewsDelegate();
30+
31+
CobaltViewsDelegate(const CobaltViewsDelegate&) = delete;
32+
CobaltViewsDelegate& operator=(const CobaltViewsDelegate&) = delete;
33+
34+
~CobaltViewsDelegate() override;
35+
36+
void OnBeforeWidgetInit(Widget::InitParams* params,
37+
internal::NativeWidgetDelegate* delegate) override;
38+
};
39+
40+
} // namespace views
41+
42+
#endif // COBALT_SHELL_BROWSER_COBALT_VIEWS_DELEGATE_H_

cobalt/shell/browser/shell_platform_data_aura.cc

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,25 +19,31 @@
1919
#include "base/memory/raw_ptr.h"
2020
#include "build/build_config.h"
2121
#include "cobalt/shell/browser/shell.h"
22-
#include "ui/aura/client/cursor_shape_client.h"
2322
#include "ui/aura/client/default_capture_client.h"
2423
#include "ui/aura/env.h"
2524
#include "ui/aura/layout_manager.h"
26-
#include "ui/aura/test/test_focus_client.h"
27-
#include "ui/aura/test/test_window_parenting_client.h"
2825
#include "ui/aura/window.h"
2926
#include "ui/aura/window_event_dispatcher.h"
3027
#include "ui/base/ime/init/input_method_factory.h"
3128
#include "ui/base/ime/input_method.h"
3229
#include "ui/ozone/public/ozone_platform.h"
3330
#include "ui/platform_window/platform_window_init_properties.h"
34-
#include "ui/wm/core/cursor_loader.h"
3531
#include "ui/wm/core/default_activation_client.h"
3632

3733
#if BUILDFLAG(IS_OZONE)
3834
#include "ui/aura/screen_ozone.h"
3935
#endif
4036

37+
#if defined(RUN_BROWSER_TESTS)
38+
#include "ui/aura/client/cursor_shape_client.h" // nogncheck
39+
#include "ui/aura/test/test_focus_client.h" // nogncheck
40+
#include "ui/aura/test/test_window_parenting_client.h" // nogncheck
41+
#include "ui/wm/core/cursor_loader.h" // nogncheck
42+
#else
43+
#include "ui/wm/core/base_focus_rules.h"
44+
#include "ui/wm/core/focus_controller.h"
45+
#endif // defined(RUN_BROWSER_TESTS)
46+
4147
namespace content {
4248

4349
namespace {
@@ -85,6 +91,22 @@ class FillLayout : public aura::LayoutManager {
8591
bool has_bounds_;
8692
};
8793

94+
#if !defined(RUN_BROWSER_TESTS)
95+
class FocusRules : public wm::BaseFocusRules {
96+
public:
97+
FocusRules() = default;
98+
FocusRules(const FocusRules&) = delete;
99+
FocusRules& operator=(const FocusRules&) = delete;
100+
~FocusRules() override = default;
101+
102+
private:
103+
// wm::BaseFocusRules:
104+
bool SupportsChildActivation(const aura::Window* window) const override {
105+
return true;
106+
}
107+
};
108+
#endif // !defined(RUN_BROWSER_TESTS)
109+
88110
} // namespace
89111

90112
ShellPlatformDataAura::ShellPlatformDataAura(const gfx::Size& initial_size) {
@@ -104,20 +126,30 @@ ShellPlatformDataAura::ShellPlatformDataAura(const gfx::Size& initial_size) {
104126
host_->window()->SetLayoutManager(
105127
std::make_unique<FillLayout>(host_->window()));
106128

129+
#if defined(RUN_BROWSER_TESTS)
107130
focus_client_ =
108131
std::make_unique<aura::test::TestFocusClient>(host_->window());
109132

110133
new wm::DefaultActivationClient(host_->window());
134+
#else
135+
// Reference void AuraContext::InitializeWindowTreeHost(aura::WindowTreeHost*
136+
// host) from ui/webui/examples/browser/ui/aura/aura_context.cc
137+
focus_client_ = std::make_unique<wm::FocusController>(new FocusRules());
138+
aura::client::SetFocusClient(host_->window(), focus_client_.get());
139+
#endif // defined(RUN_BROWSER_TESTS)
140+
111141
capture_client_ =
112142
std::make_unique<aura::client::DefaultCaptureClient>(host_->window());
143+
144+
#if defined(RUN_BROWSER_TESTS)
113145
window_parenting_client_ =
114146
std::make_unique<aura::test::TestWindowParentingClient>(host_->window());
115-
116147
// TODO(https://crbug.com/1336055): this is needed for
117148
// mouse_cursor_overlay_controller_browsertest.cc on cast_shell_linux as
118149
// currently, when is_castos = true, the views toolkit isn't used.
119150
cursor_shape_client_ = std::make_unique<wm::CursorLoader>();
120151
aura::client::SetCursorShapeClient(cursor_shape_client_.get());
152+
#endif // defined(RUN_BROWSER_TESTS)
121153
}
122154

123155
ShellPlatformDataAura::~ShellPlatformDataAura() {

cobalt/shell/browser/shell_platform_data_aura.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,12 @@
2222

2323
namespace aura {
2424
namespace client {
25-
class CursorShapeClient;
26-
class DefaultCaptureClient;
2725
class FocusClient;
26+
class DefaultCaptureClient;
27+
#if defined(RUN_BROWSER_TESTS)
28+
class CursorShapeClient;
2829
class WindowParentingClient;
30+
#endif // defined(RUN_BROWSER_TESTS)
2931
} // namespace client
3032
} // namespace aura
3133

@@ -63,8 +65,11 @@ class ShellPlatformDataAura {
6365
std::unique_ptr<aura::WindowTreeHost> host_;
6466
std::unique_ptr<aura::client::FocusClient> focus_client_;
6567
std::unique_ptr<aura::client::DefaultCaptureClient> capture_client_;
68+
69+
#if defined(RUN_BROWSER_TESTS)
6670
std::unique_ptr<aura::client::WindowParentingClient> window_parenting_client_;
6771
std::unique_ptr<aura::client::CursorShapeClient> cursor_shape_client_;
72+
#endif // defined(RUN_BROWSER_TESTS)
6873
};
6974

7075
} // namespace content

cobalt/shell/browser/shell_platform_delegate_views.cc

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,19 @@
4747
#include "ui/views/layout/box_layout_view.h"
4848
#include "ui/views/layout/flex_layout_types.h"
4949
#include "ui/views/layout/flex_layout_view.h"
50-
#include "ui/views/test/desktop_test_views_delegate.h"
5150
#include "ui/views/view.h"
5251
#include "ui/views/view_class_properties.h"
5352
#include "ui/views/widget/desktop_aura/desktop_screen.h"
5453
#include "ui/views/widget/widget.h"
5554
#include "ui/views/widget/widget_delegate.h"
5655
#include "ui/wm/core/wm_state.h"
5756

57+
#if defined(RUN_BROWSER_TESTS)
58+
#include "ui/views/test/desktop_test_views_delegate.h" // nogncheck
59+
#else
60+
#include "cobalt/shell/browser/cobalt_views_delegate.h"
61+
#endif // defined(RUN_BROWSER_TESTS)
62+
5863
namespace content {
5964

6065
struct ShellPlatformDelegate::ShellData {
@@ -318,8 +323,12 @@ void ShellPlatformDelegate::Initialize(const gfx::Size& default_window_size) {
318323
platform_->screen = views::CreateDesktopScreen();
319324
}
320325

326+
#if defined(RUN_BROWSER_TESTS)
321327
platform_->views_delegate =
322328
std::make_unique<views::DesktopTestViewsDelegate>();
329+
#else
330+
platform_->views_delegate = std::make_unique<views::CobaltViewsDelegate>();
331+
#endif // defined(RUN_BROWSER_TESTS)
323332
}
324333

325334
ShellPlatformDelegate::~ShellPlatformDelegate() = default;

0 commit comments

Comments
 (0)