-
Notifications
You must be signed in to change notification settings - Fork 9.8k
[camera] Run iOS methods on UI thread by default #4140
Changes from all commits
dea50fc
da83b51
277d923
9bd75bd
a079af9
bd7d8eb
f0a2bb0
4125244
71e321e
bdfde1a
e6b848c
d59a80d
0f35095
0b63dea
8bc557f
b53ab3e
b6acf82
9f31156
a4cf79e
2b84db2
92c2d00
2722cf4
4a1155a
04566ae
57c63ae
5c69728
e2ba654
17d1f5d
a0be148
ee453bc
cd48ddd
98fe08a
a4e5262
39d1438
3a12cbf
8e8bdfc
3e86910
9093887
1d51203
f0b9f53
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// Copyright 2013 The Flutter Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
@import camera; | ||
@import camera.Test; | ||
@import XCTest; | ||
@import AVFoundation; | ||
#import <OCMock/OCMock.h> | ||
#import "MockFLTThreadSafeFlutterResult.h" | ||
|
||
@interface CameraMethodChannelTests : XCTestCase | ||
@end | ||
|
||
@implementation CameraMethodChannelTests | ||
|
||
- (void)testCreate_ShouldCallResultOnMainThread { | ||
CameraPlugin *camera = [[CameraPlugin alloc] initWithRegistry:nil messenger:nil]; | ||
|
||
XCTestExpectation *expectation = | ||
[[XCTestExpectation alloc] initWithDescription:@"Result finished"]; | ||
|
||
// Set up mocks for initWithCameraName method | ||
id avCaptureDeviceInputMock = OCMClassMock([AVCaptureDeviceInput class]); | ||
OCMStub([avCaptureDeviceInputMock deviceInputWithDevice:[OCMArg any] error:[OCMArg anyObjectRef]]) | ||
.andReturn([AVCaptureInput alloc]); | ||
|
||
id avCaptureSessionMock = OCMClassMock([AVCaptureSession class]); | ||
OCMStub([avCaptureSessionMock alloc]).andReturn(avCaptureSessionMock); | ||
OCMStub([avCaptureSessionMock canSetSessionPreset:[OCMArg any]]).andReturn(YES); | ||
|
||
MockFLTThreadSafeFlutterResult *resultObject = | ||
[[MockFLTThreadSafeFlutterResult alloc] initWithExpectation:expectation]; | ||
|
||
// Set up method call | ||
FlutterMethodCall *call = [FlutterMethodCall | ||
methodCallWithMethodName:@"create" | ||
arguments:@{@"resolutionPreset" : @"medium", @"enableAudio" : @(1)}]; | ||
|
||
[camera handleMethodCallAsync:call result:resultObject]; | ||
|
||
// Verify the result | ||
NSDictionary *dictionaryResult = (NSDictionary *)resultObject.receivedResult; | ||
XCTAssertNotNil(dictionaryResult); | ||
XCTAssert([[dictionaryResult allKeys] containsObject:@"cameraId"]); | ||
} | ||
|
||
@end |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,45 +6,37 @@ | |
@import XCTest; | ||
@import AVFoundation; | ||
#import <OCMock/OCMock.h> | ||
#import "MockFLTThreadSafeFlutterResult.h" | ||
|
||
@interface FLTCam : NSObject <FlutterTexture, | ||
AVCaptureVideoDataOutputSampleBufferDelegate, | ||
AVCaptureAudioDataOutputSampleBufferDelegate> | ||
@property(assign, nonatomic) BOOL isPreviewPaused; | ||
- (void)pausePreviewWithResult:(FlutterResult)result; | ||
- (void)resumePreviewWithResult:(FlutterResult)result; | ||
|
||
- (void)pausePreviewWithResult:(FLTThreadSafeFlutterResult *)result; | ||
|
||
- (void)resumePreviewWithResult:(FLTThreadSafeFlutterResult *)result; | ||
@end | ||
|
||
@interface CameraPreviewPauseTests : XCTestCase | ||
@property(readonly, nonatomic) FLTCam* camera; | ||
@end | ||
|
||
@implementation CameraPreviewPauseTests | ||
|
||
- (void)setUp { | ||
_camera = [[FLTCam alloc] init]; | ||
} | ||
|
||
- (void)testPausePreviewWithResult_shouldPausePreview { | ||
XCTestExpectation* resultExpectation = | ||
[self expectationWithDescription:@"Succeeding result with nil value"]; | ||
[_camera pausePreviewWithResult:^void(id _Nullable result) { | ||
XCTAssertNil(result); | ||
[resultExpectation fulfill]; | ||
}]; | ||
[self waitForExpectationsWithTimeout:2.0 handler:nil]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't you still need this (and an expectation passed to the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think so, the Also the result object that is passed in is a simple mock implementation of the So as far as I understand everything will run synchronously after each other. |
||
XCTAssertTrue(_camera.isPreviewPaused); | ||
FLTCam *camera = [[FLTCam alloc] init]; | ||
MockFLTThreadSafeFlutterResult *resultObject = [[MockFLTThreadSafeFlutterResult alloc] init]; | ||
|
||
[camera pausePreviewWithResult:resultObject]; | ||
XCTAssertTrue(camera.isPreviewPaused); | ||
} | ||
|
||
- (void)testResumePreviewWithResult_shouldResumePreview { | ||
XCTestExpectation* resultExpectation = | ||
[self expectationWithDescription:@"Succeeding result with nil value"]; | ||
[_camera resumePreviewWithResult:^void(id _Nullable result) { | ||
XCTAssertNil(result); | ||
[resultExpectation fulfill]; | ||
}]; | ||
[self waitForExpectationsWithTimeout:2.0 handler:nil]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See explanation above. |
||
XCTAssertFalse(_camera.isPreviewPaused); | ||
FLTCam *camera = [[FLTCam alloc] init]; | ||
MockFLTThreadSafeFlutterResult *resultObject = [[MockFLTThreadSafeFlutterResult alloc] init]; | ||
|
||
[camera resumePreviewWithResult:resultObject]; | ||
XCTAssertFalse(camera.isPreviewPaused); | ||
} | ||
|
||
@end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// Copyright 2013 The Flutter Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
#ifndef MockFLTThreadSafeFlutterResult_h | ||
#define MockFLTThreadSafeFlutterResult_h | ||
|
||
/** | ||
* Extends FLTThreadSafeFlutterResult to give tests the ability to wait on the result and | ||
* read the received result. | ||
*/ | ||
@interface MockFLTThreadSafeFlutterResult : FLTThreadSafeFlutterResult | ||
@property(readonly, nonatomic, nonnull) XCTestExpectation *expectation; | ||
@property(nonatomic, nullable) id receivedResult; | ||
|
||
/** | ||
* Initializes the MockFLTThreadSafeFlutterResult with an expectation. | ||
* | ||
* The expectation is fullfilled when a result is called allowing tests to await the result in an | ||
* asynchronous manner. | ||
*/ | ||
- (nonnull instancetype)initWithExpectation:(nonnull XCTestExpectation *)expectation; | ||
@end | ||
|
||
#endif /* MockFLTThreadSafeFlutterResult_h */ |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// Copyright 2013 The Flutter Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
@import camera; | ||
@import XCTest; | ||
|
||
#import "MockFLTThreadSafeFlutterResult.h" | ||
|
||
@implementation MockFLTThreadSafeFlutterResult | ||
|
||
- (instancetype)initWithExpectation:(XCTestExpectation *)expectation { | ||
self = [super init]; | ||
_expectation = expectation; | ||
return self; | ||
} | ||
|
||
- (void)sendSuccessWithData:(id)data { | ||
self.receivedResult = data; | ||
[self.expectation fulfill]; | ||
} | ||
|
||
- (void)sendSuccess { | ||
self.receivedResult = nil; | ||
[self.expectation fulfill]; | ||
} | ||
@end |
Uh oh!
There was an error while loading. Please reload this page.