diff --git a/packages/path_provider/CHANGELOG.md b/packages/path_provider/CHANGELOG.md index 7546844b3549..498127adefe6 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 219d6660df7e..ca9ae8cf642a 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 @@ void main() { }); 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 result = getApplicationSupportDirectory(); - expect(result, throwsA(isInstanceOf())); - } + 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 c53468ae05ca..069973a5aca7 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 @@ Future getApplicationDocumentsDirectory() async { /// /// On Android this uses the `getExternalFilesDir(null)`. Future getExternalStorageDirectory() async { - if (Platform.isIOS) + if (_platform.isIOS) throw UnsupportedError("Functionality not available on iOS"); final String path = await _channel.invokeMethod('getStorageDirectory'); diff --git a/packages/path_provider/pubspec.yaml b/packages/path_provider/pubspec.yaml index 634ba1ce834b..e7e7555d93cc 100644 --- a/packages/path_provider/pubspec.yaml +++ b/packages/path_provider/pubspec.yaml @@ -3,7 +3,7 @@ description: Flutter plugin for getting commonly used locations on the Android & iOS file systems, such as the temp and app data directories. author: Flutter Team 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 @@ flutter: 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 ec02cf8bbb0e..1e8afecab1cd 100644 --- a/packages/path_provider/test/path_provider_test.dart +++ b/packages/path_provider/test/path_provider_test.dart @@ -7,6 +7,7 @@ import 'dart:io'; 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 @@ void main() { return response; }); + setUp(() { + setMockPathProviderPlatform(FakePlatform(operatingSystem: 'android')); + }); + tearDown(() { log.clear(); }); @@ -35,6 +40,18 @@ void main() { expect(directory, isNull); }); + test('getApplicationSupportDirectory test', () async { + response = null; + final Directory directory = await getApplicationSupportDirectory(); + expect( + log, + [ + isMethodCall('getApplicationSupportDirectory', arguments: null) + ], + ); + expect(directory, isNull); + }); + test('getApplicationDocumentsDirectory test', () async { response = null; final Directory directory = await getApplicationDocumentsDirectory(); @@ -47,6 +64,28 @@ void main() { expect(directory, isNull); }); + test('getExternalStorageDirectory test', () async { + response = null; + final Directory directory = await getExternalStorageDirectory(); + expect( + log, + [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 @@ void main() { 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)); + }); }