Skip to content

Commit 90587fb

Browse files
Clean up hover test. (#9826)
1 parent be950ac commit 90587fb

3 files changed

Lines changed: 78 additions & 79 deletions

File tree

packages/devtools_app/lib/src/shared/ui/hover.dart

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,8 @@ int _hoverIndexFor(double dx, TextSpan line) {
117117
const _hoverYOffset = 10.0;
118118

119119
/// Minimum distance from the side of screen to show tooltip
120-
const _hoverMargin = 16.0;
120+
@visibleForTesting
121+
const hoverMargin = 16.0;
121122

122123
/// Defines how a [HoverCardTooltip] is positioned
123124
enum HoverCardPosition {
@@ -256,19 +257,16 @@ class HoverCard {
256257
final overlaySize = overlayBox.size;
257258
final localPosition = overlayBox.globalToLocal(event.position);
258259

259-
final maxX = math.max(
260-
_hoverMargin,
261-
overlaySize.width - _hoverMargin - width,
262-
);
263-
final x = (localPosition.dx - (width / 2.0)).clamp(_hoverMargin, maxX);
260+
final maxX = math.max(hoverMargin, overlaySize.width - hoverMargin - width);
261+
final x = (localPosition.dx - (width / 2.0)).clamp(hoverMargin, maxX);
264262

265263
final maxY = math.max(
266-
_hoverMargin,
264+
hoverMargin,
267265
overlaySize.height -
268-
_hoverMargin -
266+
hoverMargin -
269267
_totalMaxHoverCardHeight(hasTitle: title != null),
270268
);
271-
final y = (localPosition.dy + _hoverYOffset).clamp(_hoverMargin, maxY);
269+
final y = (localPosition.dy + _hoverYOffset).clamp(hoverMargin, maxY);
272270

273271
return Offset(x, y);
274272
}
@@ -381,7 +379,8 @@ class HoverCardTooltip extends StatefulWidget {
381379
}) : asyncGenerateHoverCardData = null,
382380
asyncTimeout = null;
383381

384-
static const _hoverDelay = Duration(milliseconds: 500);
382+
@visibleForTesting
383+
static const hoverDelay = Duration(milliseconds: 500);
385384
static const defaultHoverWidth = 450.0;
386385

387386
/// Whether the tooltip is currently enabled.
@@ -420,7 +419,7 @@ class _HoverCardTooltipState extends State<HoverCardTooltip> {
420419

421420
void _onHoverExit() {
422421
_showTimer?.cancel();
423-
_removeTimer = Timer(HoverCardTooltip._hoverDelay, () {
422+
_removeTimer = Timer(HoverCardTooltip.hoverDelay, () {
424423
if (_currentHoverCard != null) {
425424
_hoverCardController.maybeRemoveHoverCard(_currentHoverCard!);
426425
}
@@ -448,7 +447,7 @@ class _HoverCardTooltipState extends State<HoverCardTooltip> {
448447
final generateHoverCardData = widget.generateHoverCardData;
449448
final asyncTimeout = widget.asyncTimeout;
450449

451-
_showTimer = Timer(HoverCardTooltip._hoverDelay, () {
450+
_showTimer = Timer(HoverCardTooltip.hoverDelay, () {
452451
if (asyncGenerateHoverCardData != null) {
453452
assert(generateHoverCardData == null);
454453
_showAsyncHoverCard(
@@ -596,13 +595,13 @@ class _HoverCardTooltipState extends State<HoverCardTooltip> {
596595
final box = context.findRenderObject() as RenderBox;
597596

598597
final maxX = math.max(
599-
_hoverMargin,
600-
overlayBox.size.width - _hoverMargin - width,
598+
hoverMargin,
599+
overlayBox.size.width - hoverMargin - width,
601600
);
602601
final maxY = math.max(
603-
_hoverMargin,
602+
hoverMargin,
604603
overlayBox.size.height -
605-
_hoverMargin -
604+
hoverMargin -
606605
_totalMaxHoverCardHeight(hasTitle: title != null),
607606
);
608607

@@ -612,8 +611,8 @@ class _HoverCardTooltipState extends State<HoverCardTooltip> {
612611
);
613612

614613
return Offset(
615-
offset.dx.clamp(_hoverMargin, maxX),
616-
offset.dy.clamp(_hoverMargin, maxY),
614+
offset.dx.clamp(hoverMargin, maxX),
615+
offset.dy.clamp(hoverMargin, maxY),
617616
);
618617
}
619618

packages/devtools_app/test/shared/ui/hover_positioning_test.dart

Lines changed: 59 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@ import 'package:devtools_test/helpers.dart';
77
import 'package:flutter/gestures.dart';
88
import 'package:flutter/material.dart';
99
import 'package:flutter_test/flutter_test.dart';
10-
import 'package:provider/provider.dart';
10+
11+
const _windowWidth = 800.0;
12+
const _windowHeight = 600.0;
13+
const _windowSize = Size(_windowWidth, _windowHeight);
1114

1215
void main() {
1316
Future<void> pumpHoverCardTooltip(
@@ -40,13 +43,13 @@ void main() {
4043
await gesture.addPointer(location: Offset.zero);
4144
final center = tester.getCenter(find.text('Hover Me'));
4245
await gesture.moveTo(center);
43-
await tester.pump(const Duration(milliseconds: 500));
46+
await tester.pump(HoverCardTooltip.hoverDelay);
4447
await tester.pumpAndSettle();
4548
}
4649

4750
testWidgetsWithWindowSize(
4851
'HoverCard at the bottom of the window should not overflow',
49-
const Size(800, 600),
52+
_windowSize,
5053
(WidgetTester tester) async {
5154
// Use a title to increase the height beyond the base content height.
5255
await pumpHoverCardTooltip(
@@ -66,14 +69,16 @@ void main() {
6669
final position = renderBox.localToGlobal(Offset.zero);
6770
final size = renderBox.size;
6871

69-
// _hoverMargin = 16.0
70-
expect(position.dy + size.height, lessThanOrEqualTo(600.0 - 16.0));
72+
expect(
73+
position.dy + size.height,
74+
lessThanOrEqualTo(_windowHeight - hoverMargin),
75+
);
7176
},
7277
);
7378

7479
testWidgetsWithWindowSize(
7580
'HoverCard at the right of the window should not overflow',
76-
const Size(800, 600),
81+
_windowSize,
7782
(WidgetTester tester) async {
7883
await pumpHoverCardTooltip(tester, alignment: Alignment.centerRight);
7984

@@ -88,8 +93,10 @@ void main() {
8893
final position = renderBox.localToGlobal(Offset.zero);
8994
final size = renderBox.size;
9095

91-
// _hoverMargin = 16.0
92-
expect(position.dx + size.width, lessThanOrEqualTo(800.0 - 16.0));
96+
expect(
97+
position.dx + size.width,
98+
lessThanOrEqualTo(_windowWidth - hoverMargin),
99+
);
93100
},
94101
);
95102

@@ -110,38 +117,36 @@ void main() {
110117
},
111118
);
112119

113-
testWidgetsWithWindowSize(
114-
'HoverCard height clamping with title',
115-
const Size(800, 600),
116-
(WidgetTester tester) async {
117-
await pumpHoverCardTooltip(
118-
tester,
119-
alignment: Alignment.bottomCenter,
120-
title: 'An Important Title',
121-
);
120+
testWidgetsWithWindowSize('HoverCard height clamping with title', _windowSize, (
121+
WidgetTester tester,
122+
) async {
123+
await pumpHoverCardTooltip(
124+
tester,
125+
alignment: Alignment.bottomCenter,
126+
title: 'An Important Title',
127+
);
122128

123-
final hoverContentFinderWithTitle = find.text('Hover Content');
124-
expect(hoverContentFinderWithTitle, findsOneWidget);
129+
final hoverContentFinderWithTitle = find.text('Hover Content');
130+
expect(hoverContentFinderWithTitle, findsOneWidget);
125131

126-
final containerWithTitle = find
127-
.ancestor(
128-
of: hoverContentFinderWithTitle,
129-
matching: find.byType(Container),
130-
)
131-
.last;
132+
final containerWithTitle = find
133+
.ancestor(
134+
of: hoverContentFinderWithTitle,
135+
matching: find.byType(Container),
136+
)
137+
.last;
132138

133-
final renderBoxWithTitle =
134-
tester.renderObject(containerWithTitle) as RenderBox;
135-
final positionWithTitle = renderBoxWithTitle.localToGlobal(Offset.zero);
139+
final renderBoxWithTitle =
140+
tester.renderObject(containerWithTitle) as RenderBox;
141+
final positionWithTitle = renderBoxWithTitle.localToGlobal(Offset.zero);
136142

137-
// Clamps strictly at y = 274.0 because of dynamic height containing title/divider.
138-
expect(positionWithTitle.dy, equals(274.0));
139-
},
140-
);
143+
// Clamps strictly at y = 274.0 because of dynamic height containing title/divider.
144+
expect(positionWithTitle.dy, equals(274.0));
145+
});
141146

142147
testWidgetsWithWindowSize(
143148
'HoverCard height clamping without title',
144-
const Size(800, 600),
149+
_windowSize,
145150
(WidgetTester tester) async {
146151
await pumpHoverCardTooltip(tester, alignment: Alignment.bottomCenter);
147152

@@ -166,39 +171,34 @@ void main() {
166171

167172
testWidgetsWithWindowSize(
168173
'HoverCard translates global coordinates to local coordinates for offset overlays',
169-
const Size(800, 600),
174+
_windowSize,
170175
(WidgetTester tester) async {
171176
final overlayKey = GlobalKey();
172177

173178
await tester.pumpWidget(
174-
MaterialApp(
175-
home: Scaffold(
176-
body: Padding(
177-
padding: const EdgeInsets.only(left: 50.0, top: 100.0),
178-
child: Provider<HoverCardController>.value(
179-
value: HoverCardController(),
180-
child: Overlay(
181-
key: overlayKey,
182-
initialEntries: [
183-
OverlayEntry(
184-
builder: (context) => Align(
185-
alignment: Alignment.topLeft,
186-
child: HoverCardTooltip.sync(
187-
enabled: () => true,
188-
generateHoverCardData: (event) => HoverCardData(
189-
contents: const SizedBox(
190-
width: 200,
191-
height: 250,
192-
child: Text('Hover Content'),
193-
),
194-
),
195-
child: const Text('Hover Me Offset'),
179+
wrapSimple(
180+
Padding(
181+
padding: const EdgeInsets.only(left: 50.0, top: 100.0),
182+
child: Overlay(
183+
key: overlayKey,
184+
initialEntries: [
185+
OverlayEntry(
186+
builder: (context) => Align(
187+
alignment: Alignment.topLeft,
188+
child: HoverCardTooltip.sync(
189+
enabled: () => true,
190+
generateHoverCardData: (event) => HoverCardData(
191+
contents: const SizedBox(
192+
width: 200,
193+
height: 250,
194+
child: Text('Hover Content'),
196195
),
197196
),
197+
child: const Text('Hover Me Offset'),
198198
),
199-
],
199+
),
200200
),
201-
),
201+
],
202202
),
203203
),
204204
),
@@ -210,7 +210,7 @@ void main() {
210210

211211
final center = tester.getCenter(find.text('Hover Me Offset'));
212212
await gesture.moveTo(center);
213-
await tester.pump(const Duration(milliseconds: 500));
213+
await tester.pump(HoverCardTooltip.hoverDelay);
214214
await tester.pumpAndSettle();
215215

216216
final hoverContentFinder = find.text('Hover Content');

pubspec.lock

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -951,10 +951,10 @@ packages:
951951
dependency: transitive
952952
description:
953953
name: url_launcher_web
954-
sha256: d0412fcf4c6b31ecfdb7762359b7206ffba3bbffd396c6d9f9c4616ece476c1f
954+
sha256: "85c81589622fbc87c1c683aaea164d3604a7777495a79d91e39ffcdec39ddb34"
955955
url: "https://pub.dev"
956956
source: hosted
957-
version: "2.4.2"
957+
version: "2.4.3"
958958
url_launcher_windows:
959959
dependency: transitive
960960
description:

0 commit comments

Comments
 (0)