|
| 1 | +// Copyright 2014 The Flutter Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +import 'dart:async'; |
| 6 | +import 'dart:io'; |
| 7 | + |
| 8 | +import 'package:flutter/material.dart'; |
| 9 | +import 'package:flutter/services.dart'; |
| 10 | +import 'package:flutter_api_samples/material/color_scheme/dynamic_content_color.0.dart' as example; |
| 11 | +import 'package:flutter_test/flutter_test.dart'; |
| 12 | + |
| 13 | +void main() { |
| 14 | + |
| 15 | + testWidgets('The theme colors are created dynamically from the first image', (WidgetTester tester) async { |
| 16 | + await HttpOverrides.runZoned( |
| 17 | + () async { |
| 18 | + await tester.pumpWidget( |
| 19 | + const example.DynamicColorExample(), |
| 20 | + ); |
| 21 | + |
| 22 | + expect( |
| 23 | + find.widgetWithText(AppBar, 'Content Based Dynamic Color'), |
| 24 | + findsOne, |
| 25 | + ); |
| 26 | + expect(find.byType(Switch), findsOne); |
| 27 | + expect(find.byIcon(Icons.light_mode), findsOne); |
| 28 | + |
| 29 | + // Loads the images. |
| 30 | + // Using runAsync forces the streams to complete. This is needed because |
| 31 | + // loading the fake image is a real async task. |
| 32 | + await tester.pump(); |
| 33 | + await tester.runAsync(() => Future<void>.delayed(Duration.zero)); |
| 34 | + await tester.runAsync(() => Future<void>.delayed(Duration.zero)); |
| 35 | + await tester.runAsync(() => Future<void>.delayed(Duration.zero)); |
| 36 | + await tester.runAsync(() => Future<void>.delayed(Duration.zero)); |
| 37 | + await tester.runAsync(() => Future<void>.delayed(Duration.zero)); |
| 38 | + await tester.pump(); |
| 39 | + await tester.pump(kThemeChangeDuration); |
| 40 | + |
| 41 | + expect(find.byType(Image), findsExactly(6)); |
| 42 | + |
| 43 | + expect(find.text('Light ColorScheme'), findsOne); |
| 44 | + expect(find.text('Dark ColorScheme'), findsOne); |
| 45 | + expect(find.text('primary'), findsExactly(2)); |
| 46 | + expect(find.text('onPrimary'), findsExactly(2)); |
| 47 | + expect(find.text('primaryContainer'), findsExactly(2)); |
| 48 | + expect(find.text('onPrimaryContainer'), findsExactly(2)); |
| 49 | + expect(find.text('secondary'), findsExactly(2)); |
| 50 | + expect(find.text('onSecondary'), findsExactly(2)); |
| 51 | + expect(find.text('secondaryContainer'), findsExactly(2)); |
| 52 | + expect(find.text('onSecondaryContainer'), findsExactly(2)); |
| 53 | + expect(find.text('tertiary'), findsExactly(2)); |
| 54 | + expect(find.text('onTertiary'), findsExactly(2)); |
| 55 | + expect(find.text('tertiaryContainer'), findsExactly(2)); |
| 56 | + expect(find.text('onTertiaryContainer'), findsExactly(2)); |
| 57 | + expect(find.text('error'), findsExactly(2)); |
| 58 | + expect(find.text('onError'), findsExactly(2)); |
| 59 | + expect(find.text('errorContainer'), findsExactly(2)); |
| 60 | + expect(find.text('onErrorContainer'), findsExactly(2)); |
| 61 | + expect(find.text('surface'), findsExactly(2)); |
| 62 | + expect(find.text('onSurface'), findsExactly(2)); |
| 63 | + expect(find.text('onSurfaceVariant'), findsExactly(2)); |
| 64 | + expect(find.text('outline'), findsExactly(2)); |
| 65 | + expect(find.text('shadow'), findsExactly(2)); |
| 66 | + expect(find.text('inverseSurface'), findsExactly(2)); |
| 67 | + expect(find.text('onInverseSurface'), findsExactly(2)); |
| 68 | + expect(find.text('inversePrimary'), findsExactly(2)); |
| 69 | + |
| 70 | + ThemeData themeData = Theme.of( |
| 71 | + tester.element(find.byType(Scaffold)), |
| 72 | + ); |
| 73 | + |
| 74 | + expect(themeData.colorScheme.primary, const Color(0xff575992)); |
| 75 | + expect(themeData.colorScheme.secondary, const Color(0xff5d5c72)); |
| 76 | + |
| 77 | + await tester.tap(find.byType(Switch)); |
| 78 | + await tester.pump(); |
| 79 | + |
| 80 | + // Loads the images. |
| 81 | + // Using runAsync forces the streams to complete. This is needed because |
| 82 | + // loading the fake image is a real async task. |
| 83 | + await tester.runAsync(() => Future<void>.delayed(Duration.zero)); |
| 84 | + await tester.runAsync(() => Future<void>.delayed(Duration.zero)); |
| 85 | + await tester.pump(); |
| 86 | + await tester.pump(kThemeChangeDuration); |
| 87 | + |
| 88 | + themeData = Theme.of(tester.element(find.byType(Scaffold))); |
| 89 | + |
| 90 | + expect(themeData.primaryColor, const Color(0xff131318)); |
| 91 | + expect(themeData.colorScheme.secondary, const Color(0xffc6c4dd)); |
| 92 | + }, |
| 93 | + createHttpClient: (SecurityContext? securityContext) => _FakeClient(), |
| 94 | + ); |
| 95 | + }); |
| 96 | +} |
| 97 | + |
| 98 | +class _FakeClient extends Fake implements HttpClient { |
| 99 | + @override |
| 100 | + Future<HttpClientRequest> getUrl(Uri url) async { |
| 101 | + return _FakeHttpClientRequest(); |
| 102 | + } |
| 103 | + |
| 104 | + @override |
| 105 | + bool autoUncompress = true; |
| 106 | +} |
| 107 | + |
| 108 | +class _FakeHttpClientRequest extends Fake implements HttpClientRequest { |
| 109 | + @override |
| 110 | + HttpHeaders get headers => _FakeHttpHeaders(); |
| 111 | + |
| 112 | + @override |
| 113 | + Future<HttpClientResponse> close() async { |
| 114 | + return _FakeHttpClientResponse(); |
| 115 | + } |
| 116 | +} |
| 117 | + |
| 118 | +class _FakeHttpHeaders extends Fake implements HttpHeaders {} |
| 119 | + |
| 120 | +class _FakeHttpClientResponse extends Fake implements HttpClientResponse { |
| 121 | + @override |
| 122 | + HttpClientResponseCompressionState get compressionState => HttpClientResponseCompressionState.notCompressed; |
| 123 | + |
| 124 | + @override |
| 125 | + int get contentLength => _blueSquarePng.length; |
| 126 | + |
| 127 | + @override |
| 128 | + int get statusCode => 200; |
| 129 | + |
| 130 | + @override |
| 131 | + Future<E> drain<E>([E? futureValue]) { |
| 132 | + return Future<E>.value(futureValue as E); |
| 133 | + } |
| 134 | + |
| 135 | + @override |
| 136 | + StreamSubscription<List<int>> listen( |
| 137 | + void Function(List<int> event)? onData, { |
| 138 | + Function? onError, |
| 139 | + void Function()? onDone, |
| 140 | + bool? cancelOnError, |
| 141 | + }) { |
| 142 | + return Stream<List<int>>.value(_blueSquarePng).listen( |
| 143 | + onData, |
| 144 | + onDone: onDone, |
| 145 | + onError: onError, |
| 146 | + cancelOnError: cancelOnError, |
| 147 | + ); |
| 148 | + } |
| 149 | +} |
| 150 | + |
| 151 | +/// A 50x50 blue square png. |
| 152 | +final Uint8List _blueSquarePng = Uint8List.fromList(const <int>[ |
| 153 | + 0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0x00, 0x00, 0x00, 0x0d, 0x49, |
| 154 | + 0x48, 0x44, 0x52, 0x00, 0x00, 0x00, 0x32, 0x00, 0x00, 0x00, 0x32, 0x08, 0x06, |
| 155 | + 0x00, 0x00, 0x00, 0x1e, 0x3f, 0x88, 0xb1, 0x00, 0x00, 0x00, 0x48, 0x49, 0x44, |
| 156 | + 0x41, 0x54, 0x78, 0xda, 0xed, 0xcf, 0x31, 0x0d, 0x00, 0x30, 0x08, 0x00, 0xb0, |
| 157 | + 0x61, 0x63, 0x2f, 0xfe, 0x2d, 0x61, 0x05, 0x34, 0xf0, 0x92, 0xd6, 0x41, 0x23, |
| 158 | + 0x7f, 0xf5, 0x3b, 0x20, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, |
| 159 | + 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, |
| 160 | + 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, |
| 161 | + 0x44, 0x44, 0x44, 0x36, 0x06, 0x03, 0x6e, 0x69, 0x47, 0x12, 0x8e, 0xea, 0xaa, |
| 162 | + 0x00, 0x00, 0x00, 0x00, 0x49, 0x45, 0x4e, 0x44, 0xae, 0x42, 0x60, 0x82, |
| 163 | +]); |
0 commit comments