Skip to content

Commit

Permalink
Format
Browse files Browse the repository at this point in the history
  • Loading branch information
Amir-P committed Nov 30, 2023
1 parent f825631 commit 4a53fbd
Showing 1 changed file with 67 additions and 40 deletions.
107 changes: 67 additions & 40 deletions packages/fleather/test/rendering/rendering_tools.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,22 @@ import 'package:flutter/gestures.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter/scheduler.dart';
import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart' show EnginePhase, TestDefaultBinaryMessengerBinding, fail;
import 'package:flutter_test/flutter_test.dart'
show EnginePhase, TestDefaultBinaryMessengerBinding, fail;

export 'package:flutter/foundation.dart' show FlutterError, FlutterErrorDetails;
export 'package:flutter_test/flutter_test.dart' show EnginePhase, TestDefaultBinaryMessengerBinding;

class TestRenderingFlutterBinding extends BindingBase with SchedulerBinding, ServicesBinding, GestureBinding, PaintingBinding, SemanticsBinding, RendererBinding, TestDefaultBinaryMessengerBinding {
export 'package:flutter_test/flutter_test.dart'
show EnginePhase, TestDefaultBinaryMessengerBinding;

class TestRenderingFlutterBinding extends BindingBase
with
SchedulerBinding,
ServicesBinding,
GestureBinding,
PaintingBinding,
SemanticsBinding,
RendererBinding,
TestDefaultBinaryMessengerBinding {
/// Creates a binding for testing rendering library functionality.
///
/// If [onErrors] is not null, it is called if [FlutterError] caught any errors
Expand All @@ -25,10 +35,11 @@ class TestRenderingFlutterBinding extends BindingBase with SchedulerBinding, Ser
///
/// Errors caught between frames will cause the test to fail unless
/// [FlutterError.onError] has been overridden.
TestRenderingFlutterBinding({ this.onErrors }) {
TestRenderingFlutterBinding({this.onErrors}) {
FlutterError.onError = (FlutterErrorDetails details) {
FlutterError.dumpErrorToConsole(details);
Zone.current.parent!.handleUncaughtError(details.exception, details.stack!);
Zone.current.parent!
.handleUncaughtError(details.exception, details.stack!);
};
}

Expand All @@ -37,7 +48,8 @@ class TestRenderingFlutterBinding extends BindingBase with SchedulerBinding, Ser
/// Provides access to the features exposed by this binding. The binding must
/// be initialized before using this getter; this is typically done by calling
/// [TestRenderingFlutterBinding.ensureInitialized].
static TestRenderingFlutterBinding get instance => BindingBase.checkInstance(_instance);
static TestRenderingFlutterBinding get instance =>
BindingBase.checkInstance(_instance);
static TestRenderingFlutterBinding? _instance;

@override
Expand Down Expand Up @@ -87,7 +99,8 @@ class TestRenderingFlutterBinding extends BindingBase with SchedulerBinding, Ser
/// Creates and initializes the binding. This function is
/// idempotent; calling it a second time will just return the
/// previously-created instance.
static TestRenderingFlutterBinding ensureInitialized({ VoidCallback? onErrors }) {
static TestRenderingFlutterBinding ensureInitialized(
{VoidCallback? onErrors}) {
if (_instance != null) {
return _instance!;
}
Expand Down Expand Up @@ -162,19 +175,22 @@ class TestRenderingFlutterBinding extends BindingBase with SchedulerBinding, Ser
onErrors!();
if (_errors.isNotEmpty) {
_errors.forEach(FlutterError.dumpErrorToConsole);
fail('There are more errors than the test inspected using TestRenderingFlutterBinding.takeFlutterErrorDetails.');
fail(
'There are more errors than the test inspected using TestRenderingFlutterBinding.takeFlutterErrorDetails.');
}
} else {
_errors.forEach(FlutterError.dumpErrorToConsole);
fail('Caught error while rendering frame. See preceding logs for details.');
fail(
'Caught error while rendering frame. See preceding logs for details.');
}
}
}
}

@override
void drawFrame() {
assert(phase != EnginePhase.build, 'rendering_tester does not support testing the build phase; use flutter_test instead');
assert(phase != EnginePhase.build,
'rendering_tester does not support testing the build phase; use flutter_test instead');
final FlutterExceptionHandler? oldErrorHandler = FlutterError.onError;
FlutterError.onError = _errors.add;
try {
Expand All @@ -200,19 +216,22 @@ class TestRenderingFlutterBinding extends BindingBase with SchedulerBinding, Ser
if (phase == EnginePhase.flushSemantics) {
return;
}
assert(phase == EnginePhase.flushSemantics || phase == EnginePhase.sendSemanticsUpdate);
assert(phase == EnginePhase.flushSemantics ||
phase == EnginePhase.sendSemanticsUpdate);
} finally {
FlutterError.onError = oldErrorHandler;
if (_errors.isNotEmpty) {
if (onErrors != null) {
onErrors!();
if (_errors.isNotEmpty) {
_errors.forEach(FlutterError.dumpErrorToConsole);
fail('There are more errors than the test inspected using TestRenderingFlutterBinding.takeFlutterErrorDetails.');
fail(
'There are more errors than the test inspected using TestRenderingFlutterBinding.takeFlutterErrorDetails.');
}
} else {
_errors.forEach(FlutterError.dumpErrorToConsole);
fail('Caught error while rendering frame. See preceding logs for details.');
fail(
'Caught error while rendering frame. See preceding logs for details.');
}
}
}
Expand All @@ -234,13 +253,15 @@ class TestRenderingFlutterBinding extends BindingBase with SchedulerBinding, Ser
///
/// If `onErrors` is not null, it is set as [TestRenderingFlutterBinding.onError].
void layout(
RenderBox box, { // If you want to just repump the last box, call pumpFrame().
RenderBox box, {
// If you want to just repump the last box, call pumpFrame().
BoxConstraints? constraints,
Alignment alignment = Alignment.center,
EnginePhase phase = EnginePhase.layout,
VoidCallback? onErrors,
}) {
assert(box.parent == null); // We stick the box in another, so you can't reuse it easily, sorry.
assert(box.parent ==
null); // We stick the box in another, so you can't reuse it easily, sorry.

TestRenderingFlutterBinding.instance.renderView.child = null;
if (constraints != null) {
Expand All @@ -260,8 +281,10 @@ void layout(
/// Pumps a single frame.
///
/// If `onErrors` is not null, it is set as [TestRenderingFlutterBinding.onError].
void pumpFrame({ EnginePhase phase = EnginePhase.layout, VoidCallback? onErrors }) {
assert(TestRenderingFlutterBinding.instance.renderView.child != null); // call layout() first!
void pumpFrame(
{EnginePhase phase = EnginePhase.layout, VoidCallback? onErrors}) {
assert(TestRenderingFlutterBinding.instance.renderView.child !=
null); // call layout() first!

if (onErrors != null) {
TestRenderingFlutterBinding.instance.onErrors = onErrors;
Expand All @@ -272,7 +295,7 @@ void pumpFrame({ EnginePhase phase = EnginePhase.layout, VoidCallback? onErrors
}

class TestCallbackPainter extends CustomPainter {
const TestCallbackPainter({ required this.onPaint });
const TestCallbackPainter({required this.onPaint});

final VoidCallback onPaint;

Expand Down Expand Up @@ -319,15 +342,15 @@ class RenderSizedBox extends RenderBox {
}

@override
void performLayout() { }
void performLayout() {}

@override
bool hitTestSelf(Offset position) => true;
}

class FakeTickerProvider implements TickerProvider {
@override
Ticker createTicker(TickerCallback onTick, [ bool disableAnimations = false ]) {
Ticker createTicker(TickerCallback onTick, [bool disableAnimations = false]) {
return FakeTicker();
}
}
Expand All @@ -337,7 +360,7 @@ class FakeTicker implements Ticker {
bool muted = false;

@override
void absorbTicker(Ticker originalTicker) { }
void absorbTicker(Ticker originalTicker) {}

@override
String? get debugLabel => null;
Expand All @@ -355,35 +378,37 @@ class FakeTicker implements Ticker {
bool get shouldScheduleTick => throw UnimplementedError();

@override
void dispose() { }
void dispose() {}

@override
void scheduleTick({ bool rescheduling = false }) { }
void scheduleTick({bool rescheduling = false}) {}

@override
TickerFuture start() {
throw UnimplementedError();
}

@override
void stop({ bool canceled = false }) { }
void stop({bool canceled = false}) {}

@override
void unscheduleTick() { }
void unscheduleTick() {}

@override
String toString({ bool debugIncludeStack = false }) => super.toString();
String toString({bool debugIncludeStack = false}) => super.toString();

@override
DiagnosticsNode describeForError(String name) {
return DiagnosticsProperty<Ticker>(name, this, style: DiagnosticsTreeStyle.errorProperty);
return DiagnosticsProperty<Ticker>(name, this,
style: DiagnosticsTreeStyle.errorProperty);
}
}

class TestClipPaintingContext extends PaintingContext {
TestClipPaintingContext() : this._(ContainerLayer());

TestClipPaintingContext._(this._containerLayer) : super(_containerLayer, Rect.zero);
TestClipPaintingContext._(this._containerLayer)
: super(_containerLayer, Rect.zero);

final ContainerLayer _containerLayer;

Expand Down Expand Up @@ -415,20 +440,20 @@ class TestPushLayerPaintingContext extends PaintingContext {

@override
void pushLayer(
ContainerLayer childLayer,
PaintingContextCallback painter,
Offset offset, {
Rect? childPaintBounds
}) {
ContainerLayer childLayer, PaintingContextCallback painter, Offset offset,
{Rect? childPaintBounds}) {
pushedLayers.add(childLayer);
super.pushLayer(childLayer, painter, offset, childPaintBounds: childPaintBounds);
super.pushLayer(childLayer, painter, offset,
childPaintBounds: childPaintBounds);
}
}

// Absorbs errors that don't have "overflowed" in their error details.
void absorbOverflowedErrors() {
final Iterable<FlutterErrorDetails> errorDetails = TestRenderingFlutterBinding.instance.takeAllFlutterErrorDetails();
final Iterable<FlutterErrorDetails> filtered = errorDetails.where((FlutterErrorDetails details) {
final Iterable<FlutterErrorDetails> errorDetails =
TestRenderingFlutterBinding.instance.takeAllFlutterErrorDetails();
final Iterable<FlutterErrorDetails> filtered =
errorDetails.where((FlutterErrorDetails details) {
return !details.toString().contains('overflowed');
});
if (filtered.isNotEmpty) {
Expand All @@ -438,9 +463,11 @@ void absorbOverflowedErrors() {

// Reports any FlutterErrors.
void expectNoFlutterErrors() {
final Iterable<FlutterErrorDetails> errorDetails = TestRenderingFlutterBinding.instance.takeAllFlutterErrorDetails();
final Iterable<FlutterErrorDetails> errorDetails =
TestRenderingFlutterBinding.instance.takeAllFlutterErrorDetails();
errorDetails.forEach(FlutterError.reportError);
}

RenderConstrainedBox get box200x200 =>
RenderConstrainedBox(additionalConstraints: const BoxConstraints.tightFor(height: 200.0, width: 200.0));
RenderConstrainedBox get box200x200 => RenderConstrainedBox(
additionalConstraints:
const BoxConstraints.tightFor(height: 200.0, width: 200.0));

0 comments on commit 4a53fbd

Please sign in to comment.