| // 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:async'; | |
| import 'dart:io'; | |
| import 'package:flutter/widgets.dart'; | |
| import 'package:flutter_test/flutter_test.dart'; | |
| import 'package:image_list/main.dart'; | |
| // 1x1 transparent PNG payload used to satisfy NetworkImage decoding synchronously. | |
| // dart format off | |
| const List<int> _transparentPng = <int>[ | |
| 0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A, 0x00, 0x00, 0x00, 0x0D, | |
| 0x49, 0x48, 0x44, 0x52, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, | |
| 0x08, 0x06, 0x00, 0x00, 0x00, 0x1F, 0x15, 0xC4, 0x89, 0x00, 0x00, 0x00, | |
| 0x0D, 0x49, 0x44, 0x41, 0x54, 0x78, 0x9C, 0x63, 0x00, 0x01, 0x00, 0x00, | |
| 0x05, 0x00, 0x01, 0x0D, 0x0A, 0x2D, 0xB4, 0x00, 0x00, 0x00, 0x00, 0x49, | |
| 0x45, 0x4E, 0x44, 0xAE, 0x42, 0x60, 0x82, | |
| ]; | |
| // dart format on | |
| /// Mocks HTTP network requests for Image.network to prevent dangling socket | |
| /// timers in the test binding, ensuring clean widget teardown. | |
| class _FakeHttpOverrides extends HttpOverrides { | |
| @override | |
| HttpClient createHttpClient(SecurityContext? context) => _FakeHttpClient(); | |
| } | |
| class _FakeHttpClient extends Fake implements HttpClient { | |
| @override | |
| bool autoUncompress = true; | |
| @override | |
| Future<HttpClientRequest> getUrl(Uri url) async => _FakeHttpClientRequest(); | |
| } | |
| class _FakeHttpClientRequest extends Fake implements HttpClientRequest { | |
| @override | |
| Future<HttpClientResponse> close() async => _FakeHttpClientResponse(); | |
| } | |
| class _FakeHttpClientResponse extends Fake implements HttpClientResponse { | |
| @override | |
| int get statusCode => HttpStatus.ok; | |
| @override | |
| int get contentLength => _transparentPng.length; | |
| @override | |
| HttpClientResponseCompressionState get compressionState => | |
| HttpClientResponseCompressionState.notCompressed; | |
| @override | |
| StreamSubscription<List<int>> listen( | |
| void Function(List<int> event)? onData, { | |
| Function? onError, | |
| void Function()? onDone, | |
| bool? cancelOnError, | |
| }) { | |
| return Stream<List<int>>.value(_transparentPng) | |
| .listen(onData, onError: onError, onDone: onDone, cancelOnError: cancelOnError); | |
| } | |
| } | |
| void main() { | |
| testWidgets('MyHomePage disposes AnimationControllers properly', (WidgetTester tester) async { | |
| HttpOverrides.global = _FakeHttpOverrides(); | |
| addTearDown(() => HttpOverrides.global = null); | |
| await tester.pumpWidget(const MyApp(8080)); | |
| // Trigger a rebuild by tapping the increment button | |
| await tester.tap(find.byTooltip('Increment')); | |
| await tester.pump(); | |
| // Unmount the tree, which calls dispose() on the state. | |
| await tester.pumpWidget(const SizedBox.shrink()); | |
| // Verifies that no active Tickers or controllers remain un-disposed. | |
| expect(tester.takeException(), isNull); | |
| }); | |
| } |