import 'dart:ui' as ui; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:pipelines_app/application/clipboard_failures.dart'; import 'package:pipelines_app/application/insecure_clipboard.dart'; /// A platform side that answers every platform-channel call with [reply]. class _Platform implements BinaryMessenger { _Platform(this.reply); final ByteData? Function(MethodCall call) reply; static const codec = JSONMethodCodec(); @override Future? send(String channel, ByteData? message) async => reply(codec.decodeMethodCall(message)); @override Future handlePlatformMessage( String channel, ByteData? data, ui.PlatformMessageResponseCallback? callback, ) async {} @override void setMessageHandler(String channel, MessageHandler? handler) {} } void main() { const codec = JSONMethodCodec(); final refused = codec.encodeErrorEnvelope( code: 'copy_fail', message: 'Clipboard.setData failed.', ); Future<(List, Object?)> call( String method, ByteData? Function(MethodCall) reply, ) async { final reports = []; final messenger = ClipboardReportingMessenger( _Platform(reply), report: reports.add, ); final channel = MethodChannel( SystemChannels.platform.name, codec, messenger, ); Object? error; try { await channel.invokeMethod(method, {'text': 'x'}); } on PlatformException catch (e) { error = e; } return (reports, error); } test( 'a refused clipboard write is reported and still reaches the caller', () async { final (reports, error) = await call('Clipboard.setData', (_) => refused); expect(reports, [ 'Could not copy: the browser refused access to the clipboard', ]); expect(error, isA()); }, ); test('an insecure page says why; a refused read is reported too', () async { final (reports, _) = await call( 'Clipboard.getData', (_) => codec.encodeErrorEnvelope( code: 'paste_fail', message: 'Clipboard is not available in the context.', ), ); expect(reports, [insecurePasteMessage()]); }); test('successes and passive checks are not reported', () async { final (ok, _) = await call( 'Clipboard.setData', (_) => codec.encodeSuccessEnvelope(null), ); expect(ok, isEmpty); final (checks, _) = await call( 'Clipboard.hasStrings', (_) => codec.encodeErrorEnvelope(code: 'has_strings_fail'), ); expect(checks, isEmpty); }); testWidgets('the message shows in the app', (tester) async { await tester.pumpWidget( MaterialApp( scaffoldMessengerKey: appScaffoldMessengerKey, home: const Scaffold(), ), ); expect(showAppMessage('Could not copy: denied'), isTrue); await tester.pump(); expect(find.text('Could not copy: denied'), findsOneWidget); }); }