This repository has been archived by the owner on Feb 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[camera]call engine API in main thread to fix a crash
- Loading branch information
1 parent
94e80fc
commit 959eb1a
Showing
15 changed files
with
472 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
packages/camera/camera/example/ios/Runner.xcodeproj/xcshareddata/xcschemes/Runner.xcscheme
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
packages/camera/camera/example/ios/RunnerTests/ThreadSafeEventChannelTests.m
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// 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 <OCMock/OCMock.h> | ||
|
||
@interface ThreadSafeEventChannelTests : XCTestCase | ||
@property(nonatomic, strong) FLTThreadSafeEventChannel *channel; | ||
@property(nonatomic, strong) XCTestExpectation *mainThreadExpectation; | ||
@end | ||
|
||
@implementation ThreadSafeEventChannelTests | ||
|
||
- (void)setUp { | ||
[super setUp]; | ||
id mockEventChannel = OCMClassMock([FlutterEventChannel class]); | ||
|
||
_mainThreadExpectation = [[XCTestExpectation alloc] | ||
initWithDescription:@"setStreamHandler must be called in main thread"]; | ||
_channel = [[FLTThreadSafeEventChannel alloc] initWithEventChannel:mockEventChannel]; | ||
|
||
OCMStub([mockEventChannel setStreamHandler:[OCMArg any]]).andDo(^(NSInvocation *invocation) { | ||
if (NSThread.isMainThread) { | ||
[self.mainThreadExpectation fulfill]; | ||
} | ||
}); | ||
} | ||
|
||
- (void)testSetStreamHandler_shouldStayOnMainThreadIfCalledFromMainThread { | ||
[self.channel setStreamHandler:nil | ||
completion:^{ | ||
}]; | ||
[self waitForExpectations:@[ self.mainThreadExpectation ] timeout:1]; | ||
} | ||
|
||
- (void)testSetStreamHandler_shouldDispatchToMainThreadIfCalledFromBackgroundThread { | ||
XCTestExpectation *mainThreadCompletionExpectation = [[XCTestExpectation alloc] | ||
initWithDescription:@"setStreamHandler's completion block must be called in main thread"]; | ||
|
||
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ | ||
[self.channel setStreamHandler:nil | ||
completion:^{ | ||
if (NSThread.isMainThread) { | ||
[mainThreadCompletionExpectation fulfill]; | ||
} | ||
}]; | ||
}); | ||
[self waitForExpectations:@[ | ||
self.mainThreadExpectation, | ||
mainThreadCompletionExpectation, | ||
] | ||
timeout:1]; | ||
} | ||
|
||
@end |
45 changes: 45 additions & 0 deletions
45
packages/camera/camera/example/ios/RunnerTests/ThreadSafeMethodChannelTests.m
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// 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 <OCMock/OCMock.h> | ||
|
||
@interface ThreadSafeMethodChannelTests : XCTestCase | ||
@property(nonatomic, strong) FLTThreadSafeMethodChannel *channel; | ||
@property(nonatomic, strong) XCTestExpectation *mainThreadExpectation; | ||
@end | ||
|
||
@implementation ThreadSafeMethodChannelTests | ||
|
||
- (void)setUp { | ||
[super setUp]; | ||
id mockMethodChannel = OCMClassMock([FlutterMethodChannel class]); | ||
|
||
_mainThreadExpectation = | ||
[[XCTestExpectation alloc] initWithDescription:@"invokeMethod must be called in main thread"]; | ||
_channel = [[FLTThreadSafeMethodChannel alloc] initWithMethodChannel:mockMethodChannel]; | ||
|
||
OCMStub([mockMethodChannel invokeMethod:[OCMArg any] arguments:[OCMArg any]]) | ||
.andDo(^(NSInvocation *invocation) { | ||
if (NSThread.isMainThread) { | ||
[self.mainThreadExpectation fulfill]; | ||
} | ||
}); | ||
} | ||
|
||
- (void)testInvokeMethod_shouldStayOnMainThreadIfCalledFromMainThread { | ||
[self.channel invokeMethod:@"foo" arguments:nil]; | ||
|
||
[self waitForExpectations:@[ self.mainThreadExpectation ] timeout:1]; | ||
} | ||
|
||
- (void)testInvokeMethod__shouldDispatchToMainThreadIfCalledFromBackgroundThread { | ||
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ | ||
[self.channel invokeMethod:@"foo" arguments:nil]; | ||
}); | ||
[self waitForExpectations:@[ self.mainThreadExpectation ] timeout:1]; | ||
} | ||
|
||
@end |
93 changes: 93 additions & 0 deletions
93
packages/camera/camera/example/ios/RunnerTests/ThreadSafeTextureRegistryTests.m
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
// 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 <OCMock/OCMock.h> | ||
|
||
@interface ThreadSafeTextureRegistryTests : XCTestCase | ||
@property(nonatomic, strong) FLTThreadSafeTextureRegistry *registry; | ||
@property(nonatomic, strong) XCTestExpectation *registerTextureExpectation; | ||
@property(nonatomic, strong) XCTestExpectation *registerTextureCompletionExpectation; | ||
@property(nonatomic, strong) XCTestExpectation *unregisterTextureExpectation; | ||
@property(nonatomic, strong) XCTestExpectation *textureFrameAvailableExpectation; | ||
|
||
@end | ||
|
||
@implementation ThreadSafeTextureRegistryTests | ||
|
||
- (void)setUp { | ||
[super setUp]; | ||
id mockTextureRegistry = OCMProtocolMock(@protocol(FlutterTextureRegistry)); | ||
_registry = [[FLTThreadSafeTextureRegistry alloc] initWithTextureRegistry:mockTextureRegistry]; | ||
|
||
_registerTextureExpectation = [[XCTestExpectation alloc] | ||
initWithDescription:@"registerTexture must be called in main thread"]; | ||
_unregisterTextureExpectation = [[XCTestExpectation alloc] | ||
initWithDescription:@"unregisterTexture must be called in main thread"]; | ||
_textureFrameAvailableExpectation = [[XCTestExpectation alloc] | ||
initWithDescription:@"textureFrameAvailable must be called in main thread"]; | ||
_registerTextureCompletionExpectation = [[XCTestExpectation alloc] | ||
initWithDescription:@"registerTexture's completion block must be called in main thread"]; | ||
|
||
OCMStub([mockTextureRegistry registerTexture:[OCMArg any]]).andDo(^(NSInvocation *invocation) { | ||
if (NSThread.isMainThread) { | ||
[self.registerTextureExpectation fulfill]; | ||
} | ||
}); | ||
|
||
OCMStub([mockTextureRegistry unregisterTexture:0]).andDo(^(NSInvocation *invocation) { | ||
if (NSThread.isMainThread) { | ||
[self.unregisterTextureExpectation fulfill]; | ||
} | ||
}); | ||
|
||
OCMStub([mockTextureRegistry textureFrameAvailable:0]).andDo(^(NSInvocation *invocation) { | ||
if (NSThread.isMainThread) { | ||
[self.textureFrameAvailableExpectation fulfill]; | ||
} | ||
}); | ||
} | ||
|
||
- (void)testShouldStayOnMainThreadIfCalledFromMainThread { | ||
NSObject<FlutterTexture> *anyTexture = OCMProtocolMock(@protocol(FlutterTexture)); | ||
[self.registry registerTexture:anyTexture | ||
completion:^(int64_t textureId) { | ||
if (NSThread.isMainThread) { | ||
[self.registerTextureCompletionExpectation fulfill]; | ||
} | ||
}]; | ||
[self.registry textureFrameAvailable:0]; | ||
[self.registry unregisterTexture:0]; | ||
[self waitForExpectations:@[ | ||
self.registerTextureExpectation, | ||
self.unregisterTextureExpectation, | ||
self.textureFrameAvailableExpectation, | ||
self.registerTextureCompletionExpectation, | ||
] | ||
timeout:1]; | ||
} | ||
|
||
- (void)testShouldDispatchToMainThreadIfCalledFromBackgroundThread { | ||
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ | ||
NSObject<FlutterTexture> *anyTexture = OCMProtocolMock(@protocol(FlutterTexture)); | ||
[self.registry registerTexture:anyTexture | ||
completion:^(int64_t textureId) { | ||
if (NSThread.isMainThread) { | ||
[self.registerTextureCompletionExpectation fulfill]; | ||
} | ||
}]; | ||
[self.registry textureFrameAvailable:0]; | ||
[self.registry unregisterTexture:0]; | ||
}); | ||
[self waitForExpectations:@[ | ||
self.registerTextureExpectation, | ||
self.unregisterTextureExpectation, | ||
self.textureFrameAvailableExpectation, | ||
self.registerTextureCompletionExpectation, | ||
] | ||
timeout:1]; | ||
} | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.