单元测试HelloWorld (#1219)

* AutoFishing为单元测试重构;一个简单的单元测试GetFishpondTest

* 将鼠标操作的注入挪到DesktopRegion中;提供LoadAssetImage的一个不依赖配置的重载;ChooseBait的代码中将Bv的方法替换成不依赖配置的写法;完成ChooseBait的单元测试

* 使用TimeProvider改写ChooseBait以适用单元测试;添加假的绘图上下文类;添加抛竿行为的单元测试

* 选择鱼饵行为比对上次选择的鱼饵,如果相同则直接抛竿(之前的版本曾有此逻辑),但测下来有时会选错饵导致无限抛竿,调整了相关步骤的等待间隔,希望能简单地压制此bug

* 获取鱼群时过滤“获得”界面的鱼图标:1、在Fishpond构造函数中实现。2、配有此类情况的单元测试。3、并由此降低鱼钓上后的等待时间;
修复绘制鱼群时索引不正确导致遗漏的bug;获取鱼群时绘制鱼群供分析
This commit is contained in:
FishmanTheMurloc
2025-03-05 00:30:11 +08:00
committed by GitHub
parent 99df54607d
commit aead279c78
23 changed files with 1040 additions and 366 deletions

View File

@@ -1,6 +1,7 @@
using BetterGenshinImpact.Core.Simulator;
using BetterGenshinImpact.GameTask.Model.Area.Converter;
using BetterGenshinImpact.Helpers;
using Fischless.WindowsInput;
using System.Drawing;
namespace BetterGenshinImpact.GameTask.Model.Area;
@@ -10,17 +11,37 @@ namespace BetterGenshinImpact.GameTask.Model.Area;
/// 无缩放的桌面屏幕大小
/// 主要用于点击操作
/// </summary>
public class DesktopRegion() : Region(0, 0, PrimaryScreen.WorkingArea.Width, PrimaryScreen.WorkingArea.Height)
public class DesktopRegion : Region
{
private readonly IMouseSimulator mouse;
public DesktopRegion() : base(0, 0, PrimaryScreen.WorkingArea.Width, PrimaryScreen.WorkingArea.Height)
{
mouse = Simulation.SendInput.Mouse;
}
public DesktopRegion(IMouseSimulator mouse) : base(0, 0, PrimaryScreen.WorkingArea.Width, PrimaryScreen.WorkingArea.Height)
{
this.mouse = mouse;
}
public void DesktopRegionClick(int x, int y, int w, int h)
{
Simulation.SendInput.Mouse.MoveMouseTo((x + (w * 1d / 2)) * 65535 / Width,
if (mouse == null)
{
throw new System.NullReferenceException();
}
mouse.MoveMouseTo((x + (w * 1d / 2)) * 65535 / Width,
(y + (h * 1d / 2)) * 65535 / Height).LeftButtonClick().Sleep(50).LeftButtonUp();
}
public void DesktopRegionMove(int x, int y, int w, int h)
{
Simulation.SendInput.Mouse.MoveMouseTo((x + (w * 1d / 2)) * 65535 / Width,
if (mouse == null)
{
throw new System.NullReferenceException();
}
mouse.MoveMouseTo((x + (w * 1d / 2)) * 65535 / Width,
(y + (h * 1d / 2)) * 65535 / Height);
}

View File

@@ -11,7 +11,7 @@ namespace BetterGenshinImpact.GameTask.Model.Area;
/// 游戏捕获区域类
/// 主要用于转换到遮罩窗口的坐标
/// </summary>
public class GameCaptureRegion(Bitmap bitmap, int initX, int initY, Region? owner = null, INodeConverter? converter = null) : ImageRegion(bitmap, initX, initY, owner, converter)
public class GameCaptureRegion(Bitmap bitmap, int initX, int initY, Region? owner = null, INodeConverter? converter = null, DrawContent? drawContent = null) : ImageRegion(bitmap, initX, initY, owner, converter, drawContent)
{
/// <summary>
/// 在游戏捕获图像的坐标维度进行转换到遮罩窗口的坐标维度

View File

@@ -71,7 +71,7 @@ public class ImageRegion : Region
}
}
public ImageRegion(Bitmap bitmap, int x, int y, Region? owner = null, INodeConverter? converter = null) : base(x, y, bitmap.Width, bitmap.Height, owner, converter)
public ImageRegion(Bitmap bitmap, int x, int y, Region? owner = null, INodeConverter? converter = null, DrawContent? drawContent = null) : base(x, y, bitmap.Width, bitmap.Height, owner, converter, drawContent)
{
_srcBitmap = bitmap;
}

View File

@@ -1,5 +1,6 @@
using BetterGenshinImpact.GameTask.Model.Area.Converter;
using BetterGenshinImpact.View.Drawable;
using Fischless.WindowsInput;
using OpenCvSharp;
using System;
using System.Diagnostics;
@@ -54,7 +55,7 @@ public class Region : IDisposable
{
}
public Region(int x, int y, int width, int height, Region? owner = null, INodeConverter? converter = null)
public Region(int x, int y, int width, int height, Region? owner = null, INodeConverter? converter = null, DrawContent? drawContent = null)
{
X = x;
Y = y;
@@ -62,6 +63,7 @@ public class Region : IDisposable
Height = height;
Prev = owner;
PrevConverter = converter;
this.drawContent = drawContent ?? VisionContext.Instance().DrawContent;
}
public Region(Rect rect, Region? owner = null, INodeConverter? converter = null) : this(rect.X, rect.Y, rect.Width, rect.Height, owner, converter)
@@ -75,6 +77,11 @@ public class Region : IDisposable
/// </summary>
public INodeConverter? PrevConverter { get; }
/// <summary>
/// 绘图上下文
/// </summary>
private readonly DrawContent drawContent;
// public List<Region>? NextChildren { get; protected set; }
/// <summary>
@@ -189,13 +196,13 @@ public class Region : IDisposable
public void DrawRect(int x, int y, int w, int h, string name, Pen? pen = null)
{
var drawable = ToRectDrawable(x, y, w, h, name, pen);
VisionContext.Instance().DrawContent.PutRect(name, drawable);
drawContent.PutRect(name, drawable);
}
public void DrawRect(Rect rect, string name, Pen? pen = null)
{
var drawable = ToRectDrawable(rect.X, rect.Y, rect.Width, rect.Height, name, pen);
VisionContext.Instance().DrawContent.PutRect(name, drawable);
drawContent.PutRect(name, drawable);
}
/// <summary>
@@ -259,7 +266,7 @@ public class Region : IDisposable
public void DrawLine(int x1, int y1, int x2, int y2, string name, Pen? pen = null)
{
var drawable = ToLineDrawable(x1, y1, x2, y2, name, pen);
VisionContext.Instance().DrawContent.PutLine(name, drawable);
drawContent.PutLine(name, drawable);
}
public Rect ConvertSelfPositionToGameCaptureRegion()
@@ -349,7 +356,7 @@ public class Region : IDisposable
/// <returns></returns>
public Region Derive(int x, int y, int w, int h)
{
return new Region(x, y, w, h, this, new TranslationConverter(x, y));
return new Region(x, y, w, h, this, new TranslationConverter(x, y), this.drawContent);
}
public Region Derive(Rect rect)