Skip to content

Commit

Permalink
CB-14045 - Reinit url after app freezes (#363)
Browse files Browse the repository at this point in the history
* test reinit url
* refactor: split function + check if appUrl is not empty
* Added tests.
  • Loading branch information
Sarunas Valaskevicius authored and shazron committed May 16, 2018
1 parent 191c17e commit 6e790af
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
24 changes: 24 additions & 0 deletions CordovaLib/Classes/Public/CDVViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -726,12 +726,33 @@ - (void)onAppWillTerminate:(NSNotification*)notification
}
}

- (bool)isUrlEmpty:(NSURL *)url
{
if (!url || (url == (id) [NSNull null])) {
return true;
}
NSString *urlAsString = [url absoluteString];
return (urlAsString == (id) [NSNull null] || [urlAsString length]==0 || [urlAsString isEqualToString:@"about:blank"]);
}

- (bool)checkAndReinitViewUrl
{
NSURL* appURL = [self appUrl];
if ([self isUrlEmpty: [self.webViewEngine URL]] && ![self isUrlEmpty: appURL]) {
NSURLRequest* appReq = [NSURLRequest requestWithURL:appURL cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:20.0];
[self.webViewEngine loadRequest:appReq];
return true;
}
return false;
}

/*
This method is called to let your application know that it is about to move from the active to inactive state.
You should use this method to pause ongoing tasks, disable timer, ...
*/
- (void)onAppWillResignActive:(NSNotification*)notification
{
[self checkAndReinitViewUrl];
// NSLog(@"%@",@"applicationWillResignActive");
[self.commandDelegate evalJs:@"cordova.fireDocumentEvent('resign');" scheduledOnRunLoop:NO];
}
Expand All @@ -743,6 +764,7 @@ - (void)onAppWillResignActive:(NSNotification*)notification
*/
- (void)onAppWillEnterForeground:(NSNotification*)notification
{
[self checkAndReinitViewUrl];
// NSLog(@"%@",@"applicationWillEnterForeground");
[self.commandDelegate evalJs:@"cordova.fireDocumentEvent('resume');"];

Expand All @@ -759,6 +781,7 @@ - (void)onAppWillEnterForeground:(NSNotification*)notification
// This method is called to let your application know that it moved from the inactive to active state.
- (void)onAppDidBecomeActive:(NSNotification*)notification
{
[self checkAndReinitViewUrl];
// NSLog(@"%@",@"applicationDidBecomeActive");
[self.commandDelegate evalJs:@"cordova.fireDocumentEvent('active');"];
}
Expand All @@ -769,6 +792,7 @@ - (void)onAppDidBecomeActive:(NSNotification*)notification
*/
- (void)onAppDidEnterBackground:(NSNotification*)notification
{
[self checkAndReinitViewUrl];
// NSLog(@"%@",@"applicationDidEnterBackground");
[self.commandDelegate evalJs:@"cordova.fireDocumentEvent('pause', null, true);" scheduledOnRunLoop:NO];
}
Expand Down
30 changes: 30 additions & 0 deletions tests/CordovaLibTests/CDVViewControllerTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ @interface CDVViewControllerTest : XCTestCase

@end

@interface CDVViewController ()

// expose private interface
- (bool)checkAndReinitViewUrl;
- (bool)isUrlEmpty:(NSURL*)url;

@end

@implementation CDVViewControllerTest

-(CDVViewController*)viewController{
Expand Down Expand Up @@ -89,5 +97,27 @@ -(void)testColorFromColorString{
XCTAssertNil([viewController colorFromColorString:@"#NOTHEX"]);
}

-(void)testIsUrlEmpty{
CDVViewController* viewController = [self viewController];
XCTAssertTrue([viewController isUrlEmpty:(id)[NSNull null]]);
XCTAssertTrue([viewController isUrlEmpty:nil]);
XCTAssertTrue([viewController isUrlEmpty:[NSURL URLWithString:@""]]);
XCTAssertTrue([viewController isUrlEmpty:[NSURL URLWithString:@"about:blank"]]);
}

-(void)testIfItLoadsAppUrlIfCurrentViewIsBlank{
CDVViewController* viewController = [self viewController];

NSString* appUrl = @"about:blank";
NSString* html = @"<html><body></body></html>";
[viewController.webViewEngine loadHTMLString:html baseURL:[NSURL URLWithString:appUrl]];
XCTAssertFalse([viewController checkAndReinitViewUrl]);

appUrl = @"https://cordova.apache.org";
viewController.startPage = appUrl;
[viewController.webViewEngine loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:appUrl]]];
XCTAssertTrue([viewController checkAndReinitViewUrl]);
}

@end

1 comment on commit 6e790af

@jacobg
Copy link

@jacobg jacobg commented on 6e790af Dec 19, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this commit work with CodePush using dynamic url?

Please sign in to comment.