Skip to content
This repository has been archived by the owner on Jan 15, 2021. It is now read-only.

Commit

Permalink
implement Cordova plugin for the iOS tests #771
Browse files Browse the repository at this point in the history
  • Loading branch information
larryonoff committed Jul 25, 2016
1 parent 460b864 commit 8305465
Show file tree
Hide file tree
Showing 14 changed files with 181 additions and 61 deletions.
86 changes: 86 additions & 0 deletions plugins/tests-ios/hooks/after_plugin_install.js
@@ -0,0 +1,86 @@
//
// this code was taken from https://github.com/Justin-Credible/cordova-plugin-braintree/blob/master/hooks/after_plugin_install.js
//

var fs = require("fs");
var path = require("path");
var child_process = require("child_process");

module.exports = function(context) {

// Temporary hack to run npm install on this plugin's package.json dependencies.
var pluginDir = path.resolve(__dirname, "../");

child_process.execSync("npm --prefix " + pluginDir + " install " + pluginDir);
var xcode = require("xcode");

// Need a promise so that the install waits for us to complete our project modifications
// before the plugin gets installed.
var Q = context.requireCordovaModule("q");
var deferral = new Q.defer();

var platforms = context.opts.cordova.platforms;

// We can bail out if the iOS platform isn't present.
if (platforms.indexOf("ios") === -1) {
deferral.resolve();
return deferral.promise;
}

// We need to embded frameworks to the project here.
// They need to be embedded binaries and cordova does not yet support that.
// We will use node-xcode directy to add them since that library has
// been upgraded to support embedded binaries.

// Cordova libs to get the project path and project name so we can locate the xcode project file.
var cordova_util = context.requireCordovaModule("cordova-lib/src/cordova/util"),
ConfigParser = context.requireCordovaModule("cordova-lib").configparser,
projectRoot = cordova_util.isCordova(),
xml = cordova_util.projectConfig(projectRoot),
cfg = new ConfigParser(xml);

var projectPath = path.join(projectRoot, "platforms", "ios", cfg.name() + ".xcodeproj", "project.pbxproj");
var xcodeProject = xcode.project(projectPath);

xcodeProject.parse(function(err) {

// If we couldn't parse the project, bail out.
if (err) {
deferral.reject("ThaliPluginTests - after_plugin_install: " + JSON.stringify(err));
return;
}

// Cordova project should not have more that one target.
var targetUUID = xcodeProject.getFirstTarget().uuid;

// Remove all of the frameworks because they were not embeded correctly.
var frameworkPath = cfg.name() + "/Plugins/org.thaliproject.p2p-tests/";
xcodeProject.removeFramework(frameworkPath + "XCTest.framework");

// First check to see if the Embed Framework node exists, if not, add it.
// This is all we need to do as they are added to the embedded section by default.
if (!xcodeProject.pbxEmbedFrameworksBuildPhaseObj(targetUUID)) {
buildPhaseResult = xcodeProject.addBuildPhase([], "PBXCopyFilesBuildPhase", "Embed Frameworks", targetUUID, "framework");
// No idea why, but "Framework" (value 10) is not available in node-xcode, set it here manually so libraries
// embed correctly. If we don't set it, the folder type defaults to "Shared Frameworks".
buildPhaseResult.buildPhase.dstSubfolderSpec = 10;
console.log("Adding Embedded Build Phase");
} else {
console.log("Embedded Build Phase already added");
}

// This is critical to include, otherwise the library loader cannot find the dynamic Braintree libs at runtime
// on a device.
xcodeProject.addBuildProperty("LD_RUNPATH_SEARCH_PATHS", "\"$(inherited) @executable_path/Frameworks\"", "Debug");
xcodeProject.addBuildProperty("LD_RUNPATH_SEARCH_PATHS", "\"$(inherited) @executable_path/Frameworks\"", "Release");

// Add the frameworks again. This time they will have the code-sign option set so they get code signed when being deployed to devices.
xcodeProject.addFramework(frameworkPath + "XCTest.framework", {customFramework: true, embed: true, link: true});

// Save the project file back to disk.
fs.writeFileSync(projectPath, xcodeProject.writeSync(), "utf-8");
deferral.resolve();
});

return deferral.promise;
};
23 changes: 23 additions & 0 deletions plugins/tests-ios/package.json
@@ -0,0 +1,23 @@
{
"name": "ThaliTests",
"version": "0.0.1",
"description": "ThaliPlugin Tests",
"cordova": {
"id": "org.thaliproject.p2p-tests",
"platforms": [
"ios"
]
},
"keywords": [
"cordova",
"jxcore",
"node.js",
"thali",
"test",
"cordova-ios"
],
"license": "MIT",
"dependencies": {
"xcode": "git://github.com/kurtisf/node-xcode"
}
}
50 changes: 50 additions & 0 deletions plugins/tests-ios/plugin.xml
@@ -0,0 +1,50 @@
<?xml version='1.0' encoding='utf-8'?>
<plugin xmlns="http://apache.org/cordova/ns/plugins/1.0"
xmlns:android="http://schemas.android.com/apk/res/android"
id="org.thaliproject.p2p-tests"
version="0.0.1">

<name>ThaliTests</name>
<description>ThaliPlugin Tests</description>
<license>MIT</license>
<keywords>cordova,jxcore,node.js,thali,test</keywords>

<engines>
<engine name="cordova-ios" version=">=3.9.0" />
</engines>

<hook type="after_plugin_install" src="hooks/after_plugin_install.js" />

<platform name="ios">
<config-file target="config.xml" parent="/*">
<feature name="ThaliTests">
<param name="ios-package" value="ThaliTests" />
</feature>
</config-file>

<!-- <framework
src="/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/Library/Frameworks/XCTest.framework"
arch="ARM"
target-dir="frameworks/iPhoneOS.platform"
custom="true"
/> -->
<framework
src="/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/Library/Frameworks/XCTest.framework"
arch="x86"
target-dir="Frameworks/iPhoneSimulator.platform"
custom="true"
embed="true"
/>

<header-file src="src/ios/THETestRun.h" />
<source-file src="src/ios/THETestRun.m" />
<header-file src="src/ios/THETestRunFailure.h" />
<source-file src="src/ios/THETestRunFailure.m" />
<header-file src="src/ios/THETestRunner.h" />
<source-file src="src/ios/THETestRunner.m" />
<header-file src="src/ios/THETestRunnerResult.h" />
<source-file src="src/ios/THETestRunnerResult.m" />

</platform>

</plugin>
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Expand Up @@ -46,4 +46,4 @@ NS_ASSUME_NONNULL_BEGIN

@end

NS_ASSUME_NONNULL_END
NS_ASSUME_NONNULL_END
40 changes: 20 additions & 20 deletions src/ios/THEAppContext.m
Expand Up @@ -64,7 +64,7 @@ @implementation THEAppContext
@private
// the event delegate to which we'll deliver key events
id<THEThaliEventDelegate> _eventDelegate;

// The reachability handler reference.
id reachabilityHandlerReference;

Expand All @@ -74,20 +74,20 @@ @implementation THEAppContext

// Our current app level id
NSString * _peerIdentifier;

// Peer Bluetooth.
THEPeerBluetooth * _peerBluetooth;

// Let's us know the BT radio states
CBPeripheralManager *_btPeripheralManager;
CBCentralManager *_bleManager;

// The multipeer manager, co-ordinates client and server
THEMultipeerManager *_multipeerManager;

// The mutex used to protect access to things below.
pthread_mutex_t _mutex;

// The peers dictionary.
NSMutableDictionary * _peers;
}
Expand All @@ -110,7 +110,7 @@ - (BOOL)startListeningForAdvertisements
return result;
}

// Stops client components
// Stops client components
- (BOOL)stopListeningForAdvertisements
{
BOOL result = [_multipeerManager stopListening];
Expand All @@ -131,7 +131,7 @@ - (BOOL)stopAdvertisingAndListening
}

// Connects to the peer server with the specified peer identifier.
- (BOOL)connectToPeer:(NSString *)peerIdentifier
- (BOOL)connectToPeer:(NSString *)peerIdentifier
connectCallback:(ClientConnectCallback)connectCallback
{
return [_multipeerManager connectToPeerWithPeerIdentifier:peerIdentifier
Expand Down Expand Up @@ -171,7 +171,7 @@ - (void)fireNetworkChangedEvent
}
}*/

BOOL isWifi = !([[NPReachability sharedInstance] currentReachabilityFlags] & kSCNetworkReachabilityFlagsIsWWAN);

networkStatus = @{
Expand Down Expand Up @@ -248,31 +248,31 @@ - (instancetype)init
{
// Initialize superclass.
self = [super init];

// Handle errors.
if (!self)
{
return nil;
}

_peerIdentifier = [[[NSUUID alloc] init] UUIDString];

// We don't really know yet, assume the worst, we'll get an update
// when we initialise the BT stack
// when we initialise the BT stack
_bluetoothEnabled = false;

NSDictionary<NSString *, id> *options = @{CBCentralManagerOptionShowPowerAlertKey:@0};
_btPeripheralManager = [[CBPeripheralManager alloc] initWithDelegate:self queue:nil options:options];

_bleManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil];

_multipeerManager = [[THEMultipeerManager alloc] initWithServiceType:THALI_SERVICE_TYPE
withPeerIdentifier:_peerIdentifier
withPeerDiscoveryDelegate:self
withRemoteConnectionDelegate:self];
// Get the default notification center.
NSNotificationCenter * notificationCenter = [NSNotificationCenter defaultCenter];

// Add our observers for application events.
[notificationCenter addObserver:self
selector:@selector(applicationWillResignActiveNotification:)
Expand Down Expand Up @@ -301,7 +301,7 @@ - (void)fireDiscoveryAdvertisingStateUpdate
@"discoveryActive" : [[NSNumber alloc] initWithBool:[_multipeerManager isListening]],
@"advertisingActive" : [[NSNumber alloc] initWithBool:[_multipeerManager isAdvertising]]
};

[_eventDelegate discoveryAdvertisingStateUpdate:stateUpdate];
}

Expand Down Expand Up @@ -346,14 +346,14 @@ - (void)peripheralManagerDidUpdateState:(CBPeripheralManager *)peripheral
_bluetoothEnabled = true;
}
break;

default:
{
_bluetoothEnabled = false;
}
break;
}

[self fireNetworkChangedEvent];
}

Expand All @@ -363,11 +363,11 @@ - (void)centralManagerDidUpdateState:(CBCentralManager *)central
{
case CBCentralManagerStatePoweredOn:
_bleEnabled = true;

default:
_bleEnabled = false;
}

[self fireNetworkChangedEvent];
}

Expand Down

0 comments on commit 8305465

Please sign in to comment.