|
| 1 | +// Copyright 2013 The Flutter Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +import 'package:flutter/services.dart'; |
| 6 | +import 'package:flutter_test/flutter_test.dart'; |
| 7 | +import 'package:path_provider_android/path_provider_android.dart'; |
| 8 | +import 'package:path_provider_platform_interface/path_provider_platform_interface.dart'; |
| 9 | + |
| 10 | +void main() { |
| 11 | + TestWidgetsFlutterBinding.ensureInitialized(); |
| 12 | + const String kTemporaryPath = 'temporaryPath'; |
| 13 | + const String kApplicationSupportPath = 'applicationSupportPath'; |
| 14 | + const String kLibraryPath = 'libraryPath'; |
| 15 | + const String kApplicationDocumentsPath = 'applicationDocumentsPath'; |
| 16 | + const String kExternalCachePaths = 'externalCachePaths'; |
| 17 | + const String kExternalStoragePaths = 'externalStoragePaths'; |
| 18 | + const String kDownloadsPath = 'downloadsPath'; |
| 19 | + |
| 20 | + group('PathProviderAndroid', () { |
| 21 | + late PathProviderAndroid pathProvider; |
| 22 | + final List<MethodCall> log = <MethodCall>[]; |
| 23 | + |
| 24 | + setUp(() async { |
| 25 | + pathProvider = PathProviderAndroid(); |
| 26 | + TestDefaultBinaryMessengerBinding.instance!.defaultBinaryMessenger |
| 27 | + .setMockMethodCallHandler(pathProvider.methodChannel, |
| 28 | + (MethodCall methodCall) async { |
| 29 | + log.add(methodCall); |
| 30 | + switch (methodCall.method) { |
| 31 | + case 'getTemporaryDirectory': |
| 32 | + return kTemporaryPath; |
| 33 | + case 'getApplicationSupportDirectory': |
| 34 | + return kApplicationSupportPath; |
| 35 | + case 'getLibraryDirectory': |
| 36 | + return kLibraryPath; |
| 37 | + case 'getApplicationDocumentsDirectory': |
| 38 | + return kApplicationDocumentsPath; |
| 39 | + case 'getExternalStorageDirectories': |
| 40 | + return <String>[kExternalStoragePaths]; |
| 41 | + case 'getExternalCacheDirectories': |
| 42 | + return <String>[kExternalCachePaths]; |
| 43 | + case 'getDownloadsDirectory': |
| 44 | + return kDownloadsPath; |
| 45 | + default: |
| 46 | + return null; |
| 47 | + } |
| 48 | + }); |
| 49 | + }); |
| 50 | + |
| 51 | + tearDown(() { |
| 52 | + log.clear(); |
| 53 | + }); |
| 54 | + |
| 55 | + test('getTemporaryPath', () async { |
| 56 | + final String? path = await pathProvider.getTemporaryPath(); |
| 57 | + expect( |
| 58 | + log, |
| 59 | + <Matcher>[isMethodCall('getTemporaryDirectory', arguments: null)], |
| 60 | + ); |
| 61 | + expect(path, kTemporaryPath); |
| 62 | + }); |
| 63 | + |
| 64 | + test('getApplicationSupportPath', () async { |
| 65 | + final String? path = await pathProvider.getApplicationSupportPath(); |
| 66 | + expect( |
| 67 | + log, |
| 68 | + <Matcher>[ |
| 69 | + isMethodCall('getApplicationSupportDirectory', arguments: null) |
| 70 | + ], |
| 71 | + ); |
| 72 | + expect(path, kApplicationSupportPath); |
| 73 | + }); |
| 74 | + |
| 75 | + test('getLibraryPath fails', () async { |
| 76 | + try { |
| 77 | + await pathProvider.getLibraryPath(); |
| 78 | + fail('should throw UnsupportedError'); |
| 79 | + } catch (e) { |
| 80 | + expect(e, isUnsupportedError); |
| 81 | + } |
| 82 | + }); |
| 83 | + |
| 84 | + test('getApplicationDocumentsPath', () async { |
| 85 | + final String? path = await pathProvider.getApplicationDocumentsPath(); |
| 86 | + expect( |
| 87 | + log, |
| 88 | + <Matcher>[ |
| 89 | + isMethodCall('getApplicationDocumentsDirectory', arguments: null) |
| 90 | + ], |
| 91 | + ); |
| 92 | + expect(path, kApplicationDocumentsPath); |
| 93 | + }); |
| 94 | + |
| 95 | + test('getExternalCachePaths succeeds', () async { |
| 96 | + final List<String>? result = await pathProvider.getExternalCachePaths(); |
| 97 | + expect( |
| 98 | + log, |
| 99 | + <Matcher>[isMethodCall('getExternalCacheDirectories', arguments: null)], |
| 100 | + ); |
| 101 | + expect(result!.length, 1); |
| 102 | + expect(result.first, kExternalCachePaths); |
| 103 | + }); |
| 104 | + |
| 105 | + for (final StorageDirectory? type in <StorageDirectory?>[ |
| 106 | + null, |
| 107 | + ...StorageDirectory.values |
| 108 | + ]) { |
| 109 | + test('getExternalStoragePaths (type: $type) android succeeds', () async { |
| 110 | + final List<String>? result = |
| 111 | + await pathProvider.getExternalStoragePaths(type: type); |
| 112 | + expect( |
| 113 | + log, |
| 114 | + <Matcher>[ |
| 115 | + isMethodCall( |
| 116 | + 'getExternalStorageDirectories', |
| 117 | + arguments: <String, dynamic>{'type': type?.index}, |
| 118 | + ) |
| 119 | + ], |
| 120 | + ); |
| 121 | + |
| 122 | + expect(result!.length, 1); |
| 123 | + expect(result.first, kExternalStoragePaths); |
| 124 | + }); |
| 125 | + } // end of for-loop |
| 126 | + |
| 127 | + test('getDownloadsPath fails', () async { |
| 128 | + try { |
| 129 | + await pathProvider.getDownloadsPath(); |
| 130 | + fail('should throw UnsupportedError'); |
| 131 | + } catch (e) { |
| 132 | + expect(e, isUnsupportedError); |
| 133 | + } |
| 134 | + }); |
| 135 | + }); |
| 136 | +} |
0 commit comments