--- /dev/null
+/*
+* 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.Linq;
+using Clock.Services;
+using Clock.Tests.Mock;
+using NUnit.Framework;
+
+
+namespace Clock.Tests
+{
+ [TestFixture]
+ public class AlarmManagerTests
+ {
+ [Test]
+ public void AlarmManager_GetAlarms()
+ {
+ //Arrange
+ var alarmManager = new AlarmManager(AlarmFactory.CreatePlatformAlarmWithItems(),
+ AlarmFactory.CreateAlarmRepositoryWithItems(), MockFactory.NavigationServiceMock);
+
+ //Act
+ var alarms = alarmManager.GetAlarms();
+
+ //Assert
+ Assert.That(alarms.ToList().Count, Is.EqualTo(3));
+ }
+
+ [Test]
+ public void AlarmManager_DeactivateAlarm()
+ {
+ //Arrange
+ var alarmRepository = AlarmFactory.CreateAlarmRepositoryWithItems();
+ var platformAlarmService = AlarmFactory.CreatePlatformAlarmWithItems();
+
+ var alarmManager =
+ new AlarmManager(platformAlarmService, alarmRepository, MockFactory.NavigationServiceMock);
+
+ var alarm = alarmRepository.Get(1);
+ if (alarm.PlatformAlarmId == null) return;
+
+ var platformId = (int)alarm.PlatformAlarmId;
+
+ //Act
+ alarmManager.Deactivate(alarm);
+
+ //Assert
+ Assert.That(alarm.PlatformAlarmId, Is.EqualTo(null));
+ Assert.That(platformAlarmService.GetPlatformAlarm(platformId), Is.EqualTo(null));
+ }
+
+ [Test]
+ public void AlarmManager_DeactivateNotActiveAlarm()
+ {
+ //Arrange
+ var alarmRepository = AlarmFactory.CreateAlarmRepositoryWithItems();
+ var platformAlarmService = AlarmFactory.CreatePlatformAlarmWithItems();
+
+ var alarmManager =
+ new AlarmManager(platformAlarmService, alarmRepository, MockFactory.NavigationServiceMock);
+
+ var alarm = alarmRepository.Get(3);
+
+
+ //Act
+ alarmManager.Deactivate(alarm);
+
+ //Assert
+ Assert.That(alarm.PlatformAlarmId, Is.EqualTo(null));
+ }
+
+ [Test]
+ public void AlarmManager_ActivateAlarm()
+ {
+ //Arrange
+ var alarmRepository = AlarmFactory.CreateAlarmRepositoryWithItems();
+ var platformAlarmService = AlarmFactory.CreatePlatformAlarmWithItems();
+
+ var alarmManager =
+ new AlarmManager(platformAlarmService, alarmRepository, MockFactory.NavigationServiceMock);
+
+ var alarm = alarmRepository.Get(3);
+
+ //Act
+ alarmManager.Activate(alarm);
+
+ //Assert
+ if (alarm.PlatformAlarmId == null) Assert.Fail("Platform id is null");
+
+ Assert.That(alarm.PlatformAlarmId, Is.Not.EqualTo(null));
+ Assert.That(platformAlarmService.GetPlatformAlarm((int)alarm.PlatformAlarmId), Is.Not.EqualTo(null));
+ }
+
+ [Test]
+ public void AlarmManager_ActivateActiveAlarm()
+ {
+ //Arrange
+ var alarmRepository = AlarmFactory.CreateAlarmRepositoryWithItems();
+ var platformAlarmService = AlarmFactory.CreatePlatformAlarmWithItems();
+
+ var alarmManager =
+ new AlarmManager(platformAlarmService, alarmRepository, MockFactory.NavigationServiceMock);
+
+ var alarm = alarmRepository.Get(1);
+
+ //Act
+ alarmManager.Activate(alarm);
+
+ //Assert
+ if (alarm.PlatformAlarmId == null) Assert.Fail("Platform id is null");
+
+ Assert.That(alarm.PlatformAlarmId, Is.Not.EqualTo(null));
+ Assert.That(platformAlarmService.GetPlatformAlarm((int)alarm.PlatformAlarmId), Is.Not.EqualTo(null));
+ }
+
+ [Test]
+ public void AlarmManager_ActivateNullAlarm()
+ {
+ //Arrange
+ var alarmRepository = AlarmFactory.CreateAlarmRepositoryWithItems();
+ var platformAlarmService = AlarmFactory.CreatePlatformAlarmWithItems();
+
+ var alarmManager =
+ new AlarmManager(platformAlarmService, alarmRepository, MockFactory.NavigationServiceMock);
+
+ //Assert
+ Assert.Throws(Is.TypeOf<ArgumentNullException>(), () => alarmManager.Activate(null));
+ }
+
+ [Test]
+ public void AlarmManager_DeactivateNullAlarm()
+ {
+ //Arrange
+ var alarmRepository = AlarmFactory.CreateAlarmRepositoryWithItems();
+ var platformAlarmService = AlarmFactory.CreatePlatformAlarmWithItems();
+
+ var alarmManager = new AlarmManager(platformAlarmService, alarmRepository, null);
+
+ //Assert
+ Assert.Throws(Is.TypeOf<ArgumentNullException>(), () => alarmManager.Activate(null));
+ }
+
+ [Test]
+ public void AlarmManager_DeactivateNotExistingPlatformAlarm()
+ {
+ //Arrange
+ var alarmRepository = AlarmFactory.CreateAlarmRepositoryWithItems();
+ var platformAlarmService = AlarmFactory.CreatePlatformAlarmWithItems();
+
+ var alarmManager =
+ new AlarmManager(platformAlarmService, alarmRepository, MockFactory.NavigationServiceMock);
+
+ var alarm = alarmRepository.Get(1);
+ alarm.PlatformAlarmId = 10;
+
+ //Act
+ alarmManager.Deactivate(alarm);
+
+ //Assert
+ Assert.That(alarm.PlatformAlarmId, Is.EqualTo(null));
+ }
+ }
+}
\ No newline at end of file
{
public partial class AlarmFactory
{
- private static Dictionary<int, PlatformAlarmFake> _platformAlarms;
- private static int _platformIndex = 2;
-
public static IPlatformAlarmsService CreatePlatformAlarmWithItems()
{
- _platformAlarms = new Dictionary<int, PlatformAlarmFake>
+ var platformIndex = 2;
+ var platformAlarms = new Dictionary<int, PlatformAlarmFake>
{
- {1, new PlatformAlarmFake(new DateTime(2000, 01, 01))},
- {2, new PlatformAlarmFake(new DateTime(2000, 01, 01))}
+ {1, new PlatformAlarmFake(new DateTime(2000, 01, 01)) {Id = 1}},
+ {2, new PlatformAlarmFake(new DateTime(2000, 01, 01)) {Id = 2}}
};
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);
+ 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.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]);
+ platformIndex++;
+ platformAlarms.Add(platformIndex, new PlatformAlarmFake(time));
+ }).Returns(platformAlarms[platformIndex]);
return mock.Object;
}
public bool IsActive { get; private set; }
+ public int Id { get; set; }
+
public PlatformAlarmFake(DateTime time)
{
_scheduledDateTime = time;
{
public partial class AlarmFactory
{
- private static int _index = 3;
-
public static IAlarmRepository CreateAlarmRepositoryWithItems()
{
- var alarmRepo = new Mock<IAlarmRepository>();
+ var index = 3;
+ var alarmRepository = 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"}
+ new Alarm {Id = 3, Name = "Alarm3", PlatformAlarmId = null}
};
- alarmRepo.Setup(x => x.Get(It.IsAny<int>()))
+ alarmRepository.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) =>
+ alarmRepository.Setup(x => x.Add(It.IsAny<Alarm>())).Callback((Alarm alarm) =>
{
- alarm.Id = ++_index;
+ alarm.Id = ++index;
alarms.Add(alarm);
}).Returns((Alarm alarm) => alarm.Id);
- alarmRepo.Setup(x => x.GetAll()).Returns(alarms.AsEnumerable());
+ alarmRepository.Setup(x => x.GetAll()).Returns(alarms.AsEnumerable());
- alarmRepo.Setup(x => x.Update(It.IsAny<Alarm>())).Callback((Alarm alarm) =>
+ alarmRepository.Setup(x => x.Update(It.IsAny<Alarm>())).Callback((Alarm alarm) =>
{
var first = alarms.FirstOrDefault(a => a.Id == alarm.Id);
if (first != default(Alarm))
}
});
- alarmRepo.Setup(x => x.Remove(It.IsAny<Alarm>())).Callback((Alarm alarm) =>
+ alarmRepository.Setup(x => x.Remove(It.IsAny<Alarm>())).Callback((Alarm alarm) =>
{
var alarmToDelete = alarms.FirstOrDefault(a => a.Id == alarm.Id);
if (alarmToDelete != default(Alarm))
}
});
- return alarmRepo.Object;
+ return alarmRepository.Object;
}
}
}
\ No newline at end of file
--- /dev/null
+/*
+* 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 Clock.Abstractions;
+using Moq;
+
+namespace Clock.Tests.Mock
+{
+ public static class MockFactory
+ {
+ public static INavigationService NavigationServiceMock => new Mock<INavigationService>().Object;
+ }
+}
\ No newline at end of file
public interface IPlatformAlarm
{
/// <summary>
+ /// Unique id of the alarm.
+ /// </summary>
+ int Id { get; set; }
+
+ /// <summary>
/// Cancels this alarm.
/// </summary>
void Cancel();
/// </summary>
/// <param name="time">Time of the alarm.</param>
/// <param name="days">Flags that indicate days of the week.</param>
- /// <returns>Platform alarm object.</returns>
+ /// <returns>New platform alarm object.</returns>
IPlatformAlarm CreateNewPlatformAlarm(DateTime time, DaysOfWeek days);
/// <summary>
/// <param name="platformAlarmsService">Service that will provides platform alarm functionalities.</param>
/// <param name="alarmRepository">Repository with alarms.</param>
/// <param name="navigationService">Application navigation service.</param>
- public AlarmManager(IPlatformAlarmsService platformAlarmsService, IAlarmRepository alarmRepository, INavigationService navigationService)
+ public AlarmManager(IPlatformAlarmsService platformAlarmsService, IAlarmRepository alarmRepository,
+ INavigationService navigationService)
{
_platformAlarmsService = platformAlarmsService;
_alarmRepository = alarmRepository;
/// <param name="alarm">Alarm data.</param>
public void Activate(Alarm alarm)
{
- // 1. Create platform alarm.
- // 2. Change flag in alarm
- // 3. Update alarm in db
- throw new NotImplementedException();
+ if (alarm == null)
+ {
+ throw new ArgumentNullException();
+ }
+
+ if (alarm.PlatformAlarmId != null)
+ {
+ if (_platformAlarmsService.GetPlatformAlarm((int)alarm.PlatformAlarmId) != null)
+ {
+ return;
+ }
+ }
+
+ var platformAlarm = _platformAlarmsService.CreateNewPlatformAlarm(alarm.Time, alarm.Days);
+ alarm.PlatformAlarmId = platformAlarm.Id;
+ _alarmRepository.Update(alarm);
}
public void Deactivate(Alarm alarm)
{
- // 1. Get platform alarm and cancel it
- // 2. Change flag in alarm
- // 3. Update alarm in db
- throw new NotImplementedException();
+ if (alarm == null)
+ {
+ throw new ArgumentNullException();
+ }
+
+ if (alarm.PlatformAlarmId == null)
+ {
+ return;
+ }
+
+ var platformAlarm = _platformAlarmsService.GetPlatformAlarm((int)alarm.PlatformAlarmId);
+ platformAlarm?.Cancel();
+
+ alarm.PlatformAlarmId = null;
+ _alarmRepository.Update(alarm);
}
/// <summary>
</Otherwise>
</Choose>
<ItemGroup>
+ <Compile Include="AlarmManagerTests.cs" />
<Compile Include="Mock\AlarmFactory.cs" />
+ <Compile Include="Mock\MockFactory.cs" />
<Compile Include="TimezonesTest.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>