display_feature_sub_screen_test.dart 7.62 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
// Copyright 2014 The Flutter 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';

import 'package:flutter/foundation.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_test/flutter_test.dart';

void main() {
  group('DisplayFeatureSubScreen', () {
    testWidgets('without Directionality or anchor', (WidgetTester tester) async {
      const Key childKey = Key('childKey');
16
      final MediaQueryData mediaQuery = MediaQueryData.fromWindow(WidgetsBinding.instance.window).copyWith(
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
          displayFeatures: <DisplayFeature>[
            const DisplayFeature(
              bounds: Rect.fromLTRB(390, 0, 410, 600),
              type: DisplayFeatureType.hinge,
              state: DisplayFeatureState.unknown,
            ),
          ]
      );

      await tester.pumpWidget(
        MediaQuery(
          data: mediaQuery,
          child: const DisplayFeatureSubScreen(
            child: SizedBox(
              key: childKey,
              width: double.infinity,
              height: double.infinity,
            ),
          ),
        ),
      );

      // With no Directionality or anchorpoint, the widget throws
      final String message = tester.takeException().toString();
      expect(message, contains('Directionality'));
    });

    testWidgets('with anchorPoint', (WidgetTester tester) async {
      const Key childKey = Key('childKey');
46
      final MediaQueryData mediaQuery = MediaQueryData.fromWindow(WidgetsBinding.instance.window).copyWith(
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
          displayFeatures: <DisplayFeature>[
            const DisplayFeature(
              bounds: Rect.fromLTRB(390, 0, 410, 600),
              type: DisplayFeatureType.hinge,
              state: DisplayFeatureState.unknown,
            ),
          ]
      );

      await tester.pumpWidget(
        MediaQuery(
          data: mediaQuery,
          child: const DisplayFeatureSubScreen(
            anchorPoint: Offset(600, 300),
            child: SizedBox(
              key: childKey,
              width: double.infinity,
              height: double.infinity,
            ),
          ),
        ),
      );

      // anchorPoint is in the middle of the right screen
      final RenderBox renderBox = tester.renderObject(find.byKey(childKey));
      expect(renderBox.size.width, equals(390.0));
      expect(renderBox.size.height, equals(600.0));
      expect(renderBox.localToGlobal(Offset.zero), equals(const Offset(410,0)));
    });

    testWidgets('with infinity anchorpoint', (WidgetTester tester) async {
      const Key childKey = Key('childKey');
79
      final MediaQueryData mediaQuery = MediaQueryData.fromWindow(WidgetsBinding.instance.window).copyWith(
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
          displayFeatures: <DisplayFeature>[
            const DisplayFeature(
              bounds: Rect.fromLTRB(390, 0, 410, 600),
              type: DisplayFeatureType.hinge,
              state: DisplayFeatureState.unknown,
            ),
          ]
      );

      await tester.pumpWidget(
        MediaQuery(
          data: mediaQuery,
          child: const DisplayFeatureSubScreen(
            anchorPoint: Offset.infinite,
            child: SizedBox(
              key: childKey,
              width: double.infinity,
              height: double.infinity,
            ),
          ),
        ),
      );

      // anchorPoint is infinite, so the bottom-most & right-most screen is chosen
      final RenderBox renderBox = tester.renderObject(find.byKey(childKey));
      expect(renderBox.size.width, equals(390.0));
      expect(renderBox.size.height, equals(600.0));
      expect(renderBox.localToGlobal(Offset.zero), equals(const Offset(410,0)));
    });

    testWidgets('with horizontal hinge and anchorPoint', (WidgetTester tester) async {
      const Key childKey = Key('childKey');
112
      final MediaQueryData mediaQuery = MediaQueryData.fromWindow(WidgetsBinding.instance.window).copyWith(
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
          displayFeatures: <DisplayFeature>[
            const DisplayFeature(
              bounds: Rect.fromLTRB(0, 290, 800, 310),
              type: DisplayFeatureType.hinge,
              state: DisplayFeatureState.unknown,
            ),
          ]
      );

      await tester.pumpWidget(
        MediaQuery(
          data: mediaQuery,
          child: const DisplayFeatureSubScreen(
            anchorPoint: Offset(1000, 1000),
            child: SizedBox(
              key: childKey,
              width: double.infinity,
              height: double.infinity,
            ),
          ),
        ),
      );

      final RenderBox renderBox = tester.renderObject(find.byKey(childKey));
      expect(renderBox.size.width, equals(800.0));
      expect(renderBox.size.height, equals(290.0));
      expect(renderBox.localToGlobal(Offset.zero), equals(const Offset(0,310)));
    });

    testWidgets('with multiple display features and anchorPoint', (WidgetTester tester) async {
      const Key childKey = Key('childKey');
144
      final MediaQueryData mediaQuery = MediaQueryData.fromWindow(WidgetsBinding.instance.window).copyWith(
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
          displayFeatures: <DisplayFeature>[
            const DisplayFeature(
              bounds: Rect.fromLTRB(0, 290, 800, 310),
              type: DisplayFeatureType.hinge,
              state: DisplayFeatureState.unknown,
            ),
            const DisplayFeature(
              bounds: Rect.fromLTRB(390, 0, 410, 600),
              type: DisplayFeatureType.hinge,
              state: DisplayFeatureState.unknown,
            ),
          ]
      );

      await tester.pumpWidget(
        MediaQuery(
          data: mediaQuery,
          child: const DisplayFeatureSubScreen(
            anchorPoint: Offset(1000, 1000),
            child: SizedBox(
              key: childKey,
              width: double.infinity,
              height: double.infinity,
            ),
          ),
        ),
      );

      final RenderBox renderBox = tester.renderObject(find.byKey(childKey));
      expect(renderBox.size.width, equals(390.0));
      expect(renderBox.size.height, equals(290.0));
      expect(renderBox.localToGlobal(Offset.zero), equals(const Offset(410,310)));
    });

    testWidgets('with non-splitting display features and anchorPoint', (WidgetTester tester) async {
      const Key childKey = Key('childKey');
181
      final MediaQueryData mediaQuery = MediaQueryData.fromWindow(WidgetsBinding.instance.window).copyWith(
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
          displayFeatures: <DisplayFeature>[
            // Top notch
            const DisplayFeature(
              bounds: Rect.fromLTRB(100, 0, 700, 100),
              type: DisplayFeatureType.cutout,
              state: DisplayFeatureState.unknown,
            ),
            // Bottom notch
            const DisplayFeature(
              bounds: Rect.fromLTRB(100, 500, 700, 600),
              type: DisplayFeatureType.cutout,
              state: DisplayFeatureState.unknown,
            ),
          ]
      );

      await tester.pumpWidget(
        MediaQuery(
          data: mediaQuery,
          child: const Directionality(
            textDirection: TextDirection.ltr,
            child: DisplayFeatureSubScreen(
              child: SizedBox(
                key: childKey,
                width: double.infinity,
                height: double.infinity,
              ),
            ),
          ),
        ),
      );

      // The display features provided are not wide enough to produce sub-screens
      final RenderBox renderBox = tester.renderObject(find.byKey(childKey));
      expect(renderBox.size.width, equals(800.0));
      expect(renderBox.size.height, equals(600.0));
      expect(renderBox.localToGlobal(Offset.zero), equals(Offset.zero));
    });
  });
}