|
2 | 2 | // Use of this source code is governed by a BSD-style license that can be
|
3 | 3 | // found in the LICENSE file.
|
4 | 4 |
|
| 5 | +import 'dart:ui'; |
| 6 | + |
5 | 7 | import 'package:flutter/material.dart';
|
| 8 | +import 'package:flutter/services.dart'; |
6 | 9 | import 'package:flutter_test/flutter_test.dart';
|
| 10 | +import 'package:material_3_demo/color_box.dart'; |
7 | 11 | import 'package:material_3_demo/color_palettes_screen.dart';
|
8 | 12 | import 'package:material_3_demo/main.dart';
|
9 | 13 | import 'package:material_3_demo/scheme.dart';
|
@@ -88,4 +92,66 @@ void main() {
|
88 | 92 | expect(find.text('Dark ColorScheme'), findsOneWidget);
|
89 | 93 | expect(find.byType(SchemePreview, skipOffstage: false), findsNWidgets(2));
|
90 | 94 | });
|
| 95 | + |
| 96 | + testWidgets( |
| 97 | + 'ColorBox displays correct info and copies hex color on button tap', |
| 98 | + (tester) async { |
| 99 | + TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger |
| 100 | + .setMockMessageHandler('flutter/platform', (_) async { |
| 101 | + // To intercept method calls to 'Clipboard.setData' |
| 102 | + return const JSONMethodCodec().encodeSuccessEnvelope(null); |
| 103 | + }); |
| 104 | + const hexColor = 0xFF3d3d8d; |
| 105 | + const testColor = Color(hexColor); |
| 106 | + const onTestColor = Colors.white; |
| 107 | + const testLabel = 'Test Label'; |
| 108 | + const testTone = '50'; |
| 109 | + |
| 110 | + final gesture = await tester.createGesture(kind: PointerDeviceKind.mouse); |
| 111 | + |
| 112 | + // Wrap in MaterialApp + Scaffold so we can show SnackBars |
| 113 | + await tester.pumpWidget( |
| 114 | + MaterialApp( |
| 115 | + home: Scaffold( |
| 116 | + body: ColorBox( |
| 117 | + label: testLabel, |
| 118 | + tone: testTone, |
| 119 | + color: testColor, |
| 120 | + onColor: onTestColor, |
| 121 | + height: 100, |
| 122 | + width: 100, |
| 123 | + displayPaletteInfo: true, |
| 124 | + ), |
| 125 | + ), |
| 126 | + ), |
| 127 | + ); |
| 128 | + |
| 129 | + expect(find.text(testLabel), findsOneWidget); |
| 130 | + expect(find.text(testTone), findsOneWidget); |
| 131 | + |
| 132 | + // The copy icon should NOT be there initially (only appears on hover). |
| 133 | + expect(find.byIcon(Icons.copy), findsNothing); |
| 134 | + |
| 135 | + await gesture.addPointer(location: Offset.zero); |
| 136 | + addTearDown(gesture.removePointer); |
| 137 | + await tester.pump(); |
| 138 | + await gesture.moveTo(tester.getCenter(find.byType(ColorBox))); |
| 139 | + await tester.pumpAndSettle(); |
| 140 | + |
| 141 | + expect(find.byIcon(Icons.copy), findsOneWidget); |
| 142 | + |
| 143 | + // Tap the copy icon, which copies the hex to clipboard and shows a SnackBar. |
| 144 | + await tester.tap(find.byIcon(Icons.copy)); |
| 145 | + await tester.pumpAndSettle(); |
| 146 | + |
| 147 | + expect(find.byType(SnackBar), findsOneWidget); |
| 148 | + |
| 149 | + expect( |
| 150 | + find.text( |
| 151 | + 'Copied #${hexColor.toRadixString(16).substring(2)} to clipboard', |
| 152 | + ), |
| 153 | + findsOneWidget, |
| 154 | + ); |
| 155 | + }, |
| 156 | + ); |
91 | 157 | }
|
0 commit comments