mirror of
https://github.com/flutter/flutter.git
synced 2025-06-03 00:51:18 +00:00
Migrate sky/tests/layout to sky/unit/test
Also, I've organized the tests by the render object they're testing.
This commit is contained in:
parent
c7f528dab8
commit
ccd00bc5c2
161
packages/unit/test/rendering/image_test.dart
Normal file
161
packages/unit/test/rendering/image_test.dart
Normal file
@ -0,0 +1,161 @@
|
||||
import 'dart:sky' as sky;
|
||||
|
||||
import 'package:sky/rendering.dart';
|
||||
import 'package:test/test.dart';
|
||||
|
||||
import 'layout_utils.dart';
|
||||
|
||||
class SquareImage implements sky.Image {
|
||||
int get width => 10;
|
||||
int get height => 10;
|
||||
}
|
||||
|
||||
class WideImage implements sky.Image {
|
||||
int get width => 20;
|
||||
int get height => 10;
|
||||
}
|
||||
|
||||
class TallImage implements sky.Image {
|
||||
int get width => 10;
|
||||
int get height => 20;
|
||||
}
|
||||
|
||||
void main() {
|
||||
test('Image sizing', () {
|
||||
RenderImage image;
|
||||
|
||||
image = new RenderImage(image: new SquareImage());
|
||||
layout(image,
|
||||
constraints: new BoxConstraints(
|
||||
minWidth: 25.0,
|
||||
minHeight: 25.0,
|
||||
maxWidth: 100.0,
|
||||
maxHeight: 100.0));
|
||||
expect(image.size.width, equals(25.0));
|
||||
expect(image.size.height, equals(25.0));
|
||||
|
||||
image = new RenderImage(image: new WideImage());
|
||||
layout(image,
|
||||
constraints: new BoxConstraints(
|
||||
minWidth: 5.0,
|
||||
minHeight: 30.0,
|
||||
maxWidth: 100.0,
|
||||
maxHeight: 100.0));
|
||||
expect(image.size.width, equals(60.0));
|
||||
expect(image.size.height, equals(30.0));
|
||||
|
||||
image = new RenderImage(image: new TallImage());
|
||||
layout(image,
|
||||
constraints: new BoxConstraints(
|
||||
minWidth: 50.0,
|
||||
minHeight: 5.0,
|
||||
maxWidth: 75.0,
|
||||
maxHeight: 75.0));
|
||||
expect(image.size.width, equals(50.0));
|
||||
expect(image.size.height, equals(75.0));
|
||||
|
||||
image = new RenderImage(image: new WideImage());
|
||||
layout(image,
|
||||
constraints: new BoxConstraints(
|
||||
minWidth: 5.0,
|
||||
minHeight: 5.0,
|
||||
maxWidth: 100.0,
|
||||
maxHeight: 100.0));
|
||||
expect(image.size.width, equals(20.0));
|
||||
expect(image.size.height, equals(10.0));
|
||||
|
||||
image = new RenderImage(image: new WideImage());
|
||||
layout(image,
|
||||
constraints: new BoxConstraints(
|
||||
minWidth: 5.0,
|
||||
minHeight: 5.0,
|
||||
maxWidth: 16.0,
|
||||
maxHeight: 16.0));
|
||||
expect(image.size.width, equals(16.0));
|
||||
expect(image.size.height, equals(8.0));
|
||||
|
||||
image = new RenderImage(image: new TallImage());
|
||||
layout(image,
|
||||
constraints: new BoxConstraints(
|
||||
minWidth: 5.0,
|
||||
minHeight: 5.0,
|
||||
maxWidth: 16.0,
|
||||
maxHeight: 16.0));
|
||||
expect(image.size.width, equals(8.0));
|
||||
expect(image.size.height, equals(16.0));
|
||||
|
||||
image = new RenderImage(image: new SquareImage());
|
||||
layout(image,
|
||||
constraints: new BoxConstraints(
|
||||
minWidth: 4.0,
|
||||
minHeight: 4.0,
|
||||
maxWidth: 8.0,
|
||||
maxHeight: 8.0));
|
||||
expect(image.size.width, equals(8.0));
|
||||
expect(image.size.height, equals(8.0));
|
||||
|
||||
image = new RenderImage(image: new WideImage());
|
||||
layout(image,
|
||||
constraints: new BoxConstraints(
|
||||
minWidth: 20.0,
|
||||
minHeight: 20.0,
|
||||
maxWidth: 30.0,
|
||||
maxHeight: 30.0));
|
||||
expect(image.size.width, equals(30.0));
|
||||
expect(image.size.height, equals(20.0));
|
||||
|
||||
image = new RenderImage(image: new TallImage());
|
||||
layout(image,
|
||||
constraints: new BoxConstraints(
|
||||
minWidth: 20.0,
|
||||
minHeight: 20.0,
|
||||
maxWidth: 30.0,
|
||||
maxHeight: 30.0));
|
||||
expect(image.size.width, equals(20.0));
|
||||
expect(image.size.height, equals(30.0));
|
||||
});
|
||||
|
||||
test('Null image sizing', () {
|
||||
RenderImage image;
|
||||
|
||||
image = new RenderImage();
|
||||
layout(image,
|
||||
constraints: new BoxConstraints(
|
||||
minWidth: 25.0,
|
||||
minHeight: 25.0,
|
||||
maxWidth: 100.0,
|
||||
maxHeight: 100.0));
|
||||
expect(image.size.width, equals(25.0));
|
||||
expect(image.size.height, equals(25.0));
|
||||
|
||||
image = new RenderImage(width: 50.0);
|
||||
layout(image,
|
||||
constraints: new BoxConstraints(
|
||||
minWidth: 25.0,
|
||||
minHeight: 25.0,
|
||||
maxWidth: 100.0,
|
||||
maxHeight: 100.0));
|
||||
expect(image.size.width, equals(50.0));
|
||||
expect(image.size.height, equals(25.0));
|
||||
|
||||
image = new RenderImage(height: 50.0);
|
||||
layout(image,
|
||||
constraints: new BoxConstraints(
|
||||
minWidth: 25.0,
|
||||
minHeight: 25.0,
|
||||
maxWidth: 100.0,
|
||||
maxHeight: 100.0));
|
||||
expect(image.size.width, equals(25.0));
|
||||
expect(image.size.height, equals(50.0));
|
||||
|
||||
image = new RenderImage(width: 100.0, height: 100.0);
|
||||
layout(image,
|
||||
constraints: new BoxConstraints(
|
||||
minWidth: 25.0,
|
||||
minHeight: 25.0,
|
||||
maxWidth: 75.0,
|
||||
maxHeight: 75.0));
|
||||
expect(image.size.width, equals(75.0));
|
||||
expect(image.size.height, equals(75.0));
|
||||
});
|
||||
}
|
30
packages/unit/test/rendering/layout_utils.dart
Normal file
30
packages/unit/test/rendering/layout_utils.dart
Normal file
@ -0,0 +1,30 @@
|
||||
import 'package:sky/rendering.dart';
|
||||
|
||||
const Size _kTestViewSize = const Size(800.0, 600.0);
|
||||
|
||||
class TestRenderView extends RenderView {
|
||||
TestRenderView({ RenderBox child }) : super(child: child) {
|
||||
attach();
|
||||
rootConstraints = new ViewConstraints(size: _kTestViewSize);
|
||||
scheduleInitialLayout();
|
||||
}
|
||||
|
||||
void beginFrame(double timeStamp) {
|
||||
RenderObject.flushLayout();
|
||||
}
|
||||
}
|
||||
|
||||
RenderView layout(RenderBox box, { BoxConstraints constraints }) {
|
||||
if (constraints != null) {
|
||||
box = new RenderPositionedBox(
|
||||
child: new RenderConstrainedBox(
|
||||
additionalConstraints: constraints,
|
||||
child: box
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
TestRenderView renderView = new TestRenderView(child: box);
|
||||
renderView.beginFrame(0.0);
|
||||
return renderView;
|
||||
}
|
40
packages/unit/test/rendering/stack_test.dart
Normal file
40
packages/unit/test/rendering/stack_test.dart
Normal file
@ -0,0 +1,40 @@
|
||||
import 'package:sky/rendering.dart';
|
||||
import 'package:test/test.dart';
|
||||
|
||||
import 'layout_utils.dart';
|
||||
|
||||
void main() {
|
||||
test('Stack can layout with top, right, bottom, left 0.0', () {
|
||||
RenderBox size = new RenderConstrainedBox(
|
||||
additionalConstraints: new BoxConstraints.tight(const Size(100.0, 100.0)));
|
||||
|
||||
RenderBox red = new RenderDecoratedBox(
|
||||
decoration: new BoxDecoration(
|
||||
backgroundColor: const Color(0xFFFF0000)
|
||||
),
|
||||
child: size);
|
||||
|
||||
RenderBox green = new RenderDecoratedBox(
|
||||
decoration: new BoxDecoration(
|
||||
backgroundColor: const Color(0xFFFF0000)
|
||||
));
|
||||
|
||||
RenderBox stack = new RenderStack(children: [red, green]);
|
||||
(green.parentData as StackParentData)
|
||||
..top = 0.0
|
||||
..right = 0.0
|
||||
..bottom = 0.0
|
||||
..left = 0.0;
|
||||
|
||||
layout(stack, constraints: const BoxConstraints());
|
||||
|
||||
expect(stack.size.width, equals(100.0));
|
||||
expect(stack.size.height, equals(100.0));
|
||||
|
||||
expect(red.size.width, equals(100.0));
|
||||
expect(red.size.height, equals(100.0));
|
||||
|
||||
expect(green.size.width, equals(100.0));
|
||||
expect(green.size.height, equals(100.0));
|
||||
});
|
||||
}
|
30
packages/unit/test/rendering/viewport_test.dart
Normal file
30
packages/unit/test/rendering/viewport_test.dart
Normal file
@ -0,0 +1,30 @@
|
||||
import 'package:sky/rendering.dart';
|
||||
import 'package:test/test.dart';
|
||||
|
||||
import 'layout_utils.dart';
|
||||
|
||||
void main() {
|
||||
test('Should be able to hit with negative scroll offset', () {
|
||||
RenderBox size = new RenderConstrainedBox(
|
||||
additionalConstraints: new BoxConstraints.tight(const Size(100.0, 100.0)));
|
||||
|
||||
RenderBox red = new RenderDecoratedBox(
|
||||
decoration: new BoxDecoration(
|
||||
backgroundColor: const Color(0xFFFF0000)
|
||||
),
|
||||
child: size);
|
||||
|
||||
RenderViewport viewport = new RenderViewport(child: red, scrollOffset: new Offset(0.0, -10.0));
|
||||
RenderView renderView = layout(viewport);
|
||||
|
||||
HitTestResult result;
|
||||
|
||||
result = new HitTestResult();
|
||||
renderView.hitTest(result, position: new Point(15.0, 0.0));
|
||||
expect(result.path.first.target, equals(viewport));
|
||||
|
||||
result = new HitTestResult();
|
||||
renderView.hitTest(result, position: new Point(15.0, 15.0));
|
||||
expect(result.path.first.target, equals(size));
|
||||
});
|
||||
}
|
Loading…
Reference in New Issue
Block a user