image_picker_test.dart 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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. import 'package:flutter/services.dart';
  5. import 'package:flutter_test/flutter_test.dart';
  6. import 'package:image_picker/image_picker.dart';
  7. void main() {
  8. TestWidgetsFlutterBinding.ensureInitialized();
  9. group('$ImagePicker', () {
  10. const MethodChannel channel =
  11. MethodChannel('plugins.flutter.io/image_picker');
  12. final List<MethodCall> log = <MethodCall>[];
  13. final picker = ImagePicker();
  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 picker.getImage(source: ImageSource.camera);
  24. await picker.getImage(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 picker.getImage(source: ImageSource.camera);
  47. await picker.getImage(
  48. source: ImageSource.camera,
  49. maxWidth: 10.0,
  50. );
  51. await picker.getImage(
  52. source: ImageSource.camera,
  53. maxHeight: 10.0,
  54. );
  55. await picker.getImage(
  56. source: ImageSource.camera,
  57. maxWidth: 10.0,
  58. maxHeight: 20.0,
  59. );
  60. await picker.getImage(
  61. source: ImageSource.camera, maxWidth: 10.0, imageQuality: 70);
  62. await picker.getImage(
  63. source: ImageSource.camera, maxHeight: 10.0, imageQuality: 70);
  64. await picker.getImage(
  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. picker.getImage(source: ImageSource.camera, maxWidth: -1.0),
  127. throwsArgumentError,
  128. );
  129. expect(
  130. picker.getImage(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(await picker.getImage(source: ImageSource.gallery), isNull);
  137. expect(await picker.getImage(source: ImageSource.camera), isNull);
  138. });
  139. test('camera position defaults to back', () async {
  140. await picker.getImage(source: ImageSource.camera);
  141. expect(
  142. log,
  143. <Matcher>[
  144. isMethodCall('pickImage', arguments: <String, dynamic>{
  145. 'source': 0,
  146. 'maxWidth': null,
  147. 'maxHeight': null,
  148. 'imageQuality': null,
  149. 'cameraDevice': 0,
  150. }),
  151. ],
  152. );
  153. });
  154. test('camera position can set to front', () async {
  155. await picker.getImage(
  156. source: ImageSource.camera,
  157. preferredCameraDevice: CameraDevice.front);
  158. expect(
  159. log,
  160. <Matcher>[
  161. isMethodCall('pickImage', arguments: <String, dynamic>{
  162. 'source': 0,
  163. 'maxWidth': null,
  164. 'maxHeight': null,
  165. 'imageQuality': null,
  166. 'cameraDevice': 1,
  167. }),
  168. ],
  169. );
  170. });
  171. });
  172. group('#pickVideo', () {
  173. test('passes the image source argument correctly', () async {
  174. await picker.getVideo(source: ImageSource.camera);
  175. await picker.getVideo(source: ImageSource.gallery);
  176. expect(
  177. log,
  178. <Matcher>[
  179. isMethodCall('pickVideo', arguments: <String, dynamic>{
  180. 'source': 0,
  181. 'cameraDevice': 0,
  182. 'maxDuration': null,
  183. }),
  184. isMethodCall('pickVideo', arguments: <String, dynamic>{
  185. 'source': 1,
  186. 'cameraDevice': 0,
  187. 'maxDuration': null,
  188. }),
  189. ],
  190. );
  191. });
  192. test('passes the duration argument correctly', () async {
  193. await picker.getVideo(source: ImageSource.camera);
  194. await picker.getVideo(
  195. source: ImageSource.camera,
  196. maxDuration: const Duration(seconds: 10));
  197. await picker.getVideo(
  198. source: ImageSource.camera,
  199. maxDuration: const Duration(minutes: 1));
  200. await picker.getVideo(
  201. source: ImageSource.camera, maxDuration: const Duration(hours: 1));
  202. expect(
  203. log,
  204. <Matcher>[
  205. isMethodCall('pickVideo', arguments: <String, dynamic>{
  206. 'source': 0,
  207. 'maxDuration': null,
  208. 'cameraDevice': 0,
  209. }),
  210. isMethodCall('pickVideo', arguments: <String, dynamic>{
  211. 'source': 0,
  212. 'maxDuration': 10,
  213. 'cameraDevice': 0,
  214. }),
  215. isMethodCall('pickVideo', arguments: <String, dynamic>{
  216. 'source': 0,
  217. 'maxDuration': 60,
  218. 'cameraDevice': 0,
  219. }),
  220. isMethodCall('pickVideo', arguments: <String, dynamic>{
  221. 'source': 0,
  222. 'maxDuration': 3600,
  223. 'cameraDevice': 0,
  224. }),
  225. ],
  226. );
  227. });
  228. test('handles a null video path response gracefully', () async {
  229. channel.setMockMethodCallHandler((MethodCall methodCall) => null);
  230. expect(await picker.getVideo(source: ImageSource.gallery), isNull);
  231. expect(await picker.getVideo(source: ImageSource.camera), isNull);
  232. });
  233. test('camera position defaults to back', () async {
  234. await picker.getVideo(source: ImageSource.camera);
  235. expect(
  236. log,
  237. <Matcher>[
  238. isMethodCall('pickVideo', arguments: <String, dynamic>{
  239. 'source': 0,
  240. 'cameraDevice': 0,
  241. 'maxDuration': null,
  242. }),
  243. ],
  244. );
  245. });
  246. test('camera position can set to front', () async {
  247. await picker.getVideo(
  248. source: ImageSource.camera,
  249. preferredCameraDevice: CameraDevice.front);
  250. expect(
  251. log,
  252. <Matcher>[
  253. isMethodCall('pickVideo', arguments: <String, dynamic>{
  254. 'source': 0,
  255. 'maxDuration': null,
  256. 'cameraDevice': 1,
  257. }),
  258. ],
  259. );
  260. });
  261. });
  262. group('#retrieveLostData', () {
  263. test('retrieveLostData get success response', () async {
  264. channel.setMockMethodCallHandler((MethodCall methodCall) async {
  265. return <String, String>{
  266. 'type': 'image',
  267. 'path': '/example/path',
  268. };
  269. });
  270. final LostData response = await picker.getLostData();
  271. expect(response.type, RetrieveType.image);
  272. expect(response.file.path, '/example/path');
  273. });
  274. test('retrieveLostData get error response', () async {
  275. channel.setMockMethodCallHandler((MethodCall methodCall) async {
  276. return <String, String>{
  277. 'type': 'video',
  278. 'errorCode': 'test_error_code',
  279. 'errorMessage': 'test_error_message',
  280. };
  281. });
  282. final LostData response = await picker.getLostData();
  283. expect(response.type, RetrieveType.video);
  284. expect(response.exception.code, 'test_error_code');
  285. expect(response.exception.message, 'test_error_message');
  286. });
  287. test('retrieveLostData get null response', () async {
  288. channel.setMockMethodCallHandler((MethodCall methodCall) async {
  289. return null;
  290. });
  291. expect((await picker.getLostData()).isEmpty, true);
  292. });
  293. test('retrieveLostData get both path and error should throw', () async {
  294. channel.setMockMethodCallHandler((MethodCall methodCall) async {
  295. return <String, String>{
  296. 'type': 'video',
  297. 'errorCode': 'test_error_code',
  298. 'errorMessage': 'test_error_message',
  299. 'path': '/example/path',
  300. };
  301. });
  302. expect(picker.getLostData(), throwsAssertionError);
  303. });
  304. });
  305. });
  306. }