From a8ea4d667c06edd55d0ea219de5e1ed01a95f3cd Mon Sep 17 00:00:00 2001 From: J-P Nurmi Date: Mon, 31 Jul 2023 20:15:48 +0200 Subject: [PATCH] [path_provider_platform_interface] Add getApplicationCachePath() (#4614) Platform interface changes split out from #4483. /cc @stuartmorgan --- .../CHANGELOG.md | 3 +- .../lib/path_provider_platform_interface.dart | 6 ++++ .../lib/src/method_channel_path_provider.dart | 5 ++++ .../pubspec.yaml | 2 +- .../method_channel_path_provider_test.dart | 15 ++++++++++ ...path_provider_platform_interface_test.dart | 29 +++++++++++++++++++ 6 files changed, 58 insertions(+), 2 deletions(-) create mode 100644 packages/path_provider/path_provider_platform_interface/test/path_provider_platform_interface_test.dart diff --git a/packages/path_provider/path_provider_platform_interface/CHANGELOG.md b/packages/path_provider/path_provider_platform_interface/CHANGELOG.md index f96711be4fdd0..051c0e5323cf8 100644 --- a/packages/path_provider/path_provider_platform_interface/CHANGELOG.md +++ b/packages/path_provider/path_provider_platform_interface/CHANGELOG.md @@ -1,5 +1,6 @@ -## NEXT +## 2.1.0 +* Adds getApplicationCachePath() for storing app-specific cache files. * Updates minimum supported SDK version to Flutter 3.3/Dart 2.18. * Aligns Dart and Flutter SDK constraints. diff --git a/packages/path_provider/path_provider_platform_interface/lib/path_provider_platform_interface.dart b/packages/path_provider/path_provider_platform_interface/lib/path_provider_platform_interface.dart index 517ac74d8fa0d..682d1ef0387d5 100644 --- a/packages/path_provider/path_provider_platform_interface/lib/path_provider_platform_interface.dart +++ b/packages/path_provider/path_provider_platform_interface/lib/path_provider_platform_interface.dart @@ -62,6 +62,12 @@ abstract class PathProviderPlatform extends PlatformInterface { 'getApplicationDocumentsPath() has not been implemented.'); } + /// Path to a directory where application specific cache data can be stored. + Future getApplicationCachePath() { + throw UnimplementedError( + 'getApplicationCachePath() has not been implemented.'); + } + /// Path to a directory where the application may access top level storage. /// The current operating system should be determined before issuing this /// function call, as this functionality is only available on Android. diff --git a/packages/path_provider/path_provider_platform_interface/lib/src/method_channel_path_provider.dart b/packages/path_provider/path_provider_platform_interface/lib/src/method_channel_path_provider.dart index 991be55bce8c0..02729f896168a 100644 --- a/packages/path_provider/path_provider_platform_interface/lib/src/method_channel_path_provider.dart +++ b/packages/path_provider/path_provider_platform_interface/lib/src/method_channel_path_provider.dart @@ -52,6 +52,11 @@ class MethodChannelPathProvider extends PathProviderPlatform { .invokeMethod('getApplicationDocumentsDirectory'); } + @override + Future getApplicationCachePath() { + return methodChannel.invokeMethod('getApplicationCacheDirectory'); + } + @override Future getExternalStoragePath() { if (!_platform.isAndroid) { diff --git a/packages/path_provider/path_provider_platform_interface/pubspec.yaml b/packages/path_provider/path_provider_platform_interface/pubspec.yaml index 193bb85b94a48..44eb59aa4a665 100644 --- a/packages/path_provider/path_provider_platform_interface/pubspec.yaml +++ b/packages/path_provider/path_provider_platform_interface/pubspec.yaml @@ -4,7 +4,7 @@ repository: https://github.com/flutter/packages/tree/main/packages/path_provider issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+path_provider%22 # NOTE: We strongly prefer non-breaking changes, even at the expense of a # less-clean API. See https://flutter.dev/go/platform-interface-breaking-changes -version: 2.0.6 +version: 2.1.0 environment: sdk: ">=2.18.0 <4.0.0" diff --git a/packages/path_provider/path_provider_platform_interface/test/method_channel_path_provider_test.dart b/packages/path_provider/path_provider_platform_interface/test/method_channel_path_provider_test.dart index 035e7becb9ff8..2c68ec574c8a0 100644 --- a/packages/path_provider/path_provider_platform_interface/test/method_channel_path_provider_test.dart +++ b/packages/path_provider/path_provider_platform_interface/test/method_channel_path_provider_test.dart @@ -14,6 +14,7 @@ void main() { const String kApplicationSupportPath = 'applicationSupportPath'; const String kLibraryPath = 'libraryPath'; const String kApplicationDocumentsPath = 'applicationDocumentsPath'; + const String kApplicationCachePath = 'applicationCachePath'; const String kExternalCachePaths = 'externalCachePaths'; const String kExternalStoragePaths = 'externalStoragePaths'; const String kDownloadsPath = 'downloadsPath'; @@ -39,6 +40,8 @@ void main() { return kLibraryPath; case 'getApplicationDocumentsDirectory': return kApplicationDocumentsPath; + case 'getApplicationCacheDirectory': + return kApplicationCachePath; case 'getExternalStorageDirectories': return [kExternalStoragePaths]; case 'getExternalCacheDirectories': @@ -126,6 +129,18 @@ void main() { expect(path, kApplicationDocumentsPath); }); + test('getApplicationCachePath succeeds', () async { + final String? result = + await methodChannelPathProvider.getApplicationCachePath(); + expect( + log, + [ + isMethodCall('getApplicationCacheDirectory', arguments: null) + ], + ); + expect(result, kApplicationCachePath); + }); + test('getExternalCachePaths android succeeds', () async { final List? result = await methodChannelPathProvider.getExternalCachePaths(); diff --git a/packages/path_provider/path_provider_platform_interface/test/path_provider_platform_interface_test.dart b/packages/path_provider/path_provider_platform_interface/test/path_provider_platform_interface_test.dart new file mode 100644 index 0000000000000..9d9bc851cef2d --- /dev/null +++ b/packages/path_provider/path_provider_platform_interface/test/path_provider_platform_interface_test.dart @@ -0,0 +1,29 @@ +// Copyright 2013 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +import 'package:flutter_test/flutter_test.dart'; +import 'package:path_provider_platform_interface/path_provider_platform_interface.dart'; +import 'package:path_provider_platform_interface/src/method_channel_path_provider.dart'; + +void main() { + TestWidgetsFlutterBinding.ensureInitialized(); + + group('$PathProviderPlatform', () { + test('$MethodChannelPathProvider is the default instance', () { + expect(PathProviderPlatform.instance, isA()); + }); + + test('getApplicationCachePath throws unimplemented error', () { + final ExtendsPathProviderPlatform pathProviderPlatform = + ExtendsPathProviderPlatform(); + + expect( + () => pathProviderPlatform.getApplicationCachePath(), + throwsUnimplementedError, + ); + }); + }); +} + +class ExtendsPathProviderPlatform extends PathProviderPlatform {}