matrix_utils_test.dart 3.87 KB
Newer Older
1 2 3 4
// 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.

5 6 7
import 'dart:math';

import 'package:flutter_test/flutter_test.dart';
8 9 10 11
import 'package:flutter/painting.dart';
import 'package:vector_math/vector_math_64.dart';

void main() {
Ian Hickson's avatar
Ian Hickson committed
12
  test('MatrixUtils.getAsTranslation()', () {
13
    Matrix4 test;
14
    test = Matrix4.identity();
15
    expect(MatrixUtils.getAsTranslation(test), equals(Offset.zero));
16
    test = Matrix4.zero();
17
    expect(MatrixUtils.getAsTranslation(test), isNull);
18
    test = Matrix4.rotationX(1.0);
19
    expect(MatrixUtils.getAsTranslation(test), isNull);
20
    test = Matrix4.rotationZ(1.0);
21
    expect(MatrixUtils.getAsTranslation(test), isNull);
22
    test = Matrix4.translationValues(1.0, 2.0, 0.0);
23
    expect(MatrixUtils.getAsTranslation(test), equals(const Offset(1.0, 2.0)));
24
    test = Matrix4.translationValues(1.0, 2.0, 3.0);
25 26
    expect(MatrixUtils.getAsTranslation(test), isNull);

27
    test = Matrix4.identity();
28 29 30 31
    expect(MatrixUtils.getAsTranslation(test), equals(Offset.zero));
    test.rotateX(2.0);
    expect(MatrixUtils.getAsTranslation(test), isNull);

32
    test = Matrix4.identity();
33 34 35 36
    expect(MatrixUtils.getAsTranslation(test), equals(Offset.zero));
    test.scale(2.0);
    expect(MatrixUtils.getAsTranslation(test), isNull);

37
    test = Matrix4.identity();
38 39 40 41 42 43
    expect(MatrixUtils.getAsTranslation(test), equals(Offset.zero));
    test.translate(2.0, -2.0);
    expect(MatrixUtils.getAsTranslation(test), equals(const Offset(2.0, -2.0)));
    test.translate(4.0, 8.0);
    expect(MatrixUtils.getAsTranslation(test), equals(const Offset(6.0, 6.0)));
  });
44 45 46 47 48 49 50 51

  test('cylindricalProjectionTransform identity', () {
    final Matrix4 initialState = MatrixUtils.createCylindricalProjectionTransform(
      radius: 0.0,
      angle: 0.0,
      perspective: 0.0,
    );

52
    expect(initialState, Matrix4.identity());
53 54 55 56 57 58 59 60 61
  });

  test('cylindricalProjectionTransform rotate with no radius', () {
    final Matrix4 simpleRotate = MatrixUtils.createCylindricalProjectionTransform(
      radius: 0.0,
      angle: pi / 2.0,
      perspective: 0.0,
    );

62
    expect(simpleRotate, Matrix4.rotationX(pi / 2.0));
63 64 65 66 67 68 69 70 71
  });

  test('cylindricalProjectionTransform radius does not change scale', () {
    final Matrix4 noRotation = MatrixUtils.createCylindricalProjectionTransform(
      radius: 1000000.0,
      angle: 0.0,
      perspective: 0.0,
    );

72
    expect(noRotation, Matrix4.identity());
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
  });

  test('cylindricalProjectionTransform calculation spot check', () {
    final Matrix4 actual = MatrixUtils.createCylindricalProjectionTransform(
      radius: 100.0,
      angle: pi / 3.0,
      perspective: 0.001,
    );

    expect(actual.storage, <dynamic>[
      1.0, 0.0, 0.0, 0.0,
      0.0, moreOrLessEquals(0.5), moreOrLessEquals(0.8660254037844386), moreOrLessEquals(-0.0008660254037844386),
      0.0, moreOrLessEquals(-0.8660254037844386), moreOrLessEquals(0.5), moreOrLessEquals(-0.0005),
      0.0, moreOrLessEquals(-86.60254037844386), moreOrLessEquals(-50.0), 1.05,
    ]);
  });
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123

  test('forceToPoint', () {
    const Offset forcedOffset = Offset(20, -30);
    final Matrix4 forcedTransform = MatrixUtils.forceToPoint(forcedOffset);

    expect(
      MatrixUtils.transformPoint(forcedTransform, forcedOffset),
      forcedOffset,
    );

    expect(
      MatrixUtils.transformPoint(forcedTransform, Offset.zero),
      forcedOffset,
    );

    expect(
      MatrixUtils.transformPoint(forcedTransform, const Offset(1, 1)),
      forcedOffset,
    );

    expect(
      MatrixUtils.transformPoint(forcedTransform, const Offset(-1, -1)),
      forcedOffset,
    );

    expect(
      MatrixUtils.transformPoint(forcedTransform, const Offset(-20, 30)),
      forcedOffset,
    );

    expect(
      MatrixUtils.transformPoint(forcedTransform, const Offset(-1.2344, 1422434.23)),
      forcedOffset,
    );
  });
124
}