import 'package:flutter/material.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:jiu_client/core/config/store_compliance.dart'; import 'package:jiu_client/core/theme/themes.dart'; import 'package:jiu_client/widgets/privacy_consent_gate.dart'; import 'package:shared_preferences/shared_preferences.dart'; /// 首启隐私同意闸:移动端首启弹出并阻断交互,同意持久化后不再出现; /// 不同意(非 Android)留在弹层并给提示。 Widget _app() => MaterialApp( theme: buildTheme('a'), builder: (c, child) => PrivacyConsentGate(child: child ?? const SizedBox.shrink()), home: const Scaffold(body: Text('APP')), ); void main() { tearDown(() => debugForcePrivacyConsentRequired = null); testWidgets('首启弹出,同意后关闭并持久化', (tester) async { SharedPreferences.setMockInitialValues({}); debugForcePrivacyConsentRequired = true; await tester.pumpWidget(_app()); await tester.pumpAndSettle(); expect(find.text('个人信息保护提示'), findsOneWidget); await tester.tap(find.text('同意并继续')); await tester.pumpAndSettle(); expect(find.text('个人信息保护提示'), findsNothing); final prefs = await SharedPreferences.getInstance(); expect(prefs.getBool('privacy_consent_v1'), isTrue); }); testWidgets('已同意过:不再弹出', (tester) async { SharedPreferences.setMockInitialValues({'privacy_consent_v1': true}); debugForcePrivacyConsentRequired = true; await tester.pumpWidget(_app()); await tester.pumpAndSettle(); expect(find.text('个人信息保护提示'), findsNothing); expect(find.text('APP'), findsOneWidget); }); testWidgets('不同意(非 Android):留在弹层并提示', (tester) async { SharedPreferences.setMockInitialValues({}); debugForcePrivacyConsentRequired = true; await tester.pumpWidget(_app()); await tester.pumpAndSettle(); await tester.tap(find.text('不同意')); await tester.pump(); expect(find.text('需同意后方可使用岩美酒库'), findsOneWidget); expect(find.text('个人信息保护提示'), findsOneWidget); }); testWidgets('桌面/Web 形态:不弹', (tester) async { SharedPreferences.setMockInitialValues({}); debugForcePrivacyConsentRequired = false; await tester.pumpWidget(_app()); await tester.pumpAndSettle(); expect(find.text('个人信息保护提示'), findsNothing); }); }