Skip to content

Commit

Permalink
Merge pull request #124 from nowsprinting/chore/remove_operatortype
Browse files Browse the repository at this point in the history
Use sub-interfaces of IOperator instead of OperatorType
  • Loading branch information
nowsprinting authored Apr 20, 2024
2 parents 0fae1b3 + 3eaebbd commit 4489e24
Show file tree
Hide file tree
Showing 32 changed files with 184 additions and 71 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ public class MyIntegrationTest

`GetInteractableComponents` are extensions of `GameObject` that return interactable components.

`SelectOperators` and `SelectOperatorsOfType` are extensions of `Component` that return available operators.
`SelectOperators` and `SelectOperators<T>` are extensions of `Component` that return available operators.
Operators implements `IOperator` interface. It has `OperateAsync` method that operates on the component.

Usage:
Expand All @@ -183,7 +183,7 @@ public class MyIntegrationTest
var button = await finder.FindByNameAsync("StartButton", interactable: true);

var buttonComponent = button.GetInteractableComponents().First();
var clickOperator = buttonComponent.SelectOperatorsOfType(_operators, OperatorType.Click).First();
var clickOperator = buttonComponent.SelectOperators<IClickOperator>(_operators).First();
clickOperator.OperateAsync(buttonComponent);
}
}
Expand All @@ -210,7 +210,7 @@ public class MyIntegrationTest
var components = InteractiveComponentCollector.FindInteractableComponents();

var firstComponent = components.First();
var clickAndHoldOperator = firstComponent.SelectOperatorsOfType(_operators, OperatorType.ClickAndHold).First();
var clickAndHoldOperator = firstComponent.SelectOperators<IClickAndHoldOperator>(_operators).First();
await clickAndHoldOperator.OperateAsync(firstComponent);
}
}
Expand Down Expand Up @@ -238,7 +238,7 @@ public class MyIntegrationTest
var components = InteractiveComponentCollector.FindReachableInteractableComponents();

var firstComponent = components.First();
var textInputOperator = firstComponent.SelectOperatorsOfType(_operators, OperatorType.TextInput).First();
var textInputOperator = firstComponent.SelectOperators<ITextInputOperator>(_operators).First();
textInputOperator.OperateAsync(firstComponent); // input random text
}
}
Expand Down
10 changes: 4 additions & 6 deletions Runtime/Extensions/ComponentExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ public static class ComponentExtensions
/// <param name="component"></param>
/// <param name="operators">All available operators in autopilot/tests. Usually defined in <c>MonkeyConfig</c></param>
/// <returns>Available operators</returns>
public static IEnumerable<IOperator> SelectOperators(this Component component,
IEnumerable<IOperator> operators)
public static IEnumerable<IOperator> SelectOperators(this Component component, IEnumerable<IOperator> operators)
{
return operators.Where(iOperator => iOperator.CanOperate(component));
}
Expand All @@ -30,12 +29,11 @@ public static IEnumerable<IOperator> SelectOperators(this Component component,
/// </summary>
/// <param name="component"></param>
/// <param name="operators">All available operators in autopilot/tests. Usually defined in <c>MonkeyConfig</c></param>
/// <param name="type">Operator type</param>
/// <returns>Available operators</returns>
public static IEnumerable<IOperator> SelectOperatorsOfType(this Component component,
IEnumerable<IOperator> operators, OperatorType type)
public static IEnumerable<T> SelectOperators<T>(this Component component, IEnumerable<IOperator> operators)
where T : IOperator
{
return operators.Where(iOperator => iOperator.Type == type && iOperator.CanOperate(component));
return operators.OfType<T>().Where(iOperator => iOperator.CanOperate(component));
}

/// <summary>
Expand Down
21 changes: 10 additions & 11 deletions Runtime/InteractiveComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -144,25 +144,24 @@ public IEnumerable<IOperator> GetOperators()
/// <summary>
/// Returns the operators that specify types and are available to this component.
/// </summary>
/// <param name="type">Operator type</param>
/// <returns>Available operators</returns>
[Obsolete("Use ComponentExtensions.SelectOperatorsOfType() instead.")]
public IEnumerable<IOperator> GetOperatorsByType(OperatorType type)
[Obsolete("Use ComponentExtensions.SelectOperators<T>() instead.")]
public IEnumerable<T> GetOperatorsByType<T>() where T : IOperator
{
return component.SelectOperatorsOfType(_operators, type);
return component.SelectOperators<T>(_operators);
}

/// <summary>
/// Check component can receive click (tap) event.
/// </summary>
[Obsolete]
public bool CanClick() => GetOperatorsByType(OperatorType.Click).Any();
public bool CanClick() => GetOperatorsByType<IClickOperator>().Any();

/// <summary>
/// Click component.
/// </summary>
[Obsolete]
public void Click() => GetOperatorsByType(OperatorType.Click).First().OperateAsync(component);
public void Click() => GetOperatorsByType<IClickOperator>().First().OperateAsync(component);

[Obsolete]
public bool CanTap() => CanClick();
Expand All @@ -174,15 +173,15 @@ public IEnumerable<IOperator> GetOperatorsByType(OperatorType type)
/// Check component can receive click (tap) and hold event.
/// </summary>
[Obsolete]
public bool CanClickAndHold() => GetOperatorsByType(OperatorType.ClickAndHold).Any();
public bool CanClickAndHold() => GetOperatorsByType<IClickAndHoldOperator>().Any();

/// <summary>
/// Click (touch) and hold component.
/// </summary>
[Obsolete]
public async UniTask ClickAndHold(CancellationToken cancellationToken = default)
{
var clickAndHoldOperator = GetOperatorsByType(OperatorType.ClickAndHold).First();
var clickAndHoldOperator = GetOperatorsByType<IClickAndHoldOperator>().First();
await clickAndHoldOperator.OperateAsync(component, cancellationToken);
}

Expand All @@ -197,21 +196,21 @@ public async UniTask TouchAndHold(CancellationToken cancellationToken = default)
/// Check component can input text.
/// </summary>
[Obsolete]
public bool CanTextInput() => GetOperatorsByType(OperatorType.TextInput).Any();
public bool CanTextInput() => GetOperatorsByType<ITextInputOperator>().Any();

/// <summary>
/// Input random text.
/// </summary>
[Obsolete]
public void TextInput() => GetOperatorsByType(OperatorType.TextInput).First().OperateAsync(component);
public void TextInput() => GetOperatorsByType<ITextInputOperator>().First().OperateAsync(component);

/// <summary>
/// Input specified text.
/// </summary>
[Obsolete]
public void TextInput(string text)
{
var textInputOperator = (ITextInputOperator)GetOperatorsByType(OperatorType.TextInput).First();
var textInputOperator = GetOperatorsByType<ITextInputOperator>().First();
textInputOperator.OperateAsync(component, text);
}
}
Expand Down
13 changes: 13 additions & 0 deletions Runtime/Operators/IClickAndHoldOperator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Copyright (c) 2023-2024 Koji Hasegawa.
// This software is released under the MIT License.

namespace TestHelper.Monkey.Operators
{
/// <summary>
/// Click and hold operator interface.
/// a.k.a. touch and hold, long press.
/// </summary>
public interface IClickAndHoldOperator : IOperator
{
}
}
3 changes: 3 additions & 0 deletions Runtime/Operators/IClickAndHoldOperator.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions Runtime/Operators/IClickOperator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Copyright (c) 2023-2024 Koji Hasegawa.
// This software is released under the MIT License.

namespace TestHelper.Monkey.Operators
{
/// <summary>
/// Click (tap) operator interface.
/// </summary>
public interface IClickOperator : IOperator
{
}
}
3 changes: 3 additions & 0 deletions Runtime/Operators/IClickOperator.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions Runtime/Operators/IDoubleClickOperator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Copyright (c) 2023-2024 Koji Hasegawa.
// This software is released under the MIT License.

namespace TestHelper.Monkey.Operators
{
/// <summary>
/// Double click (tap) operator interface.
/// </summary>
public interface IDoubleClickOperator : IOperator
{
}
}
3 changes: 3 additions & 0 deletions Runtime/Operators/IDoubleClickOperator.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions Runtime/Operators/IDragAndDropOperator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Copyright (c) 2023-2024 Koji Hasegawa.
// This software is released under the MIT License.

namespace TestHelper.Monkey.Operators
{
/// <summary>
/// Drag and drop operator interface.
/// </summary>
public interface IDragAndDropOperator : IOperator
{
// TODO: specify drop destination overloads
}
}
3 changes: 3 additions & 0 deletions Runtime/Operators/IDragAndDropOperator.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions Runtime/Operators/IFlickOperator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Copyright (c) 2023-2024 Koji Hasegawa.
// This software is released under the MIT License.

namespace TestHelper.Monkey.Operators
{
/// <summary>
/// Flick operator interface.
/// </summary>
public interface IFlickOperator : IOperator
{
// TODO: specify flick direction overloads
}
}
3 changes: 3 additions & 0 deletions Runtime/Operators/IFlickOperator.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions Runtime/Operators/IHoverOperator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Copyright (c) 2023-2024 Koji Hasegawa.
// This software is released under the MIT License.

namespace TestHelper.Monkey.Operators
{
/// <summary>
/// Hover mouse cursor operator interface.
/// </summary>
public interface IHoverOperator : IOperator
{
}
}
3 changes: 3 additions & 0 deletions Runtime/Operators/IHoverOperator.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 2 additions & 7 deletions Runtime/Operators/IOperator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,14 @@ namespace TestHelper.Monkey.Operators
{
/// <summary>
/// Matcher and Operator pair for monkey testing.
/// Implement the <c>IsMatch</c> method to determine whether an operation such as click is possible, and the <c>Operate</c> method to execute the operation.
/// Implement the <c>CanOperate</c> method to determine whether an operation such as click is possible, and the <c>OperateAsync</c> method to execute the operation.
/// </summary>
/// <remarks>
/// Must be implements sub-interface (e.g., <c>IClickOperator</c>) to represent the type of operator.
/// If required parameters for the operation, such as hold time, input text strategy, etc., keep them in instance fields of the implementation class.
/// </remarks>
public interface IOperator
{
/// <summary>
/// Returns operator type.
/// Intended for use in capture and playback features.
/// </summary>
OperatorType Type { get; }

/// <summary>
/// Returns if can operate target component this Operator.
/// </summary>
Expand Down
13 changes: 13 additions & 0 deletions Runtime/Operators/IPinchOperator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Copyright (c) 2023-2024 Koji Hasegawa.
// This software is released under the MIT License.

namespace TestHelper.Monkey.Operators
{
/// <summary>
/// Pinch operator interface.
/// </summary>
public interface IPinchOperator : IOperator
{
// TODO: specify pinch destination and distance overloads
}
}
3 changes: 3 additions & 0 deletions Runtime/Operators/IPinchOperator.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions Runtime/Operators/IRightClickOperator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Copyright (c) 2023-2024 Koji Hasegawa.
// This software is released under the MIT License.

namespace TestHelper.Monkey.Operators
{
/// <summary>
/// Click right button of mouse operator interface.
/// </summary>
public interface IRightClickOperator : IOperator
{
}
}
3 changes: 3 additions & 0 deletions Runtime/Operators/IRightClickOperator.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions Runtime/Operators/IScrollWheelOperator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Copyright (c) 2023-2024 Koji Hasegawa.
// This software is released under the MIT License.

namespace TestHelper.Monkey.Operators
{
/// <summary>
/// Scroll wheel of mouse operator interface.
/// scrolling up/down and tilting left/right.
/// </summary>
public interface IScrollWheelOperator : IOperator
{
// TODO: specify scroll destination and distance overloads
}
}
3 changes: 3 additions & 0 deletions Runtime/Operators/IScrollWheelOperator.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions Runtime/Operators/ISwipeOperator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Copyright (c) 2023-2024 Koji Hasegawa.
// This software is released under the MIT License.

namespace TestHelper.Monkey.Operators
{
/// <summary>
/// Swipe operator interface.
/// </summary>
public interface ISwipeOperator : IOperator
{
// TODO: specify swipe destination overloads
}
}
3 changes: 3 additions & 0 deletions Runtime/Operators/ISwipeOperator.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 0 additions & 24 deletions Runtime/Operators/OperatorType.cs

This file was deleted.

3 changes: 0 additions & 3 deletions Runtime/Operators/OperatorType.cs.meta

This file was deleted.

5 changes: 1 addition & 4 deletions Runtime/Operators/UGUIClickAndHoldOperator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace TestHelper.Monkey.Operators
/// Click and hold operator for Unity UI (uGUI) components.
/// a.k.a. touch and hold, long press.
/// </summary>
public class UGUIClickAndHoldOperator : IOperator
public class UGUIClickAndHoldOperator : IClickAndHoldOperator
{
private readonly int _holdMillis;
private readonly Func<GameObject, Vector2> _getScreenPoint;
Expand All @@ -32,9 +32,6 @@ public UGUIClickAndHoldOperator(int holdMillis = 1000, Func<GameObject, Vector2>
this._getScreenPoint = getScreenPoint ?? DefaultScreenPointStrategy.GetScreenPoint;
}

/// <inheritdoc />
public OperatorType Type => OperatorType.ClickAndHold;

/// <inheritdoc />
public bool CanOperate(Component component)
{
Expand Down
Loading

0 comments on commit 4489e24

Please sign in to comment.