Unverified Commit 70e1dc73 authored by Loïc Sharma's avatar Loïc Sharma Committed by GitHub

[Clipboard] Assert at least one clipboard data variant is provided (#122446)

[Clipboard] Assert at least one clipboard data variant is provided
parent 48c06ad2
......@@ -2,7 +2,6 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'package:flutter/foundation.dart';
import 'system_channels.dart';
......@@ -14,9 +13,12 @@ import 'system_channels.dart';
@immutable
class ClipboardData {
/// Creates data for the system clipboard.
const ClipboardData({ this.text });
const ClipboardData({ required String this.text });
/// Plain text variant of this clipboard data.
// This is nullable as other clipboard data variants, like images, may be
// added in the future. Currently, plain text is the only supported variant
// and this is guaranteed to be non-null.
final String? text;
}
......@@ -54,7 +56,7 @@ abstract final class Clipboard {
if (result == null) {
return null;
}
return ClipboardData(text: result['text'] as String?);
return ClipboardData(text: result['text'] as String);
}
/// Returns a future that resolves to true iff the clipboard contains string
......
// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';
import '../widgets/clipboard_utils.dart';
void main() {
final MockClipboard mockClipboard = MockClipboard();
TestWidgetsFlutterBinding.ensureInitialized()
.defaultBinaryMessenger.setMockMethodCallHandler(SystemChannels.platform, mockClipboard.handleMethodCall);
test('Clipboard.getData returns text', () async {
mockClipboard.clipboardData = <String, dynamic>{
'text': 'Hello world',
};
final ClipboardData? data = await Clipboard.getData(Clipboard.kTextPlain);
expect(data, isNotNull);
expect(data!.text, equals('Hello world'));
});
test('Clipboard.getData returns null', () async {
mockClipboard.clipboardData = null;
final ClipboardData? data = await Clipboard.getData(Clipboard.kTextPlain);
expect(data, isNull);
});
test('Clipboard.getData throws if text is missing', () async {
mockClipboard.clipboardData = <String, dynamic>{};
expect(() => Clipboard.getData(Clipboard.kTextPlain), throwsA(isA<TypeError>()));
});
test('Clipboard.getData throws if text is null', () async {
mockClipboard.clipboardData = <String, dynamic>{
'text': null,
};
expect(() => Clipboard.getData(Clipboard.kTextPlain), throwsA(isA<TypeError>()));
});
test('Clipboard.setData sets text', () async {
await Clipboard.setData(const ClipboardData(text: 'Hello world'));
expect(mockClipboard.clipboardData, <String, dynamic>{
'text': 'Hello world',
});
});
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment