AlarmRepo and PlatformAlarmService mocks 21/169721/2
authorMichal Kolodziejski <m.kolodziejs@samsung.com>
Thu, 8 Feb 2018 14:19:22 +0000 (15:19 +0100)
committerMichal Kolodziejski <m.kolodziejs@samsung.com>
Mon, 12 Feb 2018 11:48:27 +0000 (12:48 +0100)
Change-Id: I7ca67de02222391ae8b790d0a80ba3b7d170523a
Signed-off-by: Michal Kolodziejski <m.kolodziejs@samsung.com>
Clock.sln.DotSettings.user [new file with mode: 0644]
Clock/Clock.Tests/Mock/AlarmFactory.Platform.cs [new file with mode: 0644]
Clock/Clock.Tests/Mock/AlarmFactory.cs [new file with mode: 0644]
clock/Clock.Tests/Clock.Tests.csproj
clock/Clock.Tests/packages.config

diff --git a/Clock.sln.DotSettings.user b/Clock.sln.DotSettings.user
new file mode 100644 (file)
index 0000000..43c8553
--- /dev/null
@@ -0,0 +1,4 @@
+<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
+       <s:String x:Key="/Default/Environment/AssemblyExplorer/XmlDocument/@EntryValue">&lt;AssemblyExplorer&gt;&#xD;
+  &lt;Assembly Path="C:\Users\m.kolodziejs\.nuget\packages\sqlite-net-pcl\1.5.166-beta\lib\netstandard1.1\SQLite-net.dll" /&gt;&#xD;
+&lt;/AssemblyExplorer&gt;</s:String></wpf:ResourceDictionary>
\ No newline at end of file
diff --git a/Clock/Clock.Tests/Mock/AlarmFactory.Platform.cs b/Clock/Clock.Tests/Mock/AlarmFactory.Platform.cs
new file mode 100644 (file)
index 0000000..f0fecaf
--- /dev/null
@@ -0,0 +1,76 @@
+/*
+* Copyright 2017  Samsung Electronics Co., Ltd
+*
+* Licensed under the Flora License, Version 1.1 (the "License");
+* you may not use this file except in compliance with the License.
+* You may obtain a copy of the License at
+*
+* http://floralicense.org/license/
+*
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.
+*/
+
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using Clock.Abstractions;
+using Clock.Model;
+using Moq;
+
+namespace Clock.Tests.Mock
+{
+    public partial class AlarmFactory
+    {
+        private static Dictionary<int, PlatformAlarmFake> _platformAlarms;
+        private static int _platformIndex = 2;
+
+        public static IPlatformAlarmsService CreatePlatformAlarmWithItems()
+        {
+            _platformAlarms = new Dictionary<int, PlatformAlarmFake>
+            {
+                {1, new PlatformAlarmFake(new DateTime(2000, 01, 01))},
+                {2, new PlatformAlarmFake(new DateTime(2000, 01, 01))}
+            };
+
+            var mock = new Mock<IPlatformAlarmsService>();
+
+            mock.Setup(x => x.GetPlatformAlarm(It.IsAny<int>()))
+                .Returns<int>(i =>
+                    _platformAlarms.Keys.Contains(i) && _platformAlarms[i].IsActive ? _platformAlarms[i] : null);
+
+            mock.Setup(x => x.GetPlatformAlarms()).Returns(_platformAlarms.Values.Where(fake => fake.IsActive).AsEnumerable());
+
+            mock.Setup(x => x.CreateNewPlatformAlarm(It.IsAny<DateTime>(), It.IsAny<DaysOfWeek>()))
+                .Callback<DateTime, DaysOfWeek>((time, days) =>
+                {
+                    _platformAlarms.Add(++_platformIndex, new PlatformAlarmFake(time));
+                }).Returns(_platformAlarms[_platformIndex]);
+
+            return mock.Object;
+        }
+
+        private class PlatformAlarmFake : IPlatformAlarm
+        {
+            private readonly DateTime _scheduledDateTime;
+
+            public bool IsActive { get; private set; }
+
+            public PlatformAlarmFake(DateTime time)
+            {
+                _scheduledDateTime = time;
+                IsActive = true;
+            }
+
+            public void Cancel()
+            {
+                IsActive = false;
+            }
+
+            public DateTime GetScheduledDateTime() => _scheduledDateTime;
+        }
+    }
+}
\ No newline at end of file
diff --git a/Clock/Clock.Tests/Mock/AlarmFactory.cs b/Clock/Clock.Tests/Mock/AlarmFactory.cs
new file mode 100644 (file)
index 0000000..12bcfd9
--- /dev/null
@@ -0,0 +1,79 @@
+/*
+* Copyright 2017  Samsung Electronics Co., Ltd
+*
+* Licensed under the Flora License, Version 1.1 (the "License");
+* you may not use this file except in compliance with the License.
+* You may obtain a copy of the License at
+*
+* http://floralicense.org/license/
+*
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.
+*/
+
+using System.Collections.Generic;
+using System.Linq;
+using Clock.Abstractions.Repositories;
+using Clock.Model;
+using Moq;
+
+namespace Clock.Tests.Mock
+{
+    public partial class AlarmFactory
+    {
+        private static int _index = 3;
+
+        public static IAlarmRepository CreateAlarmRepositoryWithItems()
+        {
+            var alarmRepo = new Mock<IAlarmRepository>();
+
+            var alarms = new List<Alarm>
+            {
+                new Alarm {Id = 1, Name = "Alarm1", PlatformAlarmId = 1},
+                new Alarm {Id = 2, Name = "Alarm2", PlatformAlarmId = 2},
+                new Alarm {Id = 3, Name = "Alarm3"}
+            };
+
+            alarmRepo.Setup(x => x.Get(It.IsAny<int>()))
+                .Returns((int i) => alarms.FirstOrDefault(a => a.Id == i));
+
+            alarmRepo.Setup(x => x.Add(It.IsAny<Alarm>())).Callback((Alarm alarm) =>
+            {
+                alarm.Id = ++_index;
+                alarms.Add(alarm);
+            }).Returns((Alarm alarm) => alarm.Id);
+
+            alarmRepo.Setup(x => x.GetAll()).Returns(alarms.AsEnumerable());
+
+            alarmRepo.Setup(x => x.Update(It.IsAny<Alarm>())).Callback((Alarm alarm) =>
+            {
+                var first = alarms.FirstOrDefault(a => a.Id == alarm.Id);
+                if (first != default(Alarm))
+                {
+                    first.Name = alarm.Name;
+                    first.Days = alarm.Days;
+                    first.PlatformAlarmId = alarm.PlatformAlarmId;
+                    first.RingtonePath = alarm.RingtonePath;
+                    first.SnoozeCounter = alarm.SnoozeCounter;
+                    first.Time = alarm.Time;
+                    first.Type = alarm.Type;
+                    first.Volume = alarm.Volume;
+                }
+            });
+
+            alarmRepo.Setup(x => x.Remove(It.IsAny<Alarm>())).Callback((Alarm alarm) =>
+            {
+                var alarmToDelete = alarms.FirstOrDefault(a => a.Id == alarm.Id);
+                if (alarmToDelete != default(Alarm))
+                {
+                    alarms.Remove(alarmToDelete);
+                }
+            });
+
+            return alarmRepo.Object;
+        }
+    }
+}
\ No newline at end of file
index 27eea0a..daedc7a 100644 (file)
     <WarningLevel>4</WarningLevel>
   </PropertyGroup>
   <ItemGroup>
+    <Reference Include="Castle.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=407dd0808d44fbdc, processorArchitecture=MSIL">
+      <HintPath>..\..\packages\Castle.Core.4.2.1\lib\net45\Castle.Core.dll</HintPath>
+    </Reference>
+    <Reference Include="Moq, Version=4.8.0.0, Culture=neutral, PublicKeyToken=69f491c39445e920, processorArchitecture=MSIL">
+      <HintPath>..\..\packages\Moq.4.8.0\lib\net45\Moq.dll</HintPath>
+    </Reference>
+    <Reference Include="nunit.framework, Version=3.9.0.0, Culture=neutral, PublicKeyToken=2638cd05610744eb, processorArchitecture=MSIL">
+      <HintPath>..\..\packages\NUnit.3.9.0\lib\net45\nunit.framework.dll</HintPath>
+    </Reference>
     <Reference Include="System" />
+    <Reference Include="System.Configuration" />
+    <Reference Include="System.Threading.Tasks.Extensions, Version=4.1.1.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
+      <HintPath>..\..\packages\System.Threading.Tasks.Extensions.4.4.0\lib\netstandard2.0\System.Threading.Tasks.Extensions.dll</HintPath>
+    </Reference>
+    <Reference Include="System.ValueTuple, Version=4.0.1.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
+      <HintPath>..\..\packages\System.ValueTuple.4.3.0\lib\netstandard1.0\System.ValueTuple.dll</HintPath>
+      <Private>True</Private>
+    </Reference>
     <Reference Include="Xamarin.Forms.Core.Design">
       <HintPath>..\..\packages\Xamarin.Forms.2.4.0.282\lib\netstandard1.0\Design\Xamarin.Forms.Core.Design.dll</HintPath>
     </Reference>
@@ -54,6 +71,7 @@
     </Otherwise>
   </Choose>
   <ItemGroup>
+    <Compile Include="Mock\AlarmFactory.cs" />
     <Compile Include="TimezonesTest.cs" />
     <Compile Include="Properties\AssemblyInfo.cs" />
   </ItemGroup>
     </ProjectReference>
   </ItemGroup>
   <ItemGroup>
+    <Compile Include="Mock\AlarmFactory.Platform.cs" />
     <None Include="packages.config" />
   </ItemGroup>
+  <ItemGroup />
   <Choose>
     <When Condition="'$(VisualStudioVersion)' == '10.0' And '$(IsCodedUITest)' == 'True'">
       <ItemGroup>
index 4a40687..d434de2 100644 (file)
@@ -1,8 +1,11 @@
 <?xml version="1.0" encoding="utf-8"?>
 <packages>
-  <package id="Castle.Core" version="4.1.1" targetFramework="net452" />
-  <package id="Moq" version="4.7.99" targetFramework="net452" />
+  <package id="Castle.Core" version="4.2.1" targetFramework="net461" />
+  <package id="Moq" version="4.8.0" targetFramework="net461" />
   <package id="MSTest.TestAdapter" version="1.2.0-beta3" targetFramework="net452" />
   <package id="MSTest.TestFramework" version="1.2.0-beta3" targetFramework="net452" />
+  <package id="NUnit" version="3.9.0" targetFramework="net461" />
+  <package id="System.Threading.Tasks.Extensions" version="4.4.0" targetFramework="net461" />
+  <package id="System.ValueTuple" version="4.3.0" targetFramework="net461" />
   <package id="Xamarin.Forms" version="2.4.0.269-pre2" targetFramework="net461" />
 </packages>
\ No newline at end of file