public void RunSaveYearlyOrRunOnceSettingsTest(bool isRunOnce)
        {
            SchedulerSettings settings = new SchedulerSettings();
            mock.Setup(mgr => mgr.Save(It.IsAny<SchedulerSettings>())).Callback<SchedulerSettings>(s =>
            {
                if (isRunOnce)
                {
                    Assert.AreEqual(ScheduleType.Once, s.Type);
                }
                else
                {
                    Assert.AreEqual(ScheduleType.Yearly, s.Type);
                }

                Assert.AreEqual(ScheduledAction.Shutdown, s.Action);
                Assert.AreEqual(new DateTime(2013, 1, 29), s.Time.Value.Date);
                Assert.AreEqual(new TimeSpan(20, 30, 00), s.Time.Value.TimeOfDay);
            });

            SchedulerViewModel viewModel = new SchedulerViewModel(mock.Object);
            viewModel.EnableScheduler = true;
            viewModel.Action = SchedulerViewModel.SHUTDOWN;
            viewModel.Type = isRunOnce ? SchedulerViewModel.ONCE : SchedulerViewModel.YEARLY;
            viewModel.SelectedTime = new DateTime(1, 1, 1, 20, 30, 40);
            viewModel.SelectedDate = new DateTime(2013, 1, 29, 20, 30, 40);
            viewModel.SaveSettings();
        }
Exemple #2
0
 private static IJobDetail CreateJob(SchedulerSettings settings)
 {
     IJobDetail job = JobBuilder.Create<ShutdownJob>()
                                .UsingJobData(ACTION, (int)settings.Action)
                                .UsingJobData(CONFIRM, settings.AskToRunAction)
                                .Build();
     return job;
 }
 public void Save(SchedulerSettings settings)
 {
     Settings profile = Settings.Default;
     profile.ScheduleType = (int)settings.Type;
     profile.ScheduleTime = settings.Time ?? DateTime.MinValue;
     profile.ScheduleAskToRunAction = settings.AskToRunAction;
     profile.ScheduledAction = settings.Action == null ? 0 : (int)settings.Action;
     profile.ScheduleDayOfMonth = settings.DayOfMonth ?? 0;
 }
        public SchedulerSettings Load()
        {
            Settings profile = Settings.Default;
            SchedulerSettings settings = new SchedulerSettings
            {
                Type = (ScheduleType)profile.ScheduleType,
                Time = profile.ScheduleTime == DateTime.MinValue ? null : (DateTime?)profile.ScheduleTime,
                AskToRunAction = profile.ScheduleAskToRunAction,
                Action = profile.ScheduledAction == 0 ? null : (ScheduledAction?)profile.ScheduledAction,
                DayOfMonth = profile.ScheduleDayOfMonth == 0 ? null : (int?)profile.ScheduleDayOfMonth
            };

            return settings;
        }
Exemple #5
0
        private static ITrigger CreateTrigger(SchedulerSettings settings)
        {
            DateTime actionTime = (DateTime)settings.Time;
            DateTime today = DateTime.Now;

            TriggerBuilder builder = TriggerBuilder.Create();
            if (settings.Type == ScheduleType.Daily)
            {
                builder = builder.WithDailyTimeIntervalSchedule(s => s.OnEveryDay()
                                                                      .StartingDailyAt(new TimeOfDay(actionTime.Hour, actionTime.Minute)));
            }
            else if (settings.Type == ScheduleType.Monthly && settings.DayOfMonth != null)
            {
                DateTime monthlyStart = new DateTime(
                    today.Year,
                    today.Month,
                    (int)settings.DayOfMonth,
                    actionTime.Hour,
                    actionTime.Minute,
                    actionTime.Second);

                builder = builder.StartAt(monthlyStart.ToUniversalTime())
                                 .WithCalendarIntervalSchedule(s => s.WithIntervalInMonths(1));
            }
            else if (settings.Type == ScheduleType.Yearly)
            {
                builder = builder.StartAt(actionTime.ToUniversalTime())
                                 .WithCalendarIntervalSchedule(s => s.WithIntervalInYears(1));
            }
            else if (settings.Type == ScheduleType.Once)
            {
                builder = builder.StartAt(actionTime.ToUniversalTime());
            }

            ITrigger trigger = builder.Build();
            return trigger;
        }
        internal void SaveSettings()
        {
            // The method is internal to be able to test settings saving without
            // dependencies to Window object

            SchedulerSettings settings = new SchedulerSettings();
            if (!EnableScheduler)
            {
                settings.Type = ScheduleType.None;
                SettingsManager.Save(settings);
                return;
            }

            switch (Action)
            {
                case SHUTDOWN:
                    settings.Action = ScheduledAction.Shutdown;
                    break;

                case LOGOUT:
                    settings.Action = ScheduledAction.LogOut;
                    break;

                case RESTART:
                    settings.Action = ScheduledAction.Restart;
                    break;

                default:
                    throw new InvalidOperationException(string.Format("Unknown Action: {0}", Action));
            }

            if (Type == DAILY)
            {
                settings.Type = ScheduleType.Daily;
                DateTime time = SelectedTime ?? DateTime.MinValue;
                settings.Time = new DateTime(1, 1, 1, time.Hour, time.Minute, 0);
            }
            else if (Type == MONTHLY)
            {
                settings.Type = ScheduleType.Monthly;
                DateTime time = SelectedTime ?? DateTime.MinValue;
                settings.Time = new DateTime(1, 1, 1, time.Hour, time.Minute, 0);
                settings.DayOfMonth = SelectedDayOfMonth ?? 1;
            }
            else if (Type == YEARLY || Type == ONCE)
            {
                settings.Type = Type == YEARLY ? ScheduleType.Yearly : ScheduleType.Once;
                DateTime date = SelectedDate ?? DateTime.MinValue;
                DateTime time = SelectedTime ?? DateTime.MinValue;
                settings.Time = new DateTime(date.Year, date.Month, date.Day, time.Hour, time.Minute, 0);
            }
            else
            {
                throw new InvalidOperationException(string.Format("Unknown Schedule Type: {0}", Type));
            }

            settings.AskToRunAction = AskToRunAction;
            SettingsManager.Save(settings);
        }
        public void TestSaveRestartDailySettings()
        {
            SchedulerSettings settings = new SchedulerSettings();
            mock.Setup(mgr => mgr.Save(It.IsAny<SchedulerSettings>())).Callback<SchedulerSettings>(s =>
                {
                    Assert.AreEqual(ScheduleType.Daily, s.Type);
                    Assert.AreEqual(ScheduledAction.Restart, s.Action);
                    Assert.AreEqual(new TimeSpan(20, 30, 00), s.Time.Value.TimeOfDay);
                });

            SchedulerViewModel viewModel = new SchedulerViewModel(mock.Object);
            viewModel.EnableScheduler = true;
            viewModel.Action = SchedulerViewModel.RESTART;
            viewModel.Type = SchedulerViewModel.DAILY;
            viewModel.SelectedTime = new DateTime(1, 1, 1, 20, 30, 40);
            viewModel.SaveSettings();
        }
        public void TestSaveNoActionSettings()
        {
            SchedulerSettings settings = new SchedulerSettings();
            mock.Setup(mgr => mgr.Save(It.IsAny<SchedulerSettings>())).Callback<SchedulerSettings>(s =>
                {
                    Assert.AreEqual(ScheduleType.None, s.Type);
                });

            SchedulerViewModel viewModel = new SchedulerViewModel(mock.Object);
            viewModel.EnableScheduler = false;
            viewModel.SaveSettings();
        }
        public void TestSaveLogOutMonthlySettings()
        {
            SchedulerSettings settings = new SchedulerSettings();
            mock.Setup(mgr => mgr.Save(It.IsAny<SchedulerSettings>())).Callback<SchedulerSettings>(s =>
            {
                Assert.AreEqual(ScheduleType.Monthly, s.Type);
                Assert.AreEqual(ScheduledAction.LogOut, s.Action);
                Assert.AreEqual(new TimeSpan(20, 30, 00), s.Time.Value.TimeOfDay);
                Assert.AreEqual(20, s.DayOfMonth);
            });

            SchedulerViewModel viewModel = new SchedulerViewModel(mock.Object);
            viewModel.EnableScheduler = true;
            viewModel.Action = SchedulerViewModel.LOGOUT;
            viewModel.Type = SchedulerViewModel.MONTHLY;
            viewModel.SelectedTime = new DateTime(1, 1, 1, 20, 30, 40);
            viewModel.SelectedDayOfMonth = 20;
            viewModel.SaveSettings();
        }