Skip to content

Commit a6646c5

Browse files
RenderMichaelsandeepsuryaprasad
authored andcommitted
[dotnet] Add SystemClock singleton (SeleniumHQ#15285)
* [dotnet] Add SystemClock singleton * Fix copyright comment * Fix support UI test build
1 parent 50457c0 commit a6646c5

File tree

8 files changed

+24
-32
lines changed

8 files changed

+24
-32
lines changed

dotnet/src/support/UI/PopupWindowFinder.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ public string Invoke(Action popupMethod)
131131

132132
ReadOnlyCollection<string> existingHandles = this.driver.WindowHandles;
133133
popupMethod();
134-
WebDriverWait wait = new WebDriverWait(new SystemClock(), this.driver, this.timeout, this.sleepInterval);
134+
WebDriverWait wait = new WebDriverWait(SystemClock.Instance, this.driver, this.timeout, this.sleepInterval);
135135
string popupHandle = wait.Until<string>((d) =>
136136
{
137137
string? foundHandle = null;

dotnet/src/support/UI/SlowLoadableComponent{T}.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public abstract class SlowLoadableComponent<T> : LoadableComponent<T>
4343
/// </summary>
4444
/// <param name="timeout">The <see cref="TimeSpan"/> within which the component should be loaded.</param>
4545
protected SlowLoadableComponent(TimeSpan timeout)
46-
: this(timeout, new SystemClock())
46+
: this(timeout, SystemClock.Instance)
4747
{
4848
}
4949

dotnet/src/webdriver/Support/DefaultWait{T}.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public class DefaultWait<T> : IWait<T>
4444
/// </summary>
4545
/// <param name="input">The input value to pass to the evaluated conditions.</param>
4646
public DefaultWait(T input)
47-
: this(input, new SystemClock())
47+
: this(input, SystemClock.Instance)
4848
{
4949
}
5050

dotnet/src/webdriver/Support/SystemClock.cs

+5
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ namespace OpenQA.Selenium.Support.UI
2828
/// </summary>
2929
public class SystemClock : IClock
3030
{
31+
/// <summary>
32+
/// An instance of the <see cref="SystemClock"/> type.
33+
/// </summary>
34+
public static SystemClock Instance { get; } = new();
35+
3136
/// <summary>
3237
/// Gets the current date and time values.
3338
/// </summary>

dotnet/src/webdriver/Support/WebDriverWait.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public class WebDriverWait : DefaultWait<IWebDriver>
4040
/// <param name="driver">The WebDriver instance used to wait.</param>
4141
/// <param name="timeout">The timeout value indicating how long to wait for the condition.</param>
4242
public WebDriverWait(IWebDriver driver, TimeSpan timeout)
43-
: this(new SystemClock(), driver, timeout, DefaultSleepTimeout)
43+
: this(SystemClock.Instance, driver, timeout, DefaultSleepTimeout)
4444
{
4545
}
4646

dotnet/test/support/UI/BUILD.bazel

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ load("//dotnet:defs.bzl", "dotnet_nunit_test_suite", "framework")
22

33
SMALL_TESTS = [
44
"DefaultWaitTest.cs",
5-
"FakeClock.cs",
5+
"HandCrankClock.cs",
66
"LoadableComponentTests.cs",
77
"SelectTests.cs",
88
"SlowLoadableComponentTest.cs",

dotnet/test/support/UI/FakeClock.cs dotnet/test/support/UI/HandCrankClock.cs

+4-13
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// <copyright file="FakeClock.cs" company="Selenium Committers">
1+
// <copyright file="HandCrankClock.cs" company="Selenium Committers">
22
// Licensed to the Software Freedom Conservancy (SFC) under one
33
// or more contributor license agreements. See the NOTICE file
44
// distributed with this work for additional information
@@ -21,18 +21,10 @@
2121

2222
namespace OpenQA.Selenium.Support.UI
2323
{
24-
25-
public class FakeClock : IClock
24+
public class HandCrankClock : IClock
2625
{
27-
2826
private DateTime fakeNow = new DateTime(50000);
29-
public DateTime Now
30-
{
31-
get
32-
{
33-
return fakeNow;
34-
}
35-
}
27+
public DateTime Now => fakeNow;
3628

3729
public DateTime LaterBy(TimeSpan delay)
3830
{
@@ -45,10 +37,9 @@ public bool IsNowBefore(DateTime otherDateTime)
4537
return Now < otherDateTime;
4638
}
4739

48-
public void TimePasses(TimeSpan timespan)
40+
public void MoveTime(TimeSpan timespan)
4941
{
5042
fakeNow = fakeNow + timespan;
5143
}
5244
}
53-
5445
}

dotnet/test/support/UI/SlowLoadableComponentTest.cs

+10-14
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public void TestShouldDoNothingIfComponentIsAlreadyLoaded()
4343
public void TestShouldCauseTheLoadMethodToBeCalledIfTheComponentIsNotAlreadyLoaded()
4444
{
4545
int numberOfTimesThroughLoop = 1;
46-
SlowLoading slowLoading = new SlowLoading(TimeSpan.FromSeconds(1), new SystemClock(), numberOfTimesThroughLoop).Load();
46+
SlowLoading slowLoading = new SlowLoading(TimeSpan.FromSeconds(1), SystemClock.Instance, numberOfTimesThroughLoop).Load();
4747

4848
Assert.That(slowLoading.GetLoopCount(), Is.EqualTo(numberOfTimesThroughLoop));
4949
}
@@ -53,7 +53,7 @@ public void TestTheLoadMethodShouldOnlyBeCalledOnceIfTheComponentTakesALongTimeT
5353
{
5454
try
5555
{
56-
new OnlyOneLoad(TimeSpan.FromSeconds(5), new SystemClock(), 5).Load();
56+
new OnlyOneLoad(TimeSpan.FromSeconds(5), SystemClock.Instance, 5).Load();
5757
}
5858
catch (Exception)
5959
{
@@ -64,7 +64,7 @@ public void TestTheLoadMethodShouldOnlyBeCalledOnceIfTheComponentTakesALongTimeT
6464
[Test]
6565
public void TestShouldThrowAnErrorIfCallingLoadDoesNotCauseTheComponentToLoadBeforeTimeout()
6666
{
67-
FakeClock clock = new FakeClock();
67+
HandCrankClock clock = new HandCrankClock();
6868

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

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

9090
protected override void ExecuteLoad()
9191
{
@@ -152,11 +152,11 @@ protected override void ExecuteLoad()
152152
private class BasicSlowLoader : SlowLoadableComponent<BasicSlowLoader>
153153
{
154154

155-
private readonly FakeClock clock;
156-
public BasicSlowLoader(TimeSpan timeOut, FakeClock clock)
155+
private readonly HandCrankClock handCrankClock;
156+
public BasicSlowLoader(TimeSpan timeOut, HandCrankClock clock)
157157
: base(timeOut, clock)
158158
{
159-
this.clock = clock;
159+
this.handCrankClock = clock;
160160
}
161161

162162
protected override void ExecuteLoad()
@@ -168,15 +168,15 @@ protected override bool EvaluateLoadedStatus()
168168
{
169169
// Cheat and increment the clock here, because otherwise it's hard to
170170
// get to.
171-
clock.TimePasses(TimeSpan.FromSeconds(1));
171+
handCrankClock.MoveTime(TimeSpan.FromSeconds(1));
172172
return false; // Never loads
173173
}
174174
}
175175

176176
private class HasError : SlowLoadableComponent<HasError>
177177
{
178178

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

181181
protected override void ExecuteLoad()
182182
{
@@ -194,10 +194,6 @@ protected override void HandleErrors()
194194
}
195195
}
196196

197-
private class CustomException : Exception
198-
{
199-
200-
}
197+
private class CustomException : Exception;
201198
}
202-
203199
}

0 commit comments

Comments
 (0)