Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/dev' into issue/LMBQ-418
Browse files Browse the repository at this point in the history
  • Loading branch information
MZole committed Nov 21, 2024
2 parents 42bd037 + 2799c10 commit d1ffcb6
Show file tree
Hide file tree
Showing 9 changed files with 33 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,7 @@ public Task VerifyBlogImage() =>
// is the different rendering of text on each platform, but it can occur between different Linux distributions too.
// Here: https://pandasauce.org/post/linux-fonts/ you can find a good summary about this from 2019, but still valid
// in 2022.
// Temporarily not running Edge until https://github.com/atata-framework/atata-webdriversetup/issues/16 is fixed.
[Theory, Chrome]
[Theory, Chrome, Edge]
public Task VerifyNavbar(Browser browser) =>
ExecuteTestAfterSetupAsync(
context =>
Expand Down
7 changes: 2 additions & 5 deletions Lombiq.Tests.UI.Samples/Tests/MultiBrowserTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,15 @@ public MultiBrowserTests(ITestOutputHelper testOutputHelper)

// First, let's see a test using Edge. While the default browser is Chrome if you don't set anything, all
// ExecuteTest* methods can also accept a browser, if you want to use a different one.
#pragma warning disable xUnit1004 // Test methods should not be skipped
[Fact(Skip = "Temporarily not running Edge until https://github.com/atata-framework/atata-webdriversetup/issues/16 is fixed.")]
#pragma warning restore xUnit1004 // Test methods should not be skipped
[Fact]
public Task AnonymousHomePageShouldExistWithEdge() =>
ExecuteTestAfterSetupAsync(NavbarIsCorrect, Browser.Edge);

// This test is now marked not with the [Fact] attribute but [Theory]. With it, you can create so-called data-driven
// tests. [Chrome] and [Edge] are input parameters of the test, and thus in effect, you have now two tests:
// AnonymousHomePageShouldExistMultiBrowser once with Chrome, and once with Edge. See here for more info:
// https://andrewlock.net/creating-parameterised-tests-in-xunit-with-inlinedata-classdata-and-memberdata/.
// Temporarily not running Edge until https://github.com/atata-framework/atata-webdriversetup/issues/16 is fixed.
[Theory, Chrome]
[Theory, Chrome, Edge]
public Task AnonymousHomePageShouldExistMultiBrowser(Browser browser) =>
ExecuteTestAfterSetupAsync(NavbarIsCorrect, browser);

Expand Down
6 changes: 3 additions & 3 deletions Lombiq.Tests.UI.Samples/Tests/ShiftTimeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
namespace Lombiq.Tests.UI.Samples.Tests;

// When you enable the "Shift Time - Shortcuts - Lombiq UI Testing Toolbox" feature, it replaces Orchard Core's stock
// ICLock implementation with the custom ShiftTimeClock class. You can use the
// ~/Lombiq.Tests.UI.Samples/ShiftTime/Set?days=... action to update the ShiftTimeClock.Shift property for the current
// ICLock implementation with the custom TimeShiftingClock class. You can use the
// ~/Lombiq.Tests.UI.Samples/TimeShift/Set?days=... action to update the TimeShiftingClock.Shift property for the current
// tenant, which will trick any service that uses IClock into thinking you are in the future. This can be used to test
// features that can expire, such as a limited-time product discount in a web store, without having to wait.
public class ShiftTimeTests : UITestBase
Expand Down Expand Up @@ -46,7 +46,7 @@ await context.FillInCodeMirrorEditorWithRetriesAsync(
// This extension method navigates to the action which sets the time offset. You can set it in terms of
// days or seconds. Both accept fractions and negative values. If both days and seconds are set, they
// are added together.
await context.SetShiftTimeAsync(days: 10);
await context.SetTimeShiftAsync(TimeSpan.FromDays(10));
// Let's verify the date!
var tenDaysFromNow = await GetNowAsync(context);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
namespace Lombiq.Tests.UI.Shortcuts.Controllers;

[DevelopmentAndLocalhostOnly]
public class ShiftTimeController : Controller
public class TimeShiftController : Controller
{
private readonly IClock _clock;

public ShiftTimeController(IClock clock) => _clock = clock;
public TimeShiftController(IClock clock) => _clock = clock;

public IActionResult Set(double days, double seconds) =>
SetInner(_ => TimeSpan.FromDays(days) + TimeSpan.FromSeconds(seconds));
Expand All @@ -20,7 +20,7 @@ public IActionResult Add(double days, double seconds) =>
SetInner(current => current + TimeSpan.FromDays(days) + TimeSpan.FromSeconds(seconds));

private IActionResult SetInner(Func<TimeSpan, TimeSpan> edit) =>
ShiftTimeClock.UpdateClock(_clock, edit) is { } totalSeconds
TimeShiftingClock.UpdateClock(_clock, edit) is { } totalSeconds
? Ok(totalSeconds)
: BadRequest($"The clock is {_clock.GetType().FullName} instead of {nameof(ShiftTimeClock)}.");
: BadRequest($"The clock is {_clock.GetType().FullName} instead of {nameof(TimeShiftingClock)}.");
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace Lombiq.Tests.UI.Shortcuts.Services;
/// cref="IClock"/> into believing it's the future or past. This service is registered as singleton, so setting <see
/// cref="Shift"/> will persist in the tenant across requests.
/// </summary>
public class ShiftTimeClock : IClock
public class TimeShiftingClock : IClock
{
private readonly Clock _inner = new();

Expand All @@ -34,18 +34,18 @@ public DateTimeOffset ConvertToTimeZone(DateTimeOffset dateTimeOffset, ITimeZone
/// Use this to safely update the <see cref="Shift"/> value.
/// </summary>
/// <param name="clock">
/// The injected service, if it's <see cref="ShiftTimeClock"/> then its <see cref="Shift"/> property is updated.
/// The injected service, if it's <see cref="TimeShiftingClock"/> then its <see cref="Shift"/> property is updated.
/// </param>
/// <param name="edit">Input is the current value of <see cref="Shift"/>, output is the new value.</param>
/// <returns>
/// The total seconds of the new value of <see cref="Shift"/>, or <see langword="null"/> if <paramref name="clock"/>
/// is not <see cref="ShiftTimeClock"/>.
/// is not <see cref="TimeShiftingClock"/>.
/// </returns>
public static double? UpdateClock(IClock clock, Func<TimeSpan, TimeSpan> edit)
{
if (clock is not ShiftTimeClock shiftTimeClock) return null;
if (clock is not TimeShiftingClock timeShiftingClock) return null;

shiftTimeClock.Shift = edit(shiftTimeClock.Shift);
return shiftTimeClock.Shift.TotalSeconds;
timeShiftingClock.Shift = edit(timeShiftingClock.Shift);
return timeShiftingClock.Shift.TotalSeconds;
}
}
10 changes: 5 additions & 5 deletions Lombiq.Tests.UI.Shortcuts/ShortcutsFeatureIds.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ public static class ShortcutsFeatureIds
public const string Area = "Lombiq.Tests.UI.Shortcuts";

public const string Default = Area;
public const string FeatureToggleTestBench = Default + ".FeatureToggleTestBench";
public const string MediaCachePurge = Default + ".MediaCachePurge";
public const string ShiftTime = Default + ".ShiftTime";
public const string Swagger = Default + ".Swagger";
public const string Workflows = Default + ".Workflows";
public const string FeatureToggleTestBench = $"{Default}.{nameof(FeatureToggleTestBench)}";
public const string MediaCachePurge = $"{Default}.{nameof(MediaCachePurge)}";
public const string ShiftTime = $"{Default}.{nameof(ShiftTime)}";
public const string Swagger = $"{Default}.{nameof(Swagger)}";
public const string Workflows = $"{Default}.{nameof(Workflows)}";
}
2 changes: 1 addition & 1 deletion Lombiq.Tests.UI.Shortcuts/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,6 @@ public sealed class SetTimeStartup : StartupBase
public override void ConfigureServices(IServiceCollection services)
{
services.RemoveImplementationsOf<IClock>();
services.AddSingleton<IClock, ShiftTimeClock>();
services.AddSingleton<IClock, TimeShiftingClock>();
}
}
18 changes: 10 additions & 8 deletions Lombiq.Tests.UI/Extensions/ShortcutsUITestContextExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -744,23 +744,25 @@ public static Task ExecuteJsonRecipeSiteSettingAsync<T>(this UITestContext conte
public static Task EnableTimeShiftingAsync(this UITestContext context) => context.EnableFeatureDirectlyAsync(ShiftTime);

/// <summary>
/// Sets the time shift to a specific value. If both <paramref name="days"/> and <paramref name="seconds"/> are
/// provided, then the <see cref="TimeSpan"/> values are added together.
/// Sets the time shift to a specific value.
/// </summary>
public static Task SetShiftTimeAsync(this UITestContext context, double days = 0, double seconds = 0)
public static Task SetTimeShiftAsync(this UITestContext context, TimeSpan time)
{
context.EnsureValidOrchardCoreTenantScope();
return context.GoToAsync<ShiftTimeController>(controller => controller.Set(days, seconds));
return time.TotalDays >= 1.0
? context.GoToAsync<TimeShiftController>(controller => controller.Set(time.TotalDays, 0))
: context.GoToAsync<TimeShiftController>(controller => controller.Set(0, time.TotalSeconds));
}

/// <summary>
/// Adds the specified value to the time shift. If both <paramref name="days"/> and <paramref name="seconds"/> are
/// provided, then the <see cref="TimeSpan"/> values for both are added. Negative values are supported as well.
/// Adds the specified value to the time shift.
/// </summary>
public static Task AddShiftTimeAsync(this UITestContext context, double days = 0, double seconds = 0)
public static Task AddTimeShiftAsync(this UITestContext context, TimeSpan time)
{
context.EnsureValidOrchardCoreTenantScope();
return context.GoToAsync<ShiftTimeController>(controller => controller.Add(days, seconds));
return time.TotalDays >= 1.0
? context.GoToAsync<TimeShiftController>(controller => controller.Add(time.TotalDays, 0))
: context.GoToAsync<TimeShiftController>(controller => controller.Add(0, time.TotalSeconds));
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion Lombiq.Tests.UI/Lombiq.Tests.UI.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
<PackageReference Include="Atata.Bootstrap" Version="3.0.0" />
<PackageReference Include="Atata.HtmlValidation" Version="3.1.0" />
<PackageReference Include="Atata.WebDriverExtras" Version="3.0.0" />
<PackageReference Include="Atata.WebDriverSetup" Version="2.14.0" />
<PackageReference Include="Atata.WebDriverSetup" Version="3.1.0" />
<PackageReference Include="Azure.Storage.Blobs" Version="12.19.1" />
<PackageReference Include="Ben.Demystifier" Version="0.4.1" />
<PackageReference Include="Codeuctivity.ImageSharpCompare" Version="4.0.258" />
Expand Down

0 comments on commit d1ffcb6

Please sign in to comment.