|
| 1 | +using System; |
| 2 | +using MonoDroid.Generation; |
| 3 | +using NUnit.Framework; |
| 4 | +using Xamarin.Android.Binder; |
| 5 | + |
| 6 | +namespace generatortests; |
| 7 | + |
| 8 | +[TestFixture] |
| 9 | +class BlittableTypeTests : CodeGeneratorTestBase |
| 10 | +{ |
| 11 | + protected override CodeGenerationTarget Target => CodeGenerationTarget.XAJavaInterop1; |
| 12 | + |
| 13 | + [Test] |
| 14 | + public void MethodWithBoolReturnType () |
| 15 | + { |
| 16 | + var klass = new TestClass ("Object", "java.code.MyClass"); |
| 17 | + var method = SupportTypeBuilder.CreateMethod (klass, "IsEmpty", options, "boolean"); |
| 18 | + |
| 19 | + klass.Methods.Add (method); |
| 20 | + |
| 21 | + var actual = GetGeneratedTypeOutput (klass); |
| 22 | + |
| 23 | + // Return type should be byte |
| 24 | + Assert.That (actual, Contains.Substring ("static byte n_IsEmpty")); |
| 25 | + |
| 26 | + // Return statement should convert to 0 or 1 |
| 27 | + Assert.That (actual, Contains.Substring ("return __this.IsEmpty () ? 1 : 0")); |
| 28 | + |
| 29 | + // Ensure the marshal delegate is byte |
| 30 | + Assert.That (actual, Contains.Substring ("new _JniMarshal_PP_B")); |
| 31 | + Assert.That (actual, Does.Not.Contains ("new _JniMarshal_PP_Z")); |
| 32 | + } |
| 33 | + |
| 34 | + [Test] |
| 35 | + public void MethodWithBoolParameter () |
| 36 | + { |
| 37 | + var klass = new TestClass ("Object", "java.code.MyClass"); |
| 38 | + var method = SupportTypeBuilder.CreateMethod (klass, "SetEmpty", options, "void", parameters: new Parameter ("value", "boolean", "bool", false)); |
| 39 | + |
| 40 | + klass.Methods.Add (method); |
| 41 | + |
| 42 | + var actual = GetGeneratedTypeOutput (klass); |
| 43 | + |
| 44 | + // Method parameter should be byte |
| 45 | + Assert.That (actual, Contains.Substring ("static void n_SetEmpty_Z (IntPtr jnienv, IntPtr native__this, byte native_value)")); |
| 46 | + |
| 47 | + // Method should convert from 0 or 1 |
| 48 | + Assert.That (actual, Contains.Substring ("var value = native_value != 0;")); |
| 49 | + |
| 50 | + // Ensure the marshal delegate is byte |
| 51 | + Assert.That (actual, Contains.Substring ("new _JniMarshal_PPB_V")); |
| 52 | + Assert.That (actual, Does.Not.Contains ("new _JniMarshal_PPZ_V")); |
| 53 | + } |
| 54 | + |
| 55 | + [Test] |
| 56 | + public void BoolProperty () |
| 57 | + { |
| 58 | + var klass = SupportTypeBuilder.CreateClassWithProperty ("MyClass", "com.example.myClass", "IsEmpty", "boolean", options); |
| 59 | + var actual = GetGeneratedTypeOutput (klass); |
| 60 | + |
| 61 | + // Getter return type should be byte |
| 62 | + Assert.That (actual, Contains.Substring ("static byte n_get_IsEmpty")); |
| 63 | + |
| 64 | + // Getter return statement should convert to 0 or 1 |
| 65 | + Assert.That (actual, Contains.Substring ("return __this.IsEmpty ? 1 : 0")); |
| 66 | + |
| 67 | + // Setter parameter should be byte |
| 68 | + Assert.That (actual, Contains.Substring ("static void n_set_IsEmpty_Z (IntPtr jnienv, IntPtr native__this, byte native_value)")); |
| 69 | + |
| 70 | + // Setter should convert from 0 or 1 |
| 71 | + Assert.That (actual, Contains.Substring ("var value = native_value != 0;")); |
| 72 | + |
| 73 | + // Ensure the marshal delegate is byte |
| 74 | + Assert.That (actual, Contains.Substring ("new _JniMarshal_PP_B")); |
| 75 | + Assert.That (actual, Does.Not.Contains ("new _JniMarshal_PP_Z")); |
| 76 | + } |
| 77 | + |
| 78 | + [Test] |
| 79 | + public void MethodWithCharReturnType () |
| 80 | + { |
| 81 | + var klass = new TestClass ("Object", "java.code.MyClass"); |
| 82 | + var method = SupportTypeBuilder.CreateMethod (klass, "GetFirstLetter", options, "char"); |
| 83 | + |
| 84 | + klass.Methods.Add (method); |
| 85 | + |
| 86 | + var actual = GetGeneratedTypeOutput (klass); |
| 87 | + |
| 88 | + // Return type should be ushort |
| 89 | + Assert.That (actual, Contains.Substring ("static ushort n_GetFirstLetter")); |
| 90 | + |
| 91 | + // Return statement should convert to ushort |
| 92 | + Assert.That (actual, Contains.Substring ("return (ushort)__this.GetFirstLetter ()")); |
| 93 | + |
| 94 | + // Ensure the marshal delegate is ushort |
| 95 | + Assert.That (actual, Contains.Substring ("new _JniMarshal_PP_s")); |
| 96 | + Assert.That (actual, Does.Not.Contains ("new _JniMarshal_PP_C")); |
| 97 | + } |
| 98 | + |
| 99 | + [Test] |
| 100 | + public void MethodWithCharParameter () |
| 101 | + { |
| 102 | + var klass = new TestClass ("Object", "java.code.MyClass"); |
| 103 | + var method = SupportTypeBuilder.CreateMethod (klass, "SetFirstLetter", options, "void", parameters: new Parameter ("value", "char", "char", false)); |
| 104 | + |
| 105 | + klass.Methods.Add (method); |
| 106 | + |
| 107 | + var actual = GetGeneratedTypeOutput (klass); |
| 108 | + |
| 109 | + // Method parameter should be ushort |
| 110 | + Assert.That (actual, Contains.Substring ("static void n_SetFirstLetter_C (IntPtr jnienv, IntPtr native__this, ushort native_value)")); |
| 111 | + |
| 112 | + // Method should convert from ushort to char |
| 113 | + Assert.That (actual, Contains.Substring ("var value = (char)native_value;")); |
| 114 | + |
| 115 | + // Ensure the marshal delegate is ushort |
| 116 | + Assert.That (actual, Contains.Substring ("new _JniMarshal_PPs_V")); |
| 117 | + Assert.That (actual, Does.Not.Contains ("new _JniMarshal_PPC_V")); |
| 118 | + } |
| 119 | + |
| 120 | + [Test] |
| 121 | + public void CharProperty () |
| 122 | + { |
| 123 | + var klass = SupportTypeBuilder.CreateClassWithProperty ("MyClass", "com.example.myClass", "FirstLetter", "char", options); |
| 124 | + var actual = GetGeneratedTypeOutput (klass); |
| 125 | + |
| 126 | + // Getter return type should be ushort |
| 127 | + Assert.That (actual, Contains.Substring ("static ushort n_get_FirstLetter")); |
| 128 | + |
| 129 | + // Getter return statement should convert to ushort |
| 130 | + Assert.That (actual, Contains.Substring ("return (ushort)__this.FirstLetter")); |
| 131 | + |
| 132 | + // Setter parameter should be ushort |
| 133 | + Assert.That (actual, Contains.Substring ("static void n_set_FirstLetter_C (IntPtr jnienv, IntPtr native__this, ushort native_value)")); |
| 134 | + |
| 135 | + // Setter should convert from ushort to char |
| 136 | + Assert.That (actual, Contains.Substring ("var value = (char)native_value;")); |
| 137 | + |
| 138 | + // Ensure the marshal delegate is ushort |
| 139 | + Assert.That (actual, Contains.Substring ("new _JniMarshal_PP_s")); |
| 140 | + Assert.That (actual, Does.Not.Contains ("new _JniMarshal_PP_C")); |
| 141 | + } |
| 142 | +} |
0 commit comments