ImagePickerPluginTests.m 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. // Copyright 2019 The Chromium 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 "ImagePickerTestImages.h"
  5. @import image_picker;
  6. @import XCTest;
  7. @interface MockViewController : UIViewController
  8. @property(nonatomic, retain) UIViewController *mockPresented;
  9. @end
  10. @implementation MockViewController
  11. @synthesize mockPresented;
  12. - (UIViewController *)presentedViewController {
  13. return mockPresented;
  14. }
  15. @end
  16. @interface FLTImagePickerPlugin (Test)
  17. @property(copy, nonatomic) FlutterResult result;
  18. - (void)handleSavedPath:(NSString *)path;
  19. - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker;
  20. @end
  21. @interface ImagePickerPluginTests : XCTestCase
  22. @end
  23. @implementation ImagePickerPluginTests
  24. #pragma mark - Test camera devices, no op on simulators
  25. - (void)testPluginPickImageDeviceBack {
  26. if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
  27. return;
  28. }
  29. FLTImagePickerPlugin *plugin = [FLTImagePickerPlugin new];
  30. FlutterMethodCall *call =
  31. [FlutterMethodCall methodCallWithMethodName:@"pickImage"
  32. arguments:@{@"source" : @(0), @"cameraDevice" : @(0)}];
  33. [plugin handleMethodCall:call
  34. result:^(id _Nullable r){
  35. }];
  36. XCTAssertEqual([plugin getImagePickerController].cameraDevice,
  37. UIImagePickerControllerCameraDeviceRear);
  38. }
  39. - (void)testPluginPickImageDeviceFront {
  40. if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
  41. return;
  42. }
  43. FLTImagePickerPlugin *plugin = [FLTImagePickerPlugin new];
  44. FlutterMethodCall *call =
  45. [FlutterMethodCall methodCallWithMethodName:@"pickImage"
  46. arguments:@{@"source" : @(0), @"cameraDevice" : @(1)}];
  47. [plugin handleMethodCall:call
  48. result:^(id _Nullable r){
  49. }];
  50. XCTAssertEqual([plugin getImagePickerController].cameraDevice,
  51. UIImagePickerControllerCameraDeviceFront);
  52. }
  53. - (void)testPluginPickVideoDeviceBack {
  54. if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
  55. return;
  56. }
  57. FLTImagePickerPlugin *plugin = [FLTImagePickerPlugin new];
  58. FlutterMethodCall *call =
  59. [FlutterMethodCall methodCallWithMethodName:@"pickVideo"
  60. arguments:@{@"source" : @(0), @"cameraDevice" : @(0)}];
  61. [plugin handleMethodCall:call
  62. result:^(id _Nullable r){
  63. }];
  64. XCTAssertEqual([plugin getImagePickerController].cameraDevice,
  65. UIImagePickerControllerCameraDeviceRear);
  66. }
  67. - (void)testPluginPickImageDeviceCancelClickMultipleTimes {
  68. if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
  69. return;
  70. }
  71. FLTImagePickerPlugin *plugin = [FLTImagePickerPlugin new];
  72. FlutterMethodCall *call =
  73. [FlutterMethodCall methodCallWithMethodName:@"pickImage"
  74. arguments:@{@"source" : @(0), @"cameraDevice" : @(1)}];
  75. [plugin handleMethodCall:call
  76. result:^(id _Nullable r){
  77. }];
  78. plugin.result = ^(id result) {
  79. };
  80. [plugin imagePickerControllerDidCancel:[plugin getImagePickerController]];
  81. [plugin imagePickerControllerDidCancel:[plugin getImagePickerController]];
  82. }
  83. - (void)testPluginPickVideoDeviceFront {
  84. if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
  85. return;
  86. }
  87. FLTImagePickerPlugin *plugin = [FLTImagePickerPlugin new];
  88. FlutterMethodCall *call =
  89. [FlutterMethodCall methodCallWithMethodName:@"pickVideo"
  90. arguments:@{@"source" : @(0), @"cameraDevice" : @(1)}];
  91. [plugin handleMethodCall:call
  92. result:^(id _Nullable r){
  93. }];
  94. XCTAssertEqual([plugin getImagePickerController].cameraDevice,
  95. UIImagePickerControllerCameraDeviceFront);
  96. }
  97. #pragma mark - Test video duration
  98. - (void)testPickingVideoWithDuration {
  99. FLTImagePickerPlugin *plugin = [FLTImagePickerPlugin new];
  100. FlutterMethodCall *call = [FlutterMethodCall
  101. methodCallWithMethodName:@"pickVideo"
  102. arguments:@{@"source" : @(0), @"cameraDevice" : @(0), @"maxDuration" : @95}];
  103. [plugin handleMethodCall:call
  104. result:^(id _Nullable r){
  105. }];
  106. XCTAssertEqual([plugin getImagePickerController].videoMaximumDuration, 95);
  107. }
  108. - (void)testPluginPickImageSelectMultipleTimes {
  109. FLTImagePickerPlugin *plugin = [FLTImagePickerPlugin new];
  110. FlutterMethodCall *call =
  111. [FlutterMethodCall methodCallWithMethodName:@"pickImage"
  112. arguments:@{@"source" : @(0), @"cameraDevice" : @(0)}];
  113. [plugin handleMethodCall:call
  114. result:^(id _Nullable r){
  115. }];
  116. plugin.result = ^(id result) {
  117. };
  118. [plugin handleSavedPath:@"test"];
  119. [plugin handleSavedPath:@"test"];
  120. }
  121. - (void)testViewController {
  122. UIWindow *window = [UIWindow new];
  123. MockViewController *vc1 = [MockViewController new];
  124. window.rootViewController = vc1;
  125. UIViewController *vc2 = [UIViewController new];
  126. vc1.mockPresented = vc2;
  127. FLTImagePickerPlugin *plugin = [FLTImagePickerPlugin new];
  128. XCTAssertEqual([plugin viewControllerWithWindow:window], vc2);
  129. }
  130. @end