Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.
Merged
6 changes: 6 additions & 0 deletions packages/path_provider/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
19 changes: 7 additions & 12 deletions packages/path_provider/example/test_driver/path_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<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 {
Expand Down
13 changes: 11 additions & 2 deletions packages/path_provider/lib/path_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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.
///
Expand Down Expand Up @@ -77,7 +86,7 @@ Future<Directory> getApplicationDocumentsDirectory() async {
///
/// 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');
Expand Down
4 changes: 3 additions & 1 deletion packages/path_provider/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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 <[email protected]>
homepage: https://github.com/flutter/plugins/tree/master/packages/path_provider
version: 1.2.1
version: 1.2.2

flutter:
plugin:
Expand All @@ -14,6 +14,8 @@ flutter:
dependencies:
flutter:
sdk: flutter
platform: ^2.0.0
meta: ^1.0.5

dev_dependencies:
flutter_test:
Expand Down
53 changes: 53 additions & 0 deletions packages/path_provider/test/path_provider_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -21,6 +22,10 @@ void main() {
return response;
});

setUp(() {
setMockPathProviderPlatform(FakePlatform(operatingSystem: 'android'));
});

tearDown(() {
log.clear();
});
Expand All @@ -35,6 +40,18 @@ void main() {
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();
Expand All @@ -47,17 +64,53 @@ void main() {
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;
final Directory directory = await getTemporaryDirectory();
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));
});
}