|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.IO; |
| 4 | +using System.Text.RegularExpressions; |
| 5 | +using NUnit.Framework; |
| 6 | +using Xamarin.ProjectTools; |
| 7 | + |
| 8 | +namespace Xamarin.Android.Build.Tests |
| 9 | +{ |
| 10 | + [TestFixture] |
| 11 | + [Category ("UsesDevice")] |
| 12 | + public class UncaughtExceptionTests : DeviceTest |
| 13 | + { |
| 14 | + class LogcatLine |
| 15 | + { |
| 16 | + public string Text; |
| 17 | + public bool Found = false; |
| 18 | + public int SequenceNumber = -1; |
| 19 | + public int Count = 0; |
| 20 | + }; |
| 21 | + |
| 22 | + [Test] |
| 23 | + public void EnsureUncaughtExceptionWorks () |
| 24 | + { |
| 25 | + AssertHasDevices (); |
| 26 | + |
| 27 | + var lib = new XamarinAndroidBindingProject { |
| 28 | + ProjectName = "Scratch.Try", |
| 29 | + AndroidClassParser = "class-parse", |
| 30 | + }; |
| 31 | + |
| 32 | + lib.Imports.Add ( |
| 33 | + new Import (() => "Directory.Build.targets") { |
| 34 | + TextContent = () => |
| 35 | +@"<Project> |
| 36 | + <PropertyGroup> |
| 37 | + <JavacSourceVersion>1.8</JavacSourceVersion> |
| 38 | + <JavacTargetVersion>1.8</JavacTargetVersion> |
| 39 | + <Javac>javac</Javac> |
| 40 | + <Jar>jar</Jar> |
| 41 | + </PropertyGroup> |
| 42 | + <ItemGroup> |
| 43 | + <JavaSource Include=""java\**\*.java"" /> |
| 44 | + <AndroidJavaSource Include=""@(JavaSource)"" /> |
| 45 | + </ItemGroup> |
| 46 | + <ItemGroup> |
| 47 | + <EmbeddedJar Include=""$(OutputPath)try.jar"" /> |
| 48 | + </ItemGroup> |
| 49 | + <Target Name=""_BuildJar"" |
| 50 | + AfterTargets=""ResolveAssemblyReferences"" |
| 51 | + Inputs=""@(JavaSource);$(MSBuildThisFile)"" |
| 52 | + Outputs=""$(OutputPath)try.jar""> |
| 53 | + <PropertyGroup> |
| 54 | + <_Classes>$(IntermediateOutputPath)classes</_Classes> |
| 55 | + </PropertyGroup> |
| 56 | + <RemoveDir Directories=""$(_Classes)""/> |
| 57 | + <MakeDir Directories=""$(_Classes)"" /> |
| 58 | + <Exec Command=""$(Javac) -source $(JavacSourceVersion) -target $(JavacTargetVersion) -d "$(_Classes)" @(JavaSource->'"%(Identity)"', ' ')"" /> |
| 59 | + <Exec Command=""$(Jar) cf "$(OutputPath)try.jar" -C "$(_Classes)" ."" /> |
| 60 | + </Target> |
| 61 | +</Project> |
| 62 | +" |
| 63 | + }); |
| 64 | + |
| 65 | + lib.Sources.Add ( |
| 66 | + new BuildItem.NoActionResource ("java\\testing\\Run.java") { |
| 67 | + Encoding = new System.Text.UTF8Encoding (encoderShouldEmitUTF8Identifier: false), |
| 68 | + TextContent = () => |
| 69 | +@"package testing; |
| 70 | +
|
| 71 | +public final class Run { |
| 72 | + private Run() { |
| 73 | + } |
| 74 | +
|
| 75 | + public static interface CatchThrowableHandler { |
| 76 | + void onCatch(Throwable t); |
| 77 | + } |
| 78 | +
|
| 79 | + public static final void tryCatchFinally (Runnable r, CatchThrowableHandler c, Runnable f) { |
| 80 | + try { |
| 81 | + r.run(); |
| 82 | + } |
| 83 | + catch (Throwable t) { |
| 84 | + c.onCatch(t); |
| 85 | + } |
| 86 | + finally { |
| 87 | + f.run(); |
| 88 | + } |
| 89 | + } |
| 90 | +} |
| 91 | +" |
| 92 | + }); |
| 93 | + |
| 94 | + var app = new XamarinAndroidApplicationProject { |
| 95 | + ProjectName = "Scratch.JMJMException", |
| 96 | + }; |
| 97 | + |
| 98 | + app.SetDefaultTargetDevice (); |
| 99 | + app.AddReference (lib); |
| 100 | + |
| 101 | + app.Sources.Remove (app.GetItem ("MainActivity.cs")); |
| 102 | + |
| 103 | + string mainActivityTemplate = @"using System; |
| 104 | +using Android.App; |
| 105 | +using Android.OS; |
| 106 | +using Android.Runtime; |
| 107 | +using Android.Views; |
| 108 | +using Android.Widget; |
| 109 | +using Testing; |
| 110 | +
|
| 111 | +namespace Scratch.JMJMException |
| 112 | +{ |
| 113 | + [Register (""${JAVA_PACKAGENAME}.MainActivity""), Activity (Label = ""${PROJECT_NAME}"", MainLauncher = true, Icon = ""@drawable/icon"")] |
| 114 | + public class MainActivity : Activity |
| 115 | + { |
| 116 | + protected override void OnCreate (Bundle savedInstanceState) |
| 117 | + { |
| 118 | + base.OnCreate(savedInstanceState); |
| 119 | + Button b = new Button (this) { |
| 120 | + Text = ""Click Me!"", |
| 121 | + }; |
| 122 | +
|
| 123 | + Testing.Run.TryCatchFinally ( |
| 124 | + new Java.Lang.Runnable (() => { |
| 125 | + Console.WriteLine (""#UET-1# jon: Should be in a Java > Managed [MainActivity.OnCreate] > Java [Run.tryCatchFinally] > Managed [Run] frame. Throwing an exception...""); |
| 126 | + Console.WriteLine (new System.Diagnostics.StackTrace(fNeedFileInfo: true).ToString()); |
| 127 | + throw new Exception (""Should be in a Java > Managed [MainActivity.OnCreate] > Java [Run.tryCatchFinally] > Managed [Run] frame. Throwing an exception...""); |
| 128 | + }), |
| 129 | + new MyCatchHandler (), |
| 130 | + new Java.Lang.Runnable (() => { |
| 131 | + Console.WriteLine ($""#UET-3# jon: from Java finally block""); |
| 132 | + }) |
| 133 | + ); |
| 134 | +
|
| 135 | + SetContentView (b); |
| 136 | + } |
| 137 | + } |
| 138 | +
|
| 139 | + class MyCatchHandler : Java.Lang.Object, Run.ICatchThrowableHandler |
| 140 | + { |
| 141 | + public void OnCatch (Java.Lang.Throwable t) |
| 142 | + { |
| 143 | + Console.WriteLine ($""#UET-2# jon: MyCatchHandler.OnCatch: t={t.ToString()}""); |
| 144 | + } |
| 145 | + } |
| 146 | +} |
| 147 | +"; |
| 148 | + string mainActivity = app.ProcessSourceTemplate (mainActivityTemplate); |
| 149 | + app.Sources.Add ( |
| 150 | + new BuildItem.Source ("MainActivity.cs") { |
| 151 | + TextContent = () => mainActivity |
| 152 | + } |
| 153 | + ); |
| 154 | + |
| 155 | + var expectedLogLines = new LogcatLine[] { |
| 156 | + new LogcatLine { Text = "#UET-1#" }, |
| 157 | + new LogcatLine { Text = "#UET-2#" }, |
| 158 | + new LogcatLine { Text = "#UET-3#" }, |
| 159 | + }; |
| 160 | + |
| 161 | + string path = Path.Combine ("temp", TestName); |
| 162 | + using (var libBuilder = CreateDllBuilder (Path.Combine (path, lib.ProjectName))) |
| 163 | + using (var appBuilder = CreateApkBuilder (Path.Combine (path, app.ProjectName))) { |
| 164 | + Assert.True (libBuilder.Build (lib), "Library should have built."); |
| 165 | + Assert.IsTrue (appBuilder.Install (app), "Install should have succeeded."); |
| 166 | + |
| 167 | + ClearAdbLogcat (); |
| 168 | + |
| 169 | + AdbStartActivity ($"{app.PackageName}/{app.JavaPackageName}.MainActivity"); |
| 170 | + |
| 171 | + string logcatPath = Path.Combine (Root, appBuilder.ProjectDirectory, "logcat.log"); |
| 172 | + int sequenceCounter = 0; |
| 173 | + MonitorAdbLogcat ( |
| 174 | + (string line) => { |
| 175 | + foreach (LogcatLine ll in expectedLogLines) { |
| 176 | + if (line.IndexOf (ll.Text, StringComparison.Ordinal) < 0) { |
| 177 | + continue; |
| 178 | + } |
| 179 | + ll.Found = true; |
| 180 | + ll.Count++; |
| 181 | + ll.SequenceNumber = sequenceCounter++; |
| 182 | + break; |
| 183 | + } |
| 184 | + return false; // we must examine all the lines, and returning `true` aborts the monitoring process |
| 185 | + }, logcatPath, 15); |
| 186 | + } |
| 187 | + |
| 188 | + AssertValidLine (0, 0); |
| 189 | + AssertValidLine (1, 1); |
| 190 | + AssertValidLine (2, 2); |
| 191 | + |
| 192 | + void AssertValidLine (int idx, int expectedSequence) |
| 193 | + { |
| 194 | + LogcatLine ll = expectedLogLines [idx]; |
| 195 | + Assert.IsTrue (ll.Found, $"Logcat line {idx} was not found"); |
| 196 | + Assert.IsTrue (ll.Count == 1, $"Logcat line {idx} should have been found only once but it was found {ll.Count} times"); |
| 197 | + Assert.IsTrue (ll.SequenceNumber == expectedSequence, $"Logcat line {idx} sequence number should be {expectedSequence} but it was {ll.SequenceNumber}"); |
| 198 | + } |
| 199 | + } |
| 200 | + } |
| 201 | +} |
0 commit comments