Skip to content

Commit

Permalink
refactor(tests): Use XCTestPlans for unit testing (apache#1509)
Browse files Browse the repository at this point in the history
This allows us to set up default behaviour of re-running tests up to 3
times if they fail, which should significantly cut down the number of
failing builds in CI.

I also opted to only keep the unit tests for the case where CordovaLib
is consumed as a framework, since that's now the default behaviour for
the template app (using Swift Packages) rather than a static library.

Also cleaned up the test app project naming and moved some boilerplate
stuff over to Swift.
  • Loading branch information
dpogue authored Nov 22, 2024
1 parent 83981c9 commit 5013f7d
Show file tree
Hide file tree
Showing 24 changed files with 225 additions and 1,058 deletions.
3 changes: 3 additions & 0 deletions .ratignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,7 @@ IDEWorkspaceChecks.plist
# Testing Figures
fixtures

#Xcode Test Plans are JSON with no comment support
(.*).xctestplan

node_modules
5 changes: 1 addition & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,7 @@
"test": "npm run coverage && npm run objc-tests",
"coverage": "nyc jasmine --config=tests/spec/coverage.json",
"e2e-tests": "jasmine tests/spec/create.spec.js",
"objc-tests": "npm run objc-tests-lib && npm run objc-tests-framework",
"objc-tests-lib": "npm run xcodebuild -- -scheme CordovaLibTests",
"objc-tests-framework": "npm run xcodebuild -- -scheme CordovaFrameworkApp",
"xcodebuild": "xcodebuild -quiet test -workspace tests/cordova-ios.xcworkspace -destination \"platform=iOS Simulator,name=iPhone 15\" -derivedDataPath \"`mktemp -d 2>/dev/null || mktemp -d -t 'cordova-ios'`\"",
"objc-tests": "xcodebuild -quiet test -workspace tests/cordova-ios.xcworkspace -scheme CordovaTestApp -destination \"platform=iOS Simulator,name=${CDV_IOS_SIM:-iPhone SE (3rd generation)}\" -derivedDataPath \"`mktemp -d 2>/dev/null || mktemp -d -t 'cordova-ios'`\"",
"preobjc-tests": "killall Simulator || true",
"unit-tests": "jasmine --config=tests/spec/unit.json",
"lint": "eslint . \"templates/cordova/lib/!(*.*)\""
Expand Down
12 changes: 12 additions & 0 deletions tests/CordovaLibTests/CDVAllowListTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,18 @@ - (void)testSpecificProtocol
XCTAssertFalse([allowList URLIsAllowed:[NSURL URLWithString:@"http://www.google.com"]]);
}

- (void)testSpecificPort
{
NSArray* allowedHosts = [NSArray arrayWithObjects:
@"http://www.apache.org:8080",
nil];

CDVAllowList* allowList = [[CDVAllowList alloc] initWithArray:allowedHosts];

XCTAssertFalse([allowList URLIsAllowed:[NSURL URLWithString:@"http://www.apache.org/index.html"]]);
XCTAssertTrue([allowList URLIsAllowed:[NSURL URLWithString:@"http://www.apache.org:8080/index.html"]]);
}

- (void)testWildcardPlusOtherUrls
{
// test for https://issues.apache.org/jira/browse/CB-3394
Expand Down
3 changes: 2 additions & 1 deletion tests/CordovaLibTests/CDVPluginInitTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ Licensed to the Apache Software Foundation (ASF) under one

#import <XCTest/XCTest.h>
#import <Cordova/Cordova.h>
#import "AppDelegate.h"

#import "CordovaApp-Swift.h"

@interface CDVPluginInitTests : XCTestCase
@property AppDelegate* appDelegate;
Expand Down
58 changes: 58 additions & 0 deletions tests/CordovaLibTests/CordovaApp/AppDelegate.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
*/

import UIKit
import Cordova

@main
@objc class AppDelegate : CDVAppDelegate {
fileprivate var _window : UIWindow?
fileprivate var _viewController : ViewController?

@objc public var testViewController : CDVViewController? {
return _viewController
}

@objc func createViewController() {
_viewController = ViewController();
_viewController?.webContentFolderName = "www";
_viewController?.startPage = "index.html";

_window?.rootViewController = _viewController;
_window?.makeKeyAndVisible();
}

@objc func destroyViewController() {
_viewController = nil
}

override func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
let retVal = super.application(application, didFinishLaunchingWithOptions:launchOptions);

let bounds = UIScreen.main.bounds;
_window = UIWindow(frame: bounds);
_window?.autoresizesSubviews = true;

if NSClassFromString("CDVWebViewTest") != nil {
createViewController();
}

return retVal;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
under the License.
*/

import Cordova

@objc class SwiftInitPlugin : CDVPlugin {
let initStr : String = "Successfully initialized";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
*/
*/

#import <Cordova/Cordova.h>
import Cordova

class ViewController: CDVViewController {
}
File renamed without changes.
32 changes: 0 additions & 32 deletions tests/CordovaLibTests/CordovaLibApp/AppDelegate.h

This file was deleted.

68 changes: 0 additions & 68 deletions tests/CordovaLibTests/CordovaLibApp/AppDelegate.m

This file was deleted.

63 changes: 0 additions & 63 deletions tests/CordovaLibTests/CordovaLibApp/CordovaLibApp-Info.plist

This file was deleted.

25 changes: 0 additions & 25 deletions tests/CordovaLibTests/CordovaLibApp/ViewController.h

This file was deleted.

42 changes: 0 additions & 42 deletions tests/CordovaLibTests/CordovaLibApp/ViewController.m

This file was deleted.

20 changes: 0 additions & 20 deletions tests/CordovaLibTests/CordovaLibApp/en.lproj/InfoPlist.strings

This file was deleted.

Loading

0 comments on commit 5013f7d

Please sign in to comment.