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

Commit

Permalink
Fixes and new tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Emmanuel Garcia committed Feb 24, 2021
1 parent 730b6cc commit 1fe6814
Show file tree
Hide file tree
Showing 10 changed files with 74 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,22 @@ import 'dart:async';
import 'dart:io';
import 'package:flutter_test/flutter_test.dart';
import 'package:path_provider/path_provider.dart';
import 'package:path_provider_platform_interface/path_provider_platform_interface.dart';
import 'package:path_provider_linux/path_provider_linux.dart';
import 'package:path_provider_windows/path_provider_windows.dart';
import 'package:integration_test/integration_test.dart';

void main() {
IntegrationTestWidgetsFlutterBinding.ensureInitialized();

test('path provider instance on Windows', () {
expect(PathProviderPlatform.instance, isA<PathProviderWindows>());
}, skip: !Platform.isWindows);

test('path provider instance on Linux', () {
expect(PathProviderPlatform.instance, isA<PathProviderLinux>());
}, skip: !Platform.isLinux);

testWidgets('getTemporaryDirectory', (WidgetTester tester) async {
final Directory result = await getTemporaryDirectory();
_verifySampleFile(result, 'temporaryDirectory');
Expand Down
9 changes: 9 additions & 0 deletions packages/path_provider/path_provider/example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,15 @@ dependencies:
# the parent directory to use the current plugin's version.
path: ../

# TODO(egarciad): Remove the overrides once the packages are published.
dependency_overrides:
path_provider_macos:
path: ../../path_provider_macos
path_provider_linux:
path: ../../path_provider_linux
path_provider_windows:
path: ../../path_provider_windows

dev_dependencies:
integration_test:
path: ../../../integration_test
Expand Down
19 changes: 10 additions & 9 deletions packages/path_provider/path_provider/lib/path_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,10 @@
// found in the LICENSE file.

import 'dart:async';
import 'dart:io' show Directory, Platform;
import 'dart:io' show Directory;

import 'package:flutter/foundation.dart' show kIsWeb, visibleForTesting;
import 'package:path_provider_linux/path_provider_linux.dart';
import 'package:path_provider_windows/path_provider_windows.dart';
import 'package:flutter/foundation.dart' show visibleForTesting;
import 'package:path_provider_platform_interface/path_provider_platform_interface.dart';
import 'package:path_provider_platform_interface/src/method_channel_path_provider.dart';

export 'package:path_provider_platform_interface/path_provider_platform_interface.dart'
show StorageDirectory;
Expand Down Expand Up @@ -76,7 +73,8 @@ Future<Directory> getTemporaryDirectory() async {
/// Throws a `MissingPlatformDirectoryException` if the system is unable to
/// provide the directory.
Future<Directory> getApplicationSupportDirectory() async {
final String? path = await PathProviderPlatform.instance.getApplicationSupportPath();
final String? path =
await PathProviderPlatform.instance.getApplicationSupportPath();
if (path == null) {
throw MissingPlatformDirectoryException(
'Unable to get application support directory');
Expand Down Expand Up @@ -114,7 +112,8 @@ Future<Directory> getLibraryDirectory() async {
/// Throws a `MissingPlatformDirectoryException` if the system is unable to
/// provide the directory.
Future<Directory> getApplicationDocumentsDirectory() async {
final String? path = await PathProviderPlatform.instance.getApplicationDocumentsPath();
final String? path =
await PathProviderPlatform.instance.getApplicationDocumentsPath();
if (path == null) {
throw MissingPlatformDirectoryException(
'Unable to get application documents directory');
Expand All @@ -131,7 +130,8 @@ Future<Directory> getApplicationDocumentsDirectory() async {
///
/// On Android this uses the `getExternalFilesDir(null)`.
Future<Directory?> getExternalStorageDirectory() async {
final String? path = await PathProviderPlatform.instance.getExternalStoragePath();
final String? path =
await PathProviderPlatform.instance.getExternalStoragePath();
if (path == null) {
return null;
}
Expand All @@ -152,7 +152,8 @@ Future<Directory?> getExternalStorageDirectory() async {
/// On Android this returns Context.getExternalCacheDirs() or
/// Context.getExternalCacheDir() on API levels below 19.
Future<List<Directory>?> getExternalCacheDirectories() async {
final List<String>? paths = await PathProviderPlatform.instance.getExternalCachePaths();
final List<String>? paths =
await PathProviderPlatform.instance.getExternalCachePaths();
if (paths == null) {
return null;
}
Expand Down
9 changes: 0 additions & 9 deletions packages/path_provider/path_provider/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,6 @@ dependencies:
path_provider_linux: ^2.0.0
path_provider_windows: ^2.0.0

# TODO(egarciad): Bump dependencies above and remove the overrides.
dependency_overrides:
path_provider_macos:
path: ../path_provider_macos
path_provider_linux:
path: ../path_provider_linux
path_provider_windows:
path: ../path_provider_windows

dev_dependencies:
integration_test:
path: ../../integration_test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ import 'package:path_provider_platform_interface/path_provider_platform_interfac
/// path_provider will also depend on path_provider_windows, not just at the
/// pubspec level but the code level.
class PathProviderWindows extends PathProviderPlatform {
/// Registers the Windows implementation.
static void registerWith() {
PathProviderPlatform.instance = PathProviderWindows();
}

/// Errors on attempted instantiation of the stub. It exists only to satisfy
/// compile-time dependencies, and should never actually be created.
PathProviderWindows() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,15 @@ dev_dependencies:
path: ../../../integration_test
pedantic: ^1.10.0

# TODO(egarciad): Bump dependencies above and remove the overrides.
dependency_overrides:
shared_preferences_linux:
path: ../../shared_preferences_linux
shared_preferences_macos:
path: ../../shared_preferences_macos
shared_preferences_windows:
path: ../../shared_preferences_windows

flutter:
uses-material-design: true

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,9 @@
// found in the LICENSE file.

import 'dart:async';
import 'dart:io' show Platform;

import 'package:flutter/foundation.dart' show kIsWeb;
import 'package:meta/meta.dart';
import 'package:shared_preferences_linux/shared_preferences_linux.dart';
import 'package:shared_preferences_platform_interface/method_channel_shared_preferences.dart';
import 'package:shared_preferences_platform_interface/shared_preferences_platform_interface.dart';
import 'package:shared_preferences_windows/shared_preferences_windows.dart';

/// Wraps NSUserDefaults (on iOS) and SharedPreferences (on Android), providing
/// a persistent store for simple data.
Expand All @@ -21,7 +16,6 @@ class SharedPreferences {

static const String _prefix = 'flutter.';
static Completer<SharedPreferences>? _completer;
static bool _manualDartRegistrationNeeded = true;

/// Loads and parses the [SharedPreferences] for this app from disk.
///
Expand Down Expand Up @@ -130,7 +124,8 @@ class SharedPreferences {
} else {
_preferenceCache[key] = value;
}
return SharedPreferencesStorePlatform.instance.setValue(valueType, prefixedKey, value);
return SharedPreferencesStorePlatform.instance
.setValue(valueType, prefixedKey, value);
}

/// Always returns true.
Expand Down
10 changes: 0 additions & 10 deletions packages/shared_preferences/shared_preferences/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,6 @@ dependencies:
shared_preferences_web: ^2.0.0
shared_preferences_windows: ^2.0.0

# TODO(egarciad): Bump dependencies above and remove the overrides.
dependency_overrides:
shared_preferences_linux:
path: ../shared_preferences_linux
shared_preferences_macos:
path: ../shared_preferences_macos
shared_preferences_windows:
path: ../shared_preferences_windows


dev_dependencies:
flutter_test:
sdk: flutter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ void main() {
preferences = SharedPreferencesLinux();
});

tearDown(() {
preferences.clear();
tearDown(() async {
await preferences.clear();
});

testWidgets('reading', (WidgetTester _) async {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

// @dart=2.9

import 'dart:async';
import 'package:flutter_test/flutter_test.dart';
import 'package:shared_preferences_windows/shared_preferences_windows.dart';
import 'package:integration_test/integration_test.dart';
Expand All @@ -31,12 +30,12 @@ void main() {

SharedPreferencesWindows preferences;

setUp(() async {
setUp(() {
preferences = SharedPreferencesWindows();
});

tearDown(() {
preferences.clear();
tearDown(() async {
expect(await preferences.clear(), isTrue);
});

testWidgets('reading', (WidgetTester _) async {
Expand All @@ -49,15 +48,31 @@ void main() {
});

testWidgets('writing', (WidgetTester _) async {
await Future.wait(<Future<bool>>[
preferences.setValue(
expect(
await preferences.setValue(
'String', 'String', kTestValues2['flutter.String']),
preferences.setValue('Bool', 'bool', kTestValues2['flutter.bool']),
preferences.setValue('Int', 'int', kTestValues2['flutter.int']),
preferences.setValue(
isTrue,
);
expect(
await preferences.setValue(
'Bool', 'bool', kTestValues2['flutter.bool']),
isTrue,
);
expect(
await preferences.setValue('Int', 'int', kTestValues2['flutter.int']),
isTrue,
);
expect(
await preferences.setValue(
'Double', 'double', kTestValues2['flutter.double']),
preferences.setValue('StringList', 'List', kTestValues2['flutter.List'])
]);
isTrue,
);
expect(
await preferences.setValue(
'StringList', 'List', kTestValues2['flutter.List']),
isTrue,
);

final Map<String, Object> values = await preferences.getAll();
expect(values['String'], kTestValues2['flutter.String']);
expect(values['bool'], kTestValues2['flutter.bool']);
Expand Down

0 comments on commit 1fe6814

Please sign in to comment.