Skip to content

Commit

Permalink
✅ add tests for DebugFloatingThemeButton.
Browse files Browse the repository at this point in the history
  • Loading branch information
BirjuVachhani committed Sep 3, 2023
1 parent 5b82e8a commit abc01e2
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 0 deletions.
81 changes: 81 additions & 0 deletions test/debug_floating_theme_buttons_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* // Copyright © 2020 Birju Vachhani. All rights reserved.
* // Use of this source code is governed by an Apache license that can be
* // found in the LICENSE file.
*/

import 'package:adaptive_theme/adaptive_theme.dart';
import 'package:adaptive_theme/src/debug_floating_theme_buttons.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';

import 'test_utils.dart';

void main() {
testWidgets('debugShowFloatingThemeButton test', (tester) async {
final light = ThemeData.light();
final dark = ThemeData.dark();
await pumpMaterialApp(
tester,
light: light,
dark: dark,
mode: AdaptiveThemeMode.light,
debugShowFloatingThemeButton: true,
);

final widget = tester.widget<DebugFloatingThemeButton>(
find.byType(DebugFloatingThemeButton));
expect(widget.debugShow, isTrue);
});

testWidgets('DebugFloatingThemeButton show/hide test', (tester) async {
final light = ThemeData.light();
final dark = ThemeData.dark();
await pumpMaterialApp(
tester,
light: light,
dark: dark,
mode: AdaptiveThemeMode.light,
debugShowFloatingThemeButton: true,
);

final BuildContext context = tester.element(find.byType(Scaffold));
final manager = AdaptiveTheme.of(context);

final widgetFinder = find.byType(DebugFloatingThemeButton);
final dragIndicatorFinder = find.descendant(
of: widgetFinder, matching: find.byIcon(Icons.drag_indicator_rounded));

final widget = tester.widget<DebugFloatingThemeButton>(widgetFinder);
expect(widget.debugShow, isTrue);

final rect = tester.getRect(dragIndicatorFinder);
await tester.tapAt(rect.center);
await tester.pumpAndSettle();

final toggleButtonFinder =
find.descendant(of: widgetFinder, matching: find.byType(ToggleButtons));
ToggleButtons buttonsWidget() =>
tester.widget<ToggleButtons>(toggleButtonFinder);

// check if light theme is selected
expect(
buttonsWidget().isSelected, containsAllInOrder([true, false, false]));

// select dark theme
buttonsWidget().onPressed?.call(1);
await tester.pumpAndSettle();

expect(manager.mode, AdaptiveThemeMode.dark);
expect(
buttonsWidget().isSelected, containsAllInOrder([false, true, false]));

// select system theme
buttonsWidget().onPressed?.call(2);
await tester.pumpAndSettle();

expect(manager.mode, AdaptiveThemeMode.system);
expect(
buttonsWidget().isSelected, containsAllInOrder([false, false, true]));
});
}
2 changes: 2 additions & 0 deletions test/test_utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@ Future<void> pumpMaterialApp(
required ThemeData light,
required ThemeData dark,
required AdaptiveThemeMode mode,
bool? debugShowFloatingThemeButton,
}) async {
await tester.pumpWidget(AdaptiveTheme(
light: light,
dark: dark,
initial: mode,
debugShowFloatingThemeButton: debugShowFloatingThemeButton ?? false,
builder: (light, dark) => MaterialApp(
theme: light,
darkTheme: dark,
Expand Down

0 comments on commit abc01e2

Please sign in to comment.