-
Notifications
You must be signed in to change notification settings - Fork 564
[Xamarin.Android.Build.Tests] Add support for testing the UI #2855
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
fb81f6a
[Xamarin.Android.Build.Tests] Add support for testing the UI
dellis1972 b73e115
Removed redundant Install in PackagingTest
dellis1972 da662e0
$(ManagedRuntime) use should include $(ManagedRuntimeArgs)
jonpryor 85886e5
Include branch information
jonpryor bd00aa5
Fix indentation
jonpryor 2b1938d
Fix indentation
jonpryor File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Submodule debugger-libs
added at
b45303
Submodule nrefactory
added at
0607a4
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
146 changes: 146 additions & 0 deletions
146
src/Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/Utilities/DeviceTest.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,146 @@ | ||
| using NUnit.Framework; | ||
| using System; | ||
| using System.Collections; | ||
| using System.Collections.Generic; | ||
| using System.Diagnostics; | ||
| using System.IO; | ||
| using System.Linq; | ||
| using System.Security.Cryptography; | ||
| using System.Text; | ||
| using System.Text.RegularExpressions; | ||
| using System.Threading; | ||
| using System.Xml; | ||
| using System.Xml.Linq; | ||
| using System.Xml.XPath; | ||
| using Xamarin.ProjectTools; | ||
| using XABuildPaths = Xamarin.Android.Build.Paths; | ||
|
|
||
| namespace Xamarin.Android.Build.Tests | ||
| { | ||
| public class DeviceTest: BaseTest | ||
| { | ||
| protected static void RunAdbInput (string command, params object [] args) | ||
| { | ||
| RunAdbCommand ($"shell {command} {string.Join (" ", args)}"); | ||
| } | ||
|
|
||
| protected static string ClearAdbLogcat () | ||
| { | ||
| return RunAdbCommand ("logcat -c"); | ||
| } | ||
|
|
||
| protected static string ClearDebugProperty () | ||
| { | ||
| return RunAdbCommand ("shell setprop debug.mono.extra \"\""); | ||
| } | ||
|
|
||
| protected static void AdbStartActivity (string activity) | ||
| { | ||
| RunAdbCommand ($"shell am start -S -n \"{activity}\""); | ||
| } | ||
|
|
||
| protected void WaitFor (TimeSpan timeSpan, Func<bool> func) | ||
| { | ||
| var pause = new ManualResetEvent (false); | ||
| TimeSpan total = timeSpan; | ||
| TimeSpan interval = TimeSpan.FromMilliseconds (10); | ||
| while (total.TotalMilliseconds > 0) { | ||
| pause.WaitOne (interval); | ||
| total = total.Subtract (interval); | ||
| if (func ()) { | ||
| break; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| protected static bool MonitorAdbLogcat (Func<string, bool> action, int timeout = 10) | ||
| { | ||
| string ext = Environment.OSVersion.Platform != PlatformID.Unix ? ".exe" : ""; | ||
| string adb = Path.Combine (AndroidSdkPath, "platform-tools", "adb" + ext); | ||
| var info = new ProcessStartInfo (adb, "logcat") { | ||
| RedirectStandardOutput = true, | ||
| UseShellExecute = false, | ||
| CreateNoWindow = true, | ||
| WindowStyle = ProcessWindowStyle.Hidden, | ||
| }; | ||
| using (var proc = Process.Start (info)) { | ||
| var sw = new Stopwatch (); | ||
| try { | ||
| TimeSpan time = TimeSpan.FromSeconds (timeout); | ||
| while (time.TotalMilliseconds > 0) { | ||
| sw.Start (); | ||
| if (action (proc.StandardOutput.ReadLine ())) | ||
| return true; | ||
| time = time.Subtract (TimeSpan.FromMilliseconds (sw.ElapsedMilliseconds)); | ||
| sw.Reset (); | ||
| } | ||
| } finally { | ||
| sw.Stop (); | ||
| proc.Kill (); | ||
| proc.WaitForExit (); | ||
| } | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| protected static bool WaitForDebuggerToStart (out string output, int timeout = 60) | ||
| { | ||
| var sb = new StringBuilder (); | ||
| bool result = MonitorAdbLogcat ((line) => { | ||
| sb.AppendLine (line); | ||
| return line.IndexOf ("monodroid-debug: Trying to initialize the debugger with options", StringComparison.OrdinalIgnoreCase) > 0; | ||
| }, timeout: timeout); | ||
| output = sb.ToString (); | ||
| return result; | ||
| } | ||
|
|
||
| protected static bool WaitForActivityToStart (string activityNamespace, string activityName, out string output, int timeout = 60) | ||
| { | ||
| var sb = new StringBuilder (); | ||
| bool result = MonitorAdbLogcat ((line) => { | ||
| sb.AppendLine (line); | ||
| return line.IndexOf ("ActivityManager: Displayed", StringComparison.OrdinalIgnoreCase) > 0 && line.Contains (activityNamespace) && line.Contains (activityName); | ||
| }, timeout: timeout); | ||
| output = sb.ToString (); | ||
| return result; | ||
| } | ||
|
|
||
| protected static (int x, int y, int w, int h) GetControlBounds (string packageName, string uiElement, string text) | ||
| { | ||
| var regex = new Regex (@"[(0-9)]\d*", RegexOptions.Compiled); | ||
| var result = (x: 0, y: 0, w: 0, h: 0); | ||
| var ui = RunAdbCommand ("exec-out uiautomator dump /dev/tty"); | ||
| while (ui.Contains ("ERROR:")) { | ||
| ui = RunAdbCommand ("exec-out uiautomator dump /dev/tty"); | ||
| WaitFor (1); | ||
| } | ||
| ui = ui.Replace ("UI hierchary dumped to: /dev/tty", string.Empty).Trim (); | ||
| try { | ||
| var uiDoc = XDocument.Parse (ui); | ||
| var node = uiDoc.XPathSelectElement ($"//node[contains(@resource-id,'{uiElement}')]"); | ||
| if (node == null) | ||
| node = uiDoc.XPathSelectElement ($"//node[contains(@content-desc,'{uiElement}')]"); | ||
| if (node == null) | ||
| node = uiDoc.XPathSelectElement ($"//node[contains(@text,'{text}')]"); | ||
| if (node == null) | ||
| return result; | ||
| var bounds = node.Attribute ("bounds"); | ||
| var matches = regex.Matches (bounds.Value); | ||
| int.TryParse (matches [0].Value, out int x); | ||
| int.TryParse (matches [1].Value, out int y); | ||
| int.TryParse (matches [2].Value, out int w); | ||
| int.TryParse (matches [3].Value, out int h); | ||
| return (x: x, y: y, w: w, h: h); | ||
| } catch (Exception ex) { | ||
| // Ignore any error and return and empty | ||
| throw new InvalidOperationException ($"uiautomator returned invalid xml {ui}", ex); | ||
| } | ||
| } | ||
|
|
||
| protected static void ClickButton (string packageName, string buttonName, string buttonText) | ||
| { | ||
| var bounds = GetControlBounds (packageName, buttonName, buttonText); | ||
| RunAdbInput ("input tap", bounds.x + ((bounds.w - bounds.x) / 2), bounds.y + ((bounds.h - bounds.y) / 2)); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.