Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions CordovaLib/Classes/Public/CDVSceneDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ Licensed to the Apache Software Foundation (ASF) under one
*/

#import <Cordova/CDVSceneDelegate.h>
#import <Cordova/CDVViewController.h>
#import <Cordova/CDVPluginNotifications.h>

@interface CDVSceneDelegate ()
Expand Down Expand Up @@ -66,6 +67,22 @@ - (void)scene:(UIScene *)scene willConnectToSession:(UISceneSession *)session op

- (void)pageDidLoad:(NSNotification *)notification
{
UIView *webView = notification.object;
if (![webView isKindOfClass:[UIView class]]) {
return;
}

// An auxiliary Cordova window can finish loading before the launch window.
// Keep its notifications from releasing links before the main plugins exist.
UIViewController *rootViewController = self.window.rootViewController;
if ([rootViewController isKindOfClass:[CDVViewController class]]) {
if (((CDVViewController *)rootViewController).webView != webView) {
return;
}
} else if (self.window == nil || webView.window != self.window) {
return;
}

[[NSNotificationCenter defaultCenter] removeObserver:self name:CDVPageDidLoadNotification object:nil];

UIScene *scene = self.launchScene;
Expand Down
112 changes: 102 additions & 10 deletions tests/CordovaLibTests/CDVSceneDelegateTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ Licensed to the Apache Software Foundation (ASF) under one
*/

#import <XCTest/XCTest.h>
#import <WebKit/WebKit.h>
#import <Cordova/Cordova.h>
#import <objc/runtime.h>

Expand Down Expand Up @@ -77,6 +78,7 @@ @interface CDVSceneDelegateTests : XCTestCase
@property (nonatomic, strong) CDVSceneDelegate *sceneDelegate;
@property (nonatomic, strong) UIScene *placeholderScene;
@property (nonatomic, strong) UISceneSession *placeholderSession;
@property (nonatomic, strong) WKWebView *launchWebView;
@end

@implementation CDVSceneDelegateTests
Expand All @@ -86,9 +88,13 @@ - (void)setUp
[super setUp];
self.sceneDelegate = [[CDVSceneDelegate alloc] init];

// The scene object is only stored and passed back through; it is never
// messaged, so any non-nil placeholder cast to UIScene * is sufficient.
self.placeholderScene = (UIScene *)[NSObject new];
UIWindowScene *windowScene = (UIWindowScene *)UIApplication.sharedApplication.connectedScenes.anyObject;
self.placeholderScene = windowScene;
self.sceneDelegate.window = [[UIWindow alloc] initWithWindowScene:windowScene];
self.sceneDelegate.window.rootViewController = [UIViewController new];
self.launchWebView = [WKWebView new];
[self.sceneDelegate.window.rootViewController.view addSubview:self.launchWebView];
self.sceneDelegate.window.hidden = NO;

// You can't construct a UISceneSession directly, but you can workaround
// that by indirectly sending +alloc to the class instance
Expand All @@ -99,6 +105,8 @@ - (void)tearDown
{
// Ensure no pending launch-URL observer leaks into the next test.
[self.sceneDelegate sceneDidDisconnect:self.placeholderScene];
self.sceneDelegate.window.hidden = YES;
self.launchWebView = nil;
self.sceneDelegate = nil;
self.placeholderScene = nil;
self.placeholderSession = nil;
Expand Down Expand Up @@ -161,7 +169,7 @@ - (void)testColdLaunchURLIsReplayedAfterPageDidLoad
return [notification.object isEqual:launchURL];
};

[[NSNotificationCenter defaultCenter] postNotificationName:CDVPageDidLoadNotification object:nil];
[[NSNotificationCenter defaultCenter] postNotificationName:CDVPageDidLoadNotification object:self.launchWebView];

[self waitForExpectations:@[openURLFired] timeout:1.0];
}
Expand All @@ -180,7 +188,7 @@ - (void)testLaunchWithoutURLDoesNotPostOpenURL
initWithName:CDVPluginHandleOpenURLNotification];
notFired.inverted = YES;

[[NSNotificationCenter defaultCenter] postNotificationName:CDVPageDidLoadNotification object:nil];
[[NSNotificationCenter defaultCenter] postNotificationName:CDVPageDidLoadNotification object:self.launchWebView];

[self waitForExpectations:@[notFired] timeout:0.3];
}
Expand All @@ -201,7 +209,7 @@ - (void)testSceneDisconnectBeforePageLoadCancelsPendingURL
initWithName:CDVPluginHandleOpenURLNotification];
notFired.inverted = YES;

[[NSNotificationCenter defaultCenter] postNotificationName:CDVPageDidLoadNotification object:nil];
[[NSNotificationCenter defaultCenter] postNotificationName:CDVPageDidLoadNotification object:self.launchWebView];

[self waitForExpectations:@[notFired] timeout:0.3];
}
Expand Down Expand Up @@ -260,7 +268,7 @@ - (void)testColdLaunchUserActivityIsReplayedAfterPageDidLoad
return [notification.object isEqual:launchUserActivity];
};

[[NSNotificationCenter defaultCenter] postNotificationName:CDVPageDidLoadNotification object:nil];
[[NSNotificationCenter defaultCenter] postNotificationName:CDVPageDidLoadNotification object:self.launchWebView];

[self waitForExpectations:@[userActivityFired] timeout:1.0];
}
Expand All @@ -279,7 +287,7 @@ - (void)testLaunchWithoutUserActivityDoesNotPostContinueUserActivity
initWithName:CDVPluginContinueUserActivityNotification];
notFired.inverted = YES;

[[NSNotificationCenter defaultCenter] postNotificationName:CDVPageDidLoadNotification object:nil];
[[NSNotificationCenter defaultCenter] postNotificationName:CDVPageDidLoadNotification object:self.launchWebView];

[self waitForExpectations:@[notFired] timeout:0.3];
}
Expand All @@ -300,7 +308,7 @@ - (void)testSceneDisconnectBeforePageLoadCancelsPendingUserActivity
initWithName:CDVPluginContinueUserActivityNotification];
notFired.inverted = YES;

[[NSNotificationCenter defaultCenter] postNotificationName:CDVPageDidLoadNotification object:nil];
[[NSNotificationCenter defaultCenter] postNotificationName:CDVPageDidLoadNotification object:self.launchWebView];

[self waitForExpectations:@[notFired] timeout:0.3];
}
Expand Down Expand Up @@ -350,9 +358,93 @@ - (void)testColdLaunchWithBothURLAndUserActivityReplaysBoth
return [notification.object isEqual:launchUserActivity];
};

[[NSNotificationCenter defaultCenter] postNotificationName:CDVPageDidLoadNotification object:nil];
[[NSNotificationCenter defaultCenter] postNotificationName:CDVPageDidLoadNotification object:self.launchWebView];

[self waitForExpectations:@[openURLFired, userActivityFired] timeout:1.0];
}

// A customized app can load an auxiliary Cordova window before its main page.
// Its real navigation must not release the main window's pending launch links.
- (void)testAuxiliaryWindowPageLoadDoesNotConsumeLaunchLinks
{
NSURL *launchURL = [NSURL URLWithString:@"testscheme://main-window"];
NSUserActivity *activity = [self browsingUserActivityForURL:[NSURL URLWithString:@"https://example.com/main-window"]];
CDVFakeOpenURLContext *context = [CDVFakeOpenURLContext contextWithURL:launchURL];
CDVFakeSceneConnectionOptions *options = [CDVFakeSceneConnectionOptions
optionsWithURLContexts:[NSSet setWithObject:context] userActivities:[NSSet setWithObject:activity]];
[self.sceneDelegate scene:self.placeholderScene willConnectToSession:self.placeholderSession options:options];

__block NSUInteger earlyURLCount = 0;
__block NSUInteger earlyActivityCount = 0;
NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
id urlObserver = [center addObserverForName:CDVPluginHandleOpenURLNotification object:launchURL queue:nil usingBlock:^(NSNotification *notification) {
earlyURLCount++;
}];
id activityObserver = [center addObserverForName:CDVPluginContinueUserActivityNotification object:activity queue:nil usingBlock:^(NSNotification *notification) {
earlyActivityCount++;
}];

UIWindow *auxiliaryWindow = [[UIWindow alloc] initWithWindowScene:(UIWindowScene *)self.placeholderScene];
CDVViewController *auxiliaryController = [CDVViewController new];
auxiliaryController.webContentFolderName = @"www";
auxiliaryController.startPage = @"index.html";
auxiliaryWindow.rootViewController = auxiliaryController;
[auxiliaryWindow makeKeyAndVisible];
[auxiliaryController loadViewIfNeeded];

XCTNSNotificationExpectation *auxiliaryLoaded = [[XCTNSNotificationExpectation alloc]
initWithName:CDVPageDidLoadNotification object:auxiliaryController.webView];
[self waitForExpectations:@[auxiliaryLoaded] timeout:10];

// A plugin in the main window may not have been created at this point.
// Register its observers only after the auxiliary page finishes loading.
[center removeObserver:urlObserver];
[center removeObserver:activityObserver];
XCTAssertEqual(earlyURLCount, 0u, @"The auxiliary page delivered the main window's URL too early");
XCTAssertEqual(earlyActivityCount, 0u, @"The auxiliary page delivered the main window's activity too early");
if (earlyURLCount != 0 || earlyActivityCount != 0) {
auxiliaryWindow.hidden = YES;
return;
}

XCTNSNotificationExpectation *urlDelivered = [[XCTNSNotificationExpectation alloc]
initWithName:CDVPluginHandleOpenURLNotification object:launchURL];
XCTNSNotificationExpectation *activityDelivered = [[XCTNSNotificationExpectation alloc]
initWithName:CDVPluginContinueUserActivityNotification object:activity];

CDVViewController *mainController = [CDVViewController new];
mainController.webContentFolderName = @"www";
mainController.startPage = @"index.html";
self.sceneDelegate.window.rootViewController = mainController;
[self.sceneDelegate.window makeKeyAndVisible];
[self waitForExpectations:@[urlDelivered, activityDelivered] timeout:10];
auxiliaryWindow.hidden = YES;
}

- (void)testSecondaryWebViewInRootCordovaWindowDoesNotReplayLaunchURL
{
CDVViewController *mainController = [CDVViewController new];
mainController.webContentFolderName = @"www";
mainController.startPage = @"index.html";
self.sceneDelegate.window.rootViewController = mainController;
[mainController loadViewIfNeeded];

WKWebView *secondaryWebView = [WKWebView new];
[mainController.view addSubview:secondaryWebView];
NSURL *launchURL = [NSURL URLWithString:@"testscheme://root-cordova-window"];
[self.sceneDelegate scene:self.placeholderScene willConnectToSession:self.placeholderSession
options:[self connectionOptionsForURL:launchURL]];

__block NSUInteger deliveryCount = 0;
NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
id observer = [center addObserverForName:CDVPluginHandleOpenURLNotification object:launchURL queue:nil usingBlock:^(NSNotification *notification) {
deliveryCount++;
}];
[center postNotificationName:CDVPageDidLoadNotification object:secondaryWebView];
XCTAssertEqual(deliveryCount, 0u);
[center postNotificationName:CDVPageDidLoadNotification object:mainController.webView];
XCTAssertEqual(deliveryCount, 1u);
[center removeObserver:observer];
}

@end
Loading