Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from Mar 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 5 additions & 3 deletions packages/flutter/lib/src/services/clipboard.dart
Expand Up @@ -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';
Expand All @@ -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;
loic-sharma marked this conversation as resolved.
Show resolved Hide resolved
}

Expand Down Expand Up @@ -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
Expand Down
55 changes: 55 additions & 0 deletions packages/flutter/test/services/clipboard_test.dart
@@ -0,0 +1,55 @@
// 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',
});
});
}