|
| 1 | +/// |
| 2 | +/// [Author] Alex (https://github.com/AlexV525) |
| 3 | +/// [Date] 2022/04/27 12:14 |
| 4 | +/// |
| 5 | +import 'package:flutter/foundation.dart'; |
| 6 | +import 'package:flutter/material.dart'; |
| 7 | +import 'package:flutter_test/flutter_test.dart'; |
| 8 | +import 'package:wechat_assets_picker/wechat_assets_picker.dart'; |
| 9 | + |
| 10 | +void main() { |
| 11 | + PhotoManager.withPlugin(TestPhotoManagerPlugin()); |
| 12 | + final Finder _defaultButtonFinder = find.byType(TextButton); |
| 13 | + Widget _defaultApp({void Function(BuildContext)? onButtonPressed}) { |
| 14 | + return MaterialApp( |
| 15 | + home: Builder( |
| 16 | + builder: (BuildContext context) => Scaffold( |
| 17 | + body: Center( |
| 18 | + child: TextButton( |
| 19 | + onPressed: () => onButtonPressed?.call(context), |
| 20 | + child: const Text('Press'), |
| 21 | + ), |
| 22 | + ), |
| 23 | + ), |
| 24 | + ), |
| 25 | + ); |
| 26 | + } |
| 27 | + |
| 28 | + testWidgets('PathNameBuilder called correctly', (WidgetTester tester) async { |
| 29 | + final AssetPathEntity pathEntity = AssetPathEntity( |
| 30 | + id: 'test', |
| 31 | + name: 'pathEntity', |
| 32 | + ); |
| 33 | + final PathNameBuilder<AssetPathEntity> pathNameBuilder = |
| 34 | + (AssetPathEntity p) => 'testPathNameBuilder'; |
| 35 | + final AssetPickerConfig config = AssetPickerConfig( |
| 36 | + pathNameBuilder: pathNameBuilder, |
| 37 | + ); |
| 38 | + final DefaultAssetPickerProvider provider = |
| 39 | + DefaultAssetPickerProvider.forTest( |
| 40 | + maxAssets: config.maxAssets, |
| 41 | + pageSize: config.pageSize, |
| 42 | + pathThumbnailSize: config.pathThumbnailSize, |
| 43 | + selectedAssets: config.selectedAssets, |
| 44 | + requestType: config.requestType, |
| 45 | + sortPathDelegate: config.sortPathDelegate, |
| 46 | + filterOptions: config.filterOptions, |
| 47 | + ); |
| 48 | + provider |
| 49 | + ..currentAssets = <AssetEntity>[ |
| 50 | + const AssetEntity(id: 'test', typeInt: 0, width: 0, height: 0), |
| 51 | + ] |
| 52 | + ..currentPath = pathEntity |
| 53 | + ..hasAssetsToDisplay = true |
| 54 | + ..setPathThumbnail(pathEntity, null); |
| 55 | + final DefaultAssetPickerBuilderDelegate delegate = |
| 56 | + DefaultAssetPickerBuilderDelegate( |
| 57 | + provider: provider, |
| 58 | + initialPermission: PermissionState.authorized, |
| 59 | + pathNameBuilder: pathNameBuilder, |
| 60 | + ); |
| 61 | + await tester.pumpWidget( |
| 62 | + _defaultApp( |
| 63 | + onButtonPressed: (BuildContext context) { |
| 64 | + AssetPicker.pickAssetsWithDelegate(context, delegate: delegate); |
| 65 | + }, |
| 66 | + ), |
| 67 | + ); |
| 68 | + await tester.tap(_defaultButtonFinder); |
| 69 | + await tester.pumpAndSettle(); |
| 70 | + await tester.tap(find.byIcon(Icons.keyboard_arrow_down)); |
| 71 | + await tester.pumpAndSettle(); |
| 72 | + expect(find.text('testPathNameBuilder'), findsNWidgets(2)); |
| 73 | + }); |
| 74 | +} |
| 75 | + |
| 76 | +class TestPhotoManagerPlugin extends PhotoManagerPlugin { |
| 77 | + @override |
| 78 | + Future<PermissionState> requestPermissionExtend( |
| 79 | + PermisstionRequestOption requestOption, |
| 80 | + ) { |
| 81 | + return SynchronousFuture<PermissionState>(PermissionState.authorized); |
| 82 | + } |
| 83 | +} |
0 commit comments