-
Notifications
You must be signed in to change notification settings - Fork 4k
/
Hello.cs
1405 lines (1200 loc) · 44.9 KB
/
Hello.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#define CATCH
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Runtime.InteropServices;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;
using System.Text;
using System.Threading;
using System.Text;
using Android.App;
using Android.Content;
using Android.Content.PM;
using Android.Graphics;
using Android.Widget;
using Android.Runtime;
#if __ANDROID_7__
using Android.Service.Wallpaper;
#endif
using Android.Util;
using Android.Views;
using Javax.Net;
using Javax.Microedition.Khronos.Egl;
using Mono.Data.Sqlite;
using Path = System.IO.Path;
#if ASSEMBLY_APP
[assembly: Application (
Debuggable=true,
Label="insert label here",
ManageSpaceActivity=typeof(Mono.Samples.SanityTests.HelloActivity))]
#endif
namespace Mono.Samples.SanityTests
{
public class MyInstrumentation : Instrumentation {
public MyInstrumentation (IntPtr handle, JniHandleOwnership transfer)
: base (handle, transfer)
{
}
}
public class MyInstrumentationWithOnCreate : Instrumentation {
public MyInstrumentationWithOnCreate (IntPtr handle, JniHandleOwnership transfer)
: base (handle, transfer)
{
}
public override void OnCreate (Android.OS.Bundle arguments)
{
base.OnCreate (arguments);
}
}
public class MyAsyncTask : Android.OS.AsyncTask {
protected override Java.Lang.Object DoInBackground (params Java.Lang.Object[] values)
{
throw new NotImplementedException ();
}
}
public class IgnoreActivity : HelloActivity, ICloneable {
public object Clone ()
{
throw new NotSupportedException ();
}
}
[Service]
public class MyService : IntentService {
protected override void OnHandleIntent (Intent intent)
{
Log.Debug ("*jonp*", "MyService.OnHandleIntent invoked!");
}
}
[Activity]
public abstract class AbstractActivity : Activity {
}
public class NoOverridesApp : Application {
}
#if !ASSEMBLY_APP
[Application (
Debuggable=true,
Label="insert app label here",
Icon="@drawable/Icon",
#if __ANDROID_8__
BackupAgent=typeof(Mono.Samples.SanityTests.MyBackupAgent),
#endif
ManageSpaceActivity=typeof(Mono.Samples.SanityTests.HelloActivity),
Name="my.StartupApp")]
#endif
public class OnCreateApp : Application {
public OnCreateApp (IntPtr handle, JniHandleOwnership transfer)
: base (handle, transfer)
{
}
public override void OnCreate ()
{
Log.Info ("*jonp*", "OnCreateApp.OnCreate");
base.OnCreate ();
}
}
public class OnLowMemoryApp : Application {
public override void OnLowMemory ()
{
Log.Info ("*jonp*", "OnLowMemoryApp.OnLowMemory");
base.OnLowMemory ();
}
}
public abstract class Database
{
string FileName {get; set;}
public Database (string fileName)
{
FileName = fileName;
}
SqliteConnection GetConnection ()
{
var dbPath = Path.Combine (
Environment.GetFolderPath (Environment.SpecialFolder.Personal),
FileName);
bool exists = File.Exists (dbPath);
if (!exists) {
SqliteConnection.CreateFile (dbPath);
}
var conn = new SqliteConnection ("Data Source=" + dbPath);
if (!exists)
OnInitDb (conn);
return conn;
}
protected abstract void OnInitDb (SqliteConnection connection);
public void WithConnection (Action<SqliteConnection> action)
{
var connection = GetConnection ();
try {
connection.Open ();
action (connection);
}
finally {
connection.Close ();
}
}
public void WithCommand (Action<SqliteCommand> command)
{
WithConnection (conn => {
using (var cmd = conn.CreateCommand ())
command (cmd);
});
}
}
public class ItemsDb : Database {
public static readonly ItemsDb Instance;
static ItemsDb ()
{
if ((int)Android.OS.Build.VERSION.SdkInt < 8) {
return;
}
Instance = new ItemsDb ();
}
public ItemsDb ()
: this ("items.db3")
{
}
public ItemsDb (string filename)
: base (filename)
{
}
protected override void OnInitDb (SqliteConnection conn)
{
var commands = new[]{
"CREATE TABLE ITEMS (Key ntext, Value ntext);",
"INSERT INTO [Items] ([Key], [Value]) VALUES ('sample', 'text')",
};
foreach (var cmd in commands)
WithCommand (c => {
c.CommandText = cmd;
c.ExecuteNonQuery ();
});
}
}
public class MyView1 : View {
public MyView1 (Context c)
: base (c)
{
}
// Does _not_ override View.Left (which is `final`); should not elicit a
// compile error
public new virtual View Left {
get {return null;}
}
}
public class MyView2 : MyView1 {
public MyView2 (Context c)
: base (c)
{
}
// Does _not_ override View.Left (which is `final`); should not elicit a
// compile error
public override View Left {
get {return null;}
}
}
#if __ANDROID_7__
class CubeWallpaper : WallpaperService {
public override WallpaperService.Engine OnCreateEngine ()
{
return new CubeEngine (this);
}
class CubeEngine : WallpaperService.Engine {
public CubeEngine (CubeWallpaper s)
: base (s)
{
}
}
}
#endif
[Activity (Name="mono.samples.HelloApp",
Label="MfA Sanity Tests",
MainLauncher=true,
ConfigurationChanges=ConfigChanges.Mcc|ConfigChanges.Mnc,
LaunchMode=LaunchMode.Multiple,
ScreenOrientation=ScreenOrientation.Unspecified,
WindowSoftInputMode=SoftInput.StateUnspecified|SoftInput.AdjustUnspecified,
Theme=null)]
[IntentFilter (new[]{"my.custom.action"},
Categories = new[]{"my.custom.category"})]
[MetaData ("foo", Value="bar")]
public class HelloActivity : LibraryActivity
{
[DllImport ("foo")]
static extern void GoMonodroidGo ();
TextView textview;
public HelloActivity ()
{
}
IB bar;
protected override void OnCreate (Android.OS.Bundle bundle)
{
#if MONODROID_TIMING
Log.Info ("MonoDroid-Timing", "HelloActivity.OnCreate: time: {0}", (DateTime.Now - new DateTime (1970, 1, 1)).TotalMilliseconds);
#endif
base.OnCreate (bundle);
Console.WriteLine ("this is my\nstdout\nmessage! yay!");
Console.Error.WriteLine ("this is my\nstderr\nmessage! yay!");
#if __ANDROID_11__
if (((int)Android.OS.Build.VERSION.SdkInt) >= 11) {
// Android 11+ _really_ doesn't want you to do networking from the main thread:
// http://developer.android.com/reference/android/os/NetworkOnMainThreadException.html
// I want to do so, so...
var policy = new Android.OS.StrictMode.ThreadPolicy.Builder ()
.PermitAll()
.Build ();
Android.OS.StrictMode.SetThreadPolicy (policy);
}
#endif // __ANDROID_11__
if (typeof (OnCreateApp) != ApplicationContext.GetType ())
throw new InvalidOperationException ("Wrong Application type created!");
if (typeof (OnCreateApp) != Application.Context.GetType ())
throw new InvalidOperationException ("Wrong Application.Context type created!");
if (!object.ReferenceEquals (Application.Context, ApplicationContext))
throw new InvalidOperationException ("Wrong Application.Context instance created!");
var ignore = new HelloActivity ();
var ignore2 = new CompanyAdapter (this, 0, new string [0]);
RequestWindowFeature (WindowFeatures.NoTitle);
StartService (new Intent (this, typeof (MyService)));
textview = new TextView (this);
textview.Text = "Hello MonoDroid. Mono loves you.\n\nEmbedded\u0000Nulls";
#region BNC_635129
textview.Tag = "2";
if (Convert.ToString (textview.Tag) != "2")
throw new InvalidOperationException ("Tag(\"2\") != \"2\"!");
#endregion
TestDllImport (textview);
#region BNC_679599
bar = new B<A> ();
bar.Do ();
#endregion
var tempfile = Path.GetTempFileName ();
textview.Text += "\n\nPath.GetTempFileName()=" + tempfile;
File.Delete (tempfile);
int[] style = global::Mono.Samples.SanityTests.Resource.Styleable.NavBar;
textview.Text += "\n\nEncoding.GetEncoding(1252).EncodingName=" +
Encoding.GetEncoding (1252).EncodingName;
TestGzip ();
TestArrays ();
TestTypeConversion ();
TestJniInvocation (textview);
TestManualOverrides ();
TestBnc631336 ();
TestBnc632470 ();
TestBnc654527(textview);
var db = ItemsDb.Instance;
TestSqlite (textview);
TestProxyInstantiation ();
TestHttps (textview);
TestExceptions (textview);
TestJavaManagedJavaManagedInvocation ();
TestInputStream (textview);
TestOutputStream (textview);
TestStreamInvokers (textview);
TestNonJavaObjectGenerics (textview);
TestUrlConnectionStream (textview);
TestConvert (textview);
TestTimeZoneInfo (textview);
TestAssets (textview);
TestUdpSocket (textview);
TestNonStaticNestedType (textview);
TestFinalization (textview);
#if MAPS
TestMaps (textview);
#endif
TestNumerics (textview);
TestManagedToJniLookup_Release (textview);
TestJsonDeserializationCreatesJavaHandle (textview);
TestJsonBxc163 ();
TestJsonNullableDateTime ();
TestJsonArray ();
TestMyPaintColor (textview);
TestMyIntent (textview);
var die = new TextView (this) {
Text = "Added!",
};
WindowManager.AddView (die, new WindowManagerLayoutParams ());
WindowManager.RemoveView (die);
var scrollView = new ScrollView (this);
bool childViewAdded = false;
EventHandler<ViewGroup.ChildViewAddedEventArgs> c = (o, e) => {
Console.WriteLine ("ScrollView.ChildViewAdded: Parent={0}; Child={1}", e.Parent, e.Child);
childViewAdded = true;
};
EventHandler<ViewGroup.ChildViewRemovedEventArgs> r = (o, e) => {
Console.WriteLine ("ScrollView.ChildViewRemoved: Parent={0}; Child={1}", e.Parent, e.Child);
};
scrollView.ChildViewAdded += c;
scrollView.ChildViewRemoved += r;
scrollView.AddView (textview);
if (!childViewAdded)
throw new InvalidOperationException ("ScrollView.ChildViewAdded event not invoked!");
scrollView.ChildViewAdded -= c;
scrollView.ChildViewRemoved -= r;
SetContentView (scrollView);
textview.Touch += OnTouch;
ThreadPool.QueueUserWorkItem (o => {
Log.Info ("*jonp*", "Hello from the thread pool thread!");
RunOnUiThread (() => textview.Text += "\n\nThreadPool update!\n");
});
ThreadPool.QueueUserWorkItem (o => UseLotsOfMemory (textview));
GC.Collect ();
}
protected override void OnResume ()
{
base.OnResume ();
}
void TestDllImport (TextView textview)
{
GoMonodroidGo ();
}
#if MAPS
void TestMaps (TextView textview)
{
var p = new Android.GoogleMaps.GeoPoint (35, 100);
textview.Text += "\n\nnew Android.GoogleMaps.GeoPoint (35, 100)=" + p;
}
#endif
void TestNumerics (TextView textview)
{
var n = System.Numerics.Complex.Abs (new System.Numerics.Complex (1, 2));
textview.Text += "\n\nSystem.Numerics.Complex.Abs (new System.Numerics.Complex (1, 2))=" + n;
}
void TestManagedToJniLookup_Release (TextView textview)
{
textview.SetFilters (new Android.Text.IInputFilter[]{new Android.Text.InputFilterAllCaps ()});
textview.SetFilters (new Android.Text.IInputFilter [0]);
}
void TestJsonDeserializationCreatesJavaHandle (TextView textview)
{
Person p = new Person () {
Name = "John Smith",
Age = 900,
};
var stream = new MemoryStream ();
var serializer = new DataContractJsonSerializer (typeof (Person));
serializer.WriteObject (stream, p);
stream.Position = 0;
StreamReader sr = new StreamReader (stream);
textview.Text += "\n\nJSON Person representation: " + sr.ReadToEnd ();
stream.Position = 0;
Person p2 = (Person) serializer.ReadObject (stream);
if (p2.Name != "John Smith")
throw new InvalidOperationException ("JSON deserialization of Name");
if (p2.Age != 900)
throw new InvalidOperationException ("JSON deserialization of Age");
if (p2.Handle == IntPtr.Zero)
throw new InvalidOperationException ("Failed to instanatiage new Java instance for Person!");
}
[DataContract(Name = "DateTest")]
public class DateTest
{
[DataMember(Name = "should_have_value")]
public DateTime? ShouldHaveValue { get; set; }
}
void TestJsonBxc163 ()
{
string json = @"{""should_have_value"":""\/Date(1277355600000-0500)\/""}";
byte[] bytes = System.Text.Encoding.UTF8.GetBytes (json);
Stream inputStream = new MemoryStream (bytes);
DataContractJsonSerializer serializer = new DataContractJsonSerializer (typeof (DateTest));
DateTest t = (DateTest) serializer.ReadObject (inputStream);
if (!t.ShouldHaveValue.HasValue || t.ShouldHaveValue != new DateTime (2010, 6, 24))
throw new InvalidOperationException ("Invalid `DateTime?` value! expected 2010/06/24, got: " + t.ShouldHaveValue);
}
void TestJsonNullableDateTime ()
{
using (MemoryStream ms = new MemoryStream ())
{
DateTest foo = new DateTest ();
DataContractJsonSerializer test = new DataContractJsonSerializer (typeof (DateTest));
test.WriteObject (ms, foo);
ms.Seek (0, 0);
Console.WriteLine ("# JSON: {0}", new StreamReader (ms).ReadToEnd ());
ms.Seek (0, 0);
foo = (DateTest) test.ReadObject (ms);
Console.WriteLine ("foo.Bar={0}", foo.ShouldHaveValue);
if (foo.ShouldHaveValue.HasValue)
throw new InvalidOperationException ("foo.ShouldHaveValue should be null!");
}
}
void TestJsonArray ()
{
string json = "[\"Test1\",\"Test2\"]"; //Some dull json array
using (var ms = new MemoryStream (Encoding.Unicode.GetBytes (json)))
{
DataContractJsonSerializer serializer = new DataContractJsonSerializer (typeof (string[]));
var item = serializer.ReadObject (ms);
Console.WriteLine ("JSON string[]: {0}", item.GetType ().FullName);
var items = (string[]) item;
if (items [0] != "Test1" || items [1] != "Test2")
throw new InvalidOperationException ("JSON array didn't deserialize! Expected {\"Test1\", \"Test2\"}, got: {\"" + string.Join ("\", \"", items) + "\"}");
}
}
void TestGzip ()
{
const string expected = "Hello, compressed world!";
var o = new MemoryStream ();
using (var gzip = new StreamWriter (new GZipStream (o, CompressionMode.Compress)))
gzip.WriteLine (expected);
o = new MemoryStream (o.ToArray ());
o.Position = 0;
Log.Debug ("HelloApp", "gzip compressed data: {0}",
string.Join (" ", o.ToArray ().Select (b => b.ToString ("x2"))));
string result;
using (var gzip = new StreamReader (new GZipStream (o, CompressionMode.Decompress)))
result = gzip.ReadLine ();
if (result != expected)
throw new InvalidOperationException ("GZip test failed! Got '" + result + "'; expected '" + expected + "'.");
}
void TestArrays ()
{
using (var byteArray = new Java.Lang.Object (JNIEnv.NewArray (new byte[]{1,2,3}), JniHandleOwnership.TransferLocalRef)) {
var copy = new byte [3];
JNIEnv.CopyArray (byteArray.Handle, copy, typeof (byte));
if (copy [0] != 1 && copy [1] != 2 && copy [2] != 3)
throw new InvalidOperationException ("CopyArray (IntPtr, Array, Type) byte[] contents not copied!");
copy = new byte [3];
JNIEnv.CopyArray<byte> (byteArray.Handle, copy);
if (copy [0] != 1 && copy [1] != 2 && copy [2] != 3)
throw new InvalidOperationException ("CopyArray<byte> byte[] contents not copied!");
JNIEnv.SetArrayItem (byteArray.Handle, 1, (byte) 42);
copy = new byte [3];
JNIEnv.CopyArray (byteArray.Handle, (Array) copy);
if (copy [0] != 1 && copy [1] != 42 && copy [2] != 3)
throw new InvalidOperationException ("CopyArray(IntPtr, Array) byte[] contents not copied!");
copy = new byte[]{ 4, 5, 6 };
JNIEnv.CopyArray (copy, byteArray.Handle);
copy = JNIEnv.GetArray<byte>(byteArray.Handle);
if (copy [0] != 4 && copy [1] != 5 && copy [2] != 6)
throw new InvalidOperationException ("CopyArray(IntPtr, Array) byte[] contents not copied!");
copy = new byte[]{ 7, 8, 9 };
JNIEnv.CopyArray<byte> (copy, byteArray.Handle);
copy = JNIEnv.GetArray<byte>(byteArray.Handle);
if (copy [0] != 7 && copy [1] != 7 && copy [2] != 6)
throw new InvalidOperationException ("CopyArray(IntPtr, Array) byte[] contents not copied!");
}
using (var byteArray = new Java.Lang.Object (JNIEnv.NewArray (new byte[]{1,2,3}), JniHandleOwnership.TransferLocalRef)) {
byte[] data = JNIEnv.GetArray<byte>(byteArray.Handle);
if (data [0] != 1 && data [1] != 2 && data [2] != 3)
throw new InvalidOperationException ("GetArray<byte> byte[] contents not copied!");
object[] data2 = JNIEnv.GetObjectArray (byteArray.Handle, new[]{typeof (byte), typeof (byte), typeof (byte)});
if (((byte) data2 [0]) != 1 && ((byte) data2 [1]) != 2 && ((byte) data2 [2]) != 3)
throw new InvalidOperationException ("GetObjectArray byte[] contents not copied!");
}
var states = new []{
new[]{1, 2, 3},
new[]{4, 5, 6},
};
var colors = new[]{7, 8};
var list = new Android.Content.Res.ColorStateList (states, colors);
if (list.GetColorForState (states [0], Color.Transparent) != 7)
throw new InvalidOperationException ("list.GetColorForState(states [0]) != 7");
if (list.GetColorForState (states [1], Color.Transparent) != 8)
throw new InvalidOperationException ("list.GetColorForState(states [0]) != 7");
using (var stringArray = new Java.Lang.Object (JNIEnv.NewArray (new[]{new[]{"a", "b"}, new[]{"c", "d"}}), JniHandleOwnership.TransferLocalRef)) {
string[][] values = JNIEnv.GetArray<string[]>(stringArray.Handle);
if (values [0][0] != "a" && values [0][1] != "b" && values [1][0] != "c" && values [1][1] != "d")
throw new InvalidOperationException ("GetArray<string[]> contents not copied!");
values = new[]{new string [2], new string [2]};
JNIEnv.CopyArray (stringArray.Handle, values);
if (values [0][0] != "a" && values [0][1] != "b" && values [1][0] != "c" && values [1][1] != "d")
throw new InvalidOperationException ("GetArray<string[]> contents not copied!");
}
using (var objectArray = new Java.Lang.Object (JNIEnv.NewArray (
new Java.Lang.Object[]{this, 42L, "string"}, typeof (Java.Lang.Object)), JniHandleOwnership.TransferLocalRef)) {
object[] values = JNIEnv.GetObjectArray (objectArray.Handle, new[]{typeof(Context), typeof (int)});
if (values.Length != 3)
throw new InvalidOperationException ("GetObjectArray returned " + values.Length + " items!");
if (!object.ReferenceEquals (values [0], this))
throw new InvalidOperationException ("GetObjectArray wrong values[0]! " + values [0] +
" [" + values [0].GetType ().FullName + "]" + " Handle=" + ((IJavaObject) values [0]).Handle +
"; this.Handle=" + this.Handle +
" ReferenceEquals=" + object.ReferenceEquals (values [0], this));
if (!(values [1] is int))
throw new InvalidOperationException (string.Format ("GetObjectArray wrong values[1]! Got {0} [{1}].",
values [1], values [1] == null ? "null" : values [1].GetType ().FullName));
if (42 != (int) values [1])
throw new InvalidOperationException (string.Format ("GetObjectArray wrong values[1]! Got {0} [{1}].",
values [1], values [1] == null ? "null" : values [1].GetType ().FullName));
if ("string" != values [2].ToString ())
throw new InvalidOperationException ("GetObjectArray wrong values[2]!");
}
using (var enumArray = new Java.Lang.Object (JNIEnv.NewArray (new[]{Keycode.A}), JniHandleOwnership.TransferLocalRef)) {
var copy = JNIEnv.GetArray<Keycode>(enumArray.Handle);
if (copy == null || copy.Length != 1 || copy [0] != Keycode.A)
throw new InvalidOperationException ("GetArray<Keycode>() failed!");
}
}
void TestTypeConversion ()
{
// Sanity checking to ensure that our binding stays correct.
var v = ServerSocketFactory.Default;
Log.Info ("HelloApp", "ServerSocketFactory.Default={0}", v);
var egl = EGLContext.EGL;
Log.Info ("HelloApp", "EGLContext.EGL={0}", egl);
IEGL10 egl10 = egl.JavaCast<IEGL10>();
Log.Info ("HelloApp", "(IEGL10) EGLContext.EGL={0}", egl10.GetType().FullName);
IntPtr lref = JNIEnv.NewString ("Hello, world!");
using (Java.Lang.Object s = Java.Lang.Object.GetObject<Java.Lang.Object>(lref, JniHandleOwnership.TransferLocalRef)) {
if (s.GetType () != typeof (Java.Lang.String))
throw new InvalidOperationException ("Object.GetObject<T>() needs to return a Java.Lang.String!");
}
lref = JNIEnv.CreateInstance ("android/gesture/Gesture", "()V");
using (Java.Lang.Object g = Java.Lang.Object.GetObject<Java.Lang.Object>(lref, JniHandleOwnership.TransferLocalRef))
if (g.GetType () != typeof (Android.Gestures.Gesture))
throw new InvalidOperationException ("Object.GetObject<T>() needs to return a Android.Gestures.Gesture!");
}
void TestJniInvocation (TextView textview)
{
IntPtr Adder = JNIEnv.FindClass ("mono/android/test/Adder");
if (Adder == IntPtr.Zero)
throw new InvalidOperationException ("Couldn't find mono.android.test.Adder");
IntPtr Adder_ctor = JNIEnv.GetMethodID (Adder, "<init>", "()V");
if (Adder_ctor == IntPtr.Zero)
throw new InvalidOperationException ("Couldn't find mono.android.test.Adder.#ctor()");
IntPtr Adder_add = JNIEnv.GetMethodID (Adder, "add", "(II)I");
if (Adder_add == IntPtr.Zero)
throw new InvalidOperationException ("Couldn't find mono.android.test.Adder.add(int,int)");
IntPtr instance = JNIEnv.NewObject (Adder, Adder_ctor);
int result = JNIEnv.CallIntMethod (instance, Adder_add, new JValue (2), new JValue (3));
textview.Text += "\n\nnew Adder().add(2,3)=" + result;
var boundAdder = new Adder (instance, JniHandleOwnership.DoNotTransfer);
if (boundAdder.Add (3, 4) != 7)
throw new InvalidOperationException ("Add(3,4) != 7!");
JNIEnv.DeleteLocalRef (instance);
}
void TestManualOverrides ()
{
var adder = new Adder ();
Console.WriteLine ("Adder Class: {0}", adder.Class);
var managedAdder = new ManagedAdder ();
int result = Adder.Add (managedAdder, 3, 4);
if (result != 14)
throw new InvalidOperationException ("ManagedAdder.Add(3, 4) != 14!");
var progress = new AdderProgress ();
int sum = Adder.Sum (adder, progress, 1, 2, 3);
if (sum != 6)
throw new InvalidOperationException ("Adder.Sum(adder, 1, 2, 3) != 6! Was: " + sum + ".");
if (progress.AddInvocations != 3)
throw new InvalidOperationException ("Adder.Sum(adder, 1, 2, 3) didn't invoke progress 3 times! Was: " + progress.AddInvocations + ".");
progress.AddInvocations = 0;
sum = Adder.Sum (managedAdder, progress, 6, 7);
if (sum != 38)
throw new InvalidOperationException ("Adder.Sum(managedAdder, 6, 7) != 38! Was: " + sum + ".");
if (progress.AddInvocations != 2)
throw new InvalidOperationException ("Adder.Sum(adder, 6, 7) didn't invoke progress 2 times! Was: " + progress.AddInvocations + ".");
IntPtr javaDefaultProgress = JNIEnv.CreateInstance ("mono/android/test/Adder$DefaultProgress", "()V");
var progress2 = Java.Lang.Object.GetObject<IAdderProgress>(javaDefaultProgress, JniHandleOwnership.TransferLocalRef);
Console.WriteLine ("progress2 MCW: {0}", progress2.GetType ().FullName);
javaDefaultProgress = JNIEnv.CreateInstance ("mono/android/test/Adder$DefaultProgress", "()V");
var progress3 = new Java.Lang.Object (javaDefaultProgress, JniHandleOwnership.TransferLocalRef)
.JavaCast<IAdderProgress> ();
Console.WriteLine ("progress3 MCW: {0}", progress3.GetType ().FullName);
}
void TestBnc631336 ()
{
var pm = PackageManager;
Intent mainIntent = new Intent (Intent.ActionMain, null);
mainIntent.AddCategory (Intent.ActionDefault);
var list = pm.QueryIntentActivities (mainIntent, 0);
}
void TestBnc654527 (TextView textview)
{
var dict = new DatabaseVersionMap ();
var map = dict.JavaCast<Java.Util.IMap>();
map.Put (new Java.Lang.Integer (42), new JavaList<string> { "Foo" });
textview.Text += "\n\nBNC_654527: dict[42][0]=" + dict [42][0];
}
void TestSqlite (TextView textview)
{
if (ItemsDb.Instance == null) {
textview.Text += "\n\nSQLite is not supported on this platform.";
return;
}
textview.Text += "\n\nSQLite DB contents:";
ItemsDb.Instance.WithCommand (c => {
c.CommandText = "SELECT [Key], [Value] FROM [Items]";
var r = c.ExecuteReader ();
while (r.Read()) {
textview.Text += string.Format ("\n\tKey={0}; Value={1}",
r ["Key"].ToString (), r ["Value"].ToString ());
}
});
}
void TestProxyInstantiation ()
{
var a = new MyCustomGenericType<char> ();
var b = new MyCustomGenericType<char, int> ();
var c = new MyCustomGenericType<char, int, long> ();
var d = new OrderAdapter (this, 0, new[]{"hi"});
var e = new StringArrayAdapter (this, 0);
}
void TestHttps (TextView textview)
{
try {
string url = "https://encrypted.google.com/";
var request = (HttpWebRequest) WebRequest.Create(url);
request.Method = "GET";
var response = (HttpWebResponse) request.GetResponse ();
int len = 0;
using (var _r = new StreamReader (response.GetResponseStream ())) {
char[] buf = new char [4096];
int n;
while ((n = _r.Read (buf, 0, buf.Length)) > 0) {
/* ignore; we just want to make sure we can read */
len += n;
}
}
textview.Text += "\n\nHTTPS test succeeded! Read " + len + " bytes.";
}
catch (Exception e) {
Log.Info ("*jonp*", "https exception! {0}", e.ToString ());
textview.Text += "\n\nHTTPS test failed: " + e.Message;
}
}
void TestExceptions (TextView textview)
{
#if CATCH
try {
#endif
#if __ANDROID_7__
if (((int) Android.OS.Build.VERSION.SdkInt) < 7)
return;
Log.Info ("HelloApp", "calling Drawable.SetAlpha...");
WallpaperManager manager = WallpaperManager.GetInstance (this);
var drawable = manager.FastDrawable;
Log.Info ("HelloApp", "drawable? {0}", (drawable != null));
drawable.SetAlpha (0);
Log.Info ("HelloApp", "after Drawable.SetAlpha?!; should not be reached...");
#endif
#if CATCH
}
catch (Exception e) {
Log.Info ("HelloApp", "Yay, exception caught!" + e);
textview.Text += "\n\nEXPECTED EXCEPTION:\n" + e.ToString ();
}
#endif
}
void TestJavaManagedJavaManagedInvocation ()
{
var menu = new MyMenu ();
bool b = OnPreparePanel ((int) WindowFeatures.OptionsPanel, null, menu);
// b is false because it returns menu.HasVisibleItems
if (!b && !menu.HasVisibleItemsWasInvoked)
throw new InvalidOperationException ("MyMenu.HasVisibleItems was not invoked!");
}
void TestBnc632470 ()
{
MyView mv = new MyView (this);
}
void TestInputStream (TextView textview)
{
InputStreamAdapter jis = new InputStreamAdapter (new System.IO.MemoryStream (new byte[] { 0, 1, 2, 3, 4, 5, 6, 7 }));
byte[] buffer = new byte[10];
int cnt = jis.Read (buffer);
bool success = cnt == 8;
for (int i = 0; i < cnt; i++)
if (buffer[i] != (byte)i)
success = false;
textview.Text += "\n\nTestInputStream round trip success: " + success;
}
void TestOutputStream (TextView textview)
{
bool success = true;
MemoryStream ms = new MemoryStream ();
OutputStreamAdapter osa = new OutputStreamAdapter (ms);
osa.Write (60);
osa.Write (65);
osa.Write (70);
ms.Position = 0;
if (ms.ReadByte () != 60 || ms.ReadByte () != 65 || ms.ReadByte () != 70)
success = false;
ms.Close ();
osa.Close ();
textview.Text += "\n\nOutputStreamAdapter round trip success: " + success;
}
void TestStreamInvokers (TextView textview)
{
bool success = true;
var test_string = "hello";
Stream osa = OpenFileOutput ("test.txt", FileCreationMode.Private);
var write_bytes = System.Text.Encoding.ASCII.GetBytes (test_string);
osa.Write (write_bytes, 0, write_bytes.Length);
osa.Close ();
Stream isa = OpenFileInput ("test.txt");
var read_bytes = new byte[5];
int count = isa.Read (read_bytes, 0, read_bytes.Length);
if (count != test_string.Length)
success = false;
if (System.Text.Encoding.ASCII.GetString (read_bytes) != test_string)
success = false;
textview.Text += "\n\nStreamInvoker round trip success: " + success;
}
class NonJavaObject
{
public override string ToString ()
{
return "This object is Sooooo not on java.";
}
}
void TestNonJavaObjectGenerics (TextView textview)
{
var adapter = new Android.Widget.ArrayAdapter<NonJavaObject> (this, textview.Id);
var item = new NonJavaObject ();
adapter.Add (item);
textview.Text += "\n\n" + adapter.GetItem (0).ToString ();
var coll = new JavaDictionary<NonJavaObject, NonJavaObject> ();
coll.Add (item, item);
if (!object.ReferenceEquals (item, coll [item]))
throw new InvalidOperationException ("Unable to lookup non-java.lang.Object item in JavaDictionary!");
Log.Info ("*jonp*", "A1");
var jl1 = new Android.Runtime.JavaList<object>();
Log.Info ("*jonp*", "A2");
var v1 = new Dictionary<string, object>();
Log.Info ("*jonp*", "A3");
v1.Add ("szLongName", "Bla.pdf");
Log.Info ("*jonp*", "A4");
jl1.Add (v1);
Log.Info ("*jonp*", "A5");
var vo1 = jl1 [0];
Log.Info ("*jonp*", "A6={0}", vo1);
if (!object.ReferenceEquals (vo1, v1))
throw new InvalidOperationException ("Dict through JavaList isn't preserved!");
var jl2 = new Android.Runtime.JavaList ();
Log.Info ("*jonp*", "B2");
var v2 = new Dictionary<string, object>();
Log.Info ("*jonp*", "B3");
v2.Add ("szLongName", "Bla.pdf");
Log.Info ("*jonp*", "B4");
jl2.Add (v2);
Log.Info ("*jonp*", "B5");
var _vo2 = jl2 [0]; // Exception
Log.Info ("*jonp*", "B6={0} [{1}]", _vo2, _vo2 != null ? _vo2.GetType ().FullName : "<null>");
if (!object.ReferenceEquals (_vo2, v2))
throw new InvalidOperationException ("Dict through JavaList isn't preserved (2)!");
var vo2 = (Dictionary<string, object>) jl2 [0]; // Exception
Log.Info ("*jonp*", "B7={0}", vo2);
if (!object.ReferenceEquals (vo2, v2))
throw new InvalidOperationException ("Dict through JavaList isn't preserved (3)!");
}
void TestUrlConnectionStream (TextView textview)
{
try {
Java.Net.URL url = new Java.Net.URL("http://www.google.pt/logos/classicplus.png");
Java.Net.URLConnection urlConnection = url.OpenConnection();
Android.Graphics.BitmapFactory.DecodeStream (urlConnection.InputStream);
textview.Text += "\n\nOpened Url connection stream and loaded image";
} catch (Java.Net.UnknownHostException ex) {
textview.Text += "\n\nConnection error: " + ex.Message;
}
}
static void AssertEqual<T>(T expected, T actual)
where T : IEquatable<T>
{
if (!expected.Equals (actual))
throw new InvalidOperationException ("Expected " + expected + "; actual " + actual);
}
void TestConvert (TextView textview)
{
AssertEqual (true, Convert.ToBoolean (Java.Lang.Boolean.ValueOf (true)));
AssertEqual (1L, Convert.ToInt32 (Java.Lang.Long.ValueOf (1L)));
AssertEqual (2, Convert.ToInt32 (Java.Lang.Integer.ValueOf (2)));
AssertEqual (3.0, Convert.ToDouble (Java.Lang.Double.ValueOf (3.0)));
AssertEqual (4.0f, Convert.ToSingle (Java.Lang.Float.ValueOf (4.0f)));
AssertEqual ('d', Convert.ToChar (Java.Lang.Character.ValueOf ('d')));
}
void TestTimeZoneInfo (TextView textview)
{
textview.Text += "\n\nLocal Time Zone: " + TimeZoneInfo.Local.ToString ();
textview.Text += "\n\nUTC Time Zone: " + TimeZoneInfo.Utc.ToString ();
Action<TimeZoneInfo> p = tz => {
textview.Text += string.Format ("\n\nDisplayName: {0}:\n\tStandardName: {1}\n\tDaylightName: {2}\n\tId: {3}\n\tBaseUtfOffset: {4}\n\tSupportsDaylightSavingTime={5}",
tz.DisplayName, tz.StandardName, tz.DaylightName, tz.Id, tz.BaseUtcOffset, tz.SupportsDaylightSavingTime);
};
p (TimeZoneInfo.Local);
p (TimeZoneInfo.Utc);
p (TimeZoneInfo.FindSystemTimeZoneById ("GMT-5"));
}
void TestAssets (TextView textiew)
{
Action<string> comp = actual => {
var expected =
"Hello out there in TV land!\n" +
"Hello out there in TV land!\n" +
"Hello out there in TV land!\n" +
"Hello out there in TV land!\n" +
"Hello out there in TV land!\n";
if (string.CompareOrdinal (actual, expected) != 0)
throw new InvalidOperationException (
string.Format ("Invalid asset contents for Assets/Hello.txt! Expected: '{0}'; Actual: '{1}'", expected, actual));
};
using (var r = new StreamReader (Assets.Open ("Hello.txt"))) {
comp (r.ReadToEnd ());
}
using (var r = new StreamReader (Assets.Open ("Subdir/HelloWorld.txt"))) {
comp (r.ReadToEnd ());
}
}
void TestUdpSocket (TextView textview)
{
int Port = 9595;
var server = new UdpClient(Port);
server.BeginReceive(result => {
IPEndPoint sender = null;
var data = server.EndReceive(result, ref sender);
var value = Encoding.Unicode.GetString (data);
if (value != "hello there!")
throw new InvalidOperationException ("UDP data transfer failed!");
RunOnUiThread (() => textview.Text += "\n\nRead data from UDP: " + value);
server.Close ();
}, null);
using (var client = new UdpClient()) {