4.8.7. Testing AUTs written with Monotouch

4.8.7.1. Create a binding project

First you have to create a MonoTouch Binding Project. The project template can be found in the category C#/MonoTouch. A binding project contains a reference to the MonoTouch library as well as two C# files called ApiDefinition.cs and StructsAndEnums.cs.

4.8.7.2. Add the library to the binding project

Add the library file librc.mobile.ios.nativ.a to the new binding project. The file is part of the archive development/iOS-support.zip in the installation directory.

MonoDevelop will ask either to copy, move or link the file in the binding project. We recommend choosing copy because MonoDevelop will place a generated C# file called librc.mobile.ios.nativ.linkwith.cs next to the library file.

4.8.7.3. Setting up linker options

The file librc.mobile.ios.nativ.linkwith.cs specifies the linker options. It contains a single annotation named LinkWith. Change the annotation to:

[assembly: LinkWith 
("librc.mobile.ios.nativ.a", 
LinkTarget.Simulator | LinkTarget.ArmV6 
| LinkTarget.ArmV7,
 "-ObjC -all_load", ForceLoad = true, 
Frameworks="CFNetwork")

Doing this allows your AUT to be tested on the simulator as well as on iOS devices. The framework CFNetwork is needed to communicate with the ITE.

4.8.7.4. Defining the API contract

An Objective-C header file is provided. To use the library from within a .NET project, you have to link the library’s interface parts to .NET structures.

Linking is done in the file ApiDefinition.cs. You have to translate the header file contents into some C# interfaces. Finally you have to annotate the interfaces/methods/parameters that name the library’s objects.

Detailed information about translation and annotation may be found on Xamarins web site: http://docs.xamarin.com/ios/Guides/Advanced_Topics/Binding_Objective-C_Libraries.

To use the library, adapt the following:

Library's header file content Linking in ApiDefinition.cs
@interface ObjCClass
...
[BaseType (typeof (NSObject))]
interface ObjCClass {
@end ...
}
+(void)method1:(int)parameter; [Static, Export("method1:")]
void method1(int parameter);
-(NSString*)method2; [Export("method2")]
NSString method2();
@protocol MyDelegate
-(int)method3;
@end
[BaseType (typeof(NSObject))]
[Model]
interface MyDelegate {
[Export("method3")]
[Abstract]
int method3();
}

4.8.7.5. Building a .NET library

When linking is done, build the binding project in Release mode. When it is done, you will find a DLL in the bin/Release directory of the binding project.

Copy this DLL to your project for your AUT and add it as a reference to a .NET Assembly.

When the library changes, we recommend to remove this reference and repeat the entire process.

4.8.7.6. Add hook into AUT

When the binding DLL is referenced by your project, you can use the namespace and all interfaces defined in ApiDefinition.cs. Adding the hook is the same as described for native iOS Apps but using C# syntax. You should omit the preprocessor macros mentioned there or implement your own switch for enabling the test hook.

Native iOS Apps are required to provide a ”window” property in the AppDelegate. Your C# AppDelegate has to provide and use this property too:

[Export("window")]
UIWindow window { get; set; }


Copyright BREDEX GmbH 2015. Made available under the Eclipse Public License v1.0.