Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.

[share] Migrate unit tests to null-safety. #3660

Merged
merged 1 commit into from
Mar 2, 2021
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/share/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 2.0.1

* Migrate unit tests to sound null safety.

## 2.0.0

* Migrate to null safety.
Expand Down
4 changes: 1 addition & 3 deletions packages/share/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: share
description: Flutter plugin for sharing content via the platform share UI, using
the ACTION_SEND intent on Android and UIActivityViewController on iOS.
homepage: https://github.com/flutter/plugins/tree/master/packages/share
version: 2.0.0
version: 2.0.1

flutter:
plugin:
Expand All @@ -20,8 +20,6 @@ dependencies:
sdk: flutter

dev_dependencies:
test: ^1.16.3
mockito: ^5.0.0-nullsafety.7
flutter_test:
sdk: flutter
integration_test:
Expand Down
89 changes: 56 additions & 33 deletions packages/share/test/share_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,33 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// @dart = 2.9

import 'dart:io';
import 'dart:ui';

import 'package:flutter_test/flutter_test.dart' show TestWidgetsFlutterBinding;
import 'package:mockito/mockito.dart';
import 'package:share/share.dart';
import 'package:test/test.dart';

import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:share/share.dart';

void main() {
TestWidgetsFlutterBinding.ensureInitialized();
TestWidgetsFlutterBinding.ensureInitialized(); // Required for MethodChannels

MockMethodChannel mockChannel;
late FakeMethodChannel fakeChannel;

setUp(() {
mockChannel = MockMethodChannel();
// Re-pipe to mockito for easier verifies.
fakeChannel = FakeMethodChannel();
// Re-pipe to our fake to verify invocations.
Share.channel.setMockMethodCallHandler((MethodCall call) async {
// The explicit type can be void as the only method call has a return type of void.
await mockChannel.invokeMethod<void>(call.method, call.arguments);
await fakeChannel.invokeMethod<void>(call.method, call.arguments);
});
});

test('sharing empty fails', () {
expect(
() => Share.share(''),
throwsA(const TypeMatcher<AssertionError>()),
throwsA(isA<AssertionError>()),
);
verifyZeroInteractions(mockChannel);
expect(fakeChannel.invocation, isNull);
});

test('sharing origin sets the right params', () async {
Expand All @@ -42,34 +37,47 @@ void main() {
subject: 'some subject to share',
sharePositionOrigin: const Rect.fromLTWH(1.0, 2.0, 3.0, 4.0),
);
verify(mockChannel.invokeMethod<void>('share', <String, dynamic>{
'text': 'some text to share',
'subject': 'some subject to share',
'originX': 1.0,
'originY': 2.0,
'originWidth': 3.0,
'originHeight': 4.0,
}));

expect(
fakeChannel.invocation,
equals({
'share': {
'text': 'some text to share',
'subject': 'some subject to share',
'originX': 1.0,
'originY': 2.0,
'originWidth': 3.0,
'originHeight': 4.0,
}
}),
);
});

test('sharing empty file fails', () {
expect(
() => Share.shareFiles(['']),
throwsA(const TypeMatcher<AssertionError>()),
throwsA(isA<AssertionError>()),
);
verifyZeroInteractions(mockChannel);
expect(fakeChannel.invocation, isNull);
});

test('sharing file sets correct mimeType', () async {
final String path = 'tempfile-83649a.png';
final File file = File(path);
try {
file.createSync();

await Share.shareFiles([path]);
verify(mockChannel.invokeMethod('shareFiles', <String, dynamic>{
'paths': [path],
'mimeTypes': ['image/png'],
}));

expect(
fakeChannel.invocation,
equals({
'shareFiles': {
'paths': [path],
'mimeTypes': ['image/png'],
}
}),
);
} finally {
file.deleteSync();
}
Expand All @@ -80,15 +88,30 @@ void main() {
final File file = File(path);
try {
file.createSync();

await Share.shareFiles([path], mimeTypes: ['*/*']);
verify(mockChannel.invokeMethod('shareFiles', <String, dynamic>{
'paths': [file.path],
'mimeTypes': ['*/*'],
}));

expect(
fakeChannel.invocation,
equals({
'shareFiles': {
'paths': [file.path],
'mimeTypes': ['*/*'],
}
}),
);
} finally {
file.deleteSync();
}
});
}

class MockMethodChannel extends Mock implements MethodChannel {}
class FakeMethodChannel extends Fake implements MethodChannel {
Map<String, dynamic>? invocation;

@override
Future<T?> invokeMethod<T>(String method, [dynamic arguments]) async {
this.invocation = {method: arguments};
return null;
}
}