1// Copyright 2013 The Flutter Authors. All rights reserved. 2// Use of this source code is governed by a BSD-style license that can be 3// found in the LICENSE file. 4 5import 'dart:math' as math show sqrt; 6import 'dart:math' show pi; 7 8import 'package:ui/ui.dart'; 9 10import 'package:test/test.dart'; 11 12void main() { 13 group('Offset', () { 14 test('Offset.direction', () { 15 expect(const Offset(0.0, 0.0).direction, 0.0); 16 expect(const Offset(0.0, 1.0).direction, pi / 2.0); 17 expect(const Offset(0.0, -1.0).direction, -pi / 2.0); 18 expect(const Offset(1.0, 0.0).direction, 0.0); 19 expect(const Offset(1.0, 1.0).direction, pi / 4.0); 20 expect(const Offset(1.0, -1.0).direction, -pi / 4.0); 21 expect(const Offset(-1.0, 0.0).direction, pi); 22 expect(const Offset(-1.0, 1.0).direction, pi * 3.0 / 4.0); 23 expect(const Offset(-1.0, -1.0).direction, -pi * 3.0 / 4.0); 24 }); 25 26 test('Offset.fromDirection', () { 27 expect(Offset.fromDirection(0.0, 0.0), const Offset(0.0, 0.0)); 28 expect(Offset.fromDirection(pi / 2.0).dx, closeTo(0.0, 1e-12)); // aah, floating point math. i love you so. 29 expect(Offset.fromDirection(pi / 2.0).dy, 1.0); 30 expect(Offset.fromDirection(-pi / 2.0).dx, closeTo(0.0, 1e-12)); 31 expect(Offset.fromDirection(-pi / 2.0).dy, -1.0); 32 expect(Offset.fromDirection(0.0), const Offset(1.0, 0.0)); 33 expect(Offset.fromDirection(pi / 4.0).dx, closeTo(1.0 / math.sqrt(2.0), 1e-12)); 34 expect(Offset.fromDirection(pi / 4.0).dy, closeTo(1.0 / math.sqrt(2.0), 1e-12)); 35 expect(Offset.fromDirection(-pi / 4.0).dx, closeTo(1.0 / math.sqrt(2.0), 1e-12)); 36 expect(Offset.fromDirection(-pi / 4.0).dy, closeTo(-1.0 / math.sqrt(2.0), 1e-12)); 37 expect(Offset.fromDirection(pi).dx, -1.0); 38 expect(Offset.fromDirection(pi).dy, closeTo(0.0, 1e-12)); 39 expect(Offset.fromDirection(pi * 3.0 / 4.0).dx, closeTo(-1.0 / math.sqrt(2.0), 1e-12)); 40 expect(Offset.fromDirection(pi * 3.0 / 4.0).dy, closeTo(1.0 / math.sqrt(2.0), 1e-12)); 41 expect(Offset.fromDirection(-pi * 3.0 / 4.0).dx, closeTo(-1.0 / math.sqrt(2.0), 1e-12)); 42 expect(Offset.fromDirection(-pi * 3.0 / 4.0).dy, closeTo(-1.0 / math.sqrt(2.0), 1e-12)); 43 expect(Offset.fromDirection(0.0, 2.0), const Offset(2.0, 0.0)); 44 expect(Offset.fromDirection(pi / 6, 2.0).dx, closeTo(math.sqrt(3.0), 1e-12)); 45 expect(Offset.fromDirection(pi / 6, 2.0).dy, closeTo(1.0, 1e-12)); 46 }); 47 }); 48 49 group('Size', () { 50 test('Size created from doubles', () { 51 const Size size = Size(5.0, 7.0); 52 expect(size.width, equals(5.0)); 53 expect(size.height, equals(7.0)); 54 expect(size.shortestSide, equals(5.0)); 55 expect(size.longestSide, equals(7.0)); 56 }); 57 58 test('Size.aspectRatio', () { 59 expect(const Size(0.0, 0.0).aspectRatio, 0.0); 60 expect(const Size(-0.0, 0.0).aspectRatio, 0.0); 61 expect(const Size(0.0, -0.0).aspectRatio, 0.0); 62 expect(const Size(-0.0, -0.0).aspectRatio, 0.0); 63 expect(const Size(0.0, 1.0).aspectRatio, 0.0); 64 expect(const Size(0.0, -1.0).aspectRatio, -0.0); 65 expect(const Size(1.0, 0.0).aspectRatio, double.infinity); 66 expect(const Size(1.0, 1.0).aspectRatio, 1.0); 67 expect(const Size(1.0, -1.0).aspectRatio, -1.0); 68 expect(const Size(-1.0, 0.0).aspectRatio, -double.infinity); 69 expect(const Size(-1.0, 1.0).aspectRatio, -1.0); 70 expect(const Size(-1.0, -1.0).aspectRatio, 1.0); 71 expect(const Size(3.0, 4.0).aspectRatio, 3.0 / 4.0); 72 }); 73 }); 74 75 group('Rect', () { 76 test('Rect accessors', () { 77 final Rect r = Rect.fromLTRB(1.0, 3.0, 5.0, 7.0); 78 expect(r.left, equals(1.0)); 79 expect(r.top, equals(3.0)); 80 expect(r.right, equals(5.0)); 81 expect(r.bottom, equals(7.0)); 82 }); 83 84 test('Rect created by width and height', () { 85 final Rect r = Rect.fromLTWH(1.0, 3.0, 5.0, 7.0); 86 expect(r.left, equals(1.0)); 87 expect(r.top, equals(3.0)); 88 expect(r.right, equals(6.0)); 89 expect(r.bottom, equals(10.0)); 90 expect(r.shortestSide, equals(5.0)); 91 expect(r.longestSide, equals(7.0)); 92 }); 93 94 test('Rect intersection', () { 95 final Rect r1 = Rect.fromLTRB(0.0, 0.0, 100.0, 100.0); 96 final Rect r2 = Rect.fromLTRB(50.0, 50.0, 200.0, 200.0); 97 final Rect r3 = r1.intersect(r2); 98 expect(r3.left, equals(50.0)); 99 expect(r3.top, equals(50.0)); 100 expect(r3.right, equals(100.0)); 101 expect(r3.bottom, equals(100.0)); 102 103 final Rect r4 = r2.intersect(r1); 104 expect(r4, equals(r3)); 105 }); 106 107 test('Rect expandToInclude overlapping rects', () { 108 final Rect r1 = Rect.fromLTRB(0.0, 0.0, 100.0, 100.0); 109 final Rect r2 = Rect.fromLTRB(50.0, 50.0, 200.0, 200.0); 110 final Rect r3 = r1.expandToInclude(r2); 111 expect(r3.left, equals(0.0)); 112 expect(r3.top, equals(0.0)); 113 expect(r3.right, equals(200.0)); 114 expect(r3.bottom, equals(200.0)); 115 116 final Rect r4 = r2.expandToInclude(r1); 117 expect(r4, equals(r3)); 118 }); 119 120 test('Rect expandToInclude crossing rects', () { 121 final Rect r1 = Rect.fromLTRB(50.0, 0.0, 50.0, 200.0); 122 final Rect r2 = Rect.fromLTRB(0.0, 50.0, 200.0, 50.0); 123 final Rect r3 = r1.expandToInclude(r2); 124 expect(r3.left, equals(0.0)); 125 expect(r3.top, equals(0.0)); 126 expect(r3.right, equals(200.0)); 127 expect(r3.bottom, equals(200.0)); 128 129 final Rect r4 = r2.expandToInclude(r1); 130 expect(r4, equals(r3)); 131 }); 132 }); 133 134 group('RRect', () { 135 test('RRect.fromRectXY', () { 136 final Rect baseRect = Rect.fromLTWH(1.0, 3.0, 5.0, 7.0); 137 final RRect r = RRect.fromRectXY(baseRect, 1.0, 1.0); 138 expect(r.left, equals(1.0)); 139 expect(r.top, equals(3.0)); 140 expect(r.right, equals(6.0)); 141 expect(r.bottom, equals(10.0)); 142 expect(r.shortestSide, equals(5.0)); 143 expect(r.longestSide, equals(7.0)); 144 }); 145 }); 146} 147