Skip to content

Commit

Permalink
fix: Bug on visibility tests
Browse files Browse the repository at this point in the history
this addresses the same issue #2464 as the previous commit but with a different solution
  • Loading branch information
FritzMatthaeus committed Dec 21, 2024
1 parent 6050c87 commit 2de033c
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 23 deletions.
12 changes: 2 additions & 10 deletions packages/patrol_finders/lib/src/custom_finders/patrol_finder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -404,13 +404,9 @@ class PatrolFinder implements MatchFinder {
///
/// Timeout is globally set by [PatrolTester.config.visibleTimeout]. If you
/// want to override this global setting, set [timeout].
///
/// Provide [alignment] to check if the widget is visible in a specific area
/// of the screen. It defaults to [Alignment.center].
Future<PatrolFinder> waitUntilVisible({
Duration? timeout,
bool enablePatrolLog = true,
Alignment alignment = Alignment.center,
}) =>
wrapWithPatrolLog(
action: 'waitUntilVisible',
Expand All @@ -419,7 +415,6 @@ class PatrolFinder implements MatchFinder {
this,
timeout: timeout,
enablePatrolLog: false,
alignment: alignment,
),
enablePatrolLog: enablePatrolLog,
);
Expand Down Expand Up @@ -515,11 +510,8 @@ class PatrolFinder implements MatchFinder {
String describeMatch(Plurality plurality) => finder.describeMatch(plurality);

/// Returns true if this finder finds at least 1 visible widget.
///
/// Provide [alignment] to check if the widget is visible in a specific area
/// of the screen. It defaults to [Alignment.center].
bool visible({Alignment alignment = Alignment.center}) {
final isVisible = hitTestable(at: alignment).evaluate().isNotEmpty;
bool get visible {
final isVisible = alignments.any((alignment) => hitTestable(at: alignment).evaluate().isNotEmpty);
if (isVisible == true) {
assert(
exists == true,
Expand Down
22 changes: 9 additions & 13 deletions packages/patrol_finders/lib/src/custom_finders/patrol_tester.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'package:flutter/foundation.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:patrol_finders/patrol_finders.dart';
import 'package:patrol_finders/src/custom_finders/utils.dart';
import 'package:patrol_log/patrol_log.dart';

/// Common configuration for [PatrolTester] and [PatrolFinder].
Expand Down Expand Up @@ -486,17 +487,10 @@ class PatrolTester {
///
/// Throws a [WaitUntilVisibleTimeoutException] if more time than specified by
/// the timeout passed and no widgets were found.
///
/// Timeout is globally set by [PatrolTester.config.visibleTimeout]. If you
/// want to override this global setting, set [timeout].
///
/// Provide [alignment] to check if the widget is visible in a specific area
/// of the screen. It defaults to [Alignment.center].
Future<PatrolFinder> waitUntilVisible(
Finder finder, {
Duration? timeout,
bool enablePatrolLog = true,
Alignment alignment = Alignment.center,
}) {
return TestAsyncUtils.guard(
() => wrapWithPatrolLog(
Expand All @@ -507,20 +501,22 @@ class PatrolTester {
function: () async {
final duration = timeout ?? config.visibleTimeout;
final end = tester.binding.clock.now().add(duration);
final hitTestableFinder = finder.hitTestable(at: alignment);
while (hitTestableFinder.evaluate().isEmpty) {
final hitTestableFinders = alignments.map((alignment) => finder.hitTestable(at: alignment));
final hitTestableEvaluations = hitTestableFinders.map((finder) => finder.evaluate());
while (hitTestableEvaluations.map((result) => result.isNotEmpty).firstOrNull == null) {
final now = tester.binding.clock.now();
if (now.isAfter(end)) {
throw WaitUntilVisibleTimeoutException(
finder: finder,
duration: duration,
);
}

}
await tester.pump(const Duration(milliseconds: 100));
}

return PatrolFinder(finder: hitTestableFinder, tester: this);
return PatrolFinder(
finder: hitTestableFinders.firstWhere((finder) => finder.evaluate().isNotEmpty),
tester: this,
);
},
),
);
Expand Down
15 changes: 15 additions & 0 deletions packages/patrol_finders/lib/src/custom_finders/utils.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'package:flutter/material.dart';

/// Makes it possible to retrieve a name that this [Symbol] was created with.
extension SymbolName on Symbol {
/// Returns the name that this [Symbol] was created with.
Expand All @@ -9,3 +11,16 @@ extension SymbolName on Symbol {
return symbol.substring(8, symbol.length - 2);
}
}

/// List of all [Alignment] values.
const alignments = [
Alignment.center,
Alignment.bottomCenter,
Alignment.bottomLeft,
Alignment.bottomRight,
Alignment.centerLeft,
Alignment.centerRight,
Alignment.topCenter,
Alignment.topLeft,
Alignment.topRight,
];

0 comments on commit 2de033c

Please sign in to comment.