blob: 48c54f6804e77395ba5859340e6e933a0f623cc9 [file] [log] [blame]
// 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' show Brightness;
import 'package:flutter/gestures.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_test/flutter_test.dart';
void main() {
testWidgets('MediaQuery does not have a default', (WidgetTester tester) async {
bool tested = false;
await tester.pumpWidget(
Builder(
builder: (BuildContext context) {
tested = true;
MediaQuery.of(context); // should throw
return Container();
},
),
);
expect(tested, isTrue);
final dynamic exception = tester.takeException();
expect(exception, isNotNull);
expect(exception ,isFlutterError);
final FlutterError error = exception as FlutterError;
expect(error.diagnostics.length, 5);
expect(error.diagnostics.last, isA<ErrorHint>());
expect(
error.toStringDeep(),
equalsIgnoringHashCodes(
'FlutterError\n'
' 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'
' The ownership chain for the affected widget is: "Builder ←\n'
' [root]"\n'
' No MediaQuery ancestor could be found starting from the context\n'
' that was passed to MediaQuery.of(). This can happen because you\n'
' have not added a WidgetsApp, CupertinoApp, or MaterialApp widget\n'
' (those widgets introduce a MediaQuery), or it can happen if the\n'
' context you use comes from a widget above those widgets.\n',
),
);
});
testWidgets('MediaQuery.of finds a MediaQueryData when there is one', (WidgetTester tester) async {
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);
});
testWidgets('MediaQuery.maybeOf defaults to null', (WidgetTester tester) async {
bool tested = false;
await tester.pumpWidget(
Builder(
builder: (BuildContext context) {
final MediaQueryData? data = MediaQuery.maybeOf(context);
expect(data, isNull);
tested = true;
return Container();
},
),
);
expect(tested, isTrue);
});
testWidgets('MediaQuery.maybeOf finds a MediaQueryData when there is one', (WidgetTester tester) async {
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);
});
testWidgets('MediaQueryData.fromWindow is sane', (WidgetTester tester) async {
final MediaQueryData data = MediaQueryData.fromWindow(WidgetsBinding.instance!.window);
expect(data, hasOneLineDescription);
expect(data.hashCode, equals(data.copyWith().hashCode));
expect(data.size, equals(WidgetsBinding.instance!.window.physicalSize / WidgetsBinding.instance!.window.devicePixelRatio));
expect(data.accessibleNavigation, false);
expect(data.invertColors, false);
expect(data.disableAnimations, false);
expect(data.boldText, false);
expect(data.highContrast, false);
expect(data.platformBrightness, Brightness.light);
expect(data.gestureSettings.touchSlop, null);
});
testWidgets('MediaQueryData.copyWith defaults to source', (WidgetTester tester) async {
final MediaQueryData data = MediaQueryData.fromWindow(WidgetsBinding.instance!.window);
final MediaQueryData copied = data.copyWith();
expect(copied.size, data.size);
expect(copied.devicePixelRatio, data.devicePixelRatio);
expect(copied.textScaleFactor, data.textScaleFactor);
expect(copied.padding, data.padding);
expect(copied.viewPadding, data.viewPadding);
expect(copied.viewInsets, data.viewInsets);
expect(copied.systemGestureInsets, data.systemGestureInsets);
expect(copied.alwaysUse24HourFormat, data.alwaysUse24HourFormat);
expect(copied.accessibleNavigation, data.accessibleNavigation);
expect(copied.invertColors, data.invertColors);
expect(copied.disableAnimations, data.disableAnimations);
expect(copied.boldText, data.boldText);
expect(copied.highContrast, data.highContrast);
expect(copied.platformBrightness, data.platformBrightness);
expect(copied.gestureSettings, data.gestureSettings);
});
testWidgets('MediaQuery.copyWith copies specified values', (WidgetTester tester) async {
// 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;
const double customTextScaleFactor = 1.62;
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);
const DeviceGestureSettings gestureSettings = DeviceGestureSettings(touchSlop: 8.0);
final MediaQueryData data = MediaQueryData.fromWindow(WidgetsBinding.instance!.window);
final MediaQueryData copied = data.copyWith(
size: customSize,
devicePixelRatio: customDevicePixelRatio,
textScaleFactor: customTextScaleFactor,
padding: customPadding,
viewPadding: customViewPadding,
viewInsets: customViewInsets,
systemGestureInsets: customSystemGestureInsets,
alwaysUse24HourFormat: true,
accessibleNavigation: true,
invertColors: true,
disableAnimations: true,
boldText: true,
highContrast: true,
platformBrightness: Brightness.dark,
gestureSettings: gestureSettings,
);
expect(copied.size, customSize);
expect(copied.devicePixelRatio, customDevicePixelRatio);
expect(copied.textScaleFactor, customTextScaleFactor);
expect(copied.padding, customPadding);
expect(copied.viewPadding, customViewPadding);
expect(copied.viewInsets, customViewInsets);
expect(copied.systemGestureInsets, customSystemGestureInsets);
expect(copied.alwaysUse24HourFormat, true);
expect(copied.accessibleNavigation, true);
expect(copied.invertColors, true);
expect(copied.disableAnimations, true);
expect(copied.boldText, true);
expect(copied.highContrast, true);
expect(copied.platformBrightness, Brightness.dark);
expect(copied.gestureSettings, gestureSettings);
});
testWidgets('MediaQuery.removePadding removes specified padding', (WidgetTester tester) async {
const Size size = Size(2.0, 4.0);
const double devicePixelRatio = 2.0;
const double textScaleFactor = 1.2;
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);
late MediaQueryData unpadded;
await tester.pumpWidget(
MediaQuery(
data: const MediaQueryData(
size: size,
devicePixelRatio: devicePixelRatio,
textScaleFactor: textScaleFactor,
padding: padding,
viewPadding: viewPadding,
viewInsets: viewInsets,
alwaysUse24HourFormat: true,
accessibleNavigation: true,
invertColors: true,
disableAnimations: true,
boldText: true,
highContrast: true,
),
child: Builder(
builder: (BuildContext context) {
return MediaQuery.removePadding(
context: context,
removeLeft: true,
removeTop: true,
removeRight: true,
removeBottom: true,
child: Builder(
builder: (BuildContext context) {
unpadded = MediaQuery.of(context);
return Container();
},
),
);
},
),
),
);
expect(unpadded.size, size);
expect(unpadded.devicePixelRatio, devicePixelRatio);
expect(unpadded.textScaleFactor, textScaleFactor);
expect(unpadded.padding, EdgeInsets.zero);
expect(unpadded.viewPadding, viewInsets);
expect(unpadded.viewInsets, viewInsets);
expect(unpadded.alwaysUse24HourFormat, true);
expect(unpadded.accessibleNavigation, true);
expect(unpadded.invertColors, true);
expect(unpadded.disableAnimations, true);
expect(unpadded.boldText, true);
expect(unpadded.highContrast, true);
});
testWidgets('MediaQuery.removePadding only removes specified padding', (WidgetTester tester) async {
const Size size = Size(2.0, 4.0);
const double devicePixelRatio = 2.0;
const double textScaleFactor = 1.2;
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);
late MediaQueryData unpadded;
await tester.pumpWidget(
MediaQuery(
data: const MediaQueryData(
size: size,
devicePixelRatio: devicePixelRatio,
textScaleFactor: textScaleFactor,
padding: padding,
viewPadding: viewPadding,
viewInsets: viewInsets,
alwaysUse24HourFormat: true,
accessibleNavigation: true,
invertColors: true,
disableAnimations: true,
boldText: true,
highContrast: true,
),
child: Builder(
builder: (BuildContext context) {
return MediaQuery.removePadding(
removeTop: true,
context: context,
child: Builder(
builder: (BuildContext context) {
unpadded = MediaQuery.of(context);
return Container();
},
),
);
},
),
),
);
expect(unpadded.size, size);
expect(unpadded.devicePixelRatio, devicePixelRatio);
expect(unpadded.textScaleFactor, textScaleFactor);
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);
expect(unpadded.highContrast, true);
});
testWidgets('MediaQuery.removeViewInsets removes specified viewInsets', (WidgetTester tester) async {
const Size size = Size(2.0, 4.0);
const double devicePixelRatio = 2.0;
const double textScaleFactor = 1.2;
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);
late MediaQueryData unpadded;
await tester.pumpWidget(
MediaQuery(
data: const MediaQueryData(
size: size,
devicePixelRatio: devicePixelRatio,
textScaleFactor: textScaleFactor,
padding: padding,
viewPadding: viewPadding,
viewInsets: viewInsets,
alwaysUse24HourFormat: true,
accessibleNavigation: true,
invertColors: true,
disableAnimations: true,
boldText: true,
highContrast: true,
),
child: Builder(
builder: (BuildContext context) {
return MediaQuery.removeViewInsets(
context: context,
removeLeft: true,
removeTop: true,
removeRight: true,
removeBottom: true,
child: Builder(
builder: (BuildContext context) {
unpadded = MediaQuery.of(context);
return Container();
},
),
);
},
),
),
);
expect(unpadded.size, size);
expect(unpadded.devicePixelRatio, devicePixelRatio);
expect(unpadded.textScaleFactor, textScaleFactor);
expect(unpadded.padding, padding);
expect(unpadded.viewPadding, padding);
expect(unpadded.viewInsets, EdgeInsets.zero);
expect(unpadded.alwaysUse24HourFormat, true);
expect(unpadded.accessibleNavigation, true);
expect(unpadded.invertColors, true);
expect(unpadded.disableAnimations, true);
expect(unpadded.boldText, true);
expect(unpadded.highContrast, true);
});
testWidgets('MediaQuery.removeViewInsets removes only specified viewInsets', (WidgetTester tester) async {
const Size size = Size(2.0, 4.0);
const double devicePixelRatio = 2.0;
const double textScaleFactor = 1.2;
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);
late MediaQueryData unpadded;
await tester.pumpWidget(
MediaQuery(
data: const MediaQueryData(
size: size,
devicePixelRatio: devicePixelRatio,
textScaleFactor: textScaleFactor,
padding: padding,
viewPadding: viewPadding,
viewInsets: viewInsets,
alwaysUse24HourFormat: true,
accessibleNavigation: true,
invertColors: true,
disableAnimations: true,
boldText: true,
highContrast: true,
),
child: Builder(
builder: (BuildContext context) {
return MediaQuery.removeViewInsets(
context: context,
removeBottom: true,
child: Builder(
builder: (BuildContext context) {
unpadded = MediaQuery.of(context);
return Container();
},
),
);
},
),
),
);
expect(unpadded.size, size);
expect(unpadded.devicePixelRatio, devicePixelRatio);
expect(unpadded.textScaleFactor, textScaleFactor);
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);
expect(unpadded.highContrast, true);
});
testWidgets('MediaQuery.removeViewPadding removes specified viewPadding', (WidgetTester tester) async {
const Size size = Size(2.0, 4.0);
const double devicePixelRatio = 2.0;
const double textScaleFactor = 1.2;
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);
late MediaQueryData unpadded;
await tester.pumpWidget(
MediaQuery(
data: const MediaQueryData(
size: size,
devicePixelRatio: devicePixelRatio,
textScaleFactor: textScaleFactor,
padding: padding,
viewPadding: viewPadding,
viewInsets: viewInsets,
alwaysUse24HourFormat: true,
accessibleNavigation: true,
invertColors: true,
disableAnimations: true,
boldText: true,
highContrast: true,
),
child: Builder(
builder: (BuildContext context) {
return MediaQuery.removeViewPadding(
context: context,
removeLeft: true,
removeTop: true,
removeRight: true,
removeBottom: true,
child: Builder(
builder: (BuildContext context) {
unpadded = MediaQuery.of(context);
return Container();
},
),
);
},
),
),
);
expect(unpadded.size, size);
expect(unpadded.devicePixelRatio, devicePixelRatio);
expect(unpadded.textScaleFactor, textScaleFactor);
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);
expect(unpadded.highContrast, true);
});
testWidgets('MediaQuery.removeViewPadding removes only specified viewPadding', (WidgetTester tester) async {
const Size size = Size(2.0, 4.0);
const double devicePixelRatio = 2.0;
const double textScaleFactor = 1.2;
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);
late MediaQueryData unpadded;
await tester.pumpWidget(
MediaQuery(
data: const MediaQueryData(
size: size,
devicePixelRatio: devicePixelRatio,
textScaleFactor: textScaleFactor,
padding: padding,
viewPadding: viewPadding,
viewInsets: viewInsets,
alwaysUse24HourFormat: true,
accessibleNavigation: true,
invertColors: true,
disableAnimations: true,
boldText: true,
highContrast: true,
),
child: Builder(
builder: (BuildContext context) {
return MediaQuery.removeViewPadding(
context: context,
removeLeft: true,
child: Builder(
builder: (BuildContext context) {
unpadded = MediaQuery.of(context);
return Container();
},
),
);
},
),
),
);
expect(unpadded.size, size);
expect(unpadded.devicePixelRatio, devicePixelRatio);
expect(unpadded.textScaleFactor, textScaleFactor);
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);
expect(unpadded.highContrast, true);
});
testWidgets('MediaQuery.textScaleFactorOf', (WidgetTester tester) async {
late double outsideTextScaleFactor;
late double insideTextScaleFactor;
await tester.pumpWidget(
Builder(
builder: (BuildContext context) {
outsideTextScaleFactor = MediaQuery.textScaleFactorOf(context);
return MediaQuery(
data: const MediaQueryData(
textScaleFactor: 4.0,
),
child: Builder(
builder: (BuildContext context) {
insideTextScaleFactor = MediaQuery.textScaleFactorOf(context);
return Container();
},
),
);
},
),
);
expect(outsideTextScaleFactor, 1.0);
expect(insideTextScaleFactor, 4.0);
});
testWidgets('MediaQuery.platformBrightnessOf', (WidgetTester tester) async {
late Brightness outsideBrightness;
late Brightness insideBrightness;
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);
});
testWidgets('MediaQuery.highContrastOf', (WidgetTester tester) async {
late bool outsideHighContrast;
late bool insideHighContrast;
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);
});
testWidgets('MediaQuery.boldTextOverride', (WidgetTester tester) async {
late bool outsideBoldTextOverride;
late bool insideBoldTextOverride;
await tester.pumpWidget(
Builder(
builder: (BuildContext context) {
outsideBoldTextOverride = MediaQuery.boldTextOverride(context);
return MediaQuery(
data: const MediaQueryData(
boldText: true,
),
child: Builder(
builder: (BuildContext context) {
insideBoldTextOverride = MediaQuery.boldTextOverride(context);
return Container();
},
),
);
},
),
);
expect(outsideBoldTextOverride, false);
expect(insideBoldTextOverride, true);
});
testWidgets('MediaQuery.fromWindow creates a MediaQuery', (WidgetTester tester) async {
bool hasMediaQueryAsParentOutside = false;
bool hasMediaQueryAsParentInside = false;
await tester.pumpWidget(
Builder(
builder: (BuildContext context) {
hasMediaQueryAsParentOutside =
context.findAncestorWidgetOfExactType<MediaQuery>() != null;
return MediaQuery.fromWindow(
child: Builder(
builder: (BuildContext context) {
hasMediaQueryAsParentInside =
context.findAncestorWidgetOfExactType<MediaQuery>() != null;
return const SizedBox();
},
),
);
},
),
);
expect(hasMediaQueryAsParentOutside, false);
expect(hasMediaQueryAsParentInside, true);
});
testWidgets('MediaQueryData.fromWindow is created using window values', (WidgetTester tester) async {
final MediaQueryData windowData = MediaQueryData.fromWindow(WidgetsBinding.instance!.window);
late MediaQueryData fromWindowData;
await tester.pumpWidget(
MediaQuery.fromWindow(
child: Builder(
builder: (BuildContext context) {
fromWindowData = MediaQuery.of(context);
return const SizedBox();
},
),
),
);
expect(windowData, equals(fromWindowData));
});
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));
});
}