Exemple #1
0
        public void TestThatAllActionsGetExecutedOnStateChangeToDc()
        {
            var profile = new Profile("testProfile");

            // Build action list
            var actionList = new List <IAction>();
            var mock       = new Mock <BaseAction> {
                CallBase = true
            };

            actionList.Add(mock.Object);

            // Register the actions with the profile
            profile.AssociateActions(EventType.SwitchToBattery, actionList);

            // Create battery service
            var mockBatteryServce = new Mock <IBatteryService>();

            mockBatteryServce.Setup(b => b.OnBattery).Returns(true);
            mockBatteryServce.Setup(b => b.IsValidState).Returns(true);
            mockBatteryServce.Setup(b => b.GetSystemPowerStatus()).Returns(new SystemPowerStatus {
                BatteryLifePercent = 100
            });

            // Handle the "event"
            var profileBatteryEventHandler = new ProfileBatteryEventHandler(profile);

            profileBatteryEventHandler.HandlePowerStateChange(mockBatteryServce.Object);

            // Verify that our action was executed
            mock.Verify(a => a.Execute(), Times.Once());
        }
        /// <summary>
        ///    Initializes the application with the specified configuration file.
        /// </summary>
        /// <remarks>
        ///    This method needs to be callable multiple times since it is called by "reload" actions.
        /// </remarks>
        /// <param name = "configurationFile">The configuration file.</param>
        /// <param name = "requestedProfileName">Name of the requested profile.</param>
        public static void Initialize(string configurationFile, string requestedProfileName)
        {
            // Initialize the profile service
            var profileService = ReadConfiguration(configurationFile);

            profileService.LoadLogSetting();
            LogToFile = profileService.LogToFile;
            // Kill any other running copies - this allows the user to rerun the app to
            // reload the configuration
            KillOtherCopies();

            // Wire up power change delegate
            Profile activeProfile = GetActiveProfile(profileService, requestedProfileName);

            _profileBatteryEventHandler = new ProfileBatteryEventHandler(activeProfile);
            _profileBatteryEventHandler.StartBatteryLevelMonitor(_batteryService);

            // Wire into the system power change event (WM_POWERBROADCAST)
            SystemEvents.PowerModeChanged -= OnSystemEventsOnPowerModeChanged;
            SystemEvents.PowerModeChanged += OnSystemEventsOnPowerModeChanged;

            // Remember the last settings so we can "reload"
            _lastConfigurationFile = configurationFile;
            _lastProfile           = activeProfile.ProfileName;

            // Show the systray icon
            DisplaySystemTrayIcon(profileService);

            // Reduce our memory footprint
            ProcessService.TrimCurrentWorkingSet();
        }
Exemple #3
0
        public void TestThatAllActionsWithinThresholdGetExecutedOnStateChangeToDc()
        {
            var profile = new Profile("testProfile");

            // Build action list
            var actionList = new List <IAction>();
            var mock       = new Mock <BaseAction> {
                CallBase = true
            };

            mock.Setup(a => a.BatteryPercentMin).Returns(10);
            mock.Setup(a => a.BatteryPercentMax).Returns(50);
            actionList.Add(mock.Object);

            // Register the actions with the profile
            profile.AssociateActions(EventType.SwitchToBattery, actionList);

            // Create battery service
            var mockBatteryServce = new Mock <IBatteryService>();

            mockBatteryServce.Setup(b => b.OnBattery).Returns(true);
            mockBatteryServce.Setup(b => b.IsValidState).Returns(true);
            mockBatteryServce.Setup(b => b.GetSystemPowerStatus()).Returns(new SystemPowerStatus {
                BatteryLifePercent = 100
            });

            // Handle the "event"
            var profileBatteryEventHandler = new ProfileBatteryEventHandler(profile);

            profileBatteryEventHandler.HandlePowerStateChange(mockBatteryServce.Object);

            // Outside of 10-50% range; shouldn't be executed
            mock.Verify(a => a.Execute(), Times.Never());

            // Move the battery level to 50%
            mockBatteryServce.Setup(b => b.GetSystemPowerStatus()).Returns(new SystemPowerStatus {
                BatteryLifePercent = 50
            });

            // Handle the "event"
            profileBatteryEventHandler = new ProfileBatteryEventHandler(profile);
            profileBatteryEventHandler.HandlePowerStateChange(mockBatteryServce.Object);

            // Within the range, it should be executed
            mock.Verify(a => a.Execute(), Times.Once());
        }
Exemple #4
0
        public void TestBatteryPercentageDecrease()
        {
            var profile = new Profile("testProfile");

            // Build action list
            var actionList = new List <IAction>();
            var mock       = new Mock <BaseAction> {
                CallBase = true
            };

            actionList.Add(mock.Object);

            // Register the actions with the profile
            profile.AssociateActions(EventType.BatteryPercentDecreased, actionList);

            // Create battery service
            var mockBatteryServce = new Mock <IBatteryService>();

            mockBatteryServce.Setup(b => b.OnBattery).Returns(true);
            mockBatteryServce.Setup(b => b.IsValidState).Returns(true);
            mockBatteryServce.Setup(b => b.GetSystemPowerStatus()).Returns(new SystemPowerStatus {
                BatteryLifePercent = 100
            });

            // Handle the "event"
            var profileBatteryEventHandler = new ProfileBatteryEventHandler(profile);

            profileBatteryEventHandler.HandleBatteryLevelChange(mockBatteryServce.Object, 50, -1);

            // Should execute once
            mock.Verify(a => a.Execute(), Times.Once());

            // Should be marked as "HasExecuted"
            Assert.AreEqual(true, mock.Object.HasExecuted);

            // Handle the event again
            profileBatteryEventHandler.HandleBatteryLevelChange(mockBatteryServce.Object, 51, -1);

            // The invocation count should still be 1
            mock.Verify(a => a.Execute(), Times.Once());
        }