Commit 5a2bf184 authored by Adam Barth's avatar Adam Barth Committed by GitHub

Improve test coverage and fix bugs (#7410)

This patch improves test coverage for a number of files that had low
coverage. This patch also fixes a few minor bugs found by these tests.
parent 3c7122a7
......@@ -289,9 +289,30 @@ class PointerEventConverter {
tilt: datum.tilt
);
}
_pointers.remove(datum.device);
break;
case ui.PointerChange.remove:
assert(_pointers.containsKey(datum.device));
_PointerState state = _pointers[datum.device];
if (state.down) {
yield new PointerCancelEvent(
timeStamp: timeStamp,
pointer: state.pointer,
kind: kind,
device: datum.device,
position: position,
buttons: datum.buttons,
obscured: datum.obscured,
pressureMin: datum.pressureMin,
pressureMax: datum.pressureMax,
distance: datum.distance,
distanceMax: datum.distanceMax,
radiusMin: datum.radiusMin,
radiusMax: datum.radiusMax,
orientation: datum.orientation,
tilt: datum.tilt
);
}
_pointers.remove(datum.device);
yield new PointerRemovedEvent(
timeStamp: timeStamp,
kind: kind,
......
......@@ -100,7 +100,7 @@ class EdgeInsets {
Size get collapsedSize => new Size(horizontal, vertical);
/// An EdgeInsets with top and bottom as well as left and right flipped.
EdgeInsets get flipped => new EdgeInsets.fromLTRB(left, top, right, bottom);
EdgeInsets get flipped => new EdgeInsets.fromLTRB(right, bottom, left, top);
/// Returns a new rect that is bigger than the given rect in each direction by
/// the amount of inset in each direction. Specifically, the left edge of the
......
......@@ -155,18 +155,26 @@ class PlatformMessages {
/// The given callback will replace the currently registered callback (if any).
/// To remove the mock handler, pass `null` as the `handler` argument.
static void setMockStringMessageHandler(String channel, Future<String> handler(String message)) {
if (handler == null) {
setMockBinaryMessageHandler(channel, null);
} else {
setMockBinaryMessageHandler(channel, (ByteData message) async {
return _encodeUTF8(await handler(_decodeUTF8(message)));
});
}
}
/// Sets a message handler that intercepts outgoing messages in JSON form.
///
/// The given callback will replace the currently registered callback (if any).
/// To remove the mock handler, pass `null` as the `handler` argument.
static void setMockJSONMessageHandler(String channel, Future<dynamic> handler(dynamic message)) {
if (handler == null) {
setMockStringMessageHandler(channel, null);
} else {
setMockStringMessageHandler(channel, (String message) async {
return _encodeJSON(await handler(_decodeJSON(message)));
});
}
}
}
......@@ -132,4 +132,46 @@ void main() {
expect(events[0].runtimeType, equals(PointerDownEvent));
expect(events[1].runtimeType, equals(PointerCancelEvent));
});
test('Can expand add and hover pointers', () {
ui.PointerDataPacket packet = const ui.PointerDataPacket(
data: const <ui.PointerData>[
const ui.PointerData(change: ui.PointerChange.add, device: 24),
const ui.PointerData(change: ui.PointerChange.hover, device: 24),
const ui.PointerData(change: ui.PointerChange.remove, device: 24),
const ui.PointerData(change: ui.PointerChange.hover, device: 24),
]
);
List<PointerEvent> events = PointerEventConverter.expand(
packet.data, ui.window.devicePixelRatio).toList();
expect(events.length, 5);
expect(events[0].runtimeType, equals(PointerAddedEvent));
expect(events[1].runtimeType, equals(PointerHoverEvent));
expect(events[2].runtimeType, equals(PointerRemovedEvent));
expect(events[3].runtimeType, equals(PointerAddedEvent));
expect(events[4].runtimeType, equals(PointerHoverEvent));
});
test('Synthetic hover and cancel for misplaced down and remove', () {
ui.PointerDataPacket packet = const ui.PointerDataPacket(
data: const <ui.PointerData>[
const ui.PointerData(change: ui.PointerChange.add, device: 25, physicalX: 10.0, physicalY: 10.0),
const ui.PointerData(change: ui.PointerChange.down, device: 25, physicalX: 15.0, physicalY: 17.0),
const ui.PointerData(change: ui.PointerChange.remove, device: 25),
]
);
List<PointerEvent> events = PointerEventConverter.expand(
packet.data, ui.window.devicePixelRatio).toList();
expect(events.length, 5);
expect(events[0].runtimeType, equals(PointerAddedEvent));
expect(events[1].runtimeType, equals(PointerHoverEvent));
expect(events[1].delta, equals(const Offset(5.0, 7.0) / ui.window.devicePixelRatio));
expect(events[2].runtimeType, equals(PointerDownEvent));
expect(events[3].runtimeType, equals(PointerCancelEvent));
expect(events[4].runtimeType, equals(PointerRemovedEvent));
});
}
// 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/painting.dart';
import 'package:flutter_test/flutter_test.dart';
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.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));
expect(color.withHue(123.0), const HSVColor.fromAHSV(0.7, 123.0, 0.3, 0.6));
expect(color.withSaturation(0.9), const HSVColor.fromAHSV(0.7, 28.0, 0.9, 0.6));
expect(color.withValue(0.1), const HSVColor.fromAHSV(0.7, 28.0, 0.3, 0.1));
expect(color.toColor(), const Color(0xb399816b));
HSVColor result = HSVColor.lerp(color, const HSVColor.fromAHSV(0.3, 128.0, 0.7, 0.2), 0.25);
expect(result.alpha, 0.6);
expect(result.hue, 53.0);
expect(result.saturation, greaterThan(0.3999));
expect(result.saturation, lessThan(0.4001));
expect(result.value, 0.5);
});
}
......@@ -3,11 +3,39 @@
// found in the LICENSE file.
import 'package:flutter/painting.dart';
import 'package:test/test.dart';
import 'package:flutter_test/flutter_test.dart';
void main() {
test("EdgeInsets.lerp()", () {
test('EdgeInsets control test', () {
const EdgeInsets insets = const EdgeInsets.fromLTRB(5.0, 7.0, 11.0, 13.0);
expect(insets.toString(), 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));
expect(insets.topRight, const Offset(-11.0, 7.0));
expect(insets.bottomLeft, const Offset(5.0, -13.0));
expect(insets.bottomRight, const Offset(-11.0, -13.0));
expect(insets.collapsedSize, const Size(16.0, 20.0));
expect(insets.flipped, const EdgeInsets.fromLTRB(11.0, 13.0, 5.0, 7.0));
expect(insets.inflateRect(new Rect.fromLTRB(23.0, 32.0, 124.0, 143.0)),
new Rect.fromLTRB(18.0, 25.0, 135.0, 156.0));
expect(insets.deflateRect(new Rect.fromLTRB(23.0, 32.0, 124.0, 143.0)),
new Rect.fromLTRB(28.0, 39.0, 113.0, 130.0));
expect(insets.inflateSize(const Size(100.0, 125.0)), const Size(116.0, 145.0));
expect(insets.deflateSize(const Size(100.0, 125.0)), const Size(84.0, 105.0));
expect(insets / 2.0, const EdgeInsets.fromLTRB(2.5, 3.5, 5.5, 6.5));
expect(insets ~/ 2.0, const EdgeInsets.fromLTRB(2.0, 3.0, 5.0, 6.0));
expect(insets % 5.0, const EdgeInsets.fromLTRB(0.0, 2.0, 1.0, 3.0));
});
test('EdgeInsets.lerp()', () {
EdgeInsets a = const EdgeInsets.all(10.0);
EdgeInsets b = const EdgeInsets.all(20.0);
expect(EdgeInsets.lerp(a, b, 0.25), equals(a * 1.25));
......
......@@ -7,7 +7,7 @@ import 'package:flutter/painting.dart';
import 'package:test/test.dart';
void main() {
test("applyImageFit", () {
test('applyImageFit', () {
FittedSizes result;
result = applyImageFit(ImageFit.scaleDown, const Size(100.0, 1000.0), const Size(200.0, 2000.0));
......@@ -17,5 +17,13 @@ void main() {
result = applyImageFit(ImageFit.scaleDown, const Size(300.0, 3000.0), const Size(200.0, 2000.0));
expect(result.source, equals(const Size(300.0, 3000.0)));
expect(result.destination, equals(const Size(200.0, 2000.0)));
result = applyImageFit(ImageFit.fitWidth, const Size(2000.0, 400.0), const Size(1000.0, 100.0));
expect(result.source, equals(const Size(2000.0, 200.0)));
expect(result.destination, equals(const Size(1000.0, 100.0)));
result = applyImageFit(ImageFit.fitHeight, const Size(400.0, 2000.0), const Size(100.0, 1000.0));
expect(result.source, equals(const Size(200.0, 2000.0)));
expect(result.destination, equals(const Size(100.0, 1000.0)));
});
}
// Copyright 2016 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/services.dart';
import 'package:test/test.dart';
void main() {
test('Haptic feedback control test', () async {
List<String> log = <String>[];
PlatformMessages.setMockStringMessageHandler('flutter/platform', (String message) async {
log.add(message);
});
await HapticFeedback.vibrate();
expect(log, equals(<String>['{"method":"HapticFeedback.vibrate","args":[]}']));
});
}
// Copyright 2016 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 'dart:ui' as ui;
import 'dart:typed_data';
import 'package:flutter/services.dart';
import 'package:test/test.dart';
const List<int> transparentImage = const <int>[
0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A, 0x00, 0x00, 0x00, 0x0D, 0x49,
0x48, 0x44, 0x52, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x08, 0x06,
0x00, 0x00, 0x00, 0x1F, 0x15, 0xC4, 0x89, 0x00, 0x00, 0x00, 0x0A, 0x49, 0x44,
0x41, 0x54, 0x78, 0x9C, 0x63, 0x00, 0x01, 0x00, 0x00, 0x05, 0x00, 0x01, 0x0D,
0x0A, 0x2D, 0xB4, 0x00, 0x00, 0x00, 0x00, 0x49, 0x45, 0x4E, 0x44, 0xAE,
];
void main() {
test('Image decoder control test', () async {
ui.Image image = await decodeImageFromList(new Uint8List.fromList(transparentImage));
expect(image, isNotNull);
expect(image.width, 1);
expect(image.height, 1);
});
}
// Copyright 2016 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 'dart:io';
import 'package:flutter/services.dart';
import 'package:test/test.dart';
void main() {
test('Path provider control test', () async {
List<String> log = <String>[];
String response;
PlatformMessages.setMockStringMessageHandler('flutter/platform', (String message) async {
log.add(message);
return response;
});
Directory directory = await PathProvider.getTemporaryDirectory();
expect(log, equals(<String>['{"method":"PathProvider.getTemporaryDirectory","args":[]}']));
expect(directory, isNull);
log.clear();
directory = await PathProvider.getApplicationDocumentsDirectory();
expect(log, equals(<String>['{"method":"PathProvider.getApplicationDocumentsDirectory","args":[]}']));
expect(directory, isNull);
String fakePath = "/foo/bar/baz";
response = '{"path":"$fakePath"}';
directory = await PathProvider.getTemporaryDirectory();
expect(directory.path, equals(fakePath));
directory = await PathProvider.getApplicationDocumentsDirectory();
expect(directory.path, equals(fakePath));
});
}
// Copyright 2016 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/services.dart';
import 'package:test/test.dart';
void main() {
test('Mock string message handler control test', () async {
List<String> log = <String>[];
PlatformMessages.setMockStringMessageHandler('test1', (String message) async {
log.add(message);
});
await PlatformMessages.sendString('test1', 'hello');
expect(log, equals(<String>['hello']));
log.clear();
PlatformMessages.setMockStringMessageHandler('test1', null);
await PlatformMessages.sendString('test1', 'fail');
expect(log, isEmpty);
});
test('Mock JSON message handler control test', () async {
List<dynamic> log = <dynamic>[];
PlatformMessages.setMockJSONMessageHandler('test2', (dynamic message) async {
log.add(message);
});
await PlatformMessages.sendString('test2', '{"hello": "world"}');
expect(log, equals(<Map<String, String>>[<String, String>{'hello': 'world'}]));
log.clear();
PlatformMessages.setMockStringMessageHandler('test2', null);
await PlatformMessages.sendString('test2', '{"fail": "message"}');
expect(log, isEmpty);
});
}
// Copyright 2016 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/services.dart';
import 'package:test/test.dart';
void main() {
test('System navigator control test', () async {
List<String> log = <String>[];
PlatformMessages.setMockStringMessageHandler('flutter/platform', (String message) async {
log.add(message);
});
await SystemNavigator.pop();
expect(log, equals(<String>['{"method":"SystemNavigator.pop","args":[]}']));
});
}
// Copyright 2016 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/services.dart';
import 'package:test/test.dart';
void main() {
test('System sound control test', () async {
List<String> log = <String>[];
PlatformMessages.setMockStringMessageHandler('flutter/platform', (String message) async {
log.add(message);
});
await SystemSound.play(SystemSoundType.click);
expect(log, equals(<String>['{"method":"SystemSound.play","args":["SystemSoundType.click"]}']));
});
}
// Copyright 2016 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/services.dart';
import 'package:test/test.dart';
void main() {
test('URL launcher control test', () async {
List<String> log = <String>[];
PlatformMessages.setMockStringMessageHandler('flutter/platform', (String message) async {
log.add(message);
});
await UrlLauncher.launch('http://example.com/');
expect(log, equals(<String>['{"method":"UrlLauncher.launch","args":["http://example.com/"]}']));
});
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment