Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions KeyStats.Windows/KeyStats.Tests/DailyStatsTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using System;
using System.Text.Json;
using KeyStats.Models;
using Xunit;

namespace KeyStats.Tests;

public class DailyStatsTests
{
[Fact]
public void Initialization_ShouldSetDefaultValues()
{
var stats = new DailyStats();
Assert.Equal(0, stats.KeyPresses);
Assert.Equal(0, stats.TotalClicks);
Assert.Equal(0, stats.MouseDistance);
Assert.Equal(DateTime.Today, stats.Date.Date);
}

[Fact]
public void TotalClicks_ShouldSumAllClicks()
{
var stats = new DailyStats
{
LeftClicks = 10,
RightClicks = 5,
SideBackClicks = 2,
SideForwardClicks = 1
};
Assert.Equal(18, stats.TotalClicks);
}

[Fact]
public void Serialization_ShouldPreserveValues()
{
var original = new DailyStats
{
KeyPresses = 42,
LeftClicks = 10,
MouseDistance = 123.45
};
original.KeyPressCounts["Enter"] = 5;

var json = JsonSerializer.Serialize(original);
var deserialized = JsonSerializer.Deserialize<DailyStats>(json);

Assert.NotNull(deserialized);
Assert.Equal(42, deserialized.KeyPresses);
Assert.Equal(10, deserialized.LeftClicks);
Assert.Equal(123.45, deserialized.MouseDistance);
Assert.True(deserialized.KeyPressCounts.ContainsKey("Enter"));
Assert.Equal(5, deserialized.KeyPressCounts["Enter"]);
}
}
21 changes: 21 additions & 0 deletions KeyStats.Windows/KeyStats.Tests/KeyStats.Tests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net48</TargetFramework>
<Nullable>enable</Nullable>
<LangVersion>10.0</LangVersion>
<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="xunit" Version="2.6.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.3" />
<PackageReference Include="coverlet.collector" Version="6.0.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\KeyStats\KeyStats.csproj" />
</ItemGroup>

</Project>
87 changes: 87 additions & 0 deletions KeyStats.Windows/KeyStats.Tests/StatsManagerTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
using System;
using System.IO;
using System.Threading;
using KeyStats.Services;
using KeyStats.Models;
using Xunit;

namespace KeyStats.Tests;

public class StatsManagerTests : IDisposable
{
private readonly string _testDataFolder;
private readonly StatsManager _statsManager;
private readonly InputMonitorService _inputMonitor;

public StatsManagerTests()
{
_testDataFolder = Path.Combine(Path.GetTempPath(), "KeyStatsTests_" + Guid.NewGuid());
Directory.CreateDirectory(_testDataFolder);

StatsManager.ResetInstanceForTesting(_testDataFolder);
_statsManager = StatsManager.Instance;

// InputMonitorService is a singleton, so we get the same instance.
// We can't easily reset it, but we can use it to fire events.
_inputMonitor = InputMonitorService.Instance;
}

public void Dispose()
{
// Cleanup
StatsManager.DisposeInstance();
try
{
if (Directory.Exists(_testDataFolder))
{
Directory.Delete(_testDataFolder, true);
}
}
catch { /* Ignore cleanup errors */ }
Comment on lines +33 to +40
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

While ignoring cleanup errors in tests can be acceptable, swallowing all exceptions silently with catch {} can hide underlying problems like file locks or permission issues that might cause subsequent tests to fail unexpectedly. It's better practice to catch specific exceptions or at least log the error to aid in debugging.

        try
        {
            if (Directory.Exists(_testDataFolder))
            {
                Directory.Delete(_testDataFolder, true);
            }
        }
        catch (Exception ex)
        {
            // Log cleanup errors to aid in debugging test failures, rather than silently ignoring them.
            System.Diagnostics.Debug.WriteLine($"Failed to clean up test data folder '{_testDataFolder}': {ex.Message}");
        }

}

[Fact]
public void Initialization_ShouldStartWithZeroStats()
{
Assert.Equal(0, _statsManager.CurrentStats.KeyPresses);
Assert.Equal(0, _statsManager.CurrentStats.TotalClicks);
}

[Fact]
public void IncrementKeyPresses_ShouldIncreaseCount()
{
_inputMonitor.SimulateKeyPress("A", "TestApp", "Test App");

Assert.Equal(1, _statsManager.CurrentStats.KeyPresses);
Assert.True(_statsManager.CurrentStats.KeyPressCounts.ContainsKey("A"));
Assert.Equal(1, _statsManager.CurrentStats.KeyPressCounts["A"]);
}

[Fact]
public void IncrementClicks_ShouldIncreaseCount()
{
_inputMonitor.SimulateLeftClick("TestApp", "Test App");
Assert.Equal(1, _statsManager.CurrentStats.LeftClicks);
Assert.Equal(1, _statsManager.CurrentStats.TotalClicks);

_inputMonitor.SimulateRightClick("TestApp", "Test App");
Assert.Equal(1, _statsManager.CurrentStats.RightClicks);
Assert.Equal(2, _statsManager.CurrentStats.TotalClicks);
}

[Fact]
public void Persistence_ShouldSaveAndLoadStats()
{
_inputMonitor.SimulateKeyPress("SaveTest", "TestApp", "Test App");

// Force save
_statsManager.FlushPendingSave();

// Reset instance to simulate restart
StatsManager.ResetInstanceForTesting(_testDataFolder);
var newManager = StatsManager.Instance;

Assert.Equal(1, newManager.CurrentStats.KeyPresses);
Assert.True(newManager.CurrentStats.KeyPressCounts.ContainsKey("SaveTest"));
}
}
5 changes: 5 additions & 0 deletions KeyStats.Windows/KeyStats/GlobalUsings.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System.Runtime.CompilerServices;

// Resolve WPF vs Windows Forms namespace conflicts
// Alias WPF types to take precedence in this project
// Note: .NET Framework 4.8 不支持 global using (C# 10.0 特性)
Expand All @@ -8,3 +10,6 @@
// using Application = System.Windows.Application;
// using MessageBox = System.Windows.MessageBox;
// 等等...

// Allow internal members to be accessed by test project
[assembly: InternalsVisibleTo("KeyStats.Tests")]
30 changes: 30 additions & 0 deletions KeyStats.Windows/KeyStats/Services/InputMonitorService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,36 @@ public class InputMonitorService : IDisposable
public event Action<double>? MouseMoved;
public event Action<double, string, string>? MouseScrolled;

internal void SimulateKeyPress(string key, string appName, string displayName)
{
KeyPressed?.Invoke(key, appName, displayName);
}

internal void SimulateLeftClick(string appName, string displayName)
{
LeftMouseClicked?.Invoke(appName, displayName);
}

internal void SimulateRightClick(string appName, string displayName)
{
RightMouseClicked?.Invoke(appName, displayName);
}

internal void SimulateSideBackClick(string appName, string displayName)
{
SideBackMouseClicked?.Invoke(appName, displayName);
}

internal void SimulateSideForwardClick(string appName, string displayName)
{
SideForwardMouseClicked?.Invoke(appName, displayName);
}

internal void SimulateMouseMove(double distance)
{
MouseMoved?.Invoke(distance);
}

private InputMonitorService() { }

public void StartMonitoring()
Expand Down
30 changes: 25 additions & 5 deletions KeyStats.Windows/KeyStats/Services/StatsManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,18 @@ public class StatsManager : IDisposable
private static StatsManager? _instance;
public static StatsManager Instance => _instance ??= new StatsManager();

internal static void ResetInstanceForTesting(string? dataFolder = null)
{
DisposeInstance();
_instance = new StatsManager(dataFolder);
}

internal static void DisposeInstance()
{
_instance?.Dispose();
_instance = null;
}

private const double DefaultMetersPerPixel = AppSettings.DefaultMouseMetersPerPixel;

private readonly string _dataFolder;
Expand All @@ -42,12 +54,20 @@ public class StatsManager : IDisposable

public event Action? StatsUpdateRequested;

private StatsManager()
private StatsManager(string? dataFolder = null)
{
_dataFolder = Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
"KeyStats");
Directory.CreateDirectory(_dataFolder);
if (dataFolder != null)
{
_dataFolder = dataFolder;
Directory.CreateDirectory(_dataFolder);
}
else
{
_dataFolder = Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
"KeyStats");
Directory.CreateDirectory(_dataFolder);
}
Comment on lines +59 to +70
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The constructor logic for determining _dataFolder can be simplified using the null-coalescing operator (??). This makes the code more concise and easier to read, reducing duplication.

        _dataFolder = dataFolder ?? Path.Combine(
            Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
            "KeyStats");
        Directory.CreateDirectory(_dataFolder);


_statsFilePath = Path.Combine(_dataFolder, "daily_stats.json");
_historyFilePath = Path.Combine(_dataFolder, "history.json");
Expand Down
5 changes: 3 additions & 2 deletions KeyStats/StatsManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ class StatsManager {
case failure(pixels: Double)
}

private let userDefaults = UserDefaults.standard
private let userDefaults: UserDefaults
private let statsKey = "dailyStats"
private let historyKey = "dailyStatsHistory"
private let showKeyPressesKey = "showKeyPressesInMenuBar"
Expand Down Expand Up @@ -435,7 +435,8 @@ class StatsManager {
/// 上次鼠标位置(用于计算移动距离)
var lastMousePosition: NSPoint?

private init() {
init(userDefaults: UserDefaults = .standard) {
self.userDefaults = userDefaults
dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd"

Expand Down
60 changes: 60 additions & 0 deletions KeyStats/Tests/DailyStatsTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import XCTest
@testable import KeyStats

class DailyStatsTests: XCTestCase {

func testInitialization() {
let stats = DailyStats()
XCTAssertEqual(stats.keyPresses, 0)
XCTAssertEqual(stats.totalClicks, 0)
XCTAssertEqual(stats.mouseDistance, 0)
}

func testTotalClicks() {
var stats = DailyStats()
stats.leftClicks = 10
stats.rightClicks = 5
stats.sideBackClicks = 2
stats.sideForwardClicks = 1

XCTAssertEqual(stats.totalClicks, 18)
}

func testCorrectionRate() {
var stats = DailyStats()
stats.keyPresses = 100
stats.keyPressCounts["Delete"] = 5
stats.keyPressCounts["ForwardDelete"] = 5
stats.keyPressCounts["A"] = 90

// 10 deletes out of 100 keys
XCTAssertEqual(stats.correctionRate, 0.1, accuracy: 0.0001)
}

func testInputRatio() {
var stats = DailyStats()
stats.keyPresses = 100
stats.leftClicks = 50
// total clicks = 50

XCTAssertEqual(stats.inputRatio, 2.0, accuracy: 0.0001)
}

func testEncodingDecoding() {
var stats = DailyStats()
stats.keyPresses = 42
stats.leftClicks = 10
stats.keyPressCounts["Enter"] = 5

do {
let data = try JSONEncoder().encode(stats)
let decoded = try JSONDecoder().decode(DailyStats.self, from: data)

XCTAssertEqual(decoded.keyPresses, 42)
XCTAssertEqual(decoded.leftClicks, 10)
XCTAssertEqual(decoded.keyPressCounts["Enter"], 5)
} catch {
XCTFail("Encoding/Decoding failed: \(error)")
}
}
}
Loading