Skip to content

Commit

Permalink
Fix 'UI API called on a background thread' warning. #1100
Browse files Browse the repository at this point in the history
  • Loading branch information
antrix1989 committed Feb 15, 2018
1 parent 31b43b7 commit cfec7b2
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 0 deletions.
4 changes: 4 additions & 0 deletions ADAL/ADAL.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
230E16E61FB17A7D00ADC904 /* ADTelemetryUIEventTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 230E16E01FB17A2C00ADC904 /* ADTelemetryUIEventTests.m */; };
23189A001FAA9A6C0014B8EF /* ADAuthorityUtils.m in Sources */ = {isa = PBXBuildFile; fileRef = 231899FE1FAA9A4A0014B8EF /* ADAuthorityUtils.m */; };
23189A041FAAC1D10014B8EF /* ADAuthorityUtils.m in Sources */ = {isa = PBXBuildFile; fileRef = 231899FE1FAA9A4A0014B8EF /* ADAuthorityUtils.m */; };
2335EFAA2036450100C342D0 /* ADBrokerHelperTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 2335EFA92036450100C342D0 /* ADBrokerHelperTests.m */; };
234F3CE71F35161C00DE4AA4 /* ADAuthenticationContextTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 234F3CE51F35159500DE4AA4 /* ADAuthenticationContextTests.m */; };
234F3CED1F35182500DE4AA4 /* ADAuthenticationContextTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 234F3CEB1F35180B00DE4AA4 /* ADAuthenticationContextTests.m */; };
234F3CEE1F35182600DE4AA4 /* ADAuthenticationContextTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 234F3CEB1F35180B00DE4AA4 /* ADAuthenticationContextTests.m */; };
Expand Down Expand Up @@ -538,6 +539,7 @@
230E16E21FB17A6200ADC904 /* ADTelemetryAPIEventTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ADTelemetryAPIEventTests.m; sourceTree = "<group>"; };
231899FD1FAA9A4A0014B8EF /* ADAuthorityUtils.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ADAuthorityUtils.h; sourceTree = "<group>"; };
231899FE1FAA9A4A0014B8EF /* ADAuthorityUtils.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ADAuthorityUtils.m; sourceTree = "<group>"; };
2335EFA92036450100C342D0 /* ADBrokerHelperTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ADBrokerHelperTests.m; sourceTree = "<group>"; };
234F3CE51F35159500DE4AA4 /* ADAuthenticationContextTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ADAuthenticationContextTests.m; sourceTree = "<group>"; };
234F3CEB1F35180B00DE4AA4 /* ADAuthenticationContextTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ADAuthenticationContextTests.m; sourceTree = "<group>"; };
234F3CEF1F3519A300DE4AA4 /* ADAuthenticationContextTests.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = ADAuthenticationContextTests.h; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1414,6 +1416,7 @@
B20DC5D31F0D975A00957806 /* unit */ = {
isa = PBXGroup;
children = (
2335EFA92036450100C342D0 /* ADBrokerHelperTests.m */,
B2B7850F1F733C1E00B776BD /* NSStringTelemetryExtensionsTests.m */,
D67D3D4D1F43B17000660F32 /* ADAadAuthorityCacheTests.m */,
234F3CEF1F3519A300DE4AA4 /* ADAuthenticationContextTests.h */,
Expand Down Expand Up @@ -2059,6 +2062,7 @@
buildActionMask = 2147483647;
files = (
60C351BA1DA0D588006C8435 /* ADAL.m in Sources */,
2335EFAA2036450100C342D0 /* ADBrokerHelperTests.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
10 changes: 10 additions & 0 deletions ADAL/src/broker/ios/ADBrokerHelper.m
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,16 @@ + (void)load

+ (BOOL)canUseBroker
{
if (![NSThread isMainThread])
{
__block BOOL result = NO;
dispatch_sync(dispatch_get_main_queue(), ^{
result = [self canUseBroker];
});

return result;
}

if (![ADAppExtensionUtil isExecutingInAppExtension])
{
// Verify broker app url can be opened
Expand Down
64 changes: 64 additions & 0 deletions ADAL/tests/unit/ADBrokerHelperTests.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// Copyright (c) Microsoft Corporation.
// All rights reserved.
//
// This code is licensed under the MIT License.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files(the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions :
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

#import <XCTest/XCTest.h>
#import "ADBrokerHelper.h"
#import "ADAppExtensionUtil.h"

@interface ADBrokerHelperTests : XCTestCase

@end

@implementation ADBrokerHelperTests

- (void)setUp
{
[super setUp];
}

- (void)tearDown
{
[super tearDown];
}

- (void)testCanUseBroker_whenInvokeOnMainThread_ShouldReturnTrue
{
BOOL result = [ADBrokerHelper canUseBroker];

XCTAssertTrue(result);
}

- (void)testCanUseBroker_whenInvokeOnBgThread_ShouldReturnTrue
{
XCTestExpectation *expectation = [self expectationWithDescription:@"Get result from +canUseBroker on bg thread."];
dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void) {
BOOL result = [ADBrokerHelper canUseBroker];
XCTAssertTrue(result);

[expectation fulfill];
});

[self waitForExpectationsWithTimeout:1 handler:nil];
}

@end

0 comments on commit cfec7b2

Please sign in to comment.