forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathusage_page_fair_use_banner_test.dart
More file actions
213 lines (179 loc) · 7.77 KB
/
Copy pathusage_page_fair_use_banner_test.dart
File metadata and controls
213 lines (179 loc) · 7.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
import 'package:flutter/material.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:omi/l10n/app_localizations.dart';
import 'package:omi/pages/settings/fair_use_page.dart';
/// Wraps [child] in a MaterialApp with l10n delegates so context.l10n works.
Widget buildTestApp(Widget child) {
return MaterialApp(
localizationsDelegates: const [
AppLocalizations.delegate,
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
GlobalCupertinoLocalizations.delegate,
],
supportedLocales: const [Locale('en')],
home: child,
);
}
// ---------------------------------------------------------------------------
// Harness replicating the _buildFairUseBanner logic from UsagePage.
// This lets us test banner visibility/styling without the full UsagePage
// widget tree and its HTTP dependencies.
// ---------------------------------------------------------------------------
class FairUseBannerHarness extends StatelessWidget {
const FairUseBannerHarness({super.key, required this.fairUseStatus});
final Map<String, dynamic>? fairUseStatus;
@override
Widget build(BuildContext context) {
final l10n = AppLocalizations.of(context);
return Scaffold(
body: Column(
children: [
_buildFairUseBanner(context, l10n),
],
),
);
}
Widget _buildFairUseBanner(BuildContext context, AppLocalizations l10n) {
if (fairUseStatus == null) return const SizedBox.shrink();
final rawStage = fairUseStatus!['stage'];
final stage = rawStage is String ? rawStage : 'none';
if (stage == 'none') return const SizedBox.shrink();
Color dotColor;
String stageLabel;
switch (stage) {
case 'warning':
dotColor = const Color(0xFFFBBF24);
stageLabel = l10n.fairUseStageWarning;
break;
case 'throttle':
dotColor = const Color(0xFFF97316);
stageLabel = l10n.fairUseStageThrottle;
break;
case 'restrict':
dotColor = const Color(0xFFEF4444);
stageLabel = l10n.fairUseStageRestrict;
break;
default:
return const SizedBox.shrink();
}
return GestureDetector(
key: const Key('fair_use_banner_tap'),
onTap: () {
Navigator.of(context).push(MaterialPageRoute(builder: (context) => const FairUsePage()));
},
child: Container(
margin: const EdgeInsets.fromLTRB(16, 8, 16, 0),
padding: const EdgeInsets.symmetric(horizontal: 14, vertical: 10),
decoration: BoxDecoration(
color: dotColor.withValues(alpha: 0.08),
borderRadius: BorderRadius.circular(12),
),
child: Row(
children: [
Container(
key: const Key('fair_use_dot'),
width: 8,
height: 8,
decoration: BoxDecoration(color: dotColor, shape: BoxShape.circle),
),
const SizedBox(width: 10),
Text(
l10n.fairUseBannerStatus(stageLabel),
style: TextStyle(color: dotColor, fontSize: 13, fontWeight: FontWeight.w500),
),
const Spacer(),
Icon(Icons.chevron_right, color: dotColor, size: 18),
],
),
),
);
}
}
void main() {
group('Fair Use banner visibility', () {
testWidgets('hidden when status is null', (tester) async {
await tester.pumpWidget(buildTestApp(const FairUseBannerHarness(fairUseStatus: null)));
await tester.pumpAndSettle();
expect(find.byKey(const Key('fair_use_banner_tap')), findsNothing);
});
testWidgets('hidden when stage is none', (tester) async {
await tester.pumpWidget(buildTestApp(const FairUseBannerHarness(fairUseStatus: {'stage': 'none'})));
await tester.pumpAndSettle();
expect(find.byKey(const Key('fair_use_banner_tap')), findsNothing);
});
testWidgets('hidden for unknown stage', (tester) async {
await tester.pumpWidget(buildTestApp(const FairUseBannerHarness(fairUseStatus: {'stage': 'bogus'})));
await tester.pumpAndSettle();
expect(find.byKey(const Key('fair_use_banner_tap')), findsNothing);
});
testWidgets('visible for warning stage', (tester) async {
await tester.pumpWidget(buildTestApp(const FairUseBannerHarness(fairUseStatus: {'stage': 'warning'})));
await tester.pumpAndSettle();
expect(find.byKey(const Key('fair_use_banner_tap')), findsOneWidget);
expect(find.textContaining('Warning'), findsOneWidget);
});
testWidgets('visible for throttle stage', (tester) async {
await tester.pumpWidget(buildTestApp(const FairUseBannerHarness(fairUseStatus: {'stage': 'throttle'})));
await tester.pumpAndSettle();
expect(find.byKey(const Key('fair_use_banner_tap')), findsOneWidget);
expect(find.textContaining('Throttled'), findsOneWidget);
});
testWidgets('visible for restrict stage', (tester) async {
await tester.pumpWidget(buildTestApp(const FairUseBannerHarness(fairUseStatus: {'stage': 'restrict'})));
await tester.pumpAndSettle();
expect(find.byKey(const Key('fair_use_banner_tap')), findsOneWidget);
expect(find.textContaining('Restricted'), findsOneWidget);
});
});
group('Fair Use banner styling', () {
testWidgets('warning uses amber dot color', (tester) async {
await tester.pumpWidget(buildTestApp(const FairUseBannerHarness(fairUseStatus: {'stage': 'warning'})));
await tester.pumpAndSettle();
final dot = tester.widget<Container>(find.byKey(const Key('fair_use_dot')));
final decoration = dot.decoration as BoxDecoration;
expect(decoration.color, const Color(0xFFFBBF24));
});
testWidgets('throttle uses orange dot color', (tester) async {
await tester.pumpWidget(buildTestApp(const FairUseBannerHarness(fairUseStatus: {'stage': 'throttle'})));
await tester.pumpAndSettle();
final dot = tester.widget<Container>(find.byKey(const Key('fair_use_dot')));
final decoration = dot.decoration as BoxDecoration;
expect(decoration.color, const Color(0xFFF97316));
});
testWidgets('restrict uses red dot color', (tester) async {
await tester.pumpWidget(buildTestApp(const FairUseBannerHarness(fairUseStatus: {'stage': 'restrict'})));
await tester.pumpAndSettle();
final dot = tester.widget<Container>(find.byKey(const Key('fair_use_dot')));
final decoration = dot.decoration as BoxDecoration;
expect(decoration.color, const Color(0xFFEF4444));
});
testWidgets('banner has chevron icon', (tester) async {
await tester.pumpWidget(buildTestApp(const FairUseBannerHarness(fairUseStatus: {'stage': 'warning'})));
await tester.pumpAndSettle();
expect(find.byIcon(Icons.chevron_right), findsOneWidget);
});
});
group('Fair Use banner tap target', () {
testWidgets('tapping banner navigates to FairUsePage', (tester) async {
await tester.pumpWidget(buildTestApp(const FairUseBannerHarness(fairUseStatus: {'stage': 'restrict'})));
await tester.pumpAndSettle();
final gesture = find.byKey(const Key('fair_use_banner_tap'));
expect(gesture, findsOneWidget);
// Tap the banner
await tester.tap(gesture);
await tester.pumpAndSettle();
// Verify FairUsePage was pushed onto the navigator
expect(find.byType(FairUsePage), findsOneWidget);
});
});
group('Fair Use banner missing stage key', () {
testWidgets('hidden when status map has no stage key', (tester) async {
await tester.pumpWidget(buildTestApp(const FairUseBannerHarness(fairUseStatus: {'other': 'data'})));
await tester.pumpAndSettle();
// Default stage is 'none', so banner should be hidden
expect(find.byKey(const Key('fair_use_banner_tap')), findsNothing);
});
});
}