Skip to content

Commit

Permalink
Mod UGUIMonkeyAgent default screenshot filename strategy to TwoTiered…
Browse files Browse the repository at this point in the history
…CounterStrategy
  • Loading branch information
nowsprinting committed Oct 21, 2024
1 parent 1f09294 commit 14f5da6
Show file tree
Hide file tree
Showing 9 changed files with 127 additions and 4 deletions.
4 changes: 2 additions & 2 deletions Runtime/Agents/UGUIMonkeyAgent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
using System.Diagnostics.CodeAnalysis;
using System.Threading;
using Cysharp.Threading.Tasks;
using DeNA.Anjin.Strategies;
using TestHelper.Monkey;
using TestHelper.Monkey.Annotations.Enums;
using TestHelper.Monkey.Operators;
using TestHelper.Monkey.Random;
using TestHelper.Monkey.ScreenshotFilenameStrategies;
using TestHelper.Random;
using UnityEngine;

Expand Down Expand Up @@ -112,7 +112,7 @@ public override async UniTask Run(CancellationToken token)
? new ScreenshotOptions
{
Directory = defaultScreenshotDirectory ? null : screenshotDirectory,
FilenameStrategy = new CounterBasedStrategy(
FilenameStrategy = new TwoTieredCounterStrategy(
defaultScreenshotFilenamePrefix ? this.name : screenshotFilenamePrefix
),
SuperSize = screenshotSuperSize,
Expand Down
3 changes: 3 additions & 0 deletions Runtime/Strategies.meta

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

52 changes: 52 additions & 0 deletions Runtime/Strategies/TwoTieredCounterStrategy.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Copyright (c) 2023-2024 DeNA Co., Ltd.
// This software is released under the MIT License.

using System.Collections.Generic;
using System.Runtime.CompilerServices;
using DeNA.Anjin.Attributes;
using TestHelper.Monkey.ScreenshotFilenameStrategies;

namespace DeNA.Anjin.Strategies
{
/// <summary>
/// Screenshot filename strategy that adds a unique counter to the prefix.
/// </summary>
public class TwoTieredCounterStrategy : AbstractPrefixAndUniqueIDStrategy
{
private static readonly Dictionary<string, int> s_prefixCounters = new Dictionary<string, int>();
private readonly int _uniqueNumberOfPrefix;
private int _count;

[InitializeOnLaunchAutopilot]
internal static void ResetPrefixCounters()
{
s_prefixCounters.Clear();
}

/// <inheritdoc/>
public TwoTieredCounterStrategy(string filenamePrefix = null, [CallerMemberName] string callerMemberName = null)
: base(filenamePrefix, callerMemberName)
{
var key = base.GetFilenamePrefix();
if (!s_prefixCounters.TryAdd(key, 1))
{
s_prefixCounters[key]++;
}

_uniqueNumberOfPrefix = s_prefixCounters[key];
_count = 0;
}

/// <inheritdoc/>
protected override string GetFilenamePrefix()
{
return $"{base.GetFilenamePrefix()}{_uniqueNumberOfPrefix:D2}";
}

/// <inheritdoc/>
protected override string GetUniqueID()
{
return $"{++_count:D4}";
}
}
}
3 changes: 3 additions & 0 deletions Runtime/Strategies/TwoTieredCounterStrategy.cs.meta

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

2 changes: 1 addition & 1 deletion Tests/Runtime/Agents/UGUIMonkeyAgentTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public async Task Run_lifespanPassed_stopAgent()
public async Task Run_DefaultScreenshotFilenamePrefix_UseAgentName()
{
const string AgentName = "MyMonkeyAgent";
var filename = $"{AgentName}_0001.png";
var filename = $"{AgentName}01_0001.png";
var path = Path.Combine(_defaultOutputDirectory, filename);
if (File.Exists(path))
{
Expand Down
3 changes: 2 additions & 1 deletion Tests/Runtime/DeNA.Anjin.Tests.asmdef
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
"UniTask",
"DeNA.Anjin.Editor",
"TestHelper",
"TestHelper.RuntimeInternals"
"TestHelper.RuntimeInternals",
"TestHelper.Monkey"
],
"includePlatforms": [],
"excludePlatforms": [],
Expand Down
3 changes: 3 additions & 0 deletions Tests/Runtime/Strategies.meta

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

58 changes: 58 additions & 0 deletions Tests/Runtime/Strategies/TwoTieredCounterStrategyTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// Copyright (c) 2023-2024 DeNA Co., Ltd.
// This software is released under the MIT License.

using NUnit.Framework;

namespace DeNA.Anjin.Strategies
{
public class TwoTieredCounterStrategyTest
{
[SetUp]
public void SetUp()
{
TwoTieredCounterStrategy.ResetPrefixCounters();
}

[Test]
public void GetFilename_WithPrefix_ReturnsUniqueFilename()
{
var sut = new TwoTieredCounterStrategy("MyPrefix");

Assert.That(sut.GetFilename(), Is.EqualTo("MyPrefix01_0001.png"));
Assert.That(sut.GetFilename(), Is.EqualTo("MyPrefix01_0002.png"));
}

[Test]
public void GetFilename_WithoutPrefix_ReturnsFilenameUsingTestName()
{
var sut = new TwoTieredCounterStrategy();

Assert.That(sut.GetFilename(), Is.EqualTo($"{TestContext.CurrentContext.Test.Name}01_0001.png"));
Assert.That(sut.GetFilename(), Is.EqualTo($"{TestContext.CurrentContext.Test.Name}01_0002.png"));
}

[Test]
public void GetFilename_WithSamePrefix_ReturnsFilenameUsingUniqueNumberByPrefix()
{
var sut = new TwoTieredCounterStrategy("MyPrefix");
var sut2 = new TwoTieredCounterStrategy("MyPrefix");

Assert.That(sut.GetFilename(), Is.EqualTo("MyPrefix01_0001.png"));
Assert.That(sut.GetFilename(), Is.EqualTo("MyPrefix01_0002.png"));
Assert.That(sut2.GetFilename(), Is.EqualTo("MyPrefix02_0001.png"));
Assert.That(sut2.GetFilename(), Is.EqualTo("MyPrefix02_0002.png"));
}

[Test]
public void GetFilename_WithAnotherPrefix_ReturnsUniqueFilenameByPrefix()
{
var sut = new TwoTieredCounterStrategy("MyPrefix");
var sut2 = new TwoTieredCounterStrategy("AnotherPrefix");

Assert.That(sut.GetFilename(), Is.EqualTo("MyPrefix01_0001.png"));
Assert.That(sut.GetFilename(), Is.EqualTo("MyPrefix01_0002.png"));
Assert.That(sut2.GetFilename(), Is.EqualTo("AnotherPrefix01_0001.png"));
Assert.That(sut2.GetFilename(), Is.EqualTo("AnotherPrefix01_0002.png"));
}
}
}
3 changes: 3 additions & 0 deletions Tests/Runtime/Strategies/TwoTieredCounterStrategyTest.cs.meta

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

0 comments on commit 14f5da6

Please sign in to comment.