-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* New class (UIFind) to find the closest node(based on another node) that matches a specific with. * Changed so all kind of interaction with the device (tap, swipe, input text, keyevent, start intent) clear the UI cache.
- Loading branch information
Showing
11 changed files
with
175 additions
and
8 deletions.
There are no files selected for viewing
This file contains 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 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 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,67 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using System.Xml.Linq; | ||
using NUnit.Framework; | ||
using Testura.Android.Device.Ui.Nodes.Data; | ||
using Testura.Android.Device.Ui.Search; | ||
using Testura.Android.Util.Helpers; | ||
|
||
namespace Testura.Android.Tests.Util.Helpers | ||
{ | ||
[TestFixture] | ||
class UiFindTests | ||
{ | ||
[Test] | ||
public void FindClosestNode_WhenSearchingForNodeThatExist_ShouldGetNode() | ||
{ | ||
var root = CreateNode(); | ||
var fondNode = UiFind.ClosestNode(With.Text("second_node_1"), root.Children[0]); | ||
Assert.IsNotNull(fondNode); | ||
Assert.AreEqual(root.Children[0].Children[1], fondNode); | ||
} | ||
|
||
[Test] | ||
public void FindClosestNode_WhenComplexSearchingForNodeThatExist_ShouldGetNode() | ||
{ | ||
var root = CreateNode(); | ||
var fondNode = UiFind.ClosestNode(With.Text("third_node_1_1_1"), root.Children[0]); | ||
Assert.IsNotNull(fondNode); | ||
Assert.AreEqual(root.Children[1].Children[1].Children[1], fondNode); | ||
} | ||
|
||
[Test] | ||
public void FindClosestNode_WhenSearchingForNodeThatDontExist_ShouldGetNull() | ||
{ | ||
var root = CreateNode(); | ||
var fondNode = UiFind.ClosestNode(With.Text("second_node_23"), root.Children[0]); | ||
Assert.IsNull(fondNode); | ||
} | ||
|
||
private Node CreateNode() | ||
{ | ||
var rootNode = new Node(new XElement("node", new XAttribute("text", "root")), null); | ||
|
||
for (int n = 0; n < 2; n++) | ||
{ | ||
var secondNode = new Node(new XElement("node", new XAttribute("text", $"node_{n}")), rootNode); | ||
|
||
for (int i = 0; i < 2; i++) | ||
{ | ||
secondNode.Children.Add(new Node(new XElement("node", new XAttribute("text", $"second_node_{i}")), secondNode)); | ||
|
||
for (int d = 0; d < 2; d++) | ||
{ | ||
secondNode.Children[i].Children.Add(new Node(new XElement("node", new XAttribute("text", $"third_node_{n}_{i}_{d}")), secondNode.Children[i])); | ||
} | ||
} | ||
|
||
rootNode.Children.Add(secondNode); | ||
} | ||
|
||
return rootNode; | ||
} | ||
} | ||
} |
This file contains 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 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 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 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 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 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 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 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,80 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using Testura.Android.Device.Ui.Nodes.Data; | ||
using Testura.Android.Device.Ui.Search; | ||
|
||
namespace Testura.Android.Util.Helpers | ||
{ | ||
/// <summary> | ||
/// Provides functionality to find nodes | ||
/// </summary> | ||
public static class UiFind | ||
{ | ||
/// <summary> | ||
/// Find the closest node(based on another node) that matches a specific with | ||
/// </summary> | ||
/// <param name="with">With that the close node should match</param> | ||
/// <param name="startNode">The start node and position where we should start to search</param> | ||
/// <returns>The closest node, null if we can't find a matching node</returns> | ||
/// <example>This example show how to call the method with another node </example> | ||
/// <code> | ||
/// var node = uiObject.Values() | ||
/// UiFind.ClosestNode(With.Text("test"), node) | ||
/// </code> | ||
public static Node ClosestNode(With with, Node startNode) | ||
{ | ||
return FindNode(with.NodeSearch, startNode, new List<Node>(), 0, null)?.Node; | ||
} | ||
|
||
private static FoundNode FindNode(Func<Node, bool> func, Node current, ICollection<Node> visitedNodes, int distance, FoundNode foundNode) | ||
{ | ||
visitedNodes.Add(current); | ||
|
||
if (func.Invoke(current)) | ||
{ | ||
if (foundNode == null || distance < foundNode.Distance) | ||
{ | ||
foundNode = new FoundNode(distance, current); | ||
} | ||
} | ||
|
||
foreach (var currentChild in current.Children) | ||
{ | ||
var found = FindNode(func, currentChild, visitedNodes, distance + 1, foundNode); | ||
if (NodeOk(foundNode, found)) | ||
{ | ||
foundNode = found; | ||
} | ||
} | ||
|
||
if (current.Parent != null && !visitedNodes.Contains(current.Parent)) | ||
{ | ||
var found = FindNode(func, current.Parent, visitedNodes, distance + 1, foundNode); | ||
if (NodeOk(foundNode, found)) | ||
{ | ||
foundNode = found; | ||
} | ||
} | ||
|
||
return foundNode; | ||
} | ||
|
||
private static bool NodeOk(FoundNode foundNode, FoundNode found) | ||
{ | ||
return found != null && (foundNode == null || found.Distance < foundNode.Distance); | ||
} | ||
|
||
private class FoundNode | ||
{ | ||
public FoundNode(int distance, Node node) | ||
{ | ||
Distance = distance; | ||
Node = node; | ||
} | ||
|
||
public int Distance { get; } | ||
|
||
public Node Node { get; } | ||
} | ||
} | ||
} |