Add a basic test for DatePicker

Most of this work in this patch is building out the test framework to the point
where we can write this test.
This commit is contained in:
Adam Barth
2015-08-26 13:33:54 -07:00
parent ea2ff27356
commit 92e5a65d29
3 changed files with 94 additions and 2 deletions

View File

@@ -136,8 +136,6 @@ class DatePickerHeader extends Component {
TextStyle dayStyle = headerTheme.display3.copyWith(color: dayColor, height: 1.0, fontSize: 100.0);
TextStyle monthStyle = headerTheme.headline.copyWith(color: dayColor, height: 1.0);
TextStyle yearStyle = headerTheme.headline.copyWith(color: yearColor, height: 1.0);
DateTime firstDate = new DateTime(1900);
DateTime lastDate = new DateTime(2101);
return new Container(
child: new BlockBody([

View File

@@ -1,5 +1,7 @@
import 'dart:sky' as sky;
import 'package:sky/rendering.dart';
import 'package:sky/widgets.dart';
import 'package:test/test.dart';
const Size _kTestViewSize = const Size(800.0, 600.0);
@@ -30,6 +32,28 @@ class TestApp extends App {
}
}
class TestGestureEvent extends sky.GestureEvent {
TestGestureEvent({
this.type,
this.primaryPointer,
this.x,
this.y,
this.dx,
this.dy,
this.velocityX,
this.velocityY
});
String type;
int primaryPointer;
double x;
double y;
double dx;
double dy;
double velocityX;
double velocityY;
}
class WidgetTester {
WidgetTester() {
_app = new TestApp();
@@ -49,6 +73,42 @@ class WidgetTester {
_app.walkChildren(walk);
}
Widget findWidget(bool predicate(Widget widget)) {
try {
walkWidgets((Widget widget) {
if (predicate(widget))
throw widget;
});
} catch (e) {
if (e is Widget)
return e;
rethrow;
}
return null;
}
Text findText(String text) {
return findWidget((Widget widget) {
return widget is Text && widget.data == text;
});
}
Point getCenter(Widget widget) {
assert(widget != null);
RenderBox box = widget.renderObject as RenderBox;
assert(box != null);
return box.localToGlobal(box.size.center(Point.origin));
}
void tap(Widget widget) {
dispatchEvent(new TestGestureEvent(type: 'gesturetap'), getCenter(widget));
}
void dispatchEvent(sky.Event event, Point position) {
HitTestResult result = SkyBinding.instance.hitTest(position);
SkyBinding.instance.dispatchEvent(event, result);
}
void pumpFrame(WidgetBuilder builder) {
_app.builder = builder;
Component.flushBuild();

View File

@@ -0,0 +1,34 @@
import 'package:sky/widgets.dart';
import 'package:test/test.dart';
import 'build_utils.dart';
void main() {
test('Can select a day', () {
WidgetTester tester = new WidgetTester();
DateTime currentValue;
Widget builder() {
return new Block([
new DatePicker(
selectedDate: new DateTime.utc(2015, 6, 9, 7, 12),
firstDate: new DateTime.utc(2013),
lastDate: new DateTime.utc(2018),
onChanged: (DateTime dateTime) {
currentValue = dateTime;
}
)
]);
}
tester.pumpFrame(builder);
// TODO(abarth): We shouldn't need to pump a second frame here.
tester.pumpFrame(builder);
expect(currentValue, isNull);
tester.tap(tester.findText('30'));
expect(currentValue, equals(new DateTime(2013, 1, 30)));
});
}