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

Decouple with a mocked service #2250

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
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
4 changes: 4 additions & 0 deletions .github/workflows/action-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,10 @@ jobs:
msiexec.exe /i wad.msi /quiet /passive
shell: powershell
name: Install WindowsApplicationDriver
- run: |
netsh http add urlacl url="http://localhost:80/calctesting/file" user=everyone
shell: powershell
name: enable local urls
- uses: ilammy/msvc-dev-cmd@v1 # this is a workaround because microsoft/vstest-action is broken.
name: Setup dev tools
- run: |
Expand Down
4 changes: 2 additions & 2 deletions src/CalcViewModel/DataLoaders/DataLoaderMockConstants.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace CalculatorApp
{
namespace DataLoaders
{
static constexpr auto sc_MetadataUriLocalizeFor = L"https://go.microsoft.com/fwlink/?linkid=2091028&localizeFor=";
static constexpr auto sc_RatiosUriRelativeTo = L"https://go.microsoft.com/fwlink/?linkid=2091307&localCurrency=";
static constexpr auto sc_MetadataUriLocalizeFor = L"http://localhost/calctesting/file/?id=currency+static+data&localizeFor=";
static constexpr auto sc_RatiosUriRelativeTo = L"http://localhost/calctesting/file/?id=currency+converter+data&localCurrency=";
}
}
66 changes: 42 additions & 24 deletions src/Calculator.sln
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "TraceLogging", "TraceLoggin
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "CalcViewModelCopyForUT", "CalcViewModelCopyForUT\CalcViewModelCopyForUT.vcxproj", "{CC9B4FA7-D746-4F52-9401-0AD1B4D6B16D}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CalculatorUITestsService", "CalculatorUITestsService\CalculatorUITestsService.csproj", "{779F9E46-8970-4B48-AA04-85CFA54641EB}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|ARM = Debug|ARM
Expand All @@ -41,6 +43,30 @@ Global
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{3B773403-B0D6-4F9A-948E-512A7A5FB315}.Debug|ARM.ActiveCfg = Debug|ARM
{3B773403-B0D6-4F9A-948E-512A7A5FB315}.Debug|ARM.Build.0 = Debug|ARM
{3B773403-B0D6-4F9A-948E-512A7A5FB315}.Debug|ARM.Deploy.0 = Debug|ARM
{3B773403-B0D6-4F9A-948E-512A7A5FB315}.Debug|ARM64.ActiveCfg = Debug|ARM64
{3B773403-B0D6-4F9A-948E-512A7A5FB315}.Debug|ARM64.Build.0 = Debug|ARM64
{3B773403-B0D6-4F9A-948E-512A7A5FB315}.Debug|ARM64.Deploy.0 = Debug|ARM64
{3B773403-B0D6-4F9A-948E-512A7A5FB315}.Debug|x64.ActiveCfg = Debug|x64
{3B773403-B0D6-4F9A-948E-512A7A5FB315}.Debug|x64.Build.0 = Debug|x64
{3B773403-B0D6-4F9A-948E-512A7A5FB315}.Debug|x64.Deploy.0 = Debug|x64
{3B773403-B0D6-4F9A-948E-512A7A5FB315}.Debug|x86.ActiveCfg = Debug|x86
{3B773403-B0D6-4F9A-948E-512A7A5FB315}.Debug|x86.Build.0 = Debug|x86
{3B773403-B0D6-4F9A-948E-512A7A5FB315}.Debug|x86.Deploy.0 = Debug|x86
{3B773403-B0D6-4F9A-948E-512A7A5FB315}.Release|ARM.ActiveCfg = Release|ARM
{3B773403-B0D6-4F9A-948E-512A7A5FB315}.Release|ARM.Build.0 = Release|ARM
{3B773403-B0D6-4F9A-948E-512A7A5FB315}.Release|ARM.Deploy.0 = Release|ARM
{3B773403-B0D6-4F9A-948E-512A7A5FB315}.Release|ARM64.ActiveCfg = Release|ARM64
{3B773403-B0D6-4F9A-948E-512A7A5FB315}.Release|ARM64.Build.0 = Release|ARM64
{3B773403-B0D6-4F9A-948E-512A7A5FB315}.Release|ARM64.Deploy.0 = Release|ARM64
{3B773403-B0D6-4F9A-948E-512A7A5FB315}.Release|x64.ActiveCfg = Release|x64
{3B773403-B0D6-4F9A-948E-512A7A5FB315}.Release|x64.Build.0 = Release|x64
{3B773403-B0D6-4F9A-948E-512A7A5FB315}.Release|x64.Deploy.0 = Release|x64
{3B773403-B0D6-4F9A-948E-512A7A5FB315}.Release|x86.ActiveCfg = Release|x86
{3B773403-B0D6-4F9A-948E-512A7A5FB315}.Release|x86.Build.0 = Release|x86
{3B773403-B0D6-4F9A-948E-512A7A5FB315}.Release|x86.Deploy.0 = Release|x86
{311E866D-8B93-4609-A691-265941FEE101}.Debug|ARM.ActiveCfg = Debug|ARM
{311E866D-8B93-4609-A691-265941FEE101}.Debug|ARM.Build.0 = Debug|ARM
{311E866D-8B93-4609-A691-265941FEE101}.Debug|ARM64.ActiveCfg = Debug|ARM64
Expand Down Expand Up @@ -169,30 +195,6 @@ Global
{FC81FF41-02CD-4CD9-9BC5-45A1E39AC6ED}.Release|x64.Build.0 = Release|x64
{FC81FF41-02CD-4CD9-9BC5-45A1E39AC6ED}.Release|x86.ActiveCfg = Release|Win32
{FC81FF41-02CD-4CD9-9BC5-45A1E39AC6ED}.Release|x86.Build.0 = Release|Win32
{3B773403-B0D6-4F9A-948E-512A7A5FB315}.Debug|ARM.ActiveCfg = Debug|ARM
{3B773403-B0D6-4F9A-948E-512A7A5FB315}.Debug|ARM.Build.0 = Debug|ARM
{3B773403-B0D6-4F9A-948E-512A7A5FB315}.Debug|ARM.Deploy.0 = Debug|ARM
{3B773403-B0D6-4F9A-948E-512A7A5FB315}.Debug|ARM64.ActiveCfg = Debug|ARM64
{3B773403-B0D6-4F9A-948E-512A7A5FB315}.Debug|ARM64.Build.0 = Debug|ARM64
{3B773403-B0D6-4F9A-948E-512A7A5FB315}.Debug|ARM64.Deploy.0 = Debug|ARM64
{3B773403-B0D6-4F9A-948E-512A7A5FB315}.Debug|x64.ActiveCfg = Debug|x64
{3B773403-B0D6-4F9A-948E-512A7A5FB315}.Debug|x64.Build.0 = Debug|x64
{3B773403-B0D6-4F9A-948E-512A7A5FB315}.Debug|x64.Deploy.0 = Debug|x64
{3B773403-B0D6-4F9A-948E-512A7A5FB315}.Debug|x86.ActiveCfg = Debug|x86
{3B773403-B0D6-4F9A-948E-512A7A5FB315}.Debug|x86.Build.0 = Debug|x86
{3B773403-B0D6-4F9A-948E-512A7A5FB315}.Debug|x86.Deploy.0 = Debug|x86
{3B773403-B0D6-4F9A-948E-512A7A5FB315}.Release|ARM.ActiveCfg = Release|ARM
{3B773403-B0D6-4F9A-948E-512A7A5FB315}.Release|ARM.Build.0 = Release|ARM
{3B773403-B0D6-4F9A-948E-512A7A5FB315}.Release|ARM.Deploy.0 = Release|ARM
{3B773403-B0D6-4F9A-948E-512A7A5FB315}.Release|ARM64.ActiveCfg = Release|ARM64
{3B773403-B0D6-4F9A-948E-512A7A5FB315}.Release|ARM64.Build.0 = Release|ARM64
{3B773403-B0D6-4F9A-948E-512A7A5FB315}.Release|ARM64.Deploy.0 = Release|ARM64
{3B773403-B0D6-4F9A-948E-512A7A5FB315}.Release|x64.ActiveCfg = Release|x64
{3B773403-B0D6-4F9A-948E-512A7A5FB315}.Release|x64.Build.0 = Release|x64
{3B773403-B0D6-4F9A-948E-512A7A5FB315}.Release|x64.Deploy.0 = Release|x64
{3B773403-B0D6-4F9A-948E-512A7A5FB315}.Release|x86.ActiveCfg = Release|x86
{3B773403-B0D6-4F9A-948E-512A7A5FB315}.Release|x86.Build.0 = Release|x86
{3B773403-B0D6-4F9A-948E-512A7A5FB315}.Release|x86.Deploy.0 = Release|x86
{CC9B4FA7-D746-4F52-9401-0AD1B4D6B16D}.Debug|ARM.ActiveCfg = Debug|ARM
{CC9B4FA7-D746-4F52-9401-0AD1B4D6B16D}.Debug|ARM.Build.0 = Debug|ARM
{CC9B4FA7-D746-4F52-9401-0AD1B4D6B16D}.Debug|ARM64.ActiveCfg = Debug|ARM64
Expand All @@ -207,6 +209,22 @@ Global
{CC9B4FA7-D746-4F52-9401-0AD1B4D6B16D}.Release|x64.Build.0 = Release|x64
{CC9B4FA7-D746-4F52-9401-0AD1B4D6B16D}.Release|x86.ActiveCfg = Release|Win32
{CC9B4FA7-D746-4F52-9401-0AD1B4D6B16D}.Release|x86.Build.0 = Release|Win32
{779F9E46-8970-4B48-AA04-85CFA54641EB}.Debug|ARM.ActiveCfg = Debug|Any CPU
{779F9E46-8970-4B48-AA04-85CFA54641EB}.Debug|ARM.Build.0 = Debug|Any CPU
{779F9E46-8970-4B48-AA04-85CFA54641EB}.Debug|ARM64.ActiveCfg = Debug|Any CPU
{779F9E46-8970-4B48-AA04-85CFA54641EB}.Debug|ARM64.Build.0 = Debug|Any CPU
{779F9E46-8970-4B48-AA04-85CFA54641EB}.Debug|x64.ActiveCfg = Debug|Any CPU
{779F9E46-8970-4B48-AA04-85CFA54641EB}.Debug|x64.Build.0 = Debug|Any CPU
{779F9E46-8970-4B48-AA04-85CFA54641EB}.Debug|x86.ActiveCfg = Debug|Any CPU
{779F9E46-8970-4B48-AA04-85CFA54641EB}.Debug|x86.Build.0 = Debug|Any CPU
{779F9E46-8970-4B48-AA04-85CFA54641EB}.Release|ARM.ActiveCfg = Release|Any CPU
{779F9E46-8970-4B48-AA04-85CFA54641EB}.Release|ARM.Build.0 = Release|Any CPU
{779F9E46-8970-4B48-AA04-85CFA54641EB}.Release|ARM64.ActiveCfg = Release|Any CPU
{779F9E46-8970-4B48-AA04-85CFA54641EB}.Release|ARM64.Build.0 = Release|Any CPU
{779F9E46-8970-4B48-AA04-85CFA54641EB}.Release|x64.ActiveCfg = Release|Any CPU
{779F9E46-8970-4B48-AA04-85CFA54641EB}.Release|x64.Build.0 = Release|Any CPU
{779F9E46-8970-4B48-AA04-85CFA54641EB}.Release|x86.ActiveCfg = Release|Any CPU
{779F9E46-8970-4B48-AA04-85CFA54641EB}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
12 changes: 12 additions & 0 deletions src/CalculatorUITestFramework/CalculatorUITestFramework.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,18 @@
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<None Remove="MockCurrencyConverterData.json" />
<None Remove="MockCurrencyStaticData.json" />
</ItemGroup>
<ItemGroup>
<Content Include="MockCurrencyConverterData.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="MockCurrencyStaticData.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>
<ItemGroup>
<PackageReference Include="Appium.WebDriver" Version="4.4.0" />
<PackageReference Include="MSTest.TestFramework" Version="2.2.10" />
Expand Down
42 changes: 42 additions & 0 deletions src/CalculatorUITestFramework/MockCurrencyConverterData.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
[
{
"An": "MAR",
"Rt": 1.00
},
{
"An": "MON",
"Rt": 0.50
},
{
"An": "NEP",
"Rt": 0.00125
},
{
"An": "SAT",
"Rt": 0.25
},
{
"An": "URA",
"Rt": 2.75
},
{
"An": "VEN",
"Rt": 900.00
},
{
"An": "JUP",
"Rt": 1.23456789123456789
},
{
"An": "MER",
"Rt": 2.00
},
{
"An": "JPY",
"Rt": 0.00125
},
{
"An": "JOD",
"Rt": 0.25
}
]
72 changes: 72 additions & 0 deletions src/CalculatorUITestFramework/MockCurrencyStaticData.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
[
{
"CountryCode": "MAR",
"CountryName": "Mars",
"CurrencyCode": "MAR",
"CurrencyName": "The Martian Dollar",
"CurrencySymbol": "¤"
},
{
"CountryCode": "MON",
"CountryName": "Moon",
"CurrencyCode": "MON",
"CurrencyName": "Moon Bucks",
"CurrencySymbol": "¤"
},
{
"CountryCode": "NEP",
"CountryName": "Neptune",
"CurrencyCode": "NEP",
"CurrencyName": "Space Coins",
"CurrencySymbol": "¤"
},
{
"CountryCode": "SAT",
"CountryName": "Saturn",
"CurrencyCode": "SAT",
"CurrencyName": "Rings",
"CurrencySymbol": "¤"
},
{
"CountryCode": "URA",
"CountryName": "Uranus",
"CurrencyCode": "URA",
"CurrencyName": "Galaxy Credits",
"CurrencySymbol": "¤"
},
{
"CountryCode": "VEN",
"CountryName": "Venus",
"CurrencyCode": "VEN",
"CurrencyName": "Venusian Seashells",
"CurrencySymbol": "¤"
},
{
"CountryCode": "JUP",
"CountryName": "Jupiter",
"CurrencyCode": "JUP",
"CurrencyName": "Gas Money",
"CurrencySymbol": "¤"
},
{
"CountryCode": "MER",
"CountryName": "Mercury",
"CurrencyCode": "MER",
"CurrencyName": "Sun Notes",
"CurrencySymbol": "¤"
},
{
"CountryCode": "TEST1",
"CountryName": "Test No Fractional Digits",
"CurrencyCode": "JPY",
"CurrencyName": "Test No Fractional Digits",
"CurrencySymbol": "¤"
},
{
"CountryCode": "TEST2",
"CountryName": "Test Fractional Digits",
"CurrencyCode": "JOD",
"CurrencyName": "Test Fractional Digits",
"CurrencySymbol": "¤"
}
]
83 changes: 83 additions & 0 deletions src/CalculatorUITestFramework/MockedCurrencyServer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
using System;
using System.IO;
using System.Net;
using System.Web;

namespace CalculatorUITestFramework
{
public class MockedCurrencyServer : IDisposable
{
private HttpListener _listener;

public MockedCurrencyServer()
{
_listener = new HttpListener
{
Prefixes =
{
// to trust below URL on your dev machine, run below command in cmd as admin
// netsh http add urlacl url="http://localhost:80/calctesting/file" user=everyone
"http://localhost/calctesting/file/",
}
};
_listener.Start();
_listener.BeginGetContext(OnGetContext, _listener);
}

~MockedCurrencyServer() { DisposeImpl(); }

public void Dispose()
{
DisposeImpl();
GC.SuppressFinalize(this);
}

private void DisposeImpl()
{
_listener.Stop();
}

private static void OnGetContext(IAsyncResult result)
{
var listener = (HttpListener)result.AsyncState;
var context = listener.EndGetContext(result);
var request = context.Request;
var response = context.Response;
if (request.HttpMethod == "GET" && TryGetServerFile(request.Url, out var content))
{
response.Headers.Add("Accept-Ranges", "bytes");
response.Headers.Add("Content-Type", "application/json");
var data = System.Text.Encoding.UTF8.GetBytes(content);
response.StatusCode = 200;
response.ContentLength64 = data.Length;
response.OutputStream.Write(data, 0, data.Length);
response.OutputStream.Close();
response.Close();
}
else
{
response.StatusCode = 400;
response.OutputStream.Close();
}
listener.BeginGetContext(OnGetContext, listener);
}

private static bool TryGetServerFile(Uri uri, out string content)
{
var query = HttpUtility.ParseQueryString(uri.Query);
var name = query["id"];
switch (name)
{
case "currency converter data":
content = File.ReadAllText("MockCurrencyConverterData.json");
return true;
case "currency static data":
content = File.ReadAllText("MockCurrencyStaticData.json");
return true;
default:
content = null;
return false;
tian-lt marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
}
3 changes: 3 additions & 0 deletions src/CalculatorUITests/CurrencyConverterFunctionalTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ namespace CalculatorUITests
public class CurrencyConverterFunctionalTests
{
private static readonly UnitConverterPage page = new UnitConverterPage();
private static MockedCurrencyServer _currencyServer;

public TestContext TestContext { get; set; }

Expand All @@ -21,6 +22,7 @@ public class CurrencyConverterFunctionalTests
public static void ClassInitialize(TestContext context)
{
// Create session to launch a Calculator window
_currencyServer = new MockedCurrencyServer();
CalculatorDriver.Instance.SetupCalculatorSession(context);
}

Expand All @@ -32,6 +34,7 @@ public static void ClassCleanup()
{
// Tear down Calculator session.
CalculatorDriver.Instance.TearDownCalculatorSession();
_currencyServer.Dispose();
}

/// <summary>
Expand Down
14 changes: 14 additions & 0 deletions src/CalculatorUITestsService/CalculatorUITestsService.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

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

</Project>
5 changes: 5 additions & 0 deletions src/CalculatorUITestsService/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
using CalculatorUITestFramework;

using var server = new MockedCurrencyServer();
Console.WriteLine("Press any key to exit:");
Console.ReadKey();
Loading