media_query_test.dart 59.6 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5
import 'dart:ui' show Brightness, DisplayFeature, DisplayFeatureState, DisplayFeatureType, GestureSettings;
6

7
import 'package:flutter/gestures.dart';
8
import 'package:flutter/material.dart';
9
import 'package:flutter_test/flutter_test.dart';
10

11 12
class _MediaQueryAspectCase {
  const _MediaQueryAspectCase(this.method, this.data);
13
  final void Function(BuildContext) method;
14 15 16 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
  final MediaQueryData data;
}

class _MediaQueryAspectVariant extends TestVariant<_MediaQueryAspectCase> {
  _MediaQueryAspectVariant({
    required this.values
  });

  @override
  final List<_MediaQueryAspectCase> values;

  static _MediaQueryAspectCase? aspect;

  @override
  String describeValue(_MediaQueryAspectCase value) {
    return value.method.toString();
  }

  @override
  Future<_MediaQueryAspectCase?> setUp(_MediaQueryAspectCase value) async {
    final _MediaQueryAspectCase? oldAspect = aspect;
    aspect = value;
    return oldAspect;
  }

  @override
  Future<void> tearDown(_MediaQueryAspectCase value, _MediaQueryAspectCase? memento) async {
    aspect = memento;
  }
}

45
void main() {
46
  testWidgets('MediaQuery does not have a default', (WidgetTester tester) async {
47
    late final FlutterError error;
48 49
    // Cannot use tester.pumpWidget here because it wraps the widget in a View,
    // which introduces a MediaQuery ancestor.
50 51 52
    await tester.pumpWidget(
      wrapWithView: false,
      Builder(
53
        builder: (BuildContext context) {
54 55 56 57 58 59 60 61 62
          try {
            MediaQuery.of(context);
          } on FlutterError catch (e) {
            error = e;
          }
          return View(
            view: tester.view,
            child: const SizedBox(),
          );
63 64
        },
      ),
65
    );
66 67
    expect(
      error.toStringDeep(),
68
      startsWith(
69
        'FlutterError\n'
70 71 72 73 74
        '   No MediaQuery widget ancestor found.\n'
        '   Builder widgets require a MediaQuery widget ancestor.\n'
        '   The specific widget that could not find a MediaQuery ancestor\n'
        '   was:\n'
        '     Builder\n'
75 76 77 78 79 80 81
        '   The ownership chain for the affected widget is: "Builder ←', // Full ownership chain omitted, not relevant for test.
      ),
    );
    expect(
      error.toStringDeep(),
      endsWith(
        '[root]"\n' // End of ownership chain.
82
        '   No MediaQuery ancestor could be found starting from the context\n'
83 84 85
        '   that was passed to MediaQuery.of(). This can happen because the\n'
        '   context used is not a descendant of a View widget, which\n'
        '   introduces a MediaQuery.\n'
86 87
      ),
    );
88
  });
89

90
  testWidgets('MediaQuery.of finds a MediaQueryData when there is one', (WidgetTester tester) async {
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
    bool tested = false;
    await tester.pumpWidget(
      MediaQuery(
        data: const MediaQueryData(),
        child: Builder(
          builder: (BuildContext context) {
            final MediaQueryData data = MediaQuery.of(context);
            expect(data, isNotNull);
            tested = true;
            return Container();
          },
        ),
      ),
    );
    final dynamic exception = tester.takeException();
    expect(exception, isNull);
    expect(tested, isTrue);
  });

110
  testWidgets('MediaQuery.maybeOf defaults to null', (WidgetTester tester) async {
111
    bool tested = false;
112 113
    // Cannot use tester.pumpWidget here because it wraps the widget in a View,
    // which introduces a MediaQuery ancestor.
114 115 116
    await tester.pumpWidget(
      wrapWithView: false,
      Builder(
117
        builder: (BuildContext context) {
118
          final MediaQueryData? data = MediaQuery.maybeOf(context);
119 120
          expect(data, isNull);
          tested = true;
121 122 123 124
          return View(
            view: tester.view,
            child: const SizedBox(),
          );
125 126
        },
      ),
127
    );
128 129
    expect(tested, isTrue);
  });
130

131
  testWidgets('MediaQuery.maybeOf finds a MediaQueryData when there is one', (WidgetTester tester) async {
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
    bool tested = false;
    await tester.pumpWidget(
      MediaQuery(
        data: const MediaQueryData(),
        child: Builder(
          builder: (BuildContext context) {
            final MediaQueryData? data = MediaQuery.maybeOf(context);
            expect(data, isNotNull);
            tested = true;
            return Container();
          },
        ),
      ),
    );
    expect(tested, isTrue);
  });

149
  testWidgets('MediaQueryData.fromView is sane', (WidgetTester tester) async {
150
    final MediaQueryData data = MediaQueryData.fromView(tester.view);
151 152
    expect(data, hasOneLineDescription);
    expect(data.hashCode, equals(data.copyWith().hashCode));
153
    expect(data.size, equals(tester.view.physicalSize / tester.view.devicePixelRatio));
154 155 156
    expect(data.accessibleNavigation, false);
    expect(data.invertColors, false);
    expect(data.disableAnimations, false);
157
    expect(data.boldText, false);
158
    expect(data.highContrast, false);
159
    expect(data.onOffSwitchLabels, false);
160
    expect(data.platformBrightness, Brightness.light);
161
    expect(data.gestureSettings.touchSlop, null);
162
    expect(data.displayFeatures, isEmpty);
163
  });
164

165
  testWidgets('MediaQueryData.fromView uses platformData if provided', (WidgetTester tester) async {
166
    const MediaQueryData platformData = MediaQueryData(
167
      textScaler: TextScaler.linear(1234),
168 169 170 171 172 173
      platformBrightness: Brightness.dark,
      accessibleNavigation: true,
      invertColors: true,
      disableAnimations: true,
      boldText: true,
      highContrast: true,
174
      onOffSwitchLabels: true,
175 176 177 178
      alwaysUse24HourFormat: true,
      navigationMode: NavigationMode.directional,
    );

179
    final MediaQueryData data = MediaQueryData.fromView(tester.view, platformData: platformData);
180 181
    expect(data, hasOneLineDescription);
    expect(data.hashCode, data.copyWith().hashCode);
182 183
    expect(data.size, tester.view.physicalSize / tester.view.devicePixelRatio);
    expect(data.devicePixelRatio, tester.view.devicePixelRatio);
184
    expect(data.textScaler, TextScaler.linear(platformData.textScaleFactor));
185
    expect(data.platformBrightness, platformData.platformBrightness);
186 187 188 189
    expect(data.padding, EdgeInsets.fromViewPadding(tester.view.padding, tester.view.devicePixelRatio));
    expect(data.viewPadding, EdgeInsets.fromViewPadding(tester.view.viewPadding, tester.view.devicePixelRatio));
    expect(data.viewInsets, EdgeInsets.fromViewPadding(tester.view.viewInsets, tester.view.devicePixelRatio));
    expect(data.systemGestureInsets, EdgeInsets.fromViewPadding(tester.view.systemGestureInsets, tester.view.devicePixelRatio));
190 191 192 193 194
    expect(data.accessibleNavigation, platformData.accessibleNavigation);
    expect(data.invertColors, platformData.invertColors);
    expect(data.disableAnimations, platformData.disableAnimations);
    expect(data.boldText, platformData.boldText);
    expect(data.highContrast, platformData.highContrast);
195
    expect(data.onOffSwitchLabels, platformData.onOffSwitchLabels);
196 197
    expect(data.alwaysUse24HourFormat, platformData.alwaysUse24HourFormat);
    expect(data.navigationMode, platformData.navigationMode);
198 199
    expect(data.gestureSettings, DeviceGestureSettings.fromView(tester.view));
    expect(data.displayFeatures, tester.view.displayFeatures);
200 201
  });

202
  testWidgets('MediaQueryData.fromView uses data from platformDispatcher if no platformData is provided', (WidgetTester tester) async {
203
    tester.platformDispatcher
204 205 206
      ..textScaleFactorTestValue = 123
      ..platformBrightnessTestValue = Brightness.dark
      ..accessibilityFeaturesTestValue = FakeAccessibilityFeatures.allOn;
207
    addTearDown(() => tester.platformDispatcher.clearAllTestValues());
208

209
    final MediaQueryData data = MediaQueryData.fromView(tester.view);
210 211
    expect(data, hasOneLineDescription);
    expect(data.hashCode, data.copyWith().hashCode);
212 213
    expect(data.size, tester.view.physicalSize / tester.view.devicePixelRatio);
    expect(data.devicePixelRatio, tester.view.devicePixelRatio);
214
    expect(data.textScaler, TextScaler.linear(tester.platformDispatcher.textScaleFactor));
215 216 217 218 219 220 221 222 223 224
    expect(data.platformBrightness, tester.platformDispatcher.platformBrightness);
    expect(data.padding, EdgeInsets.fromViewPadding(tester.view.padding, tester.view.devicePixelRatio));
    expect(data.viewPadding, EdgeInsets.fromViewPadding(tester.view.viewPadding, tester.view.devicePixelRatio));
    expect(data.viewInsets, EdgeInsets.fromViewPadding(tester.view.viewInsets, tester.view.devicePixelRatio));
    expect(data.systemGestureInsets, EdgeInsets.fromViewPadding(tester.view.systemGestureInsets, tester.view.devicePixelRatio));
    expect(data.accessibleNavigation, tester.platformDispatcher.accessibilityFeatures.accessibleNavigation);
    expect(data.invertColors, tester.platformDispatcher.accessibilityFeatures.invertColors);
    expect(data.disableAnimations, tester.platformDispatcher.accessibilityFeatures.disableAnimations);
    expect(data.boldText, tester.platformDispatcher.accessibilityFeatures.boldText);
    expect(data.highContrast, tester.platformDispatcher.accessibilityFeatures.highContrast);
225
    expect(data.onOffSwitchLabels, tester.platformDispatcher.accessibilityFeatures.onOffSwitchLabels);
226
    expect(data.alwaysUse24HourFormat, tester.platformDispatcher.alwaysUse24HourFormat);
227
    expect(data.navigationMode, NavigationMode.traditional);
228 229
    expect(data.gestureSettings, DeviceGestureSettings.fromView(tester.view));
    expect(data.displayFeatures, tester.view.displayFeatures);
230 231
  });

232
  testWidgets('MediaQuery.fromView injects a new MediaQuery with data from view, preserving platform-specific data', (WidgetTester tester) async {
233
    const MediaQueryData platformData = MediaQueryData(
234
      textScaler: TextScaler.linear(1234),
235 236 237 238 239 240
      platformBrightness: Brightness.dark,
      accessibleNavigation: true,
      invertColors: true,
      disableAnimations: true,
      boldText: true,
      highContrast: true,
241
      onOffSwitchLabels: true,
242 243 244 245 246 247 248 249
      alwaysUse24HourFormat: true,
      navigationMode: NavigationMode.directional,
    );

    late MediaQueryData data;
    await tester.pumpWidget(MediaQuery(
      data: platformData,
      child: MediaQuery.fromView(
250
        view: tester.view,
251 252 253 254 255 256 257 258 259 260
        child: Builder(
          builder: (BuildContext context) {
            data = MediaQuery.of(context);
            return const Placeholder();
          },
        )
      )
    ));

    expect(data, isNot(platformData));
261 262
    expect(data.size, tester.view.physicalSize / tester.view.devicePixelRatio);
    expect(data.devicePixelRatio, tester.view.devicePixelRatio);
263
    expect(data.textScaler, TextScaler.linear(platformData.textScaleFactor));
264
    expect(data.platformBrightness, platformData.platformBrightness);
265 266 267 268
    expect(data.padding, EdgeInsets.fromViewPadding(tester.view.padding, tester.view.devicePixelRatio));
    expect(data.viewPadding, EdgeInsets.fromViewPadding(tester.view.viewPadding, tester.view.devicePixelRatio));
    expect(data.viewInsets, EdgeInsets.fromViewPadding(tester.view.viewInsets, tester.view.devicePixelRatio));
    expect(data.systemGestureInsets, EdgeInsets.fromViewPadding(tester.view.systemGestureInsets, tester.view.devicePixelRatio));
269 270 271 272 273
    expect(data.accessibleNavigation, platformData.accessibleNavigation);
    expect(data.invertColors, platformData.invertColors);
    expect(data.disableAnimations, platformData.disableAnimations);
    expect(data.boldText, platformData.boldText);
    expect(data.highContrast, platformData.highContrast);
274
    expect(data.onOffSwitchLabels, platformData.onOffSwitchLabels);
275 276
    expect(data.alwaysUse24HourFormat, platformData.alwaysUse24HourFormat);
    expect(data.navigationMode, platformData.navigationMode);
277 278
    expect(data.gestureSettings, DeviceGestureSettings.fromView(tester.view));
    expect(data.displayFeatures, tester.view.displayFeatures);
279 280
  });

281
  testWidgets('MediaQuery.fromView injects a new MediaQuery with data from view when no surrounding MediaQuery exists', (WidgetTester tester) async {
282
    tester.platformDispatcher
283 284 285
      ..textScaleFactorTestValue = 123
      ..platformBrightnessTestValue = Brightness.dark
      ..accessibilityFeaturesTestValue = FakeAccessibilityFeatures.allOn;
286
    addTearDown(() => tester.platformDispatcher.clearAllTestValues());
287 288 289

    late MediaQueryData data;
    MediaQueryData? outerData;
290 291 292
    await tester.pumpWidget(
      wrapWithView: false,
      Builder(
293 294 295
        builder: (BuildContext context) {
          outerData = MediaQuery.maybeOf(context);
          return MediaQuery.fromView(
296
              view: tester.view,
297 298 299
              child: Builder(
                builder: (BuildContext context) {
                  data = MediaQuery.of(context);
300 301 302 303
                  return View(
                    view: tester.view,
                    child: const SizedBox(),
                  );
304 305 306 307 308 309 310 311
                },
              )
          );
        },
      ),
    );

    expect(outerData, isNull);
312 313
    expect(data.size, tester.view.physicalSize / tester.view.devicePixelRatio);
    expect(data.devicePixelRatio, tester.view.devicePixelRatio);
314
    expect(data.textScaler, TextScaler.linear(tester.platformDispatcher.textScaleFactor));
315 316 317 318 319 320 321 322 323 324
    expect(data.platformBrightness, tester.platformDispatcher.platformBrightness);
    expect(data.padding, EdgeInsets.fromViewPadding(tester.view.padding, tester.view.devicePixelRatio));
    expect(data.viewPadding, EdgeInsets.fromViewPadding(tester.view.viewPadding, tester.view.devicePixelRatio));
    expect(data.viewInsets, EdgeInsets.fromViewPadding(tester.view.viewInsets, tester.view.devicePixelRatio));
    expect(data.systemGestureInsets, EdgeInsets.fromViewPadding(tester.view.systemGestureInsets, tester.view.devicePixelRatio));
    expect(data.accessibleNavigation, tester.platformDispatcher.accessibilityFeatures.accessibleNavigation);
    expect(data.invertColors, tester.platformDispatcher.accessibilityFeatures.invertColors);
    expect(data.disableAnimations, tester.platformDispatcher.accessibilityFeatures.disableAnimations);
    expect(data.boldText, tester.platformDispatcher.accessibilityFeatures.boldText);
    expect(data.highContrast, tester.platformDispatcher.accessibilityFeatures.highContrast);
325
    expect(data.onOffSwitchLabels, tester.platformDispatcher.accessibilityFeatures.onOffSwitchLabels);
326
    expect(data.alwaysUse24HourFormat, tester.platformDispatcher.alwaysUse24HourFormat);
327
    expect(data.navigationMode, NavigationMode.traditional);
328 329
    expect(data.gestureSettings, DeviceGestureSettings.fromView(tester.view));
    expect(data.displayFeatures, tester.view.displayFeatures);
330 331
  });

332
  testWidgets('MediaQuery.fromView updates on notifications (no parent data)', (WidgetTester tester) async {
333 334 335 336
    addTearDown(() => tester.platformDispatcher.clearAllTestValues());
    addTearDown(() => tester.view.reset());

    tester.platformDispatcher
337 338 339
      ..textScaleFactorTestValue = 123
      ..platformBrightnessTestValue = Brightness.dark
      ..accessibilityFeaturesTestValue = FakeAccessibilityFeatures.allOn;
340
    tester.view.devicePixelRatio = 44;
341 342 343 344

    late MediaQueryData data;
    MediaQueryData? outerData;
    int rebuildCount = 0;
345 346 347
    await tester.pumpWidget(
      wrapWithView: false,
      Builder(
348 349 350
        builder: (BuildContext context) {
          outerData = MediaQuery.maybeOf(context);
          return MediaQuery.fromView(
351
              view: tester.view,
352 353 354 355
              child: Builder(
                builder: (BuildContext context) {
                  rebuildCount++;
                  data = MediaQuery.of(context);
356 357 358 359
                  return View(
                    view: tester.view,
                    child: const SizedBox(),
                  );
360 361 362 363 364 365 366 367 368 369
                },
              ),
          );
        },
      ),
    );

    expect(outerData, isNull);
    expect(rebuildCount, 1);

370
    expect(data.textScaler, const TextScaler.linear(123));
371
    tester.platformDispatcher.textScaleFactorTestValue = 456;
372
    await tester.pump();
373
    expect(data.textScaler, const TextScaler.linear(456));
374 375 376
    expect(rebuildCount, 2);

    expect(data.platformBrightness, Brightness.dark);
377
    tester.platformDispatcher.platformBrightnessTestValue = Brightness.light;
378 379 380 381 382
    await tester.pump();
    expect(data.platformBrightness, Brightness.light);
    expect(rebuildCount, 3);

    expect(data.accessibleNavigation, true);
383
    tester.platformDispatcher.accessibilityFeaturesTestValue = const FakeAccessibilityFeatures();
384 385 386 387 388
    await tester.pump();
    expect(data.accessibleNavigation, false);
    expect(rebuildCount, 4);

    expect(data.devicePixelRatio, 44);
389
    tester.view.devicePixelRatio = 55;
390 391 392 393 394
    await tester.pump();
    expect(data.devicePixelRatio, 55);
    expect(rebuildCount, 5);
  });

395
  testWidgets('MediaQuery.fromView updates on notifications (with parent data)', (WidgetTester tester) async {
396 397 398 399
    addTearDown(() => tester.platformDispatcher.clearAllTestValues());
    addTearDown(() => tester.view.reset());

    tester.platformDispatcher
400 401 402
      ..textScaleFactorTestValue = 123
      ..platformBrightnessTestValue = Brightness.dark
      ..accessibilityFeaturesTestValue = FakeAccessibilityFeatures.allOn;
403
    tester.view.devicePixelRatio = 44;
404 405 406 407 408 409

    late MediaQueryData data;
    int rebuildCount = 0;
    await tester.pumpWidget(
      MediaQuery(
        data: const MediaQueryData(
410
          textScaler: TextScaler.linear(44),
411 412 413 414
          platformBrightness: Brightness.dark,
          accessibleNavigation: true,
        ),
        child: MediaQuery.fromView(
415
          view: tester.view,
416 417 418 419 420 421 422 423 424 425 426 427 428
          child: Builder(
            builder: (BuildContext context) {
              rebuildCount++;
              data = MediaQuery.of(context);
              return const Placeholder();
            },
          ),
        ),
      ),
    );

    expect(rebuildCount, 1);

429
    expect(data.textScaler, const TextScaler.linear(44));
430
    tester.platformDispatcher.textScaleFactorTestValue = 456;
431
    await tester.pump();
432
    expect(data.textScaler, const TextScaler.linear(44));
433 434 435
    expect(rebuildCount, 1);

    expect(data.platformBrightness, Brightness.dark);
436
    tester.platformDispatcher.platformBrightnessTestValue = Brightness.light;
437 438 439 440 441
    await tester.pump();
    expect(data.platformBrightness, Brightness.dark);
    expect(rebuildCount, 1);

    expect(data.accessibleNavigation, true);
442
    tester.platformDispatcher.accessibilityFeaturesTestValue = const FakeAccessibilityFeatures();
443 444 445 446 447
    await tester.pump();
    expect(data.accessibleNavigation, true);
    expect(rebuildCount, 1);

    expect(data.devicePixelRatio, 44);
448
    tester.view.devicePixelRatio = 55;
449 450 451 452 453
    await tester.pump();
    expect(data.devicePixelRatio, 55);
    expect(rebuildCount, 2);
  });

454
  testWidgets('MediaQuery.fromView updates when parent data changes', (WidgetTester tester) async {
455 456
    late MediaQueryData data;
    int rebuildCount = 0;
457
    TextScaler textScaler = const TextScaler.linear(55);
458 459 460 461 462 463
    late StateSetter stateSetter;
    await tester.pumpWidget(
      StatefulBuilder(
        builder: (BuildContext context, StateSetter setState) {
          stateSetter = setState;
          return MediaQuery(
464
            data: MediaQueryData(textScaler: textScaler),
465
            child: MediaQuery.fromView(
466
              view: tester.view,
467 468 469 470 471 472 473 474 475 476 477 478 479 480
              child: Builder(
                builder: (BuildContext context) {
                  rebuildCount++;
                  data = MediaQuery.of(context);
                  return const Placeholder();
                },
              ),
            ),
          );
        },
      ),
    );

    expect(rebuildCount, 1);
481
    expect(data.textScaler, const TextScaler.linear(55));
482 483

    stateSetter(() {
484
      textScaler = const TextScaler.linear(66);
485 486
    });
    await tester.pump();
487
    expect(data.textScaler, const TextScaler.linear(66));
488 489 490
    expect(rebuildCount, 2);
  });

491
  testWidgets('MediaQueryData.copyWith defaults to source', (WidgetTester tester) async {
492
    final MediaQueryData data = MediaQueryData.fromView(tester.view);
493 494 495
    final MediaQueryData copied = data.copyWith();
    expect(copied.size, data.size);
    expect(copied.devicePixelRatio, data.devicePixelRatio);
496
    expect(copied.textScaler, data.textScaler);
497
    expect(copied.padding, data.padding);
498
    expect(copied.viewPadding, data.viewPadding);
499
    expect(copied.viewInsets, data.viewInsets);
500
    expect(copied.systemGestureInsets, data.systemGestureInsets);
501
    expect(copied.alwaysUse24HourFormat, data.alwaysUse24HourFormat);
502 503 504
    expect(copied.accessibleNavigation, data.accessibleNavigation);
    expect(copied.invertColors, data.invertColors);
    expect(copied.disableAnimations, data.disableAnimations);
505
    expect(copied.boldText, data.boldText);
506
    expect(copied.highContrast, data.highContrast);
507
    expect(copied.onOffSwitchLabels, data.onOffSwitchLabels);
508
    expect(copied.platformBrightness, data.platformBrightness);
509
    expect(copied.gestureSettings, data.gestureSettings);
510
    expect(copied.displayFeatures, data.displayFeatures);
511 512
  });

513
  testWidgets('MediaQuery.copyWith copies specified values', (WidgetTester tester) async {
514 515 516 517
    // Random and unique double values are used to ensure that the correct
    // values are copied over exactly
    const Size customSize = Size(3.14, 2.72);
    const double customDevicePixelRatio = 1.41;
518
    const TextScaler customTextScaler = TextScaler.linear(1.23);
519 520 521 522
    const EdgeInsets customPadding = EdgeInsets.all(9.10938);
    const EdgeInsets customViewPadding = EdgeInsets.all(11.24031);
    const EdgeInsets customViewInsets = EdgeInsets.all(1.67262);
    const EdgeInsets customSystemGestureInsets = EdgeInsets.all(1.5556);
523
    const DeviceGestureSettings gestureSettings = DeviceGestureSettings(touchSlop: 8.0);
524 525 526 527 528 529 530
    const List<DisplayFeature> customDisplayFeatures = <DisplayFeature>[
      DisplayFeature(
        bounds: Rect.zero,
        type: DisplayFeatureType.cutout,
        state: DisplayFeatureState.unknown,
      ),
    ];
531

532
    final MediaQueryData data = MediaQueryData.fromView(tester.view);
533
    final MediaQueryData copied = data.copyWith(
534 535
      size: customSize,
      devicePixelRatio: customDevicePixelRatio,
536
      textScaler: customTextScaler,
537 538 539 540
      padding: customPadding,
      viewPadding: customViewPadding,
      viewInsets: customViewInsets,
      systemGestureInsets: customSystemGestureInsets,
541
      alwaysUse24HourFormat: true,
542 543 544
      accessibleNavigation: true,
      invertColors: true,
      disableAnimations: true,
545
      boldText: true,
546
      highContrast: true,
547
      onOffSwitchLabels: true,
548
      platformBrightness: Brightness.dark,
549
      navigationMode: NavigationMode.directional,
550
      gestureSettings: gestureSettings,
551
      displayFeatures: customDisplayFeatures,
552
    );
553 554
    expect(copied.size, customSize);
    expect(copied.devicePixelRatio, customDevicePixelRatio);
555
    expect(copied.textScaler, customTextScaler);
556 557 558 559
    expect(copied.padding, customPadding);
    expect(copied.viewPadding, customViewPadding);
    expect(copied.viewInsets, customViewInsets);
    expect(copied.systemGestureInsets, customSystemGestureInsets);
560
    expect(copied.alwaysUse24HourFormat, true);
561 562 563
    expect(copied.accessibleNavigation, true);
    expect(copied.invertColors, true);
    expect(copied.disableAnimations, true);
564
    expect(copied.boldText, true);
565
    expect(copied.highContrast, true);
566
    expect(copied.onOffSwitchLabels, true);
567
    expect(copied.platformBrightness, Brightness.dark);
568
    expect(copied.navigationMode, NavigationMode.directional);
569
    expect(copied.gestureSettings, gestureSettings);
570
    expect(copied.displayFeatures, customDisplayFeatures);
571
  });
572

573
  testWidgets('MediaQuery.removePadding removes specified padding', (WidgetTester tester) async {
574 575
    const Size size = Size(2.0, 4.0);
    const double devicePixelRatio = 2.0;
576
    const TextScaler textScaler = TextScaler.linear(1.2);
577
    const EdgeInsets padding = EdgeInsets.only(top: 1.0, right: 2.0, left: 3.0, bottom: 4.0);
578
    const EdgeInsets viewPadding = EdgeInsets.only(top: 6.0, right: 8.0, left: 10.0, bottom: 12.0);
579
    const EdgeInsets viewInsets = EdgeInsets.only(top: 5.0, right: 6.0, left: 7.0, bottom: 8.0);
580 581 582 583 584 585 586
    const List<DisplayFeature> displayFeatures = <DisplayFeature>[
      DisplayFeature(
        bounds: Rect.zero,
        type: DisplayFeatureType.cutout,
        state: DisplayFeatureState.unknown,
      ),
    ];
587

588
    late MediaQueryData unpadded;
589 590 591 592 593
    await tester.pumpWidget(
      MediaQuery(
        data: const MediaQueryData(
          size: size,
          devicePixelRatio: devicePixelRatio,
594
          textScaler: textScaler,
595
          padding: padding,
596
          viewPadding: viewPadding,
597 598 599 600 601 602
          viewInsets: viewInsets,
          alwaysUse24HourFormat: true,
          accessibleNavigation: true,
          invertColors: true,
          disableAnimations: true,
          boldText: true,
603
          highContrast: true,
604
          onOffSwitchLabels: true,
605
          navigationMode: NavigationMode.directional,
606
          displayFeatures: displayFeatures,
607 608 609 610 611 612 613 614 615 616 617
        ),
        child: Builder(
          builder: (BuildContext context) {
            return MediaQuery.removePadding(
              context: context,
              removeLeft: true,
              removeTop: true,
              removeRight: true,
              removeBottom: true,
              child: Builder(
                builder: (BuildContext context) {
618
                  unpadded = MediaQuery.of(context);
619
                  return Container();
620
                },
621 622 623 624
              ),
            );
          },
        ),
625
      ),
626 627 628 629
    );

    expect(unpadded.size, size);
    expect(unpadded.devicePixelRatio, devicePixelRatio);
630
    expect(unpadded.textScaler, textScaler);
631
    expect(unpadded.padding, EdgeInsets.zero);
632
    expect(unpadded.viewPadding, viewInsets);
633 634 635 636 637 638
    expect(unpadded.viewInsets, viewInsets);
    expect(unpadded.alwaysUse24HourFormat, true);
    expect(unpadded.accessibleNavigation, true);
    expect(unpadded.invertColors, true);
    expect(unpadded.disableAnimations, true);
    expect(unpadded.boldText, true);
639
    expect(unpadded.highContrast, true);
640
    expect(unpadded.onOffSwitchLabels, true);
641
    expect(unpadded.navigationMode, NavigationMode.directional);
642
    expect(unpadded.displayFeatures, displayFeatures);
643
  });
644

645
  testWidgets('MediaQuery.removePadding only removes specified padding', (WidgetTester tester) async {
646 647
    const Size size = Size(2.0, 4.0);
    const double devicePixelRatio = 2.0;
648
    const TextScaler textScaler = TextScaler.linear(1.2);
649 650 651
    const EdgeInsets padding = EdgeInsets.only(top: 1.0, right: 2.0, left: 3.0, bottom: 4.0);
    const EdgeInsets viewPadding = EdgeInsets.only(top: 6.0, right: 8.0, left: 10.0, bottom: 12.0);
    const EdgeInsets viewInsets = EdgeInsets.only(top: 5.0, right: 6.0, left: 7.0, bottom: 8.0);
652 653 654 655 656 657 658
    const List<DisplayFeature> displayFeatures = <DisplayFeature>[
      DisplayFeature(
        bounds: Rect.zero,
        type: DisplayFeatureType.cutout,
        state: DisplayFeatureState.unknown,
      ),
    ];
659

660
    late MediaQueryData unpadded;
661 662 663 664 665
    await tester.pumpWidget(
      MediaQuery(
        data: const MediaQueryData(
          size: size,
          devicePixelRatio: devicePixelRatio,
666
          textScaler: textScaler,
667 668 669 670 671 672 673 674
          padding: padding,
          viewPadding: viewPadding,
          viewInsets: viewInsets,
          alwaysUse24HourFormat: true,
          accessibleNavigation: true,
          invertColors: true,
          disableAnimations: true,
          boldText: true,
675
          highContrast: true,
676
          onOffSwitchLabels: true,
677
          navigationMode: NavigationMode.directional,
678
          displayFeatures: displayFeatures,
679 680 681 682 683 684 685 686
        ),
        child: Builder(
          builder: (BuildContext context) {
            return MediaQuery.removePadding(
              removeTop: true,
              context: context,
              child: Builder(
                builder: (BuildContext context) {
687
                  unpadded = MediaQuery.of(context);
688 689 690 691 692 693 694 695 696 697 698
                  return Container();
                },
              ),
            );
          },
        ),
      ),
    );

    expect(unpadded.size, size);
    expect(unpadded.devicePixelRatio, devicePixelRatio);
699
    expect(unpadded.textScaler, textScaler);
700 701 702 703 704 705 706 707
    expect(unpadded.padding, padding.copyWith(top: 0));
    expect(unpadded.viewPadding, viewPadding.copyWith(top: viewInsets.top));
    expect(unpadded.viewInsets, viewInsets);
    expect(unpadded.alwaysUse24HourFormat, true);
    expect(unpadded.accessibleNavigation, true);
    expect(unpadded.invertColors, true);
    expect(unpadded.disableAnimations, true);
    expect(unpadded.boldText, true);
708
    expect(unpadded.highContrast, true);
709
    expect(unpadded.onOffSwitchLabels, true);
710
    expect(unpadded.navigationMode, NavigationMode.directional);
711
    expect(unpadded.displayFeatures, displayFeatures);
712 713
  });

714
  testWidgets('MediaQuery.removeViewInsets removes specified viewInsets', (WidgetTester tester) async {
715
    const Size size = Size(2.0, 4.0);
716
    const double devicePixelRatio = 2.0;
717
    const TextScaler textScaler = TextScaler.linear(1.2);
718
    const EdgeInsets padding = EdgeInsets.only(top: 5.0, right: 6.0, left: 7.0, bottom: 8.0);
719
    const EdgeInsets viewPadding = EdgeInsets.only(top: 6.0, right: 8.0, left: 10.0, bottom: 12.0);
720
    const EdgeInsets viewInsets = EdgeInsets.only(top: 1.0, right: 2.0, left: 3.0, bottom: 4.0);
721 722 723 724 725 726 727
    const List<DisplayFeature> displayFeatures = <DisplayFeature>[
      DisplayFeature(
        bounds: Rect.zero,
        type: DisplayFeatureType.cutout,
        state: DisplayFeatureState.unknown,
      ),
    ];
728

729
    late MediaQueryData unpadded;
730
    await tester.pumpWidget(
731
      MediaQuery(
732 733 734
        data: const MediaQueryData(
          size: size,
          devicePixelRatio: devicePixelRatio,
735
          textScaler: textScaler,
736
          padding: padding,
737
          viewPadding: viewPadding,
738 739
          viewInsets: viewInsets,
          alwaysUse24HourFormat: true,
740 741 742
          accessibleNavigation: true,
          invertColors: true,
          disableAnimations: true,
743
          boldText: true,
744
          highContrast: true,
745
          onOffSwitchLabels: true,
746
          navigationMode: NavigationMode.directional,
747
          displayFeatures: displayFeatures,
748
        ),
749
        child: Builder(
750
          builder: (BuildContext context) {
751
            return MediaQuery.removeViewInsets(
752 753 754 755 756
              context: context,
              removeLeft: true,
              removeTop: true,
              removeRight: true,
              removeBottom: true,
757
              child: Builder(
758
                builder: (BuildContext context) {
759
                  unpadded = MediaQuery.of(context);
760
                  return Container();
761
                },
762 763 764 765
              ),
            );
          },
        ),
766
      ),
767 768 769 770
    );

    expect(unpadded.size, size);
    expect(unpadded.devicePixelRatio, devicePixelRatio);
771
    expect(unpadded.textScaler, textScaler);
772
    expect(unpadded.padding, padding);
773
    expect(unpadded.viewPadding, padding);
774 775
    expect(unpadded.viewInsets, EdgeInsets.zero);
    expect(unpadded.alwaysUse24HourFormat, true);
776 777 778
    expect(unpadded.accessibleNavigation, true);
    expect(unpadded.invertColors, true);
    expect(unpadded.disableAnimations, true);
779
    expect(unpadded.boldText, true);
780
    expect(unpadded.highContrast, true);
781
    expect(unpadded.onOffSwitchLabels, true);
782
    expect(unpadded.navigationMode, NavigationMode.directional);
783
    expect(unpadded.displayFeatures, displayFeatures);
784
  });
785

786
  testWidgets('MediaQuery.removeViewInsets removes only specified viewInsets', (WidgetTester tester) async {
787 788
    const Size size = Size(2.0, 4.0);
    const double devicePixelRatio = 2.0;
789
    const TextScaler textScaler = TextScaler.linear(1.2);
790 791 792
    const EdgeInsets padding = EdgeInsets.only(top: 5.0, right: 6.0, left: 7.0, bottom: 8.0);
    const EdgeInsets viewPadding = EdgeInsets.only(top: 6.0, right: 8.0, left: 10.0, bottom: 12.0);
    const EdgeInsets viewInsets = EdgeInsets.only(top: 1.0, right: 2.0, left: 3.0, bottom: 4.0);
793 794 795 796 797 798 799
    const List<DisplayFeature> displayFeatures = <DisplayFeature>[
      DisplayFeature(
        bounds: Rect.zero,
        type: DisplayFeatureType.cutout,
        state: DisplayFeatureState.unknown,
      ),
    ];
800

801
    late MediaQueryData unpadded;
802 803 804 805 806
    await tester.pumpWidget(
      MediaQuery(
        data: const MediaQueryData(
          size: size,
          devicePixelRatio: devicePixelRatio,
807
          textScaler: textScaler,
808 809 810 811 812 813 814 815
          padding: padding,
          viewPadding: viewPadding,
          viewInsets: viewInsets,
          alwaysUse24HourFormat: true,
          accessibleNavigation: true,
          invertColors: true,
          disableAnimations: true,
          boldText: true,
816
          highContrast: true,
817
          onOffSwitchLabels: true,
818
          navigationMode: NavigationMode.directional,
819
          displayFeatures: displayFeatures,
820 821 822 823 824 825 826 827
        ),
        child: Builder(
          builder: (BuildContext context) {
            return MediaQuery.removeViewInsets(
              context: context,
              removeBottom: true,
              child: Builder(
                builder: (BuildContext context) {
828
                  unpadded = MediaQuery.of(context);
829 830 831 832 833 834 835 836 837 838 839
                  return Container();
                },
              ),
            );
          },
        ),
      ),
    );

    expect(unpadded.size, size);
    expect(unpadded.devicePixelRatio, devicePixelRatio);
840
    expect(unpadded.textScaler, textScaler);
841 842 843 844 845 846 847 848
    expect(unpadded.padding, padding);
    expect(unpadded.viewPadding, viewPadding.copyWith(bottom: 8));
    expect(unpadded.viewInsets, viewInsets.copyWith(bottom: 0));
    expect(unpadded.alwaysUse24HourFormat, true);
    expect(unpadded.accessibleNavigation, true);
    expect(unpadded.invertColors, true);
    expect(unpadded.disableAnimations, true);
    expect(unpadded.boldText, true);
849
    expect(unpadded.highContrast, true);
850
    expect(unpadded.onOffSwitchLabels, true);
851
    expect(unpadded.navigationMode, NavigationMode.directional);
852
    expect(unpadded.displayFeatures, displayFeatures);
853 854
  });

855
  testWidgets('MediaQuery.removeViewPadding removes specified viewPadding', (WidgetTester tester) async {
856 857
    const Size size = Size(2.0, 4.0);
    const double devicePixelRatio = 2.0;
858
    const TextScaler textScaler = TextScaler.linear(1.2);
859 860 861
    const EdgeInsets padding = EdgeInsets.only(top: 5.0, right: 6.0, left: 7.0, bottom: 8.0);
    const EdgeInsets viewPadding = EdgeInsets.only(top: 6.0, right: 8.0, left: 10.0, bottom: 12.0);
    const EdgeInsets viewInsets = EdgeInsets.only(top: 1.0, right: 2.0, left: 3.0, bottom: 4.0);
862 863 864 865 866 867 868
    const List<DisplayFeature> displayFeatures = <DisplayFeature>[
      DisplayFeature(
        bounds: Rect.zero,
        type: DisplayFeatureType.cutout,
        state: DisplayFeatureState.unknown,
      ),
    ];
869

870
    late MediaQueryData unpadded;
871 872 873 874 875
    await tester.pumpWidget(
      MediaQuery(
        data: const MediaQueryData(
          size: size,
          devicePixelRatio: devicePixelRatio,
876
          textScaler: textScaler,
877
          padding: padding,
878
          viewPadding: viewPadding,
879 880 881 882 883 884
          viewInsets: viewInsets,
          alwaysUse24HourFormat: true,
          accessibleNavigation: true,
          invertColors: true,
          disableAnimations: true,
          boldText: true,
885
          highContrast: true,
886
          onOffSwitchLabels: true,
887
          navigationMode: NavigationMode.directional,
888
          displayFeatures: displayFeatures,
889 890 891 892 893 894 895 896 897 898 899
        ),
        child: Builder(
          builder: (BuildContext context) {
            return MediaQuery.removeViewPadding(
              context: context,
              removeLeft: true,
              removeTop: true,
              removeRight: true,
              removeBottom: true,
              child: Builder(
                builder: (BuildContext context) {
900
                  unpadded = MediaQuery.of(context);
901
                  return Container();
902
                },
903 904 905 906
              ),
            );
          },
        ),
907
      ),
908 909 910 911
    );

    expect(unpadded.size, size);
    expect(unpadded.devicePixelRatio, devicePixelRatio);
912
    expect(unpadded.textScaler, textScaler);
913 914 915 916 917 918 919 920
    expect(unpadded.padding, EdgeInsets.zero);
    expect(unpadded.viewPadding, EdgeInsets.zero);
    expect(unpadded.viewInsets, viewInsets);
    expect(unpadded.alwaysUse24HourFormat, true);
    expect(unpadded.accessibleNavigation, true);
    expect(unpadded.invertColors, true);
    expect(unpadded.disableAnimations, true);
    expect(unpadded.boldText, true);
921
    expect(unpadded.highContrast, true);
922
    expect(unpadded.onOffSwitchLabels, true);
923
    expect(unpadded.navigationMode, NavigationMode.directional);
924
    expect(unpadded.displayFeatures, displayFeatures);
925 926
  });

927
  testWidgets('MediaQuery.removeViewPadding removes only specified viewPadding', (WidgetTester tester) async {
928 929
    const Size size = Size(2.0, 4.0);
    const double devicePixelRatio = 2.0;
930
    const TextScaler textScaler = TextScaler.linear(1.2);
931 932 933
    const EdgeInsets padding = EdgeInsets.only(top: 5.0, right: 6.0, left: 7.0, bottom: 8.0);
    const EdgeInsets viewPadding = EdgeInsets.only(top: 6.0, right: 8.0, left: 10.0, bottom: 12.0);
    const EdgeInsets viewInsets = EdgeInsets.only(top: 1.0, right: 2.0, left: 3.0, bottom: 4.0);
934 935 936 937 938 939 940
    const List<DisplayFeature> displayFeatures = <DisplayFeature>[
      DisplayFeature(
        bounds: Rect.zero,
        type: DisplayFeatureType.cutout,
        state: DisplayFeatureState.unknown,
      ),
    ];
941

942
    late MediaQueryData unpadded;
943 944 945 946 947
    await tester.pumpWidget(
      MediaQuery(
        data: const MediaQueryData(
          size: size,
          devicePixelRatio: devicePixelRatio,
948
          textScaler: textScaler,
949 950 951 952 953 954 955 956
          padding: padding,
          viewPadding: viewPadding,
          viewInsets: viewInsets,
          alwaysUse24HourFormat: true,
          accessibleNavigation: true,
          invertColors: true,
          disableAnimations: true,
          boldText: true,
957
          highContrast: true,
958
          onOffSwitchLabels: true,
959
          navigationMode: NavigationMode.directional,
960
          displayFeatures: displayFeatures,
961 962 963 964 965 966 967 968
        ),
        child: Builder(
          builder: (BuildContext context) {
            return MediaQuery.removeViewPadding(
              context: context,
              removeLeft: true,
              child: Builder(
                builder: (BuildContext context) {
969
                  unpadded = MediaQuery.of(context);
970 971 972 973 974 975 976 977 978 979 980
                  return Container();
                },
              ),
            );
          },
        ),
      ),
    );

    expect(unpadded.size, size);
    expect(unpadded.devicePixelRatio, devicePixelRatio);
981
    expect(unpadded.textScaler, textScaler);
982 983 984 985 986 987 988 989
    expect(unpadded.padding, padding.copyWith(left: 0));
    expect(unpadded.viewPadding, viewPadding.copyWith(left: 0));
    expect(unpadded.viewInsets, viewInsets);
    expect(unpadded.alwaysUse24HourFormat, true);
    expect(unpadded.accessibleNavigation, true);
    expect(unpadded.invertColors, true);
    expect(unpadded.disableAnimations, true);
    expect(unpadded.boldText, true);
990
    expect(unpadded.highContrast, true);
991
    expect(unpadded.onOffSwitchLabels, true);
992
    expect(unpadded.navigationMode, NavigationMode.directional);
993
    expect(unpadded.displayFeatures, displayFeatures);
994 995
  });

996
  testWidgets('MediaQuery.textScalerOf', (WidgetTester tester) async {
997 998
    late TextScaler outsideTextScaler;
    late TextScaler insideTextScaler;
999 1000 1001 1002

    await tester.pumpWidget(
      Builder(
        builder: (BuildContext context) {
1003
          outsideTextScaler = MediaQuery.textScalerOf(context);
1004 1005
          return MediaQuery(
            data: const MediaQueryData(
1006
              textScaler: TextScaler.linear(4.0),
1007 1008 1009
            ),
            child: Builder(
              builder: (BuildContext context) {
1010
                insideTextScaler = MediaQuery.textScalerOf(context);
1011 1012 1013 1014 1015 1016 1017 1018
                return Container();
              },
            ),
          );
        },
      ),
    );

1019 1020
    expect(outsideTextScaler, TextScaler.noScaling);
    expect(insideTextScaler, const TextScaler.linear(4.0));
1021
  });
1022

1023
  testWidgets('MediaQuery.platformBrightnessOf', (WidgetTester tester) async {
1024 1025
    late Brightness outsideBrightness;
    late Brightness insideBrightness;
1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049

    await tester.pumpWidget(
      Builder(
        builder: (BuildContext context) {
          outsideBrightness = MediaQuery.platformBrightnessOf(context);
          return MediaQuery(
            data: const MediaQueryData(
              platformBrightness: Brightness.dark,
            ),
            child: Builder(
              builder: (BuildContext context) {
                insideBrightness = MediaQuery.platformBrightnessOf(context);
                return Container();
              },
            ),
          );
        },
      ),
    );

    expect(outsideBrightness, Brightness.light);
    expect(insideBrightness, Brightness.dark);
  });

1050
  testWidgets('MediaQuery.highContrastOf', (WidgetTester tester) async {
1051 1052
    late bool outsideHighContrast;
    late bool insideHighContrast;
1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076

    await tester.pumpWidget(
      Builder(
        builder: (BuildContext context) {
          outsideHighContrast = MediaQuery.highContrastOf(context);
          return MediaQuery(
            data: const MediaQueryData(
              highContrast: true,
            ),
            child: Builder(
              builder: (BuildContext context) {
                insideHighContrast = MediaQuery.highContrastOf(context);
                return Container();
              },
            ),
          );
        },
      ),
    );

    expect(outsideHighContrast, false);
    expect(insideHighContrast, true);
  });

1077
  testWidgets('MediaQuery.onOffSwitchLabelsOf', (WidgetTester tester) async {
1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103
    late bool outsideOnOffSwitchLabels;
    late bool insideOnOffSwitchLabels;

    await tester.pumpWidget(
      Builder(
        builder: (BuildContext context) {
          outsideOnOffSwitchLabels = MediaQuery.onOffSwitchLabelsOf(context);
          return MediaQuery(
            data: const MediaQueryData(
              onOffSwitchLabels: true,
            ),
            child: Builder(
              builder: (BuildContext context) {
                insideOnOffSwitchLabels = MediaQuery.onOffSwitchLabelsOf(context);
                return Container();
              },
            ),
          );
        },
      ),
    );

    expect(outsideOnOffSwitchLabels, false);
    expect(insideOnOffSwitchLabels, true);
  });

1104
  testWidgets('MediaQuery.boldTextOf', (WidgetTester tester) async {
1105 1106
    late bool outsideBoldTextOverride;
    late bool insideBoldTextOverride;
1107 1108

    await tester.pumpWidget(
1109
      Builder(
1110
        builder: (BuildContext context) {
1111
          outsideBoldTextOverride = MediaQuery.boldTextOf(context);
1112
          return MediaQuery(
1113 1114 1115
            data: const MediaQueryData(
              boldText: true,
            ),
1116
            child: Builder(
1117
              builder: (BuildContext context) {
1118
                insideBoldTextOverride = MediaQuery.boldTextOf(context);
1119
                return Container();
1120 1121 1122 1123 1124 1125 1126 1127 1128 1129
              },
            ),
          );
        },
      ),
    );

    expect(outsideBoldTextOverride, false);
    expect(insideBoldTextOverride, true);
  });
1130

1131
  testWidgets('MediaQuery.fromView creates a MediaQuery', (WidgetTester tester) async {
1132 1133
    MediaQuery? mediaQueryOutside;
    MediaQuery? mediaQueryInside;
1134 1135 1136 1137

    await tester.pumpWidget(
      Builder(
        builder: (BuildContext context) {
1138
          mediaQueryOutside = context.findAncestorWidgetOfExactType<MediaQuery>();
1139 1140
          return MediaQuery.fromView(
            view: View.of(context),
1141 1142
            child: Builder(
              builder: (BuildContext context) {
1143
                mediaQueryInside = context.findAncestorWidgetOfExactType<MediaQuery>();
1144 1145 1146 1147 1148 1149 1150 1151
                return const SizedBox();
              },
            ),
          );
        },
      ),
    );

1152 1153
    expect(mediaQueryInside, isNotNull);
    expect(mediaQueryOutside, isNot(mediaQueryInside));
1154 1155
  });

1156
  testWidgets('MediaQueryData.fromWindow is created using window values', (WidgetTester tester) async {
1157
    final MediaQueryData windowData = MediaQueryData.fromWindow(tester.view);
1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172
    late MediaQueryData fromWindowData;

    await tester.pumpWidget(
      MediaQuery.fromWindow(
        child: Builder(
          builder: (BuildContext context) {
            fromWindowData = MediaQuery.of(context);
            return const SizedBox();
          },
        ),
      ),
    );

    expect(windowData, equals(fromWindowData));
  });
1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190

  test('DeviceGestureSettings has reasonable hashCode', () {
    final DeviceGestureSettings settingsA = DeviceGestureSettings(touchSlop: nonconst(16));
    final DeviceGestureSettings settingsB = DeviceGestureSettings(touchSlop: nonconst(8));
    final DeviceGestureSettings settingsC = DeviceGestureSettings(touchSlop: nonconst(16));

    expect(settingsA.hashCode, settingsC.hashCode);
    expect(settingsA.hashCode, isNot(settingsB.hashCode));
  });

  test('DeviceGestureSettings has reasonable equality', () {
    final DeviceGestureSettings settingsA = DeviceGestureSettings(touchSlop: nonconst(16));
    final DeviceGestureSettings settingsB = DeviceGestureSettings(touchSlop: nonconst(8));
    final DeviceGestureSettings settingsC = DeviceGestureSettings(touchSlop: nonconst(16));

    expect(settingsA, equals(settingsC));
    expect(settingsA, isNot(settingsB));
  });
1191

1192
  testWidgets('MediaQuery.removeDisplayFeatures removes specified display features and padding', (WidgetTester tester) async {
1193 1194
    const Size size = Size(82.0, 40.0);
    const double devicePixelRatio = 2.0;
1195
    const TextScaler textScaler = TextScaler.linear(1.2);
1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220
    const EdgeInsets padding = EdgeInsets.only(top: 1.0, right: 2.0, left: 3.0, bottom: 4.0);
    const EdgeInsets viewPadding = EdgeInsets.only(top: 6.0, right: 8.0, left: 10.0, bottom: 12.0);
    const EdgeInsets viewInsets = EdgeInsets.only(top: 5.0, right: 6.0, left: 7.0, bottom: 8.0);
    const List<DisplayFeature> displayFeatures = <DisplayFeature>[
      DisplayFeature(
        bounds: Rect.fromLTRB(40, 0, 42, 40),
        type: DisplayFeatureType.hinge,
        state: DisplayFeatureState.postureFlat,
      ),
      DisplayFeature(
        bounds: Rect.fromLTRB(70, 10, 74, 14),
        type: DisplayFeatureType.cutout,
        state: DisplayFeatureState.unknown,
      ),
    ];

    // A section of the screen that intersects no display feature or padding area
    const Rect subScreen = Rect.fromLTRB(20, 10, 40, 20);

    late MediaQueryData subScreenMediaQuery;
    await tester.pumpWidget(
      MediaQuery(
        data: const MediaQueryData(
          size: size,
          devicePixelRatio: devicePixelRatio,
1221
          textScaler: textScaler,
1222 1223 1224 1225 1226 1227 1228 1229 1230
          padding: padding,
          viewPadding: viewPadding,
          viewInsets: viewInsets,
          alwaysUse24HourFormat: true,
          accessibleNavigation: true,
          invertColors: true,
          disableAnimations: true,
          boldText: true,
          highContrast: true,
1231
          onOffSwitchLabels: true,
1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251
          displayFeatures: displayFeatures,
        ),
        child: Builder(
          builder: (BuildContext context) {
            return MediaQuery(
              data: MediaQuery.of(context).removeDisplayFeatures(subScreen),
              child: Builder(
                builder: (BuildContext context) {
                  subScreenMediaQuery = MediaQuery.of(context);
                  return Container();
                },
              ),
            );
          },
        ),
      ),
    );

    expect(subScreenMediaQuery.size, size);
    expect(subScreenMediaQuery.devicePixelRatio, devicePixelRatio);
1252
    expect(subScreenMediaQuery.textScaler, textScaler);
1253 1254 1255 1256 1257 1258 1259 1260 1261
    expect(subScreenMediaQuery.padding, EdgeInsets.zero);
    expect(subScreenMediaQuery.viewPadding, EdgeInsets.zero);
    expect(subScreenMediaQuery.viewInsets, EdgeInsets.zero);
    expect(subScreenMediaQuery.alwaysUse24HourFormat, true);
    expect(subScreenMediaQuery.accessibleNavigation, true);
    expect(subScreenMediaQuery.invertColors, true);
    expect(subScreenMediaQuery.disableAnimations, true);
    expect(subScreenMediaQuery.boldText, true);
    expect(subScreenMediaQuery.highContrast, true);
1262
    expect(subScreenMediaQuery.onOffSwitchLabels, true);
1263 1264 1265
    expect(subScreenMediaQuery.displayFeatures, isEmpty);
  });

1266
  testWidgets('MediaQuery.removePadding only removes specified display features and padding', (WidgetTester tester) async {
1267 1268
    const Size size = Size(82.0, 40.0);
    const double devicePixelRatio = 2.0;
1269
    const TextScaler textScaler = TextScaler.linear(1.2);
1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295
    const EdgeInsets padding = EdgeInsets.only(top: 1.0, right: 2.0, left: 3.0, bottom: 4.0);
    const EdgeInsets viewPadding = EdgeInsets.only(top: 6.0, right: 8.0, left: 46.0, bottom: 12.0);
    const EdgeInsets viewInsets = EdgeInsets.only(top: 5.0, right: 6.0, left: 7.0, bottom: 8.0);
    const DisplayFeature cutoutDisplayFeature = DisplayFeature(
      bounds: Rect.fromLTRB(70, 10, 74, 14),
      type: DisplayFeatureType.cutout,
      state: DisplayFeatureState.unknown,
    );
    const List<DisplayFeature> displayFeatures = <DisplayFeature>[
      DisplayFeature(
        bounds: Rect.fromLTRB(40, 0, 42, 40),
        type: DisplayFeatureType.hinge,
        state: DisplayFeatureState.postureFlat,
      ),
      cutoutDisplayFeature,
    ];

    // A section of the screen that does contain display features and padding
    const Rect subScreen = Rect.fromLTRB(42, 0, 82, 40);

    late MediaQueryData subScreenMediaQuery;
    await tester.pumpWidget(
      MediaQuery(
        data: const MediaQueryData(
          size: size,
          devicePixelRatio: devicePixelRatio,
1296
          textScaler: textScaler,
1297 1298 1299 1300 1301 1302 1303 1304 1305
          padding: padding,
          viewPadding: viewPadding,
          viewInsets: viewInsets,
          alwaysUse24HourFormat: true,
          accessibleNavigation: true,
          invertColors: true,
          disableAnimations: true,
          boldText: true,
          highContrast: true,
1306
          onOffSwitchLabels: true,
1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326
          displayFeatures: displayFeatures,
        ),
        child: Builder(
          builder: (BuildContext context) {
            return MediaQuery(
              data: MediaQuery.of(context).removeDisplayFeatures(subScreen),
              child: Builder(
                builder: (BuildContext context) {
                  subScreenMediaQuery = MediaQuery.of(context);
                  return Container();
                },
              ),
            );
          },
        ),
      ),
    );

    expect(subScreenMediaQuery.size, size);
    expect(subScreenMediaQuery.devicePixelRatio, devicePixelRatio);
1327
    expect(subScreenMediaQuery.textScaler, textScaler);
1328 1329 1330 1331 1332 1333
    expect(
      subScreenMediaQuery.padding,
      const EdgeInsets.only(top: 1.0, right: 2.0, bottom: 4.0),
    );
    expect(
      subScreenMediaQuery.viewPadding,
1334
      const EdgeInsets.only(top: 6.0, left: 4.0, right: 8.0, bottom: 12.0),
1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345
    );
    expect(
      subScreenMediaQuery.viewInsets,
      const EdgeInsets.only(top: 5.0, right: 6.0, bottom: 8.0),
    );
    expect(subScreenMediaQuery.alwaysUse24HourFormat, true);
    expect(subScreenMediaQuery.accessibleNavigation, true);
    expect(subScreenMediaQuery.invertColors, true);
    expect(subScreenMediaQuery.disableAnimations, true);
    expect(subScreenMediaQuery.boldText, true);
    expect(subScreenMediaQuery.highContrast, true);
1346
    expect(subScreenMediaQuery.onOffSwitchLabels, true);
1347 1348
    expect(subScreenMediaQuery.displayFeatures, <DisplayFeature>[cutoutDisplayFeature]);
  });
1349

1350
  testWidgets('MediaQueryData.gestureSettings is set from view.gestureSettings', (WidgetTester tester) async {
1351 1352
    tester.view.gestureSettings = const GestureSettings(physicalDoubleTapSlop: 100, physicalTouchSlop: 100);
    addTearDown(() => tester.view.resetGestureSettings());
1353

1354
    expect(MediaQueryData.fromView(tester.view).gestureSettings.touchSlop, closeTo(33.33, 0.1)); // Repeating, of course
1355
  });
1356

1357
  testWidgets('MediaQuery can be partially depended-on', (WidgetTester tester) async {
1358 1359
    MediaQueryData data = const MediaQueryData(
      size: Size(800, 600),
1360
      textScaler: TextScaler.linear(1.1),
1361 1362 1363
    );

    int sizeBuildCount = 0;
1364
    int textScalerBuildCount = 0;
1365 1366 1367 1368 1369 1370 1371 1372

    final Widget showSize = Builder(
      builder: (BuildContext context) {
        sizeBuildCount++;
        return Text('size: ${MediaQuery.sizeOf(context)}');
      }
    );

1373
    final Widget showTextScaler = Builder(
1374
      builder: (BuildContext context) {
1375 1376
        textScalerBuildCount++;
        return Text('textScaler: ${MediaQuery.textScalerOf(context)}');
1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387
      }
    );

    final Widget page = StatefulBuilder(
      builder: (BuildContext context, StateSetter setState) {
        return MediaQuery(
          data: data,
          child: Center(
            child: Column(
              children: <Widget>[
                showSize,
1388
                showTextScaler,
1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399
                ElevatedButton(
                  onPressed: () {
                    setState(() {
                      data = data.copyWith(size: Size(data.size.width + 100, data.size.height));
                    });
                  },
                  child: const Text('Increase width by 100')
                ),
                ElevatedButton(
                  onPressed: () {
                    setState(() {
1400
                      data = data.copyWith(textScaler: TextScaler.noScaling);
1401 1402
                    });
                  },
1403
                  child: const Text('Disable text scaling')
1404 1405 1406 1407 1408 1409 1410 1411 1412 1413
                )
              ]
            )
          )
        );
      },
    );

    await tester.pumpWidget(MaterialApp(home: page));
    expect(find.text('size: Size(800.0, 600.0)'), findsOneWidget);
1414
    expect(find.text('textScaler: linear (1.1x)'), findsOneWidget);
1415
    expect(sizeBuildCount, 1);
1416
    expect(textScalerBuildCount, 1);
1417 1418 1419 1420

    await tester.tap(find.text('Increase width by 100'));
    await tester.pumpAndSettle();
    expect(find.text('size: Size(900.0, 600.0)'), findsOneWidget);
1421
    expect(find.text('textScaler: linear (1.1x)'), findsOneWidget);
1422
    expect(sizeBuildCount, 2);
1423
    expect(textScalerBuildCount, 1);
1424

1425
    await tester.tap(find.text('Disable text scaling'));
1426 1427
    await tester.pumpAndSettle();
    expect(find.text('size: Size(900.0, 600.0)'), findsOneWidget);
1428
    expect(find.text('textScaler: no scaling'), findsOneWidget);
1429
    expect(sizeBuildCount, 2);
1430
    expect(textScalerBuildCount, 2);
1431
  });
1432

1433
  testWidgets('MediaQuery partial dependencies', (WidgetTester tester) async {
1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498
    MediaQueryData data = const MediaQueryData();

    int buildCount = 0;

    final Widget builder = Builder(
      builder: (BuildContext context) {
        _MediaQueryAspectVariant.aspect!.method(context);
        buildCount++;
        return const SizedBox.shrink();
      }
    );

    final Widget page = StatefulBuilder(
      builder: (BuildContext context, StateSetter setState) {
        return MediaQuery(
          data: data,
          child: ListView(
            children: <Widget>[
              builder,
              ElevatedButton(
                onPressed: () {
                  setState(() {
                    data = _MediaQueryAspectVariant.aspect!.data;
                  });
                },
                child: const Text('Change data')
              ),
              ElevatedButton(
                onPressed: () {
                  setState(() {
                    data = data.copyWith();
                  });
                },
                child: const Text('Copy data')
              )
            ]
          )
        );
      },
    );

    await tester.pumpWidget(MaterialApp(home: page));
    expect(buildCount, 1);

    await tester.tap(find.text('Copy data'));
    await tester.pumpAndSettle();
    expect(buildCount, 1);

    await tester.tap(find.text('Change data'));
    await tester.pumpAndSettle();
    expect(buildCount, 2);

    await tester.tap(find.text('Copy data'));
    await tester.pumpAndSettle();
    expect(buildCount, 2);
  }, variant: _MediaQueryAspectVariant(
    values: <_MediaQueryAspectCase>[
      const _MediaQueryAspectCase(MediaQuery.sizeOf, MediaQueryData(size: Size(1, 1))),
      const _MediaQueryAspectCase(MediaQuery.maybeSizeOf, MediaQueryData(size: Size(1, 1))),
      const _MediaQueryAspectCase(MediaQuery.orientationOf, MediaQueryData(size: Size(2, 1))),
      const _MediaQueryAspectCase(MediaQuery.maybeOrientationOf, MediaQueryData(size: Size(2, 1))),
      const _MediaQueryAspectCase(MediaQuery.devicePixelRatioOf, MediaQueryData(devicePixelRatio: 1.1)),
      const _MediaQueryAspectCase(MediaQuery.maybeDevicePixelRatioOf, MediaQueryData(devicePixelRatio: 1.1)),
      const _MediaQueryAspectCase(MediaQuery.textScaleFactorOf, MediaQueryData(textScaleFactor: 1.1)),
      const _MediaQueryAspectCase(MediaQuery.maybeTextScaleFactorOf, MediaQueryData(textScaleFactor: 1.1)),
1499 1500
      const _MediaQueryAspectCase(MediaQuery.textScalerOf, MediaQueryData(textScaler: TextScaler.linear(1.1))),
      const _MediaQueryAspectCase(MediaQuery.maybeTextScalerOf, MediaQueryData(textScaler: TextScaler.linear(1.1))),
1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518
      const _MediaQueryAspectCase(MediaQuery.platformBrightnessOf, MediaQueryData(platformBrightness: Brightness.dark)),
      const _MediaQueryAspectCase(MediaQuery.maybePlatformBrightnessOf, MediaQueryData(platformBrightness: Brightness.dark)),
      const _MediaQueryAspectCase(MediaQuery.paddingOf, MediaQueryData(padding: EdgeInsets.all(1))),
      const _MediaQueryAspectCase(MediaQuery.maybePaddingOf, MediaQueryData(padding: EdgeInsets.all(1))),
      const _MediaQueryAspectCase(MediaQuery.viewInsetsOf, MediaQueryData(viewInsets: EdgeInsets.all(1))),
      const _MediaQueryAspectCase(MediaQuery.maybeViewInsetsOf, MediaQueryData(viewInsets: EdgeInsets.all(1))),
      const _MediaQueryAspectCase(MediaQuery.systemGestureInsetsOf, MediaQueryData(systemGestureInsets: EdgeInsets.all(1))),
      const _MediaQueryAspectCase(MediaQuery.maybeSystemGestureInsetsOf, MediaQueryData(systemGestureInsets: EdgeInsets.all(1))),
      const _MediaQueryAspectCase(MediaQuery.viewPaddingOf, MediaQueryData(viewPadding: EdgeInsets.all(1))),
      const _MediaQueryAspectCase(MediaQuery.maybeViewPaddingOf, MediaQueryData(viewPadding: EdgeInsets.all(1))),
      const _MediaQueryAspectCase(MediaQuery.alwaysUse24HourFormatOf, MediaQueryData(alwaysUse24HourFormat: true)),
      const _MediaQueryAspectCase(MediaQuery.maybeAlwaysUse24HourFormatOf, MediaQueryData(alwaysUse24HourFormat: true)),
      const _MediaQueryAspectCase(MediaQuery.accessibleNavigationOf, MediaQueryData(accessibleNavigation: true)),
      const _MediaQueryAspectCase(MediaQuery.maybeAccessibleNavigationOf, MediaQueryData(accessibleNavigation: true)),
      const _MediaQueryAspectCase(MediaQuery.invertColorsOf, MediaQueryData(invertColors: true)),
      const _MediaQueryAspectCase(MediaQuery.maybeInvertColorsOf, MediaQueryData(invertColors: true)),
      const _MediaQueryAspectCase(MediaQuery.highContrastOf, MediaQueryData(highContrast: true)),
      const _MediaQueryAspectCase(MediaQuery.maybeHighContrastOf, MediaQueryData(highContrast: true)),
1519 1520
      const _MediaQueryAspectCase(MediaQuery.onOffSwitchLabelsOf, MediaQueryData(onOffSwitchLabels: true)),
      const _MediaQueryAspectCase(MediaQuery.maybeOnOffSwitchLabelsOf, MediaQueryData(onOffSwitchLabels: true)),
1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532
      const _MediaQueryAspectCase(MediaQuery.disableAnimationsOf, MediaQueryData(disableAnimations: true)),
      const _MediaQueryAspectCase(MediaQuery.maybeDisableAnimationsOf, MediaQueryData(disableAnimations: true)),
      const _MediaQueryAspectCase(MediaQuery.boldTextOf, MediaQueryData(boldText: true)),
      const _MediaQueryAspectCase(MediaQuery.maybeBoldTextOf, MediaQueryData(boldText: true)),
      const _MediaQueryAspectCase(MediaQuery.navigationModeOf, MediaQueryData(navigationMode: NavigationMode.directional)),
      const _MediaQueryAspectCase(MediaQuery.maybeNavigationModeOf, MediaQueryData(navigationMode: NavigationMode.directional)),
      const _MediaQueryAspectCase(MediaQuery.gestureSettingsOf, MediaQueryData(gestureSettings: DeviceGestureSettings(touchSlop: 1))),
      const _MediaQueryAspectCase(MediaQuery.maybeGestureSettingsOf, MediaQueryData(gestureSettings: DeviceGestureSettings(touchSlop: 1))),
      const _MediaQueryAspectCase(MediaQuery.displayFeaturesOf, MediaQueryData(displayFeatures: <DisplayFeature>[DisplayFeature(bounds: Rect.zero, type: DisplayFeatureType.unknown, state: DisplayFeatureState.unknown)])),
      const _MediaQueryAspectCase(MediaQuery.maybeDisplayFeaturesOf, MediaQueryData(displayFeatures: <DisplayFeature>[DisplayFeature(bounds: Rect.zero, type: DisplayFeatureType.unknown, state: DisplayFeatureState.unknown)])),
    ]
  ));
1533
}