using System; using System.Threading.Tasks; using System.Threading; using System.Reflection; using System.IO.IsolatedStorage; using System.Collections.Generic; using Xamarin.Forms; using Xamarin.Forms.Core.UnitTests; using System.Security.Cryptography; using System.Text; using FileMode = System.IO.FileMode; using FileAccess = System.IO.FileAccess; using FileShare = System.IO.FileShare; using Stream = System.IO.Stream; [assembly:Dependency (typeof(MockDeserializer))] [assembly:Dependency (typeof(MockResourcesProvider))] namespace Xamarin.Forms.Core.UnitTests { internal class MockPlatformServices : Internals.IPlatformServices { Action invokeOnMainThread; Action openUriAction; Func> getStreamAsync; public MockPlatformServices (Action invokeOnMainThread = null, Action openUriAction = null, Func> getStreamAsync = null) { this.invokeOnMainThread = invokeOnMainThread; this.openUriAction = openUriAction; this.getStreamAsync = getStreamAsync; } static MD5CryptoServiceProvider checksum = new MD5CryptoServiceProvider (); public string GetMD5Hash (string input) { var bytes = checksum.ComputeHash (Encoding.UTF8.GetBytes (input)); var ret = new char [32]; for (int i = 0; i < 16; i++){ ret [i*2] = (char)hex (bytes [i] >> 4); ret [i*2+1] = (char)hex (bytes [i] & 0xf); } return new string (ret); } static int hex (int v) { if (v < 10) return '0' + v; return 'a' + v-10; } public double GetNamedSize (NamedSize size, Type targetElement, bool useOldSizes) { switch (size) { case NamedSize.Default: return 10; case NamedSize.Micro: return 4; case NamedSize.Small: return 8; case NamedSize.Medium: return 12; case NamedSize.Large: return 16; default: throw new ArgumentOutOfRangeException ("size"); } } public void OpenUriAction (Uri uri) { if (openUriAction != null) openUriAction (uri); else throw new NotImplementedException (); } public bool IsInvokeRequired { get { return false; } } public string RuntimePlatform { get; set; } public void BeginInvokeOnMainThread (Action action) { if (invokeOnMainThread == null) action (); else invokeOnMainThread (action); } public Internals.Ticker CreateTicker() { return new MockTicker(); } public void StartTimer (TimeSpan interval, Func callback) { Timer timer = null; TimerCallback onTimeout = o => BeginInvokeOnMainThread (() => { if (callback ()) return; timer.Dispose (); }); timer = new Timer (onTimeout, null, interval, interval); } public Task GetStreamAsync (Uri uri, CancellationToken cancellationToken) { if (getStreamAsync == null) throw new NotImplementedException (); return getStreamAsync (uri, cancellationToken); } public Assembly[] GetAssemblies () { return AppDomain.CurrentDomain.GetAssemblies (); } public Internals.IIsolatedStorageFile GetUserStoreForApplication () { return new MockIsolatedStorageFile (IsolatedStorageFile.GetUserStoreForAssembly ()); } public class MockIsolatedStorageFile : Internals.IIsolatedStorageFile { readonly IsolatedStorageFile isolatedStorageFile; public MockIsolatedStorageFile (IsolatedStorageFile isolatedStorageFile) { this.isolatedStorageFile = isolatedStorageFile; } public Task GetDirectoryExistsAsync (string path) { return Task.FromResult (isolatedStorageFile.DirectoryExists (path)); } public Task CreateDirectoryAsync (string path) { isolatedStorageFile.CreateDirectory (path); return Task.FromResult (true); } public Task OpenFileAsync (string path, FileMode mode, FileAccess access) { Stream stream = isolatedStorageFile.OpenFile (path, mode, access); return Task.FromResult (stream); } public Task OpenFileAsync (string path, FileMode mode, FileAccess access, FileShare share) { Stream stream = isolatedStorageFile.OpenFile (path, mode, access, share); return Task.FromResult (stream); } public Task GetFileExistsAsync (string path) { return Task.FromResult (isolatedStorageFile.FileExists (path)); } public Task GetLastWriteTimeAsync (string path) { return Task.FromResult (isolatedStorageFile.GetLastWriteTime (path)); } } public void QuitApplication() { } } internal class MockDeserializer : Internals.IDeserializer { public Task> DeserializePropertiesAsync () { return Task.FromResult> (new Dictionary ()); } public Task SerializePropertiesAsync (IDictionary properties) { return Task.FromResult (false); } } internal class MockResourcesProvider : Internals.ISystemResourcesProvider { public Internals.IResourceDictionary GetSystemResources () { var dictionary = new ResourceDictionary (); Style style; style = new Style (typeof(Label)); dictionary [Device.Styles.BodyStyleKey] = style; style = new Style (typeof(Label)); style.Setters.Add (Label.FontSizeProperty, 50); dictionary [Device.Styles.TitleStyleKey] = style; style = new Style (typeof(Label)); style.Setters.Add (Label.FontSizeProperty, 40); dictionary [Device.Styles.SubtitleStyleKey] = style; style = new Style (typeof(Label)); style.Setters.Add (Label.FontSizeProperty, 30); dictionary [Device.Styles.CaptionStyleKey] = style; style = new Style (typeof(Label)); style.Setters.Add (Label.FontSizeProperty, 20); dictionary [Device.Styles.ListItemTextStyleKey] = style; style = new Style (typeof(Label)); style.Setters.Add (Label.FontSizeProperty, 10); dictionary [Device.Styles.ListItemDetailTextStyleKey] = style; return dictionary; } } public class MockApplication : Application { public MockApplication () { } } internal class MockTicker : Internals.Ticker { bool _enabled; protected override void EnableTimer() { _enabled = true; while (_enabled) { SendSignals(16); } } protected override void DisableTimer() { _enabled = false; } } }