display_feature_sub_screen_test.dart 8.25 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
// 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/widgets.dart';
import 'package:flutter_test/flutter_test.dart';

void main() {
  group('DisplayFeatureSubScreen', () {
12
    testWidgets('without Directionality or anchor', (WidgetTester tester) async {
13
      const Key childKey = Key('childKey');
14
      final MediaQueryData mediaQuery = MediaQueryData.fromView(tester.view).copyWith(
15 16 17 18 19 20 21 22 23 24 25 26 27
          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(
28
            child: SizedBox.expand(
29 30 31 32 33 34 35 36 37 38 39
              key: childKey,
            ),
          ),
        ),
      );

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

40
    testWidgets('with anchorPoint', (WidgetTester tester) async {
41
      const Key childKey = Key('childKey');
42
      final MediaQueryData mediaQuery = MediaQueryData.fromView(tester.view).copyWith(
43 44 45 46 47 48 49 50 51 52 53 54 55 56
          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),
57
            child: SizedBox.expand(
58 59 60 61 62 63 64 65 66 67 68 69 70
              key: childKey,
            ),
          ),
        ),
      );

      // 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)));
    });

71
    testWidgets('with infinity anchorpoint', (WidgetTester tester) async {
72
      const Key childKey = Key('childKey');
73
      final MediaQueryData mediaQuery = MediaQueryData.fromView(tester.view).copyWith(
74 75 76 77 78 79 80 81 82 83 84 85 86 87
          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,
88
            child: SizedBox.expand(
89 90 91 92 93 94 95 96 97 98 99 100 101
              key: childKey,
            ),
          ),
        ),
      );

      // 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)));
    });

102
    testWidgets('with horizontal hinge and anchorPoint', (WidgetTester tester) async {
103
      const Key childKey = Key('childKey');
104
      final MediaQueryData mediaQuery = MediaQueryData.fromView(tester.view).copyWith(
105 106 107 108 109 110 111 112 113 114 115 116 117 118
          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),
119
            child: SizedBox.expand(
120 121 122 123 124 125 126 127 128 129 130 131
              key: childKey,
            ),
          ),
        ),
      );

      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)));
    });

132
    testWidgets('with multiple display features and anchorPoint', (WidgetTester tester) async {
133
      const Key childKey = Key('childKey');
134
      final MediaQueryData mediaQuery = MediaQueryData.fromView(tester.view).copyWith(
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
          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),
154
            child: SizedBox.expand(
155 156 157 158 159 160 161 162 163 164 165 166
              key: childKey,
            ),
          ),
        ),
      );

      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)));
    });

167
    testWidgets('with non-splitting display features and anchorPoint', (WidgetTester tester) async {
168
      const Key childKey = Key('childKey');
169
      final MediaQueryData mediaQuery = MediaQueryData.fromView(tester.view).copyWith(
170 171 172 173 174 175 176 177 178 179 180 181 182
          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,
            ),
183 184 185 186 187
            const DisplayFeature(
              bounds: Rect.fromLTRB(0, 300, 800, 300),
              type: DisplayFeatureType.fold,
              state: DisplayFeatureState.postureFlat,
            ),
188 189 190 191 192 193 194 195 196
          ]
      );

      await tester.pumpWidget(
        MediaQuery(
          data: mediaQuery,
          child: const Directionality(
            textDirection: TextDirection.ltr,
            child: DisplayFeatureSubScreen(
197
              child: SizedBox.expand(
198 199 200 201 202 203 204 205 206 207 208 209 210
                key: childKey,
              ),
            ),
          ),
        ),
      );

      // 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));
    });
211

212
    testWidgets('with size 0 display feature in half-opened posture and anchorPoint', (WidgetTester tester) async {
213
      const Key childKey = Key('childKey');
214
      final MediaQueryData mediaQuery = MediaQueryData.fromView(tester.view).copyWith(
215 216 217 218 219 220 221 222 223 224 225 226 227 228
          displayFeatures: <DisplayFeature>[
            const DisplayFeature(
              bounds: Rect.fromLTRB(0, 300, 800, 300),
              type: DisplayFeatureType.fold,
              state: DisplayFeatureState.postureHalfOpened,
            ),
          ]
      );

      await tester.pumpWidget(
        MediaQuery(
          data: mediaQuery,
          child: const DisplayFeatureSubScreen(
            anchorPoint: Offset(1000, 1000),
229
            child: SizedBox.expand(
230 231 232 233 234 235 236 237 238 239 240
              key: childKey,
            ),
          ),
        ),
      );

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