Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[dotnet] Add SystemClock singleton #15285

Merged
merged 5 commits into from
Feb 25, 2025
Merged
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
2 changes: 1 addition & 1 deletion dotnet/src/support/UI/PopupWindowFinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ public string Invoke(Action popupMethod)

ReadOnlyCollection<string> existingHandles = this.driver.WindowHandles;
popupMethod();
WebDriverWait wait = new WebDriverWait(new SystemClock(), this.driver, this.timeout, this.sleepInterval);
WebDriverWait wait = new WebDriverWait(SystemClock.Instance, this.driver, this.timeout, this.sleepInterval);
string popupHandle = wait.Until<string>((d) =>
{
string? foundHandle = null;
Expand Down
2 changes: 1 addition & 1 deletion dotnet/src/support/UI/SlowLoadableComponent{T}.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public abstract class SlowLoadableComponent<T> : LoadableComponent<T>
/// </summary>
/// <param name="timeout">The <see cref="TimeSpan"/> within which the component should be loaded.</param>
protected SlowLoadableComponent(TimeSpan timeout)
: this(timeout, new SystemClock())
: this(timeout, SystemClock.Instance)
{
}

Expand Down
2 changes: 1 addition & 1 deletion dotnet/src/webdriver/Support/DefaultWait{T}.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public class DefaultWait<T> : IWait<T>
/// </summary>
/// <param name="input">The input value to pass to the evaluated conditions.</param>
public DefaultWait(T input)
: this(input, new SystemClock())
: this(input, SystemClock.Instance)
{
}

Expand Down
5 changes: 5 additions & 0 deletions dotnet/src/webdriver/Support/SystemClock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ namespace OpenQA.Selenium.Support.UI
/// </summary>
public class SystemClock : IClock
{
/// <summary>
/// An instance of the <see cref="SystemClock"/> type.
/// </summary>
public static SystemClock Instance { get; } = new();

/// <summary>
/// Gets the current date and time values.
/// </summary>
Expand Down
2 changes: 1 addition & 1 deletion dotnet/src/webdriver/Support/WebDriverWait.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public class WebDriverWait : DefaultWait<IWebDriver>
/// <param name="driver">The WebDriver instance used to wait.</param>
/// <param name="timeout">The timeout value indicating how long to wait for the condition.</param>
public WebDriverWait(IWebDriver driver, TimeSpan timeout)
: this(new SystemClock(), driver, timeout, DefaultSleepTimeout)
: this(SystemClock.Instance, driver, timeout, DefaultSleepTimeout)
{
}

Expand Down
2 changes: 1 addition & 1 deletion dotnet/test/support/UI/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ load("//dotnet:defs.bzl", "dotnet_nunit_test_suite", "framework")

SMALL_TESTS = [
"DefaultWaitTest.cs",
"FakeClock.cs",
"HandCrankClock.cs",
"LoadableComponentTests.cs",
"SelectTests.cs",
"SlowLoadableComponentTest.cs",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// <copyright file="FakeClock.cs" company="Selenium Committers">
// <copyright file="HandCrankClock.cs" company="Selenium Committers">
// Licensed to the Software Freedom Conservancy (SFC) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
Expand All @@ -21,18 +21,10 @@

namespace OpenQA.Selenium.Support.UI
{

public class FakeClock : IClock
public class HandCrankClock : IClock
{

private DateTime fakeNow = new DateTime(50000);
public DateTime Now
{
get
{
return fakeNow;
}
}
public DateTime Now => fakeNow;

public DateTime LaterBy(TimeSpan delay)
{
Expand All @@ -45,10 +37,9 @@ public bool IsNowBefore(DateTime otherDateTime)
return Now < otherDateTime;
}

public void TimePasses(TimeSpan timespan)
public void MoveTime(TimeSpan timespan)
{
fakeNow = fakeNow + timespan;
}
}

}
24 changes: 10 additions & 14 deletions dotnet/test/support/UI/SlowLoadableComponentTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public void TestShouldDoNothingIfComponentIsAlreadyLoaded()
public void TestShouldCauseTheLoadMethodToBeCalledIfTheComponentIsNotAlreadyLoaded()
{
int numberOfTimesThroughLoop = 1;
SlowLoading slowLoading = new SlowLoading(TimeSpan.FromSeconds(1), new SystemClock(), numberOfTimesThroughLoop).Load();
SlowLoading slowLoading = new SlowLoading(TimeSpan.FromSeconds(1), SystemClock.Instance, numberOfTimesThroughLoop).Load();

Assert.That(slowLoading.GetLoopCount(), Is.EqualTo(numberOfTimesThroughLoop));
}
Expand All @@ -53,7 +53,7 @@ public void TestTheLoadMethodShouldOnlyBeCalledOnceIfTheComponentTakesALongTimeT
{
try
{
new OnlyOneLoad(TimeSpan.FromSeconds(5), new SystemClock(), 5).Load();
new OnlyOneLoad(TimeSpan.FromSeconds(5), SystemClock.Instance, 5).Load();
}
catch (Exception)
{
Expand All @@ -64,7 +64,7 @@ public void TestTheLoadMethodShouldOnlyBeCalledOnceIfTheComponentTakesALongTimeT
[Test]
public void TestShouldThrowAnErrorIfCallingLoadDoesNotCauseTheComponentToLoadBeforeTimeout()
{
FakeClock clock = new FakeClock();
HandCrankClock clock = new HandCrankClock();

Assert.That(
() => new BasicSlowLoader(TimeSpan.FromSeconds(2), clock).Load(),
Expand All @@ -85,7 +85,7 @@ public void TestShouldCancelLoadingIfAnErrorIsDetected()
private class DetonatingSlowLoader : SlowLoadableComponent<DetonatingSlowLoader>
{

public DetonatingSlowLoader() : base(TimeSpan.FromSeconds(1), new SystemClock()) { }
public DetonatingSlowLoader() : base(TimeSpan.FromSeconds(1), SystemClock.Instance) { }

protected override void ExecuteLoad()
{
Expand Down Expand Up @@ -152,11 +152,11 @@ protected override void ExecuteLoad()
private class BasicSlowLoader : SlowLoadableComponent<BasicSlowLoader>
{

private readonly FakeClock clock;
public BasicSlowLoader(TimeSpan timeOut, FakeClock clock)
private readonly HandCrankClock handCrankClock;
public BasicSlowLoader(TimeSpan timeOut, HandCrankClock clock)
: base(timeOut, clock)
{
this.clock = clock;
this.handCrankClock = clock;
}

protected override void ExecuteLoad()
Expand All @@ -168,15 +168,15 @@ protected override bool EvaluateLoadedStatus()
{
// Cheat and increment the clock here, because otherwise it's hard to
// get to.
clock.TimePasses(TimeSpan.FromSeconds(1));
handCrankClock.MoveTime(TimeSpan.FromSeconds(1));
return false; // Never loads
}
}

private class HasError : SlowLoadableComponent<HasError>
{

public HasError() : base(TimeSpan.FromSeconds(1000), new FakeClock()) { }
public HasError() : base(TimeSpan.FromSeconds(1000), new HandCrankClock()) { }

protected override void ExecuteLoad()
{
Expand All @@ -194,10 +194,6 @@ protected override void HandleErrors()
}
}

private class CustomException : Exception
{

}
private class CustomException : Exception;
}

}