Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Simplify unit test detection and design mode detection.

Remove old platform directives.
Move UnitTestDetector and DesignModeDetector to their own files.
  • Loading branch information...
commit 8b9489852884b509c423359ffb7ad9fa11770d54 1 parent c8a0e4f
Johan Laanstra jlaanstra authored
3  ReactiveUI.Tests/ReactiveUI.Tests_Net45.csproj
View
@@ -84,9 +84,6 @@
<Reference Include="System.Reactive.Runtime.Remoting">
<HintPath>..\ext\net45\System.Reactive.Runtime.Remoting.dll</HintPath>
</Reference>
- <Reference Include="System.Reactive.Windows.Forms">
- <HintPath>..\ext\net45\System.Reactive.Windows.Forms.dll</HintPath>
- </Reference>
<Reference Include="System.Reactive.Windows.Threading, Version=2.1.30204.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\ext\net45\System.Reactive.Windows.Threading.dll</HintPath>
44 ReactiveUI.Tests/RxAppTest.cs
View
@@ -29,27 +29,7 @@ public void SchedulerShouldBeImmediateInTestRunner()
[Fact]
public void UnitTestDetectorIdentifiesThisTestAsAnXUnitTest()
{
- string[] testAssemblies = new[] {
- "CSUNIT",
- "NUNIT",
- "XUNIT",
- "MBUNIT",
- "TESTDRIVEN",
- "QUALITYTOOLS.TIPS.UNITTEST.ADAPTER",
- "QUALITYTOOLS.UNITTESTING.SILVERLIGHT",
- "PEX",
- "MSBUILD",
- "NBEHAVE",
- "TESTPLATFORM",
- };
-
- string[] designEnvironments = new[] {
- "BLEND.EXE",
- "MONODEVELOP",
- "SHARPDEVELOP.EXE",
- };
-
- var isInUnitTestRunner = RealUnitTestDetector.InUnitTestRunner(testAssemblies, designEnvironments);
+ var isInUnitTestRunner = UnitTestDetector.IsInUnitTestRunner();
Assert.True(isInUnitTestRunner);
}
@@ -57,27 +37,9 @@ public void UnitTestDetectorIdentifiesThisTestAsAnXUnitTest()
[Fact]
public void UnitTestDetectorDoesNotIdentifyThisTestWhenXUnitAssemblyNotChecked()
{
- // XUnit assembly name removed
- string[] testAssembliesWithoutNunit = new[] {
- "CSUNIT",
- "NUNIT",
- "MBUNIT",
- "TESTDRIVEN",
- "QUALITYTOOLS.TIPS.UNITTEST.ADAPTER",
- "QUALITYTOOLS.UNITTESTING.SILVERLIGHT",
- "PEX",
- "MSBUILD",
- "NBEHAVE",
- "TESTPLATFORM",
- };
-
- string[] designEnvironments = new[] {
- "BLEND.EXE",
- "MONODEVELOP",
- "SHARPDEVELOP.EXE",
- };
+ string vsUnitTest = "Microsoft.VisualStudio.TestTools.UnitTesting.TestMethodAttribute, Microsoft.VisualStudio.QualityTools.UnitTestFramework";
- var isInUnitTestRunner = RealUnitTestDetector.InUnitTestRunner(testAssembliesWithoutNunit, designEnvironments);
+ var isInUnitTestRunner = UnitTestDetector.IsInUnitTestRunner(vsUnitTest);
Assert.False(isInUnitTestRunner);
}
50 ReactiveUI/DesignModeDetector.cs
View
@@ -0,0 +1,50 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Reflection;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace ReactiveUI
+{
+ public class DesignModeDetector
+ {
+ public static bool IsInDesignMode()
+ {
+ // Check Silverlight Design Mode
+ var dm = Type.GetType("System.ComponentModel.DesignerProperties, System.Windows, Version=2.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e", false);
+ if (dm != null)
+ {
+ MethodInfo mInfo = dm.GetMethod("GetIsInDesignMode");
+ Type dependencyObject = Type.GetType("System.Windows.DependencyObject, System.Windows, Version=2.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e", false);
+ if (dependencyObject != null)
+ {
+ return (bool)mInfo.Invoke(null, new object[] { Activator.CreateInstance(dependencyObject) });
+ }
+ return false;
+ }
+
+ // Check .NET
+ var cmdm = Type.GetType("System.ComponentModel.DesignerProperties, PresentationFramework, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35", false);
+ if (cmdm != null) // loaded the assembly, could be .net
+ {
+ MethodInfo mInfo = cmdm.GetMethod("GetIsInDesignMode");
+ Type dependencyObject = Type.GetType("System.Windows.DependencyObject, WindowsBase, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35", false);
+ if(dependencyObject != null)
+ {
+ return (bool)mInfo.Invoke(null, new object[] { Activator.CreateInstance(dependencyObject) });
+ }
+ return false;
+ }
+
+ // check WinRT next
+ var wadm = Type.GetType("Windows.ApplicationModel.DesignMode, Windows, ContentType=WindowsRuntime", false);
+ if (wadm != null)
+ {
+ return (bool)cmdm.GetProperty("IsDesignModeEnabled").GetMethod.Invoke(null, null);
+ }
+
+ return false;
+ }
+ }
+}
16 ReactiveUI/NullDefaultPropertyBindingProvider.cs
View
@@ -0,0 +1,16 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace ReactiveUI
+{
+ public class NullDefaultPropertyBindingProvider : IDefaultPropertyBindingProvider
+ {
+ public Tuple<string, int> GetPropertyForControl(object control)
+ {
+ return null;
+ }
+ }
+}
3  ReactiveUI/ReactiveUI.csproj
View
@@ -50,6 +50,7 @@
<Compile Include="CompatMixins.cs" />
<Compile Include="ContractStubs.cs" />
<Compile Include="DefaultPropertyBinding.cs" />
+ <Compile Include="DesignModeDetector.cs" />
<Compile Include="INPCObservableForProperty.cs" />
<Compile Include="Interfaces.cs" />
<Compile Include="IRNPCObservableForProperty.cs" />
@@ -57,6 +58,7 @@
<Compile Include="MakeObjectReactiveHelper.cs" />
<Compile Include="MemoizingMRUCache.cs" />
<Compile Include="MessageBus.cs" />
+ <Compile Include="NullDefaultPropertyBindingProvider.cs" />
<Compile Include="ObservableAsPropertyHelper.cs" />
<Compile Include="ObservableAsyncMRUCache.cs" />
<Compile Include="ObservedChangedMixin.cs" />
@@ -78,6 +80,7 @@
<Compile Include="RxRouting.cs" />
<Compile Include="ScheduledSubject.cs" />
<Compile Include="ReactiveValidatedObject.cs" />
+ <Compile Include="UnitTestDetector.cs" />
<Compile Include="VariadicTemplates.cs">
<AutoGen>True</AutoGen>
<DesignTime>True</DesignTime>
136 ReactiveUI/RxApp.cs
View
@@ -16,10 +16,6 @@
using System.Threading.Tasks;
-#if SILVERLIGHT
-using System.Windows;
-#endif
-
#if WINRT
using Windows.ApplicationModel;
using System.Reactive.Windows.Foundation;
@@ -242,7 +238,7 @@ public static bool InUnitTestRunner()
// NB: This is in a separate static ctor to avoid a deadlock on
// the static ctor lock when blocking on async methods
- _inUnitTestRunner = RealUnitTestDetector.InUnitTestRunner();
+ _inUnitTestRunner = UnitTestDetector.IsInUnitTestRunner() || DesignModeDetector.IsInDesignMode();
return _inUnitTestRunner.Value;
}
@@ -351,7 +347,6 @@ static IEnumerable<string> attemptToEarlyLoadReactiveUIDLLs()
{
var guiLibs = new[] {
"ReactiveUI.Xaml",
- "ReactiveUI.Routing",
"ReactiveUI.Gtk",
"ReactiveUI.Cocoa",
"ReactiveUI.Android",
@@ -359,36 +354,9 @@ static IEnumerable<string> attemptToEarlyLoadReactiveUIDLLs()
"ReactiveUI.Mobile",
};
-#if WINRT || WP8 || PORTABLE
+#if PORTABLE
// NB: WinRT hates your Freedom
- return new[] {"ReactiveUI.Xaml", "ReactiveUI.Routing", "ReactiveUI.Mobile", };
-#elif SILVERLIGHT
- return new[] {"ReactiveUI.Xaml", "ReactiveUI.Routing"};
-#else
- var name = Assembly.GetExecutingAssembly().GetName();
- var suffix = getArchSuffixForPath(Assembly.GetExecutingAssembly().Location);
-
- return guiLibs.SelectMany(x => {
- var fullName = String.Format("{0}{1}, Version={2}, Culture=neutral, PublicKeyToken=null", x, suffix, name.Version.ToString());
-
- var assemblyLocation = Assembly.GetExecutingAssembly().Location;
- if (String.IsNullOrEmpty(assemblyLocation))
- return Enumerable.Empty<string>();
-
- var path = Path.Combine(Path.GetDirectoryName(assemblyLocation), x + suffix + ".dll");
- if (!File.Exists(path) && !RxApp.InUnitTestRunner()) {
- LogHost.Default.Debug("Couldn't find {0}", path);
- return Enumerable.Empty<string>();
- }
-
- try {
- Assembly.Load(fullName);
- return new[] {x};
- } catch (Exception ex) {
- LogHost.Default.DebugException("Couldn't load " + x, ex);
- return Enumerable.Empty<string>();
- }
- });
+ return new[] { "ReactiveUI.Xaml", "ReactiveUI.Mobile", "ReactiveUI.NLog", };
#endif
}
@@ -400,103 +368,7 @@ static string getArchSuffixForPath(string path)
}
}
- public class NullDefaultPropertyBindingProvider : IDefaultPropertyBindingProvider
- {
- public Tuple<string, int> GetPropertyForControl(object control)
- {
- return null;
- }
- }
-
- internal static class RealUnitTestDetector
- {
- public static bool InUnitTestRunner(string[] testAssemblies, string[] designEnvironments)
- {
-#if SILVERLIGHT
- // NB: Deployment.Current.Parts throws an exception when accessed in Blend
- try {
- var ret = Deployment.Current.Parts.Any(x =>
- testAssemblies.Any(name => x.Source.ToUpperInvariant().Contains(name)));
-
- if (ret) {
- return ret;
- }
- } catch(Exception) {
- return true;
- }
-
- try {
- if (Application.Current.RootVisual != null && System.ComponentModel.DesignerProperties.GetIsInDesignMode(Application.Current.RootVisual)) {
- return false;
- }
- } catch {
- return true;
- }
-
- return false;
-#elif WINRT
- if (DesignMode.DesignModeEnabled) return true;
-
- var depPackages = Package.Current.Dependencies.Select(x => x.Id.FullName);
- if (depPackages.Any(x => testAssemblies.Any(name => x.ToUpperInvariant().Contains(name)))) return true;
-
-
- var fileTask = Task.Factory.StartNew(async () =>
- {
- var files = await Package.Current.InstalledLocation.GetFilesAsync();
- return files.Select(x => x.Path).ToArray();
- }, TaskCreationOptions.HideScheduler).Unwrap();
-
- return fileTask.Result.Any(x => testAssemblies.Any(name => x.ToUpperInvariant().Contains(name)));
-#elif PORTABLE
- //Figure out portable way to do this.
- return false;
-#else
- // Try to detect whether we're in design mode - bonus points,
- // without access to any WPF references :-/
- var entry = Assembly.GetEntryAssembly();
- if (entry != null) {
- var exeName = (new FileInfo(entry.Location)).Name.ToUpperInvariant();
-
- if (designEnvironments.Any(x => x.Contains(exeName))) {
- return true;
- }
- }
-
- return AppDomain.CurrentDomain.GetAssemblies().Any(x =>
- testAssemblies.Any(name => x.FullName.ToUpperInvariant().Contains(name)));
-#endif
- }
-
- public static bool InUnitTestRunner()
- {
- // XXX: This is hacky and evil, but I can't think of any better way
- // to do this
- string[] testAssemblies = new[] {
- "CSUNIT",
- "NUNIT",
- "XUNIT",
- "MBUNIT",
- "TESTDRIVEN",
- "QUALITYTOOLS.TIPS.UNITTEST.ADAPTER",
- "QUALITYTOOLS.UNITTESTING.SILVERLIGHT",
- "PEX",
- "MSBUILD",
- "NBEHAVE",
- "TESTPLATFORM",
- };
-
- string[] designEnvironments = new[] {
- "BLEND.EXE",
- "MONODEVELOP",
- "SHARPDEVELOP.EXE",
- "XDESPROC.EXE",
- };
-
- return InUnitTestRunner(testAssemblies, designEnvironments);
- }
-
- }
+
}
// vim: tw=120 ts=4 sw=4 et :
30 ReactiveUI/UnitTestDetector.cs
View
@@ -0,0 +1,30 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Reflection;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace ReactiveUI
+{
+ public class UnitTestDetector
+ {
+ /// <summary>
+ /// Private constructor to prevent instantiation.
+ /// </summary>
+ private UnitTestDetector() { }
+
+ /// <summary>
+ /// Detects if the app is running in a Unit Test runner by trying to load
+ /// the TestScheduler.
+ /// </summary>
+ /// <param name="testType">Type of the test.</param>
+ /// <returns></returns>
+ public static bool IsInUnitTestRunner(string testType = null)
+ {
+ // assuming Microsoft.Reactive.Testing is always used
+ string testSchedulerAQN = testType ?? "Microsoft.Reactive.Testing.TestScheduler, Microsoft.Reactive.Testing";
+ return Type.GetType(testSchedulerAQN, false) != null;
+ }
+ }
+}
Please sign in to comment.
Something went wrong with that request. Please try again.