old_image_picker_test.dart 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. // Copyright 2019 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. // ignore_for_file: deprecated_member_use, deprecated_member_use_from_same_package
  5. import 'package:flutter/services.dart';
  6. import 'package:flutter_test/flutter_test.dart';
  7. import 'package:image_picker/image_picker.dart';
  8. void main() {
  9. TestWidgetsFlutterBinding.ensureInitialized();
  10. group('$ImagePicker', () {
  11. const MethodChannel channel =
  12. MethodChannel('plugins.flutter.io/image_picker');
  13. final List<MethodCall> log = <MethodCall>[];
  14. setUp(() {
  15. channel.setMockMethodCallHandler((MethodCall methodCall) async {
  16. log.add(methodCall);
  17. return '';
  18. });
  19. log.clear();
  20. });
  21. group('#pickImage', () {
  22. test('passes the image source argument correctly', () async {
  23. await ImagePicker.pickImage(source: ImageSource.camera);
  24. await ImagePicker.pickImage(source: ImageSource.gallery);
  25. expect(
  26. log,
  27. <Matcher>[
  28. isMethodCall('pickImage', arguments: <String, dynamic>{
  29. 'source': 0,
  30. 'maxWidth': null,
  31. 'maxHeight': null,
  32. 'imageQuality': null,
  33. 'cameraDevice': 0
  34. }),
  35. isMethodCall('pickImage', arguments: <String, dynamic>{
  36. 'source': 1,
  37. 'maxWidth': null,
  38. 'maxHeight': null,
  39. 'imageQuality': null,
  40. 'cameraDevice': 0
  41. }),
  42. ],
  43. );
  44. });
  45. test('passes the width and height arguments correctly', () async {
  46. await ImagePicker.pickImage(source: ImageSource.camera);
  47. await ImagePicker.pickImage(
  48. source: ImageSource.camera,
  49. maxWidth: 10.0,
  50. );
  51. await ImagePicker.pickImage(
  52. source: ImageSource.camera,
  53. maxHeight: 10.0,
  54. );
  55. await ImagePicker.pickImage(
  56. source: ImageSource.camera,
  57. maxWidth: 10.0,
  58. maxHeight: 20.0,
  59. );
  60. await ImagePicker.pickImage(
  61. source: ImageSource.camera, maxWidth: 10.0, imageQuality: 70);
  62. await ImagePicker.pickImage(
  63. source: ImageSource.camera, maxHeight: 10.0, imageQuality: 70);
  64. await ImagePicker.pickImage(
  65. source: ImageSource.camera,
  66. maxWidth: 10.0,
  67. maxHeight: 20.0,
  68. imageQuality: 70);
  69. expect(
  70. log,
  71. <Matcher>[
  72. isMethodCall('pickImage', arguments: <String, dynamic>{
  73. 'source': 0,
  74. 'maxWidth': null,
  75. 'maxHeight': null,
  76. 'imageQuality': null,
  77. 'cameraDevice': 0
  78. }),
  79. isMethodCall('pickImage', arguments: <String, dynamic>{
  80. 'source': 0,
  81. 'maxWidth': 10.0,
  82. 'maxHeight': null,
  83. 'imageQuality': null,
  84. 'cameraDevice': 0
  85. }),
  86. isMethodCall('pickImage', arguments: <String, dynamic>{
  87. 'source': 0,
  88. 'maxWidth': null,
  89. 'maxHeight': 10.0,
  90. 'imageQuality': null,
  91. 'cameraDevice': 0
  92. }),
  93. isMethodCall('pickImage', arguments: <String, dynamic>{
  94. 'source': 0,
  95. 'maxWidth': 10.0,
  96. 'maxHeight': 20.0,
  97. 'imageQuality': null,
  98. 'cameraDevice': 0
  99. }),
  100. isMethodCall('pickImage', arguments: <String, dynamic>{
  101. 'source': 0,
  102. 'maxWidth': 10.0,
  103. 'maxHeight': null,
  104. 'imageQuality': 70,
  105. 'cameraDevice': 0
  106. }),
  107. isMethodCall('pickImage', arguments: <String, dynamic>{
  108. 'source': 0,
  109. 'maxWidth': null,
  110. 'maxHeight': 10.0,
  111. 'imageQuality': 70,
  112. 'cameraDevice': 0
  113. }),
  114. isMethodCall('pickImage', arguments: <String, dynamic>{
  115. 'source': 0,
  116. 'maxWidth': 10.0,
  117. 'maxHeight': 20.0,
  118. 'imageQuality': 70,
  119. 'cameraDevice': 0
  120. }),
  121. ],
  122. );
  123. });
  124. test('does not accept a negative width or height argument', () {
  125. expect(
  126. ImagePicker.pickImage(source: ImageSource.camera, maxWidth: -1.0),
  127. throwsArgumentError,
  128. );
  129. expect(
  130. ImagePicker.pickImage(source: ImageSource.camera, maxHeight: -1.0),
  131. throwsArgumentError,
  132. );
  133. });
  134. test('handles a null image path response gracefully', () async {
  135. channel.setMockMethodCallHandler((MethodCall methodCall) => null);
  136. expect(
  137. await ImagePicker.pickImage(source: ImageSource.gallery), isNull);
  138. expect(await ImagePicker.pickImage(source: ImageSource.camera), isNull);
  139. });
  140. test('camera position defaults to back', () async {
  141. await ImagePicker.pickImage(source: ImageSource.camera);
  142. expect(
  143. log,
  144. <Matcher>[
  145. isMethodCall('pickImage', arguments: <String, dynamic>{
  146. 'source': 0,
  147. 'maxWidth': null,
  148. 'maxHeight': null,
  149. 'imageQuality': null,
  150. 'cameraDevice': 0,
  151. }),
  152. ],
  153. );
  154. });
  155. test('camera position can set to front', () async {
  156. await ImagePicker.pickImage(
  157. source: ImageSource.camera,
  158. preferredCameraDevice: CameraDevice.front);
  159. expect(
  160. log,
  161. <Matcher>[
  162. isMethodCall('pickImage', arguments: <String, dynamic>{
  163. 'source': 0,
  164. 'maxWidth': null,
  165. 'maxHeight': null,
  166. 'imageQuality': null,
  167. 'cameraDevice': 1,
  168. }),
  169. ],
  170. );
  171. });
  172. });
  173. group('#pickVideo', () {
  174. test('passes the image source argument correctly', () async {
  175. await ImagePicker.pickVideo(source: ImageSource.camera);
  176. await ImagePicker.pickVideo(source: ImageSource.gallery);
  177. expect(
  178. log,
  179. <Matcher>[
  180. isMethodCall('pickVideo', arguments: <String, dynamic>{
  181. 'source': 0,
  182. 'cameraDevice': 0,
  183. 'maxDuration': null,
  184. }),
  185. isMethodCall('pickVideo', arguments: <String, dynamic>{
  186. 'source': 1,
  187. 'cameraDevice': 0,
  188. 'maxDuration': null,
  189. }),
  190. ],
  191. );
  192. });
  193. test('passes the duration argument correctly', () async {
  194. await ImagePicker.pickVideo(source: ImageSource.camera);
  195. await ImagePicker.pickVideo(
  196. source: ImageSource.camera,
  197. maxDuration: const Duration(seconds: 10));
  198. await ImagePicker.pickVideo(
  199. source: ImageSource.camera,
  200. maxDuration: const Duration(minutes: 1));
  201. await ImagePicker.pickVideo(
  202. source: ImageSource.camera, maxDuration: const Duration(hours: 1));
  203. expect(
  204. log,
  205. <Matcher>[
  206. isMethodCall('pickVideo', arguments: <String, dynamic>{
  207. 'source': 0,
  208. 'maxDuration': null,
  209. 'cameraDevice': 0,
  210. }),
  211. isMethodCall('pickVideo', arguments: <String, dynamic>{
  212. 'source': 0,
  213. 'maxDuration': 10,
  214. 'cameraDevice': 0,
  215. }),
  216. isMethodCall('pickVideo', arguments: <String, dynamic>{
  217. 'source': 0,
  218. 'maxDuration': 60,
  219. 'cameraDevice': 0,
  220. }),
  221. isMethodCall('pickVideo', arguments: <String, dynamic>{
  222. 'source': 0,
  223. 'maxDuration': 3600,
  224. 'cameraDevice': 0,
  225. }),
  226. ],
  227. );
  228. });
  229. test('handles a null video path response gracefully', () async {
  230. channel.setMockMethodCallHandler((MethodCall methodCall) => null);
  231. expect(
  232. await ImagePicker.pickVideo(source: ImageSource.gallery), isNull);
  233. expect(await ImagePicker.pickVideo(source: ImageSource.camera), isNull);
  234. });
  235. test('camera position defaults to back', () async {
  236. await ImagePicker.pickVideo(source: ImageSource.camera);
  237. expect(
  238. log,
  239. <Matcher>[
  240. isMethodCall('pickVideo', arguments: <String, dynamic>{
  241. 'source': 0,
  242. 'cameraDevice': 0,
  243. 'maxDuration': null,
  244. }),
  245. ],
  246. );
  247. });
  248. test('camera position can set to front', () async {
  249. await ImagePicker.pickVideo(
  250. source: ImageSource.camera,
  251. preferredCameraDevice: CameraDevice.front);
  252. expect(
  253. log,
  254. <Matcher>[
  255. isMethodCall('pickVideo', arguments: <String, dynamic>{
  256. 'source': 0,
  257. 'maxDuration': null,
  258. 'cameraDevice': 1,
  259. }),
  260. ],
  261. );
  262. });
  263. });
  264. group('#retrieveLostData', () {
  265. test('retrieveLostData get success response', () async {
  266. channel.setMockMethodCallHandler((MethodCall methodCall) async {
  267. return <String, String>{
  268. 'type': 'image',
  269. 'path': '/example/path',
  270. };
  271. });
  272. final LostDataResponse response = await ImagePicker.retrieveLostData();
  273. expect(response.type, RetrieveType.image);
  274. expect(response.file.path, '/example/path');
  275. });
  276. test('retrieveLostData get error response', () async {
  277. channel.setMockMethodCallHandler((MethodCall methodCall) async {
  278. return <String, String>{
  279. 'type': 'video',
  280. 'errorCode': 'test_error_code',
  281. 'errorMessage': 'test_error_message',
  282. };
  283. });
  284. final LostDataResponse response = await ImagePicker.retrieveLostData();
  285. expect(response.type, RetrieveType.video);
  286. expect(response.exception.code, 'test_error_code');
  287. expect(response.exception.message, 'test_error_message');
  288. });
  289. test('retrieveLostData get null response', () async {
  290. channel.setMockMethodCallHandler((MethodCall methodCall) async {
  291. return null;
  292. });
  293. expect((await ImagePicker.retrieveLostData()).isEmpty, true);
  294. });
  295. test('retrieveLostData get both path and error should throw', () async {
  296. channel.setMockMethodCallHandler((MethodCall methodCall) async {
  297. return <String, String>{
  298. 'type': 'video',
  299. 'errorCode': 'test_error_code',
  300. 'errorMessage': 'test_error_message',
  301. 'path': '/example/path',
  302. };
  303. });
  304. expect(ImagePicker.retrieveLostData(), throwsAssertionError);
  305. });
  306. });
  307. });
  308. }