-
Notifications
You must be signed in to change notification settings - Fork 532
/
NinePatchTests.cs
86 lines (78 loc) · 3.72 KB
/
NinePatchTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
using System;
using System.Diagnostics.CodeAnalysis;
using Android.App;
using Android.Content.Res;
using Android.Graphics;
using Android.Graphics.Drawables;
using Android.Runtime;
using NUnit.Framework;
namespace Android.GraphicsTests
{
// https://bugzilla.xamarin.com/show_bug.cgi?id=23823
[TestFixture]
public class NinePatchTests
{
object[] NinePatchDrawables = {
new object[] {
Xamarin.Android.RuntimeTests.Resource.Drawable.image,
"image" // Drawable from App project.
},
new object[] {
Mono.Android_Test.Library.Resource.Drawable.tile,
"tile" // Drawable from Library project.
}
};
[Test, TestCaseSource (nameof (NinePatchDrawables))]
[DynamicDependency (DynamicallyAccessedMemberTypes.All, typeof (NinePatchDrawable))]
public void DrawableFromRes_ShouldBeTypeNinePatchDrawable (int resId, string name)
{
var d = Application.Context.Resources.GetDrawable (resId);
Assert.IsNotNull (d, $"An image should have been retrieved from resource `{name}`.");
Assert.IsNotNull (d as NinePatchDrawable, $"The drawable created from resource `{name}` should be a NinePatchDrawable.");
}
[Test, TestCaseSource (nameof (NinePatchDrawables))]
[DynamicDependency (DynamicallyAccessedMemberTypes.All, typeof (NinePatchDrawable))]
public void DrawableFromResStream_ShouldBeTypeNinePatchDrawable (int resId, string name)
{
var value = new Android.Util.TypedValue ();
InputStreamInvoker si = GetResourceStream (resId, value);
var d = Drawable.CreateFromResourceStream (Application.Context.Resources, value, si, value.String.ToString (), null);
Assert.IsNotNull (d, $"An image should have been retrieved from resource `{name}`.");
Assert.IsNotNull (d as NinePatchDrawable, $"The drawable created from resource `{name}` should be a NinePatchDrawable.");
}
[Test, TestCaseSource (nameof (NinePatchDrawables))]
public void BitmapFromDecodeRes_ShouldContainNinePatchChunk (int resId, string name)
{
Bitmap bm = BitmapFactory.DecodeResource (Application.Context.Resources, resId);
byte[] chunk = bm.GetNinePatchChunk ();
Assert.IsTrue (NinePatch.IsNinePatchChunk (chunk),
$"Bitmap decoded from resource `{name}` did not contain a valid NinePatch chunk.");
}
[Test, TestCaseSource (nameof (NinePatchDrawables))]
public void BitmapFromDecodeResStream_ShouldContainNinePatchChunk (int resId, string name)
{
var value = new Android.Util.TypedValue();
InputStreamInvoker si = GetResourceStream (resId, value);
Bitmap bm = BitmapFactory.DecodeResourceStream (Application.Context.Resources, value, si, null, null);
byte[] chunk = bm.GetNinePatchChunk ();
Assert.IsTrue(NinePatch.IsNinePatchChunk (chunk),
$"Bitmap decoded from resource stream with id `{name}` did not contain a valid NinePatch chunk.");
}
InputStreamInvoker GetResourceStream (int resId, Android.Util.TypedValue outValue)
{
Application.Context.Resources.GetValue (resId, outValue, true);
IntPtr sp = OpenNonAsset(Application.Context.Resources.Assets, outValue.AssetCookie, outValue.String.ToString (), 2 /* AssetManager.ACCESS_STREAMING */);
Java.IO.InputStream s = Java.Lang.Object.GetObject<Java.IO.InputStream> (sp, JniHandleOwnership.TransferLocalRef);
return new InputStreamInvoker (s);
}
IntPtr AssetManager_openNonAsset;
IntPtr OpenNonAsset (AssetManager manager, int cookie, string fileName, int accessMode)
{
if (AssetManager_openNonAsset == IntPtr.Zero)
AssetManager_openNonAsset = JNIEnv.GetMethodID (manager.Class.Handle, "openNonAsset", "(ILjava/lang/String;I)Ljava/io/InputStream;");
using (var f = new Java.Lang.String (fileName)) {
return JNIEnv.CallObjectMethod (manager.Handle, AssetManager_openNonAsset, new JValue (cookie), new JValue (f), new JValue (accessMode));
}
}
}
}