18807b8ac2
Over plain http on a tailnet address the page is not a secure context and
navigator.clipboard is undefined, so the web engine refused every copy.
ClipboardReportingMessenger now answers clipboard calls through the
browser's legacy clipboard when the API is missing (web only, conditional
import): copy uses execCommand('copy') with a one-shot copy-event listener
(no focus or selection change); a paste key's Clipboard.getData takes the
browser's own paste event, the key reported not-handled so the browser
still fires it. A Paste button/menu says to use Ctrl+V or the https address.
Secure pages keep the engine path. Capture kit: --insecure and clipboard=.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
107 lines
3.0 KiB
Dart
107 lines
3.0 KiB
Dart
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<ByteData?>? send(String channel, ByteData? message) async =>
|
|
reply(codec.decodeMethodCall(message));
|
|
|
|
@override
|
|
Future<void> 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<String>, Object?)> call(
|
|
String method,
|
|
ByteData? Function(MethodCall) reply,
|
|
) async {
|
|
final reports = <String>[];
|
|
final messenger = ClipboardReportingMessenger(
|
|
_Platform(reply),
|
|
report: reports.add,
|
|
);
|
|
final channel = MethodChannel(
|
|
SystemChannels.platform.name,
|
|
codec,
|
|
messenger,
|
|
);
|
|
Object? error;
|
|
try {
|
|
await channel.invokeMethod<void>(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<PlatformException>());
|
|
},
|
|
);
|
|
|
|
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);
|
|
});
|
|
}
|