[path_provider] Add missing tests (#1990)
* Fix getApplicationSupportDirectory integration test
* Add missing unit tests
* setMockPathProviderPlatform
diff --git a/packages/path_provider/CHANGELOG.md b/packages/path_provider/CHANGELOG.md
index 7546844..498127a 100644
--- a/packages/path_provider/CHANGELOG.md
+++ b/packages/path_provider/CHANGELOG.md
@@ -1,3 +1,9 @@
+## 1.2.2
+
+* Correct the integration test for Android's `getApplicationSupportDirectory` call.
+* Introduce `setMockPathProviderPlatform` for API for tests.
+* Adds missing unit and integration tests.
+
## 1.2.1
* Fix fall through bug.
diff --git a/packages/path_provider/example/test_driver/path_provider.dart b/packages/path_provider/example/test_driver/path_provider.dart
index 219d666..ca9ae8c 100644
--- a/packages/path_provider/example/test_driver/path_provider.dart
+++ b/packages/path_provider/example/test_driver/path_provider.dart
@@ -36,18 +36,13 @@
});
test('getApplicationSupportDirectory', () async {
- if (Platform.isIOS) {
- final Directory result = await getApplicationSupportDirectory();
- final String uuid = Uuid().v1();
- final File file = File('${result.path}/$uuid.txt');
- file.writeAsStringSync('Hello world!');
- expect(file.readAsStringSync(), 'Hello world!');
- expect(result.listSync(), isNotEmpty);
- file.deleteSync();
- } else if (Platform.isAndroid) {
- final Future<Directory> result = getApplicationSupportDirectory();
- expect(result, throwsA(isInstanceOf<UnsupportedError>()));
- }
+ final Directory result = await getApplicationSupportDirectory();
+ final String uuid = Uuid().v1();
+ final File file = File('${result.path}/$uuid.txt');
+ file.writeAsStringSync('Hello world!');
+ expect(file.readAsStringSync(), 'Hello world!');
+ expect(result.listSync(), isNotEmpty);
+ file.deleteSync();
});
test('getExternalStorageDirectory', () async {
diff --git a/packages/path_provider/lib/path_provider.dart b/packages/path_provider/lib/path_provider.dart
index c53468a..069973a 100644
--- a/packages/path_provider/lib/path_provider.dart
+++ b/packages/path_provider/lib/path_provider.dart
@@ -3,13 +3,22 @@
// found in the LICENSE file.
import 'dart:async';
-import 'dart:io';
+import 'dart:io' show Directory;
import 'package:flutter/services.dart';
+import 'package:meta/meta.dart';
+import 'package:platform/platform.dart';
const MethodChannel _channel =
MethodChannel('plugins.flutter.io/path_provider');
+Platform _platform = const LocalPlatform();
+
+@visibleForTesting
+void setMockPathProviderPlatform(Platform platform) {
+ _platform = platform;
+}
+
/// Path to the temporary directory on the device that is not backed up and is
/// suitable for storing caches of downloaded files.
///
@@ -77,7 +86,7 @@
///
/// On Android this uses the `getExternalFilesDir(null)`.
Future<Directory> getExternalStorageDirectory() async {
- if (Platform.isIOS)
+ if (_platform.isIOS)
throw UnsupportedError("Functionality not available on iOS");
final String path =
await _channel.invokeMethod<String>('getStorageDirectory');
diff --git a/packages/path_provider/pubspec.yaml b/packages/path_provider/pubspec.yaml
index 634ba1c..e7e7555 100644
--- a/packages/path_provider/pubspec.yaml
+++ b/packages/path_provider/pubspec.yaml
@@ -3,7 +3,7 @@
iOS file systems, such as the temp and app data directories.
author: Flutter Team <flutter-dev@googlegroups.com>
homepage: https://github.com/flutter/plugins/tree/master/packages/path_provider
-version: 1.2.1
+version: 1.2.2
flutter:
plugin:
@@ -14,6 +14,8 @@
dependencies:
flutter:
sdk: flutter
+ platform: ^2.0.0
+ meta: ^1.0.5
dev_dependencies:
flutter_test:
diff --git a/packages/path_provider/test/path_provider_test.dart b/packages/path_provider/test/path_provider_test.dart
index ec02cf8..1e8afec 100644
--- a/packages/path_provider/test/path_provider_test.dart
+++ b/packages/path_provider/test/path_provider_test.dart
@@ -7,6 +7,7 @@
import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:path_provider/path_provider.dart';
+import 'package:platform/platform.dart';
void main() {
TestWidgetsFlutterBinding.ensureInitialized();
@@ -21,6 +22,10 @@
return response;
});
+ setUp(() {
+ setMockPathProviderPlatform(FakePlatform(operatingSystem: 'android'));
+ });
+
tearDown(() {
log.clear();
});
@@ -35,6 +40,18 @@
expect(directory, isNull);
});
+ test('getApplicationSupportDirectory test', () async {
+ response = null;
+ final Directory directory = await getApplicationSupportDirectory();
+ expect(
+ log,
+ <Matcher>[
+ isMethodCall('getApplicationSupportDirectory', arguments: null)
+ ],
+ );
+ expect(directory, isNull);
+ });
+
test('getApplicationDocumentsDirectory test', () async {
response = null;
final Directory directory = await getApplicationDocumentsDirectory();
@@ -47,6 +64,28 @@
expect(directory, isNull);
});
+ test('getExternalStorageDirectory test', () async {
+ response = null;
+ final Directory directory = await getExternalStorageDirectory();
+ expect(
+ log,
+ <Matcher>[isMethodCall('getStorageDirectory', arguments: null)],
+ );
+ expect(directory, isNull);
+ });
+
+ test('getExternalStorageDirectory iOS test', () async {
+ setMockPathProviderPlatform(FakePlatform(operatingSystem: 'ios'));
+
+ response = null;
+ try {
+ await getExternalStorageDirectory();
+ fail('should throw UnsupportedError');
+ } catch (e) {
+ expect(e, isUnsupportedError);
+ }
+ });
+
test('TemporaryDirectory path test', () async {
final String fakePath = "/foo/bar/baz";
response = fakePath;
@@ -54,10 +93,24 @@
expect(directory.path, equals(fakePath));
});
+ test('ApplicationSupportDirectory path test', () async {
+ final String fakePath = "/foo/bar/baz";
+ response = fakePath;
+ final Directory directory = await getApplicationSupportDirectory();
+ expect(directory.path, equals(fakePath));
+ });
+
test('ApplicationDocumentsDirectory path test', () async {
final String fakePath = "/foo/bar/baz";
response = fakePath;
final Directory directory = await getApplicationDocumentsDirectory();
expect(directory.path, equals(fakePath));
});
+
+ test('ExternalStorageDirectory path test', () async {
+ final String fakePath = "/foo/bar/baz";
+ response = fakePath;
+ final Directory directory = await getExternalStorageDirectory();
+ expect(directory.path, equals(fakePath));
+ });
}