From 8101f13a35609e2fac991c0cbf0eb9090f0be168 Mon Sep 17 00:00:00 2001 From: Kostia Sokolovskyi Date: Thu, 4 Apr 2024 01:39:17 +0200 Subject: [PATCH] Add tests for theme_extension.1.dart API example. (#145819) This PR contributes to https://github.com/flutter/flutter/issues/130459 ### Description - Adds tests for `examples/api/lib/material/theme/theme_extension.1.dart` --- dev/bots/check_code_samples.dart | 1 - .../theme/theme_extension.1_test.dart | 112 ++++++++++++++++++ 2 files changed, 112 insertions(+), 1 deletion(-) create mode 100644 examples/api/test/material/theme/theme_extension.1_test.dart diff --git a/dev/bots/check_code_samples.dart b/dev/bots/check_code_samples.dart index a6144fc524..53e6cfabff 100644 --- a/dev/bots/check_code_samples.dart +++ b/dev/bots/check_code_samples.dart @@ -311,7 +311,6 @@ class SampleChecker { final Set _knownMissingTests = { 'examples/api/test/material/bottom_app_bar/bottom_app_bar.2_test.dart', 'examples/api/test/material/bottom_app_bar/bottom_app_bar.1_test.dart', - 'examples/api/test/material/theme/theme_extension.1_test.dart', 'examples/api/test/material/material_state/material_state_border_side.0_test.dart', 'examples/api/test/material/material_state/material_state_outlined_border.0_test.dart', 'examples/api/test/material/material_state/material_state_property.0_test.dart', diff --git a/examples/api/test/material/theme/theme_extension.1_test.dart b/examples/api/test/material/theme/theme_extension.1_test.dart new file mode 100644 index 0000000000..f3045d5de1 --- /dev/null +++ b/examples/api/test/material/theme/theme_extension.1_test.dart @@ -0,0 +1,112 @@ +// 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 'package:flutter/material.dart'; +import 'package:flutter_api_samples/material/theme/theme_extension.1.dart' + as example; +import 'package:flutter_test/flutter_test.dart'; + +void main() { + testWidgets('ThemeExtension can be obtained', (WidgetTester tester) async { + await tester.pumpWidget( + const example.ThemeExtensionExampleApp(), + ); + + final ThemeData theme = Theme.of( + tester.element(find.byType(example.Home)), + ); + final example.MyColors colors = theme.extension()!; + + expect(colors.brandColor, equals(const Color(0xFF1E88E5))); + expect(colors.danger, equals(const Color(0xFFE53935))); + }); + + testWidgets('ThemeExtension can be changed', (WidgetTester tester) async { + await tester.pumpWidget( + const example.ThemeExtensionExampleApp(), + ); + + ThemeData theme = Theme.of( + tester.element(find.byType(example.Home)), + ); + example.MyColors colors = theme.extension()!; + + expect(colors.brandColor, equals(const Color(0xFF1E88E5))); + expect(colors.danger, equals(const Color(0xFFE53935))); + + // Tap the IconButton to switch theme mode from light to dark. + await tester.tap(find.byType(IconButton)); + await tester.pumpAndSettle(); + + theme = Theme.of( + tester.element(find.byType(example.Home)), + ); + colors = theme.extension()!; + + expect(colors.brandColor, equals(const Color(0xFF90CAF9))); + expect(colors.danger, equals(const Color(0xFFEF9A9A))); + }); + + testWidgets('Home uses MyColors extension correctly', (WidgetTester tester) async { + await tester.pumpWidget( + MaterialApp( + theme: ThemeData.light().copyWith( + extensions: >[ + const example.MyColors( + brandColor: Color(0xFF0000FF), + danger: Color(0xFFFF0000), + ), + ], + ), + home: example.Home( + isLightTheme: true, + toggleTheme: () {}, + ), + ), + ); + + expect( + find.byType(example.Home), + paints + ..rect(color: const Color(0xFF0000FF)) + ..rect(color: const Color(0xFFFF0000)), + ); + }); + + testWidgets('Home updates IconButton correctly', (WidgetTester tester) async { + await tester.pumpWidget( + const example.ThemeExtensionExampleApp(), + ); + + example.Home home = tester.widget(find.byType(example.Home)); + IconButton iconButton = tester.widget(find.byType(IconButton)); + ThemeData theme = Theme.of( + tester.element(find.byType(example.Home)), + ); + + expect(theme.brightness, equals(Brightness.light)); + expect(home.isLightTheme, isTrue); + expect( + iconButton.icon, + isA().having((Icon i) => i.icon, 'icon', equals(Icons.nightlight)), + ); + + // Tap the IconButton to switch theme mode from light to dark. + await tester.tap(find.byType(IconButton)); + await tester.pumpAndSettle(); + + home = tester.widget(find.byType(example.Home)); + iconButton = tester.widget(find.byType(IconButton)); + theme = Theme.of( + tester.element(find.byType(example.Home)), + ); + + expect(theme.brightness, equals(Brightness.dark)); + expect(home.isLightTheme, isFalse); + expect( + iconButton.icon, + isA().having((Icon i) => i.icon, 'icon', equals(Icons.wb_sunny)), + ); + }); +}