async public void SyncOperation_NeedsMerge_DontNeedSave()
        {
            ApplicationState state = new ApplicationState();

            ApplicationState.Current  = state;
            state.SessionData         = new SessionData();
            state.SessionData.Profile = new ProfileDTO()
            {
                GlobalId = Guid.NewGuid()
            };
            state.MyDays = new Dictionary <CacheKey, TrainingDaysHolder>();
            var      holder = state.GetTrainingDayHolder(null);
            DateTime time   = ExtensionMethods.MonthDate(DateTime.UtcNow);
            var      day    = new TrainingDayDTO()
            {
                TrainingDate = time.AddDays(1)
            };

            holder.TrainingDays.Add(time.AddDays(1), new TrainingDayInfo(day)
            {
                IsModified = true
            });
            //state.MyDays.Add(time.AddDays(1), new TrainingDayInfo(day) { IsModified = true });


            bool eventCompleted = false;

            MockSynchronizationViewModel viewModel = new MockSynchronizationViewModel(state);

            viewModel.Action = MergeAction.UseServer;
            viewModel.NeedsMergeException       = true;
            viewModel.FromServer                = day;
            viewModel.ErrorInSecondSave         = true;
            viewModel.SynchronizationCompleted += delegate
            {
                eventCompleted = true;
            };

            await viewModel.Synchronize();

            Assert.AreEqual(1, viewModel.Maximum);
            Assert.AreEqual(1, viewModel.SaveCount);
            Assert.IsTrue(eventCompleted);
            Assert.IsTrue(viewModel.FirstSave);
            Assert.IsFalse(viewModel.SecondSave);
            Assert.IsFalse(viewModel.Items[0].DayInfo.IsConflict);
            Assert.IsFalse(viewModel.Items[0].DayInfo.IsModified);
            Assert.AreEqual(MergeState.Finished, viewModel.Items[0].State);
            Assert.IsTrue(viewModel.Merged);
            Assert.IsTrue(viewModel.IsInProgressGood);
            EnqueueTestComplete();
        }
        async public void SyncOperation_Correct()
        {
            ApplicationState state = new ApplicationState();

            ApplicationState.Current  = state;
            state.SessionData         = new SessionData();
            state.SessionData.Profile = new ProfileDTO()
            {
                GlobalId = Guid.NewGuid()
            };
            state.MyDays = new Dictionary <CacheKey, TrainingDaysHolder>();
            var      holder = state.GetTrainingDayHolder(null);
            DateTime time   = ExtensionMethods.MonthDate(DateTime.UtcNow);
            var      day    = new TrainingDayDTO()
            {
                TrainingDate = time.AddDays(1)
            };
            var size = new SizeEntryDTO();

            day.Objects.Add(size);
            size.TrainingDay = day;
            holder.TrainingDays.Add(time.AddDays(1), new TrainingDayInfo(day)
            {
                IsModified = true
            });


            bool eventCompleted = false;

            MockSynchronizationViewModel viewModel = new MockSynchronizationViewModel(state);

            viewModel.SynchronizationCompleted += delegate
            {
                eventCompleted = true;
            };
            var items = viewModel.Items.ToArray();

            await viewModel.Synchronize();

            Assert.AreEqual(1, viewModel.Maximum);
            Assert.AreEqual(1, viewModel.SaveCount);
            Assert.AreEqual(MergeState.Finished, items[0].State);
            Assert.IsTrue(eventCompleted);
            Assert.IsTrue(viewModel.FirstSave);
            Assert.IsFalse(viewModel.Merged);
            Assert.IsFalse(items[0].DayInfo.IsModified);
            Assert.IsTrue(viewModel.IsInProgressGood);
            Assert.AreNotEqual(Guid.Empty, day.GlobalId);
            EnqueueTestComplete();
        }
        async public void SyncOperation_UploadGPSCoordinates_RemovedGPSTrackerEntry()
        {
            ApplicationState state = new ApplicationState();

            ApplicationState.Current  = state;
            state.SessionData         = new SessionData();
            state.SessionData.Profile = new ProfileDTO()
            {
                GlobalId = Guid.NewGuid()
            };
            state.MyDays = new Dictionary <CacheKey, TrainingDaysHolder>();
            var      holder = state.GetTrainingDayHolder(null);
            DateTime time   = ExtensionMethods.MonthDate(DateTime.UtcNow);
            var      day    = new TrainingDayDTO()
            {
                TrainingDate = time.AddDays(1)
            };
            var tdi = new TrainingDayInfo(day)
            {
                IsModified = true
            };
            //these points should be detected to save
            GPSTrackerEntryDTO gpsEntry = new GPSTrackerEntryDTO();

            gpsEntry.Exercise = new ExerciseLightDTO()
            {
                GlobalId = Guid.NewGuid()
            };
            tdi.TrainingDay.Objects.Add(gpsEntry);
            gpsEntry.TrainingDay = tdi.TrainingDay;
            var points = new List <GPSPoint>();

            points.Add(new GPSPoint(1, 2, 3, 4, 6));
            tdi.GPSCoordinates.Add(new LocalObjectKey(gpsEntry.InstanceId, KeyType.InstanceId), new GPSPointsBag(points, false));

            holder.TrainingDays.Add(time.AddDays(1), tdi);


            bool eventCompleted = false;

            MockSynchronizationViewModel viewModel = new MockSynchronizationViewModel(state);

            viewModel.UploadGPSError            = ErrorCode.ObjectNotFound;
            viewModel.RemoveFromServer          = typeof(GPSTrackerEntryDTO);
            viewModel.SynchronizationCompleted += delegate
            {
                eventCompleted = true;
            };
            var items = viewModel.Items.ToArray();
            await viewModel.Synchronize();

            Assert.AreEqual(2, viewModel.Maximum);
            Assert.AreEqual(1, viewModel.SaveCount);
            Assert.AreEqual(0, viewModel.GPSUploadCount);
            Assert.AreEqual(MergeState.Finished, items[0].State);
            Assert.AreEqual(MergeState.Finished, items[1].State);
            Assert.AreNotEqual(Guid.Empty, day.GlobalId);
            Assert.IsTrue(eventCompleted);
            Assert.IsTrue(viewModel.FirstSave);
            Assert.IsFalse(viewModel.Merged);
            Assert.IsTrue(viewModel.IsInProgressGood);
            EnqueueTestComplete();
        }
        async public void SyncOperation()
        {
            ApplicationState state = new ApplicationState();

            ApplicationState.Current  = state;
            state.SessionData         = new SessionData();
            state.SessionData.Profile = new ProfileDTO()
            {
                GlobalId = Guid.NewGuid()
            };
            state.MyDays = new Dictionary <CacheKey, TrainingDaysHolder>();
            var      holder = state.GetTrainingDayHolder(null);
            DateTime time   = ExtensionMethods.MonthDate(DateTime.UtcNow);
            var      day    = new TrainingDayDTO()
            {
                TrainingDate = time.AddDays(1)
            };

            holder.TrainingDays.Add(time.AddDays(1), new TrainingDayInfo(day)
            {
                IsModified = true
            });
            holder.TrainingDays.Add(time.AddDays(2), new TrainingDayInfo(new TrainingDayDTO()
            {
                TrainingDate = time.AddDays(2)
            }));
            holder.TrainingDays.Add(time.AddDays(3), new TrainingDayInfo(new TrainingDayDTO()
            {
                TrainingDate = time.AddDays(3)
            })
            {
                IsModified = true
            });
            holder.TrainingDays.Add(time.AddDays(4), new TrainingDayInfo(new TrainingDayDTO()
            {
                TrainingDate = time.AddDays(4)
            })
            {
                IsModified = true
            });
            holder.TrainingDays.Add(time.AddDays(5), new TrainingDayInfo(new TrainingDayDTO()
            {
                TrainingDate = time.AddDays(5)
            }));
            holder.TrainingDays.Add(time.AddDays(6), new TrainingDayInfo(new TrainingDayDTO()
            {
                TrainingDate = time.AddDays(6)
            }));

            bool eventCompleted = false;

            MockSynchronizationViewModel viewModel = new MockSynchronizationViewModel(state);

            viewModel.SynchronizationCompleted += delegate
            {
                eventCompleted = true;
            };

            await viewModel.Synchronize();

            Assert.AreEqual(3, viewModel.Maximum);
            Assert.AreEqual(3, viewModel.SaveCount);
            Assert.IsTrue(eventCompleted);
            Assert.IsTrue(viewModel.IsInProgressGood);
            EnqueueTestComplete();
        }