Skip to content
This repository was archived by the owner on Jun 21, 2023. It is now read-only.

Commit c3fd4c7

Browse files
committed
Enhance AppDomainContext and readability of tests
1 parent e7df656 commit c3fd4c7

7 files changed

+263
-228
lines changed

test/GitHub.UI.UnitTests/GitHub.UI.UnitTests.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,9 @@
5555
<ItemGroup>
5656
<Compile Include="Helpers\AppDomainContext.cs" />
5757
<Compile Include="Helpers\ResourceDictionaryUtilities.cs" />
58-
<Compile Include="Helpers\SharedDictionaryManagerBaseTests.cs" />
5958
<Compile Include="Helpers\SharedDictionaryManagerTests.cs" />
60-
<Compile Include="Helpers\LoadingResourceDictionaryTests.cs" />
59+
<Compile Include="Helpers\SharedDictionaryManagerIntegrationTests.cs" />
60+
<Compile Include="Helpers\LoadingResourceDictionaryIntegrationTests.cs" />
6161
<Compile Include="Helpers\Urls.cs" />
6262
<Compile Include="Properties\AssemblyInfo.cs" />
6363
</ItemGroup>
Lines changed: 54 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,76 @@
11
using System;
2+
using System.Reflection;
3+
using System.Collections.Generic;
24

35
namespace GitHub.UI.Helpers.UnitTests
46
{
57
public class AppDomainContext : IDisposable
68
{
79
AppDomain domain;
810

9-
public AppDomainContext(AppDomainSetup setup)
11+
public static void Invoke(AppDomainSetup setup, Action remoteAction)
1012
{
13+
using (var context = new AppDomainContext(setup))
14+
{
15+
context.Invoke(remoteAction);
16+
}
17+
}
18+
19+
public AppDomainContext(AppDomainSetup setup = null)
20+
{
21+
if(setup == null)
22+
{
23+
setup = new AppDomainSetup();
24+
setup.ApplicationBase = AppDomain.CurrentDomain.BaseDirectory; // don't use the process base dir
25+
}
26+
1127
var friendlyName = GetType().FullName;
1228
domain = AppDomain.CreateDomain(friendlyName, null, setup);
1329
}
1430

31+
public void Dispose()
32+
{
33+
AppDomain.Unload(domain);
34+
}
35+
36+
public void Invoke(Action remoteAction)
37+
{
38+
var targetType = remoteAction.Target.GetType();
39+
var fieldValues = new Dictionary<string, object>();
40+
foreach (var field in targetType.GetFields())
41+
{
42+
var value = field.GetValue(remoteAction.Target);
43+
fieldValues[field.Name] = value;
44+
}
45+
46+
var remove = CreateInstance<Remote>();
47+
var assemblyFile = targetType.Assembly.Location;
48+
var typeName = targetType.FullName;
49+
var methodName = remoteAction.Method.Name;
50+
remove.Invoke(assemblyFile, typeName, methodName, fieldValues);
51+
}
52+
1553
public T CreateInstance<T>()
1654
{
1755
return (T)domain.CreateInstanceFromAndUnwrap(typeof(T).Assembly.CodeBase, typeof(T).FullName);
1856
}
1957

20-
public void Dispose()
58+
class Remote : MarshalByRefObject
2159
{
22-
AppDomain.Unload(domain);
60+
internal void Invoke(string assemblyFile, string typeName, string methodName, Dictionary<string, object> fieldValues)
61+
{
62+
var assembly = Assembly.LoadFrom(assemblyFile);
63+
var type = assembly.GetType(typeName);
64+
var obj = Activator.CreateInstance(type);
65+
foreach (var field in type.GetFields())
66+
{
67+
var value = fieldValues[field.Name];
68+
field.SetValue(obj, value);
69+
}
70+
71+
var dele = (Action)Delegate.CreateDelegate(typeof(Action), obj, methodName);
72+
dele();
73+
}
2374
}
2475
}
2576
}

test/GitHub.UI.UnitTests/Helpers/LoadingResourceDictionaryTests.cs renamed to test/GitHub.UI.UnitTests/Helpers/LoadingResourceDictionaryIntegrationTests.cs

Lines changed: 13 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
namespace GitHub.UI.Helpers.UnitTests
77
{
8-
public class LoadingResourceDictionaryTests
8+
public class LoadingResourceDictionaryIntegrationTests
99
{
1010
public class TheSourceProperty
1111
{
@@ -16,14 +16,15 @@ public class TheSourceProperty
1616
public void SetInLoadFromContext(string url)
1717
{
1818
var setup = new AppDomainSetup { ApplicationBase = "NOTHING_HERE" };
19-
using (var context = new AppDomainContext(setup))
19+
AppDomainContext.Invoke(setup, () =>
2020
{
21-
var remote = context.CreateInstance<LoadingResourceDictionaryContext>();
21+
var target = new LoadingResourceDictionary();
22+
var packUri = ResourceDictionaryUtilities.ToPackUri(url);
2223

23-
int count = remote.CountMergedDictionaries(url);
24+
target.Source = packUri;
2425

25-
Assert.That(count, Is.GreaterThan(0));
26-
}
26+
Assert.That(target.MergedDictionaries.Count, Is.GreaterThan(0));
27+
});
2728
}
2829

2930
[Description("Load assembly using LoadFrom on application base")]
@@ -32,24 +33,12 @@ public void SetInLoadFromContext(string url)
3233
[RequiresThread(System.Threading.ApartmentState.STA)]
3334
public void SetInLoadContext(string url)
3435
{
35-
var local = new LoadingResourceDictionaryContext();
36+
var target = new LoadingResourceDictionary();
37+
var packUri = ResourceDictionaryUtilities.ToPackUri(url);
3638

37-
int count = local.CountMergedDictionaries(url);
39+
target.Source = packUri;
3840

39-
Assert.That(count, Is.GreaterThan(0));
40-
}
41-
42-
class LoadingResourceDictionaryContext : MarshalByRefObject
43-
{
44-
internal int CountMergedDictionaries(string url)
45-
{
46-
var target = new LoadingResourceDictionary();
47-
var packUri = ResourceDictionaryUtilities.ToPackUri(url);
48-
49-
target.Source = packUri;
50-
51-
return target.MergedDictionaries.Count;
52-
}
41+
Assert.That(target.MergedDictionaries.Count, Is.GreaterThan(0));
5342
}
5443
}
5544

@@ -65,20 +54,17 @@ public void SetInLoadFromContext(string url)
6554
{
6655
var remote = context.CreateInstance<ResourceDictionaryContext>();
6756

68-
Assert.Throws<FileNotFoundException>(() => remote.CountMergedDictionaries(url));
57+
Assert.Throws<FileNotFoundException>(() => remote.CountResourceDictionaryAndSetSource(url));
6958
}
7059
}
7160

7261
class ResourceDictionaryContext : MarshalByRefObject
7362
{
74-
internal int CountMergedDictionaries(string url)
63+
internal void CountResourceDictionaryAndSetSource(string url)
7564
{
7665
var target = new ResourceDictionary();
7766
var packUri = ResourceDictionaryUtilities.ToPackUri(url);
78-
7967
target.Source = packUri;
80-
81-
return target.MergedDictionaries.Count;
8268
}
8369
}
8470
}

test/GitHub.UI.UnitTests/Helpers/ResourceDictionaryUtilities.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,18 @@ public static Uri ToPackUri(string url)
1717
return new Uri(url);
1818
}
1919

20+
public static string DumpMergedDictionaries(ResourceDictionary target, string url)
21+
{
22+
SetProperty(target, "Source", ToPackUri(url));
23+
return DumpResourceDictionary(target);
24+
}
25+
26+
static void SetProperty(object target, string name, object value)
27+
{
28+
var prop = target.GetType().GetProperty(name);
29+
prop.SetValue(target, value);
30+
}
31+
2032
public static string DumpResourceDictionary(ResourceDictionary rd, string indent = "")
2133
{
2234
var writer = new StringWriter();

test/GitHub.UI.UnitTests/Helpers/SharedDictionaryManagerBaseTests.cs

Lines changed: 0 additions & 156 deletions
This file was deleted.

0 commit comments

Comments
 (0)