Skip to content

Commit 5013f7d

Browse files
authored
refactor(tests): Use XCTestPlans for unit testing (#1509)
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.
1 parent 83981c9 commit 5013f7d

24 files changed

+225
-1058
lines changed

.ratignore

+3
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,7 @@ IDEWorkspaceChecks.plist
1414
# Testing Figures
1515
fixtures
1616

17+
#Xcode Test Plans are JSON with no comment support
18+
(.*).xctestplan
19+
1720
node_modules

package.json

+1-4
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,7 @@
1919
"test": "npm run coverage && npm run objc-tests",
2020
"coverage": "nyc jasmine --config=tests/spec/coverage.json",
2121
"e2e-tests": "jasmine tests/spec/create.spec.js",
22-
"objc-tests": "npm run objc-tests-lib && npm run objc-tests-framework",
23-
"objc-tests-lib": "npm run xcodebuild -- -scheme CordovaLibTests",
24-
"objc-tests-framework": "npm run xcodebuild -- -scheme CordovaFrameworkApp",
25-
"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'`\"",
22+
"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'`\"",
2623
"preobjc-tests": "killall Simulator || true",
2724
"unit-tests": "jasmine --config=tests/spec/unit.json",
2825
"lint": "eslint . \"templates/cordova/lib/!(*.*)\""

tests/CordovaLibTests/CDVAllowListTests.m

+12
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,18 @@ - (void)testSpecificProtocol
239239
XCTAssertFalse([allowList URLIsAllowed:[NSURL URLWithString:@"http://www.google.com"]]);
240240
}
241241

242+
- (void)testSpecificPort
243+
{
244+
NSArray* allowedHosts = [NSArray arrayWithObjects:
245+
@"http://www.apache.org:8080",
246+
nil];
247+
248+
CDVAllowList* allowList = [[CDVAllowList alloc] initWithArray:allowedHosts];
249+
250+
XCTAssertFalse([allowList URLIsAllowed:[NSURL URLWithString:@"http://www.apache.org/index.html"]]);
251+
XCTAssertTrue([allowList URLIsAllowed:[NSURL URLWithString:@"http://www.apache.org:8080/index.html"]]);
252+
}
253+
242254
- (void)testWildcardPlusOtherUrls
243255
{
244256
// test for https://issues.apache.org/jira/browse/CB-3394

tests/CordovaLibTests/CDVPluginInitTests.m

+2-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ Licensed to the Apache Software Foundation (ASF) under one
1919

2020
#import <XCTest/XCTest.h>
2121
#import <Cordova/Cordova.h>
22-
#import "AppDelegate.h"
22+
23+
#import "CordovaApp-Swift.h"
2324

2425
@interface CDVPluginInitTests : XCTestCase
2526
@property AppDelegate* appDelegate;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
Licensed to the Apache Software Foundation (ASF) under one
3+
or more contributor license agreements. See the NOTICE file
4+
distributed with this work for additional information
5+
regarding copyright ownership. The ASF licenses this file
6+
to you under the Apache License, Version 2.0 (the
7+
"License"); you may not use this file except in compliance
8+
with the License. You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing,
13+
software distributed under the License is distributed on an
14+
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
KIND, either express or implied. See the License for the
16+
specific language governing permissions and limitations
17+
under the License.
18+
*/
19+
20+
import UIKit
21+
import Cordova
22+
23+
@main
24+
@objc class AppDelegate : CDVAppDelegate {
25+
fileprivate var _window : UIWindow?
26+
fileprivate var _viewController : ViewController?
27+
28+
@objc public var testViewController : CDVViewController? {
29+
return _viewController
30+
}
31+
32+
@objc func createViewController() {
33+
_viewController = ViewController();
34+
_viewController?.webContentFolderName = "www";
35+
_viewController?.startPage = "index.html";
36+
37+
_window?.rootViewController = _viewController;
38+
_window?.makeKeyAndVisible();
39+
}
40+
41+
@objc func destroyViewController() {
42+
_viewController = nil
43+
}
44+
45+
override func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
46+
let retVal = super.application(application, didFinishLaunchingWithOptions:launchOptions);
47+
48+
let bounds = UIScreen.main.bounds;
49+
_window = UIWindow(frame: bounds);
50+
_window?.autoresizesSubviews = true;
51+
52+
if NSClassFromString("CDVWebViewTest") != nil {
53+
createViewController();
54+
}
55+
56+
return retVal;
57+
}
58+
}

tests/CordovaLibTests/CordovaLibApp/SwiftInitPlugin.swift renamed to tests/CordovaLibTests/CordovaApp/Plugins/SwiftInitPlugin.swift

+2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
under the License.
1818
*/
1919

20+
import Cordova
21+
2022
@objc class SwiftInitPlugin : CDVPlugin {
2123
let initStr : String = "Successfully initialized";
2224

tests/CordovaLibTests/CordovaLibApp/Bridging-Header.h renamed to tests/CordovaLibTests/CordovaApp/ViewController.swift

+4-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515
KIND, either express or implied. See the License for the
1616
specific language governing permissions and limitations
1717
under the License.
18-
*/
18+
*/
1919

20-
#import <Cordova/Cordova.h>
20+
import Cordova
2121

22+
class ViewController: CDVViewController {
23+
}

tests/CordovaLibTests/CordovaLibApp/AppDelegate.h

-32
This file was deleted.

tests/CordovaLibTests/CordovaLibApp/AppDelegate.m

-68
This file was deleted.

tests/CordovaLibTests/CordovaLibApp/CordovaLibApp-Info.plist

-63
This file was deleted.

tests/CordovaLibTests/CordovaLibApp/ViewController.h

-25
This file was deleted.

tests/CordovaLibTests/CordovaLibApp/ViewController.m

-42
This file was deleted.

tests/CordovaLibTests/CordovaLibApp/en.lproj/InfoPlist.strings

-20
This file was deleted.

0 commit comments

Comments
 (0)