mirror of
https://github.com/flutter/flutter.git
synced 2025-06-03 00:51:18 +00:00

This pull request aims to improve code readability, based on feedback gathered in a recent design doc. <br> There are two factors that hugely impact how easy it is to understand a piece of code: **verbosity** and **complexity**. Reducing **verbosity** is important, because boilerplate makes a project more difficult to navigate. It also has a tendency to make one's eyes gloss over, and subtle typos/bugs become more likely to slip through. Reducing **complexity** makes the code more accessible to more people. This is especially important for open-source projects like Flutter, where the code is read by those who make contributions, as well as others who read through source code as they debug their own projects. <hr> <br> The following examples show how pattern-matching might affect these two factors: <details> <summary><h3>Example 1 (GOOD)</h3> [click to expand]</summary> ```dart if (ancestor case InheritedElement(:final InheritedTheme widget)) { themes.add(widget); } ``` Without using patterns, this might expand to ```dart if (ancestor is InheritedElement) { final InheritedWidget widget = ancestor.widget; if (widget is InheritedTheme) { themes.add(widget); } } ``` Had `ancestor` been a non-local variable, it would need to be "converted" as well: ```dart final Element ancestor = this.ancestor; if (ancestor is InheritedElement) { final InheritedWidget inheritedWidget = ancestor.widget; if (widget is InheritedTheme) { themes.add(theme); } } ``` </details> <details> <summary><h3>Example 2 (BAD) </h3> [click to expand]</summary> ```dart if (widget case PreferredSizeWidget(preferredSize: Size(:final double height))) { return height; } ``` Assuming `widget` is a non-local variable, this would expand to: ```dart final Widget widget = this.widget; if (widget is PreferredSizeWidget) { return widget.preferredSize.height; } ``` <br> </details> In both of the examples above, an `if-case` statement simultaneously verifies that an object meets the specified criteria and performs a variable assignment accordingly. But there are some differences: Example 2 uses a more deeply-nested pattern than Example 1 but makes fewer useful checks. **Example 1:** - checks that `ancestor` is an `InheritedElement` - checks that the inherited element's `widget` is an `InheritedTheme` **Example 2:** - checks that `widget` is a `PreferredSizeWidget` (every `PreferredSizeWidget` has a `size` field, and every `Size` has a `height` field) <br> <hr> I feel hesitant to try presenting a set of cut-and-dry rules as to which scenarios should/shouldn't use pattern-matching, since there are an abundance of different types of patterns, and an abundance of different places where they might be used. But hopefully the conversations we've had recently will help us converge toward a common intuition of how pattern-matching can best be utilized for improved readability. <br><br> - resolves https://github.com/flutter/flutter/issues/152313 - Design Doc: [flutter.dev/go/dart-patterns](https://flutter.dev/go/dart-patterns)
150 lines
5.8 KiB
Dart
150 lines
5.8 KiB
Dart
// Copyright 2014 The Flutter Authors. All rights reserved.
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
// found in the LICENSE file.
|
|
|
|
// ATTENTION!
|
|
//
|
|
// This file is not named "*_test.dart", and as such will not run when you run
|
|
// "flutter test". It is only intended to be run as part of the
|
|
// flutter_gallery_instrumentation_test devicelab test.
|
|
|
|
import 'dart:async';
|
|
|
|
import 'package:flutter/cupertino.dart';
|
|
import 'package:flutter/gestures.dart' show PointerDeviceKind, kPrimaryButton;
|
|
import 'package:flutter/material.dart';
|
|
import 'package:flutter/scheduler.dart';
|
|
import 'package:flutter/services.dart';
|
|
import 'package:flutter_gallery/gallery/app.dart' show GalleryApp;
|
|
import 'package:flutter_gallery/gallery/demos.dart';
|
|
import 'package:flutter_test/flutter_test.dart';
|
|
|
|
// Reports success or failure to the native code.
|
|
const MethodChannel _kTestChannel = MethodChannel('io.flutter.demo.gallery/TestLifecycleListener');
|
|
|
|
// We don't want to wait for animations to complete before tapping the
|
|
// back button in the demos with these titles.
|
|
const List<String> _kUnsynchronizedDemoTitles = <String>[
|
|
'Progress indicators',
|
|
'Activity Indicator',
|
|
'Video',
|
|
];
|
|
|
|
// These demos can't be backed out of by tapping a button whose
|
|
// tooltip is 'Back'.
|
|
const List<String> _kSkippedDemoTitles = <String>[
|
|
'Progress indicators',
|
|
'Activity Indicator',
|
|
'Video',
|
|
];
|
|
|
|
// There are 3 places where the Gallery demos are traversed.
|
|
// 1- In widget tests such as dev/integration_tests/flutter_gallery/test/smoke_test.dart
|
|
// 2- In driver tests such as dev/integration_tests/flutter_gallery/test_driver/transitions_perf_test.dart
|
|
// 3- In on-device instrumentation tests such as dev/integration_tests/flutter_gallery/test/live_smoketest.dart
|
|
//
|
|
// If you change navigation behavior in the Gallery or in the framework, make
|
|
// sure all 3 are covered.
|
|
|
|
Future<void> main() async {
|
|
try {
|
|
// Verify that _kUnsynchronizedDemos and _kSkippedDemos identify
|
|
// demos that actually exist.
|
|
final List<String> allDemoTitles = kAllGalleryDemos.map((GalleryDemo demo) => demo.title).toList();
|
|
if (!Set<String>.from(allDemoTitles).containsAll(_kUnsynchronizedDemoTitles)) {
|
|
fail('Unrecognized demo titles in _kUnsynchronizedDemosTitles: $_kUnsynchronizedDemoTitles');
|
|
}
|
|
if (!Set<String>.from(allDemoTitles).containsAll(_kSkippedDemoTitles)) {
|
|
fail('Unrecognized demo names in _kSkippedDemoTitles: $_kSkippedDemoTitles');
|
|
}
|
|
|
|
print('Starting app...');
|
|
runApp(const GalleryApp(testMode: true));
|
|
final _LiveWidgetController controller = _LiveWidgetController(WidgetsBinding.instance);
|
|
for (final GalleryDemoCategory category in kAllGalleryDemoCategories) {
|
|
print('Tapping "${category.name}" section...');
|
|
await controller.tap(find.text(category.name));
|
|
for (final GalleryDemo demo in kGalleryCategoryToDemos[category]!) {
|
|
final Finder demoItem = find.text(demo.title);
|
|
print('Scrolling to "${demo.title}"...');
|
|
await controller.scrollIntoView(demoItem, alignment: 0.5);
|
|
if (_kSkippedDemoTitles.contains(demo.title)) {
|
|
continue;
|
|
}
|
|
for (int i = 0; i < 2; i += 1) {
|
|
print('Tapping "${demo.title}"...');
|
|
await controller.tap(demoItem); // Launch the demo
|
|
controller.frameSync = !_kUnsynchronizedDemoTitles.contains(demo.title);
|
|
print('Going back to demo list...');
|
|
await controller.tap(backFinder);
|
|
controller.frameSync = true;
|
|
}
|
|
}
|
|
print('Going back to home screen...');
|
|
await controller.tap(find.byTooltip('Back'));
|
|
}
|
|
print('Finished successfully!');
|
|
_kTestChannel.invokeMethod<void>('success');
|
|
} catch (error, stack) {
|
|
print('Caught error: $error\n$stack');
|
|
_kTestChannel.invokeMethod<void>('failure');
|
|
}
|
|
}
|
|
|
|
final Finder backFinder = find.byElementPredicate(
|
|
(Element element) => switch (element.widget) {
|
|
Tooltip(message: 'Back') => true,
|
|
CupertinoNavigationBarBackButton() => true,
|
|
_ => false,
|
|
},
|
|
description: 'Material or Cupertino back button',
|
|
);
|
|
|
|
class _LiveWidgetController extends LiveWidgetController {
|
|
_LiveWidgetController(super.binding);
|
|
|
|
/// With [frameSync] enabled, Flutter Driver will wait to perform an action
|
|
/// until there are no pending frames in the app under test.
|
|
bool frameSync = true;
|
|
|
|
/// Waits until at the end of a frame the provided [condition] is [true].
|
|
Future<void> _waitUntilFrame(bool Function() condition, [Completer<void>? completer]) {
|
|
completer ??= Completer<void>();
|
|
if (!condition()) {
|
|
SchedulerBinding.instance.addPostFrameCallback((Duration timestamp) {
|
|
_waitUntilFrame(condition, completer);
|
|
});
|
|
} else {
|
|
completer.complete();
|
|
}
|
|
return completer.future;
|
|
}
|
|
|
|
/// Runs `finder` repeatedly until it finds one or more [Element]s.
|
|
Future<FinderBase<Element>> _waitForElement(FinderBase<Element> finder) async {
|
|
if (frameSync) {
|
|
await _waitUntilFrame(() => binding.transientCallbackCount == 0);
|
|
}
|
|
await _waitUntilFrame(() => finder.tryEvaluate());
|
|
if (frameSync) {
|
|
await _waitUntilFrame(() => binding.transientCallbackCount == 0);
|
|
}
|
|
return finder;
|
|
}
|
|
|
|
Future<void> scrollIntoView(FinderBase<Element> finder, {required double alignment}) async {
|
|
final FinderBase<Element> target = await _waitForElement(finder);
|
|
await Scrollable.ensureVisible(target.evaluate().single, duration: const Duration(milliseconds: 100), alignment: alignment);
|
|
}
|
|
|
|
@override
|
|
Future<void> tap(FinderBase<Element> finder, {
|
|
int? pointer,
|
|
int buttons = kPrimaryButton,
|
|
bool warnIfMissed = true,
|
|
PointerDeviceKind kind = PointerDeviceKind.touch,
|
|
}) async {
|
|
await super.tap(await _waitForElement(finder), pointer: pointer, buttons: buttons, warnIfMissed: warnIfMissed, kind: kind);
|
|
}
|
|
}
|