Skip to content

Commit

Permalink
Fix InUnitTestRunner for WinRT
Browse files Browse the repository at this point in the history
  • Loading branch information
anaisbetts committed Dec 27, 2012
1 parent be5c861 commit a225f46
Showing 1 changed file with 38 additions and 3 deletions.
41 changes: 38 additions & 3 deletions ReactiveUI/RxApp.cs
Expand Up @@ -7,18 +7,24 @@
using System.Diagnostics.Contracts;
using System.Linq;
using System.Linq.Expressions;
using System.Reactive.Linq;
using System.Reactive.Subjects;
using System.Reflection;
using System.Text.RegularExpressions;
using System.Threading;

using System.Threading.Tasks;
using System.Reactive.Threading.Tasks;

#if SILVERLIGHT
using System.Windows;
#endif

#if WINRT
using Windows.ApplicationModel;
using System.Reactive.Windows.Foundation;
using System.Reactive.Threading.Tasks;
#endif

namespace ReactiveUI
{
/*
Expand Down Expand Up @@ -225,6 +231,8 @@ static RxApp()
/// </summary>
public static IObserver<Exception> DefaultExceptionHandler { get; set; }

static bool? _inUnitTestRunner;

/// <summary>
/// InUnitTestRunner attempts to determine heuristically if the current
/// application is running in a unit test framework.
Expand All @@ -237,6 +245,14 @@ public static bool InUnitTestRunner()
return InUnitTestRunnerOverride.Value;
}

if (_inUnitTestRunner.HasValue) return _inUnitTestRunner.Value;

_inUnitTestRunner = inUnitTestRunner();
return _inUnitTestRunner.Value;
}

static bool inUnitTestRunner()
{
// XXX: This is hacky and evil, but I can't think of any better way
// to do this
string[] testAssemblies = new[] {
Expand All @@ -250,6 +266,7 @@ public static bool InUnitTestRunner()
"PEX",
"MSBUILD",
"NBEHAVE",
"TESTPLATFORM",
};

string[] designEnvironments = new[] {
Expand Down Expand Up @@ -281,8 +298,17 @@ public static bool InUnitTestRunner()

return false;
#elif WINRT
// NB: We have no way to detect if we're in design mode in WinRT.
return false;
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 files = Package.Current.InstalledLocation.GetFilesAsync().ToObservable()
.Select(x => x.Select(y => y.Path).ToArray())
.First();

return files.Any(x => testAssemblies.Any(name => x.ToUpperInvariant().Contains(name)));

#else
// Try to detect whether we're in design mode - bonus points,
// without access to any WPF references :-/
Expand All @@ -297,6 +323,15 @@ public static bool InUnitTestRunner()
#endif
}

#if WINRT
public static async Task<string[]> getAssemblyListWinRT()
{
var pkgRoot = Package.Current.InstalledLocation;
var files = await pkgRoot.GetFilesAsync();
return files.Select(x => x.Path).ToArray();
}
#endif

/// <summary>
/// GetFieldNameForProperty returns the corresponding backing field name
/// for a given property name, using the convention specified in
Expand Down

0 comments on commit a225f46

Please sign in to comment.