Add smoke tests for all the examples, fix 17 broken examples. (#89021)

This adds a smoke test for every single API example. It also fixes 17 tests that had bugs in them, or were otherwise broken, and even fixes one actual bug in the framework, and one limitation in the framework.

The bug in the framework is that NetworkImage's _loadAsync method had await response.drain<List<int>>();, but if the response is null, it will throw a cryptic exception saying that Null can't be assigned to List<int>. The fix was just to use await response.drain<void>(); instead.

The limitation is that RelativePositionedTransition takes an Animation<Rect> rect parameter, and if you want to use a RectTween with it, the value emitted there is Rect?, and one of the examples was just casting from Animation<Rect> to Animation<Rect?>, which is invalid, so I modified RelativePositionedTransition to take a Rect? and just use Rect.zero if the rect is null.
This commit is contained in:
Greg Spencer
2021-09-28 09:32:06 -07:00
committed by GitHub
parent d508a247be
commit ab2b0851a2
113 changed files with 908 additions and 343 deletions

View File

@@ -22,6 +22,7 @@
//****************************************************************************
//* ▼▼▼▼▼▼▼▼ code-imports ▼▼▼▼▼▼▼▼ (do not modify or remove section marker)
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter/widgets.dart';
@@ -33,15 +34,29 @@ import 'package:flutter/widgets.dart';
void main() {
WidgetsFlutterBinding.ensureInitialized();
print(measureWidget(const SizedBox(width: 640, height: 480)));
final Size size = measureWidget(const SizedBox(width: 640, height: 480));
// Just displays the size calculated above.
runApp(
WidgetsApp(
title: 'BuildOwner Sample',
color: const Color(0xff000000),
builder: (BuildContext context, Widget? child) {
return Scaffold(
body: Center(
child: Text(size.toString()),
),
);
},
),
);
}
Size measureWidget(Widget widget) {
final PipelineOwner pipelineOwner = PipelineOwner();
final MeasurementView rootView = pipelineOwner.rootNode = MeasurementView();
final BuildOwner buildOwner = BuildOwner(focusManager: FocusManager());
final RenderObjectToWidgetElement<RenderBox> element =
RenderObjectToWidgetAdapter<RenderBox>(
final RenderObjectToWidgetElement<RenderBox> element = RenderObjectToWidgetAdapter<RenderBox>(
container: rootView,
debugShortDescription: '[root]',
child: widget,
@@ -57,8 +72,7 @@ Size measureWidget(Widget widget) {
}
}
class MeasurementView extends RenderBox
with RenderObjectWithChildMixin<RenderBox> {
class MeasurementView extends RenderBox with RenderObjectWithChildMixin<RenderBox> {
@override
void performLayout() {
assert(child != null);

View File

@@ -12,45 +12,94 @@
//***************************************************************************
//* ▼▼▼▼▼▼▼▼ description ▼▼▼▼▼▼▼▼ (do not modify or remove section marker)
// This example shows how to override the standard error widget builder in release
// mode, but use the standard one in debug mode.
//
// The error occurs when you click the "Error Prone" button.
//* ▲▲▲▲▲▲▲▲ description ▲▲▲▲▲▲▲▲ (do not modify or remove section marker)
//***************************************************************************
//********************************************************************
//* ▼▼▼▼▼▼▼▼ code ▼▼▼▼▼▼▼▼ (do not modify or remove section marker)
//*************************************************************************
//* ▼▼▼▼▼▼▼▼ code-imports ▼▼▼▼▼▼▼▼ (do not modify or remove section marker)
import 'package:flutter/material.dart';
//* ▲▲▲▲▲▲▲▲ code-imports ▲▲▲▲▲▲▲▲ (do not modify or remove section marker)
//*************************************************************************
//********************************************************************
//* ▼▼▼▼▼▼▼▼ code ▼▼▼▼▼▼▼▼ (do not modify or remove section marker)
void main() {
// Set the ErrorWidget's builder before the app is started.
ErrorWidget.builder = (FlutterErrorDetails details) {
// This is how to tell if you're in debug mode: Assertions are only executed in
// debug mode.
bool inDebug = false;
assert(() {
inDebug = true;
return true;
}());
// In debug mode, use the normal error widget which shows
// the error message:
// If we're in debug mode, use the normal error widget which shows the error
// message:
if (inDebug) {
return ErrorWidget(details.exception);
}
// In release builds, show a yellow-on-blue message instead:
return Container(
alignment: Alignment.center,
child: const Text(
'Error!',
style: TextStyle(color: Colors.yellow),
child: Text(
'Error!\n${details.exception}',
style: const TextStyle(color: Colors.yellow),
textAlign: TextAlign.center,
textDirection: TextDirection.ltr,
),
);
};
// Here we would normally runApp() the root widget, but to demonstrate
// the error handling we artificially fail:
return runApp(Builder(
builder: (BuildContext context) {
throw 'oh no, an error';
},
));
// Start the app.
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({Key? key}) : super(key: key);
static const String _title = 'ErrorWidget Sample';
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
bool throwError = false;
@override
Widget build(BuildContext context) {
if (throwError) {
// Since the error widget is only used during a build, in this contrived example,
// we purposely throw an exception in a build function.
return Builder(
builder: (BuildContext context) {
throw Exception('oh no, an error');
},
);
} else {
return MaterialApp(
title: MyApp._title,
home: Scaffold(
appBar: AppBar(title: const Text(MyApp._title)),
body: Center(
child: TextButton(
onPressed: () {
setState(() { throwError = true; });
},
child: const Text('Error Prone')),
),
),
);
}
}
}
//* ▲▲▲▲▲▲▲▲ code ▲▲▲▲▲▲▲▲ (do not modify or remove section marker)