2
// Test the generated API `init` selectors are usable by the binding consumers
5
// Sebastien Pouliot <sebastien@xamarin.com>
7
// Copyright 2012-2013 Xamarin Inc.
9
// Licensed under the Apache License, Version 2.0 (the "License");
10
// you may not use this file except in compliance with the License.
11
// You may obtain a copy of the License at
13
// http://www.apache.org/licenses/LICENSE-2.0
15
// Unless required by applicable law or agreed to in writing, software
16
// distributed under the License is distributed on an "AS IS" BASIS,
17
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18
// See the License for the specific language governing permissions and
19
// limitations under the License.
23
using System.Reflection;
25
using NUnit.Framework;
28
using MonoMac.Foundation;
30
using MonoTouch.Foundation;
33
namespace TouchUnit.Bindings {
35
public abstract class ApiCtorInitTest : ApiBaseTest {
37
string instance_type_name;
41
/// Gets or sets a value indicating whether this test fixture will log untested types.
43
/// <value><c>true</c> if log untested types; otherwise, <c>false</c>.</value>
44
public bool LogUntestedTypes { get; set; }
47
/// Override this method if you want the test to skip some specific types.
48
/// By default types decorated with [Model] will be skipped.
50
/// <param name="type">The Type to be tested</param>
51
protected virtual bool Skip (Type type)
53
// skip delegate (and other protocol references)
54
foreach (object ca in type.GetCustomAttributes (false)) {
55
if (ca is ModelAttribute)
62
/// Checks that the Handle property of the specified NSObject instance is not null (not IntPtr.Zero).
64
/// <param name="obj">NSObject instance to validate</param>
65
protected virtual void CheckHandle (NSObject obj)
67
bool result = obj.Handle != IntPtr.Zero;
68
if (!ContinueOnFailure)
69
Assert.IsTrue (result, instance_type_name + ".Handle");
71
Console.WriteLine ("[FAIL] {0} : Handle", instance_type_name);
77
/// Checks that ToString does not return null (not helpful for debugging) and that it does not crash.
79
/// <param name="obj">NSObject instance to validate</param>
80
protected virtual void CheckToString (NSObject obj)
82
bool result = obj.ToString () != null;
83
if (!ContinueOnFailure)
84
Assert.IsTrue (result, instance_type_name + ".ToString");
86
Console.WriteLine ("[FAIL] {0} : ToString", instance_type_name);
92
/// Dispose the specified NSObject instance. In some cases objects cannot be disposed safely.
93
/// Override this method to keep them alive while the remaining tests execute.
95
/// <param name="obj">NSObject instance to dispose</param>
96
/// <param name="type">Type of the object, to be used if special logic is required.</param>
97
protected virtual void Dispose (NSObject obj, Type type)
103
public void DefaultCtorAllowed ()
108
foreach (Type t in Assembly.GetTypes ()) {
109
if (t.IsAbstract || !NSObjectType.IsAssignableFrom (t))
112
if (Skip (t) || SkipDueToAttribute (t))
115
var ctor = t.GetConstructor (Type.EmptyTypes);
116
if ((ctor == null) || ctor.IsAbstract) {
117
if (LogUntestedTypes)
118
Console.WriteLine ("[WARNING] {0} was skipped because it had no default constructor", t);
122
instance_type_name = t.FullName;
124
Console.WriteLine ("{0}. {1}", n, instance_type_name);
128
obj = ctor.Invoke (null) as NSObject;
133
catch (TargetInvocationException tie) {
134
// Objective-C exception thrown
135
if (!ContinueOnFailure)
138
Console.WriteLine ("[FAIL] {0} : {1}", instance_type_name, tie.InnerException);
143
Assert.AreEqual (0, Errors, "{0} potential errors found in {1} default ctor validated", Errors, n);