Skip to content

Commit

Permalink
Improve test coverage (flutter#7430)
Browse files Browse the repository at this point in the history
Also, make the exception handling for global key listeners slightly more
robust.
  • Loading branch information
abarth authored Jan 11, 2017
1 parent 42ccffc commit 2ee04c9
Show file tree
Hide file tree
Showing 18 changed files with 463 additions and 65 deletions.
16 changes: 9 additions & 7 deletions packages/flutter/lib/src/widgets/framework.dart
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ abstract class GlobalKey<T extends State<StatefulWidget>> extends Key {
void _register(Element element) {
assert(() {
if (_registry.containsKey(this)) {
int oldCount = _debugDuplicates.putIfAbsent(this, () => 1);
final int oldCount = _debugDuplicates.putIfAbsent(this, () => 1);
assert(oldCount >= 1);
_debugDuplicates[this] = oldCount + 1;
}
Expand All @@ -163,7 +163,7 @@ abstract class GlobalKey<T extends State<StatefulWidget>> extends Key {
void _unregister(Element element) {
assert(() {
if (_registry.containsKey(this) && _debugDuplicates.containsKey(this)) {
int oldCount = _debugDuplicates[this];
final int oldCount = _debugDuplicates[this];
assert(oldCount >= 2);
if (oldCount == 2) {
_debugDuplicates.remove(this);
Expand Down Expand Up @@ -257,17 +257,19 @@ abstract class GlobalKey<T extends State<StatefulWidget>> extends Key {
for (GlobalKey key in _removedKeys) {
if (!_registry.containsKey(key) && _removeListeners.containsKey(key)) {
Set<GlobalKeyRemoveListener> localListeners = new HashSet<GlobalKeyRemoveListener>.from(_removeListeners[key]);
for (GlobalKeyRemoveListener listener in localListeners)
listener(key);
for (GlobalKeyRemoveListener listener in localListeners) {
try {
listener(key);
} catch (e, stack) {
_debugReportException('while notifying GlobalKey listener', e, stack);
}
}
}
}
} catch (e, stack) {
_debugReportException('while notifying GlobalKey listeners', e, stack);
} finally {
_removedKeys.clear();
}
}

}

/// A global key with a debugging label.
Expand Down
2 changes: 0 additions & 2 deletions packages/flutter/lib/src/widgets/gesture_detector.dart
Original file line number Diff line number Diff line change
Expand Up @@ -525,7 +525,6 @@ class _GestureSemantics extends SingleChildRenderObjectWidget {
return;
}
}
assert(false);
}

void _handleVerticalDragUpdate(DragUpdateDetails updateDetails) {
Expand Down Expand Up @@ -553,7 +552,6 @@ class _GestureSemantics extends SingleChildRenderObjectWidget {
return;
}
}
assert(false);
}

@override
Expand Down
14 changes: 14 additions & 0 deletions packages/flutter/lib/src/widgets/media_query.dart
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,20 @@ class MediaQueryData {
return size.width > size.height ? Orientation.landscape : Orientation.portrait;
}

MediaQueryData copyWith({
Size size,
double devicePixelRatio,
double textScaleFactor,
EdgeInsets padding,
}) {
return new MediaQueryData(
size: size ?? this.size,
devicePixelRatio: devicePixelRatio ?? this.devicePixelRatio,
textScaleFactor: textScaleFactor ?? this.textScaleFactor,
padding: padding ?? this.padding,
);
}

@override
bool operator ==(Object other) {
if (other.runtimeType != runtimeType)
Expand Down
10 changes: 4 additions & 6 deletions packages/flutter/test/animation/animation_controller_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import 'package:flutter_test/flutter_test.dart';
import 'package:flutter/animation.dart';
import 'package:flutter/widgets.dart';

import 'animation_tester.dart';

void main() {
setUp(() {
WidgetsFlutterBinding.ensureInitialized();
Expand Down Expand Up @@ -206,17 +204,17 @@ void main() {
duration: const Duration(milliseconds: 100),
vsync: const TestVSync(),
);
expect(controller.toString(), hasOneLineDescription);
expect(controller, hasOneLineDescription);
controller.forward();
WidgetsBinding.instance.handleBeginFrame(const Duration(milliseconds: 10));
WidgetsBinding.instance.handleBeginFrame(const Duration(milliseconds: 20));
expect(controller.toString(), hasOneLineDescription);
expect(controller, hasOneLineDescription);
WidgetsBinding.instance.handleBeginFrame(const Duration(milliseconds: 30));
expect(controller.toString(), hasOneLineDescription);
expect(controller, hasOneLineDescription);
controller.reverse();
WidgetsBinding.instance.handleBeginFrame(const Duration(milliseconds: 40));
WidgetsBinding.instance.handleBeginFrame(const Duration(milliseconds: 50));
expect(controller.toString(), hasOneLineDescription);
expect(controller, hasOneLineDescription);
controller.stop();
});

Expand Down
24 changes: 11 additions & 13 deletions packages/flutter/test/animation/animations_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,23 @@ import 'package:flutter_test/flutter_test.dart';
import 'package:flutter/animation.dart';
import 'package:flutter/widgets.dart';

import 'animation_tester.dart';

void main() {
setUp(() {
WidgetsFlutterBinding.ensureInitialized();
WidgetsBinding.instance.resetEpoch();
});

test('toString control test', () {
expect(kAlwaysCompleteAnimation.toString(), hasOneLineDescription);
expect(kAlwaysDismissedAnimation.toString(), hasOneLineDescription);
expect(const AlwaysStoppedAnimation<double>(0.5).toString(), hasOneLineDescription);
expect(kAlwaysCompleteAnimation, hasOneLineDescription);
expect(kAlwaysDismissedAnimation, hasOneLineDescription);
expect(const AlwaysStoppedAnimation<double>(0.5), hasOneLineDescription);
CurvedAnimation curvedAnimation = new CurvedAnimation(
parent: kAlwaysDismissedAnimation,
curve: Curves.ease
);
expect(curvedAnimation.toString(), hasOneLineDescription);
expect(curvedAnimation, hasOneLineDescription);
curvedAnimation.reverseCurve = Curves.elasticOut;
expect(curvedAnimation.toString(), hasOneLineDescription);
expect(curvedAnimation, hasOneLineDescription);
AnimationController controller = new AnimationController(
duration: const Duration(milliseconds: 500),
vsync: const TestVSync(),
Expand All @@ -37,17 +35,17 @@ void main() {
curve: Curves.ease,
reverseCurve: Curves.elasticOut
);
expect(curvedAnimation.toString(), hasOneLineDescription);
expect(curvedAnimation, hasOneLineDescription);
controller.stop();
});

test('ProxyAnimation.toString control test', () {
ProxyAnimation animation = new ProxyAnimation();
expect(animation.value, 0.0);
expect(animation.status, AnimationStatus.dismissed);
expect(animation.toString(), hasOneLineDescription);
expect(animation, hasOneLineDescription);
animation.parent = kAlwaysDismissedAnimation;
expect(animation.toString(), hasOneLineDescription);
expect(animation, hasOneLineDescription);
});

test('ProxyAnimation set parent generates value changed', () {
Expand Down Expand Up @@ -88,7 +86,7 @@ void main() {
expect(didReceiveCallback, isFalse);
controller.value = 0.7;
expect(didReceiveCallback, isFalse);
expect(animation.toString(), hasOneLineDescription);
expect(animation, hasOneLineDescription);
});

test('TrainHoppingAnimation', () {
Expand All @@ -107,11 +105,11 @@ void main() {
});
expect(didSwitchTrains, isFalse);
expect(animation.value, 0.5);
expect(animation.toString(), hasOneLineDescription);
expect(animation, hasOneLineDescription);
nextTrain.value = 0.25;
expect(didSwitchTrains, isTrue);
expect(animation.value, 0.25);
expect(animation.toString(), hasOneLineDescription);
expect(animation, hasOneLineDescription);
expect(animation.toString(), contains('no next'));
});
}
2 changes: 0 additions & 2 deletions packages/flutter/test/animation/tween_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import 'package:flutter_test/flutter_test.dart';
import 'package:flutter/animation.dart';
import 'package:flutter/widgets.dart';

import 'animation_tester.dart';

void main() {
test('Can chain tweens', () {
Tween<double> tween = new Tween<double>(begin: 0.30, end: 0.50);
Expand Down
2 changes: 1 addition & 1 deletion packages/flutter/test/painting/colors_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ void main() {
test('HSVColor control test', () {
const HSVColor color = const HSVColor.fromAHSV(0.7, 28.0, 0.3, 0.6);

expect(color.toString(), hasOneLineDescription);
expect(color, hasOneLineDescription);
expect(color.hashCode, equals(new HSVColor.fromAHSV(0.7, 28.0, 0.3, 0.6).hashCode));

expect(color.withAlpha(0.8), const HSVColor.fromAHSV(0.8, 28.0, 0.3, 0.6));
Expand Down
2 changes: 1 addition & 1 deletion packages/flutter/test/painting/edge_insets_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ void main() {
test('EdgeInsets control test', () {
const EdgeInsets insets = const EdgeInsets.fromLTRB(5.0, 7.0, 11.0, 13.0);

expect(insets.toString(), hasOneLineDescription);
expect(insets, hasOneLineDescription);
expect(insets.hashCode, equals(new EdgeInsets.fromLTRB(5.0, 7.0, 11.0, 13.0).hashCode));

expect(insets.topLeft, const Offset(5.0, 7.0));
Expand Down
81 changes: 81 additions & 0 deletions packages/flutter/test/widgets/framework_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'package:flutter_test/flutter_test.dart';
import 'package:flutter/widgets.dart';

void main() {
testWidgets('UniqueKey control test', (WidgetTester tester) async {
Key key = new UniqueKey();
expect(key, hasOneLineDescription);
expect(key, isNot(equals(new UniqueKey())));
});

testWidgets('ObjectKey control test', (WidgetTester tester) async {
Object a = new Object();
Object b = new Object();
Key keyA = new ObjectKey(a);
Key keyA2 = new ObjectKey(a);
Key keyB = new ObjectKey(b);

expect(keyA, hasOneLineDescription);
expect(keyA, equals(keyA2));
expect(keyA.hashCode, equals(keyA2.hashCode));
expect(keyA, isNot(equals(keyB)));
});

testWidgets('GlobalKey duplication', (WidgetTester tester) async {
Key key = new GlobalKey(debugLabel: 'problematic');

await tester.pumpWidget(new Stack(
children: <Widget>[
new Container(
key: const ValueKey<int>(1),
),
new Container(
key: const ValueKey<int>(2),
),
new Container(
key: key
),
],
));

await tester.pumpWidget(new Stack(
children: <Widget>[
new Container(
key: const ValueKey<int>(1),
child: new SizedBox(key: key),
),
new Container(
key: const ValueKey<int>(2),
child: new Placeholder(key: key),
),
],
));

expect(tester.takeException(), isNotNull);
});

testWidgets('GlobalKey notification exception handling', (WidgetTester tester) async {
GlobalKey key = new GlobalKey();

await tester.pumpWidget(new Container(key: key));

GlobalKey.registerRemoveListener(key, (GlobalKey key) {
throw new Exception('Misbehaving listener');
});

bool didReceiveCallback = false;
GlobalKey.registerRemoveListener(key, (GlobalKey key) {
expect(didReceiveCallback, isFalse);
didReceiveCallback = true;
});

await tester.pumpWidget(new Placeholder());

expect(tester.takeException(), isNotNull);
expect(didReceiveCallback, isTrue);
});
}
5 changes: 4 additions & 1 deletion packages/flutter/test/widgets/media_query_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ void main() {
await tester.pumpWidget(
new Builder(
builder: (BuildContext context) {
size = MediaQuery.of(context).size;
final MediaQueryData data = MediaQuery.of(context);
expect(data, hasOneLineDescription);
expect(data.hashCode, equals(data.copyWith().hashCode));
size = data.size;
return new Container();
}
)
Expand Down
13 changes: 13 additions & 0 deletions packages/flutter/test/widgets/performance_overlay_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'package:flutter_test/flutter_test.dart';
import 'package:flutter/widgets.dart';

void main() {
testWidgets('Performance overlay smoke test', (WidgetTester tester) async {
await tester.pumpWidget(new PerformanceOverlay());
await tester.pumpWidget(new PerformanceOverlay.allEnabled());
});
}
22 changes: 22 additions & 0 deletions packages/flutter/test/widgets/placeholder_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'package:flutter_test/flutter_test.dart';
import 'package:flutter/widgets.dart';

void main() {
testWidgets('Placeholder control test', (WidgetTester tester) async {
GlobalKey<PlaceholderState> key = new GlobalKey<PlaceholderState>();

await tester.pumpWidget(new Placeholder(key: key));

key.currentState.child = new Text('target');

expect(find.text('target'), findsNothing);

await tester.pump();

expect(find.text('target'), findsOneWidget);
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ void sendFakeKeyEvent(Map<String, dynamic> data) {

void main() {
testWidgets('Can dispose without keyboard', (WidgetTester tester) async {
await tester.pumpWidget(new RawKeyboardListener(child: new Container()));
await tester.pumpWidget(new RawKeyboardListener(child: new Container()));
await tester.pumpWidget(new Container());
});
Expand Down
6 changes: 6 additions & 0 deletions packages/flutter/test/widgets/routes_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,11 @@ Future<Null> runNavigatorTest(
}

void main() {
testWidgets('Route settings', (WidgetTester tester) async {
RouteSettings settings = const RouteSettings(name: 'A');
expect(settings, hasOneLineDescription);
});

testWidgets('Route management - push, replace, pop', (WidgetTester tester) async {
GlobalKey<NavigatorState> navigatorKey = new GlobalKey<NavigatorState>();
await tester.pumpWidget(new Navigator(
Expand Down Expand Up @@ -320,6 +325,7 @@ void main() {
'B: didChangeNext C',
]
);
expect(routeC.isActive, isTrue);
TestRoute routeB;
await runNavigatorTest(
tester,
Expand Down
Loading

0 comments on commit 2ee04c9

Please sign in to comment.