├── .gitattributes ├── .gitignore ├── LICENSE ├── MockingAndUnitTestingXamarinFormsApp.sln └── MockingAndUnitTestingXamarinFormsApp ├── MockingAndUnitTestingXamarinFormsApp.csproj ├── Mocks └── ApplicationMock.cs ├── ResourcesFixture.cs └── Xamarin.Forms.Mocks └── MockForms.cs /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ## Ignore Visual Studio temporary files, build results, and 2 | ## files generated by popular Visual Studio add-ons. 3 | ## 4 | ## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore 5 | 6 | # User-specific files 7 | *.suo 8 | *.user 9 | *.userosscache 10 | *.sln.docstates 11 | *.vcxproj.filters 12 | 13 | # User-specific files (MonoDevelop/Xamarin Studio) 14 | *.userprefs 15 | 16 | # Build results 17 | [Dd]ebug/ 18 | [Dd]ebugPublic/ 19 | [Rr]elease/ 20 | [Rr]eleases/ 21 | x64/ 22 | x86/ 23 | bld/ 24 | [Bb]in/ 25 | [Oo]bj/ 26 | [Ll]og/ 27 | 28 | # Visual Studio 2015 cache/options directory 29 | .vs/ 30 | # Uncomment if you have tasks that create the project's static files in wwwroot 31 | #wwwroot/ 32 | 33 | # MSTest test Results 34 | [Tt]est[Rr]esult*/ 35 | [Bb]uild[Ll]og.* 36 | 37 | # NUNIT 38 | *.VisualState.xml 39 | TestResult.xml 40 | 41 | # Build Results of an ATL Project 42 | [Dd]ebugPS/ 43 | [Rr]eleasePS/ 44 | dlldata.c 45 | 46 | # .NET Core 47 | project.lock.json 48 | project.fragment.lock.json 49 | artifacts/ 50 | **/Properties/launchSettings.json 51 | 52 | *_i.c 53 | *_p.c 54 | *_i.h 55 | *.ilk 56 | *.meta 57 | *.obj 58 | *.pch 59 | *.pdb 60 | *.pgc 61 | *.pgd 62 | *.rsp 63 | *.sbr 64 | *.tlb 65 | *.tli 66 | *.tlh 67 | *.tmp 68 | *.tmp_proj 69 | *.log 70 | *.vspscc 71 | *.vssscc 72 | .builds 73 | *.pidb 74 | *.svclog 75 | *.scc 76 | 77 | # Chutzpah Test files 78 | _Chutzpah* 79 | 80 | # Visual C++ cache files 81 | ipch/ 82 | *.aps 83 | *.ncb 84 | *.opendb 85 | *.opensdf 86 | *.sdf 87 | *.cachefile 88 | *.VC.db 89 | *.VC.VC.opendb 90 | 91 | # Visual Studio profiler 92 | *.psess 93 | *.vsp 94 | *.vspx 95 | *.sap 96 | 97 | # TFS 2012 Local Workspace 98 | $tf/ 99 | 100 | # Guidance Automation Toolkit 101 | *.gpState 102 | 103 | # ReSharper is a .NET coding add-in 104 | _ReSharper*/ 105 | *.[Rr]e[Ss]harper 106 | *.DotSettings.user 107 | 108 | # JustCode is a .NET coding add-in 109 | .JustCode 110 | 111 | # TeamCity is a build add-in 112 | _TeamCity* 113 | 114 | # DotCover is a Code Coverage Tool 115 | *.dotCover 116 | 117 | # Visual Studio code coverage results 118 | *.coverage 119 | *.coveragexml 120 | 121 | # NCrunch 122 | _NCrunch_* 123 | .*crunch*.local.xml 124 | nCrunchTemp_* 125 | 126 | # MightyMoose 127 | *.mm.* 128 | AutoTest.Net/ 129 | 130 | # Web workbench (sass) 131 | .sass-cache/ 132 | 133 | # Installshield output folder 134 | [Ee]xpress/ 135 | 136 | # DocProject is a documentation generator add-in 137 | DocProject/buildhelp/ 138 | DocProject/Help/*.HxT 139 | DocProject/Help/*.HxC 140 | DocProject/Help/*.hhc 141 | DocProject/Help/*.hhk 142 | DocProject/Help/*.hhp 143 | DocProject/Help/Html2 144 | DocProject/Help/html 145 | 146 | # Click-Once directory 147 | publish/ 148 | 149 | # Publish Web Output 150 | *.[Pp]ublish.xml 151 | *.azurePubxml 152 | # TODO: Comment the next line if you want to checkin your web deploy settings 153 | # but database connection strings (with potential passwords) will be unencrypted 154 | *.pubxml 155 | *.publishproj 156 | 157 | # Microsoft Azure Web App publish settings. Comment the next line if you want to 158 | # checkin your Azure Web App publish settings, but sensitive information contained 159 | # in these scripts will be unencrypted 160 | PublishScripts/ 161 | 162 | # NuGet Packages 163 | *.nupkg 164 | # The packages folder can be ignored because of Package Restore 165 | **/packages/* 166 | # except build/, which is used as an MSBuild target. 167 | !**/packages/build/ 168 | # Uncomment if necessary however generally it will be regenerated when needed 169 | #!**/packages/repositories.config 170 | # NuGet v3's project.json files produces more ignoreable files 171 | *.nuget.props 172 | *.nuget.targets 173 | 174 | # Microsoft Azure Build Output 175 | csx/ 176 | *.build.csdef 177 | 178 | # Microsoft Azure Emulator 179 | ecf/ 180 | rcf/ 181 | 182 | # Windows Store app package directories and files 183 | AppPackages/ 184 | BundleArtifacts/ 185 | Package.StoreAssociation.xml 186 | _pkginfo.txt 187 | 188 | # Visual Studio cache files 189 | # files ending in .cache can be ignored 190 | *.[Cc]ache 191 | # but keep track of directories ending in .cache 192 | !*.[Cc]ache/ 193 | 194 | # Others 195 | ClientBin/ 196 | ~$* 197 | *~ 198 | *.dbmdl 199 | *.dbproj.schemaview 200 | *.jfm 201 | *.pfx 202 | *.publishsettings 203 | node_modules/ 204 | orleans.codegen.cs 205 | 206 | # Since there are multiple workflows, uncomment next line to ignore bower_components 207 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) 208 | #bower_components/ 209 | 210 | # RIA/Silverlight projects 211 | Generated_Code/ 212 | 213 | # Backup & report files from converting an old project file 214 | # to a newer Visual Studio version. Backup files are not needed, 215 | # because we have git ;-) 216 | _UpgradeReport_Files/ 217 | Backup*/ 218 | UpgradeLog*.XML 219 | UpgradeLog*.htm 220 | 221 | # SQL Server files 222 | *.mdf 223 | *.ldf 224 | 225 | # Business Intelligence projects 226 | *.rdl.data 227 | *.bim.layout 228 | *.bim_*.settings 229 | 230 | # Microsoft Fakes 231 | FakesAssemblies/ 232 | 233 | # GhostDoc plugin setting file 234 | *.GhostDoc.xml 235 | 236 | # Node.js Tools for Visual Studio 237 | .ntvs_analysis.dat 238 | 239 | # Visual Studio 6 build log 240 | *.plg 241 | 242 | # Visual Studio 6 workspace options file 243 | *.opt 244 | 245 | # Visual Studio 6 auto-generated workspace file (contains which files were open etc.) 246 | *.vbw 247 | 248 | # Visual Studio LightSwitch build output 249 | **/*.HTMLClient/GeneratedArtifacts 250 | **/*.DesktopClient/GeneratedArtifacts 251 | **/*.DesktopClient/ModelManifest.xml 252 | **/*.Server/GeneratedArtifacts 253 | **/*.Server/ModelManifest.xml 254 | _Pvt_Extensions 255 | 256 | # Paket dependency manager 257 | .paket/paket.exe 258 | paket-files/ 259 | 260 | # FAKE - F# Make 261 | .fake/ 262 | 263 | # JetBrains Rider 264 | .idea/ 265 | *.sln.iml 266 | 267 | # CodeRush 268 | .cr/ 269 | 270 | # Python Tools for Visual Studio (PTVS) 271 | __pycache__/ 272 | *.pyc 273 | 274 | # Cake - Uncomment if you are using it 275 | # tools/ 276 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Brian Lagunas 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /MockingAndUnitTestingXamarinFormsApp.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio 15 4 | VisualStudioVersion = 15.0.26430.14 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MockingAndUnitTestingXamarinFormsApp", "MockingAndUnitTestingXamarinFormsApp\MockingAndUnitTestingXamarinFormsApp.csproj", "{104E0D1B-FADB-4BEA-86BC-71D245AB2F3C}" 7 | EndProject 8 | Global 9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 10 | Debug|Any CPU = Debug|Any CPU 11 | Release|Any CPU = Release|Any CPU 12 | EndGlobalSection 13 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 14 | {104E0D1B-FADB-4BEA-86BC-71D245AB2F3C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 15 | {104E0D1B-FADB-4BEA-86BC-71D245AB2F3C}.Debug|Any CPU.Build.0 = Debug|Any CPU 16 | {104E0D1B-FADB-4BEA-86BC-71D245AB2F3C}.Release|Any CPU.ActiveCfg = Release|Any CPU 17 | {104E0D1B-FADB-4BEA-86BC-71D245AB2F3C}.Release|Any CPU.Build.0 = Release|Any CPU 18 | EndGlobalSection 19 | GlobalSection(SolutionProperties) = preSolution 20 | HideSolutionNode = FALSE 21 | EndGlobalSection 22 | EndGlobal 23 | -------------------------------------------------------------------------------- /MockingAndUnitTestingXamarinFormsApp/MockingAndUnitTestingXamarinFormsApp.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | netcoreapp1.1 5 | Tests 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /MockingAndUnitTestingXamarinFormsApp/Mocks/ApplicationMock.cs: -------------------------------------------------------------------------------- 1 | using Xamarin.Forms; 2 | 3 | namespace Tests.Mocks 4 | { 5 | public class ApplicationMock : Application 6 | { 7 | public ApplicationMock() 8 | { 9 | 10 | } 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /MockingAndUnitTestingXamarinFormsApp/ResourcesFixture.cs: -------------------------------------------------------------------------------- 1 | using Tests.Mocks; 2 | using Xunit; 3 | 4 | namespace Tests 5 | { 6 | public class ResourcesFixture 7 | { 8 | public ResourcesFixture() 9 | { 10 | Tests.Xamarin.Forms.Mocks.MockForms.Init(); 11 | } 12 | 13 | [Fact] 14 | public void ApplicationIsNotNull() 15 | { 16 | var app = new ApplicationMock(); 17 | Assert.NotNull(app); 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /MockingAndUnitTestingXamarinFormsApp/Xamarin.Forms.Mocks/MockForms.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.IO; 4 | using System.Reflection; 5 | using System.Threading; 6 | using System.Threading.Tasks; 7 | using Xamarin.Forms; 8 | using Xamarin.Forms.Internals; 9 | 10 | namespace Tests.Xamarin.Forms.Mocks 11 | { 12 | public static class MockForms 13 | { 14 | public static void Init() 15 | { 16 | Device.Info = new MockDeviceInfo(); 17 | Device.PlatformServices = new MockPlatformServices(); 18 | 19 | DependencyService.Register(); 20 | DependencyService.Register(); 21 | } 22 | 23 | internal class MockPlatformServices : IPlatformServices 24 | { 25 | Action _invokeOnMainThread; 26 | Action _openUriAction; 27 | Func> _getStreamAsync; 28 | 29 | public MockPlatformServices(Action invokeOnMainThread = null, Action openUriAction = null, Func> getStreamAsync = null) 30 | { 31 | _invokeOnMainThread = invokeOnMainThread; 32 | _openUriAction = openUriAction; 33 | _getStreamAsync = getStreamAsync; 34 | } 35 | 36 | public string GetMD5Hash(string input) 37 | { 38 | throw new NotImplementedException(); 39 | } 40 | static int hex(int v) 41 | { 42 | if (v < 10) 43 | return '0' + v; 44 | return 'a' + v - 10; 45 | } 46 | 47 | public double GetNamedSize(NamedSize size, Type targetElement, bool useOldSizes) 48 | { 49 | switch (size) 50 | { 51 | case NamedSize.Default: 52 | return 10; 53 | case NamedSize.Micro: 54 | return 4; 55 | case NamedSize.Small: 56 | return 8; 57 | case NamedSize.Medium: 58 | return 12; 59 | case NamedSize.Large: 60 | return 16; 61 | default: 62 | throw new ArgumentOutOfRangeException("size"); 63 | } 64 | } 65 | 66 | public void OpenUriAction(Uri uri) 67 | { 68 | if (_openUriAction != null) 69 | _openUriAction(uri); 70 | else 71 | throw new NotImplementedException(); 72 | } 73 | 74 | public bool IsInvokeRequired 75 | { 76 | get { return false; } 77 | } 78 | 79 | public string RuntimePlatform { get; set; } 80 | 81 | public void BeginInvokeOnMainThread(Action action) 82 | { 83 | if (_invokeOnMainThread == null) 84 | action(); 85 | else 86 | _invokeOnMainThread(action); 87 | } 88 | 89 | public Ticker CreateTicker() 90 | { 91 | return new MockTicker(); 92 | } 93 | 94 | public void StartTimer(TimeSpan interval, Func callback) 95 | { 96 | Timer timer = null; 97 | TimerCallback onTimeout = o => BeginInvokeOnMainThread(() => { 98 | if (callback()) 99 | return; 100 | 101 | timer.Dispose(); 102 | }); 103 | timer = new Timer(onTimeout, null, interval, interval); 104 | } 105 | 106 | public Task GetStreamAsync(Uri uri, CancellationToken cancellationToken) 107 | { 108 | if (_getStreamAsync == null) 109 | throw new NotImplementedException(); 110 | return _getStreamAsync(uri, cancellationToken); 111 | } 112 | 113 | public Assembly[] GetAssemblies() 114 | { 115 | return new Assembly[0]; 116 | } 117 | 118 | public IIsolatedStorageFile GetUserStoreForApplication() 119 | { 120 | throw new NotImplementedException(); 121 | } 122 | } 123 | 124 | internal class MockDeserializer : IDeserializer 125 | { 126 | public Task> DeserializePropertiesAsync() 127 | { 128 | return Task.FromResult>(new Dictionary()); 129 | } 130 | 131 | public Task SerializePropertiesAsync(IDictionary properties) 132 | { 133 | return Task.FromResult(false); 134 | } 135 | } 136 | 137 | internal class MockResourcesProvider : ISystemResourcesProvider 138 | { 139 | public IResourceDictionary GetSystemResources() 140 | { 141 | var dictionary = new ResourceDictionary(); 142 | Style style; 143 | style = new Style(typeof(Label)); 144 | dictionary[Device.Styles.BodyStyleKey] = style; 145 | 146 | style = new Style(typeof(Label)); 147 | style.Setters.Add(Label.FontSizeProperty, 50); 148 | dictionary[Device.Styles.TitleStyleKey] = style; 149 | 150 | style = new Style(typeof(Label)); 151 | style.Setters.Add(Label.FontSizeProperty, 40); 152 | dictionary[Device.Styles.SubtitleStyleKey] = style; 153 | 154 | style = new Style(typeof(Label)); 155 | style.Setters.Add(Label.FontSizeProperty, 30); 156 | dictionary[Device.Styles.CaptionStyleKey] = style; 157 | 158 | style = new Style(typeof(Label)); 159 | style.Setters.Add(Label.FontSizeProperty, 20); 160 | dictionary[Device.Styles.ListItemTextStyleKey] = style; 161 | 162 | style = new Style(typeof(Label)); 163 | style.Setters.Add(Label.FontSizeProperty, 10); 164 | dictionary[Device.Styles.ListItemDetailTextStyleKey] = style; 165 | 166 | return dictionary; 167 | } 168 | } 169 | 170 | internal class MockTicker : Ticker 171 | { 172 | bool _enabled; 173 | 174 | protected override void EnableTimer() 175 | { 176 | _enabled = true; 177 | 178 | while (_enabled) 179 | { 180 | SendSignals(16); 181 | } 182 | } 183 | 184 | protected override void DisableTimer() 185 | { 186 | _enabled = false; 187 | } 188 | } 189 | 190 | internal class MockDeviceInfo : DeviceInfo 191 | { 192 | public override Size PixelScreenSize => throw new NotImplementedException(); 193 | 194 | public override Size ScaledScreenSize => throw new NotImplementedException(); 195 | 196 | public override double ScalingFactor => throw new NotImplementedException(); 197 | } 198 | } 199 | } 200 | --------------------------------------------------------------------------------