public ChargeTask(MobilePhone phone, Action ShowBattery)
        {
            _phone                = phone;
            this.ShowBattery      = ShowBattery;
            _tokenCancelDischarge = new CancellationTokenSource();
            var token = _tokenCancelDischarge.Token;

            DischargingTask = Task.Run(
                async() => {
                // if not in unit test — discharging process is always active
                while (Endless < TestIterations)
                {
                    if (IsTestDischarge)
                    {
                        Endless++;
                    }
                    ShowBattery();
                    await Task.Delay(DischargeInterval, token);
                    lock (_phone.BatteryBase) {
                        _phone.Discharge();
                    }
                    if (token.IsCancellationRequested)
                    {
                        break;
                    }
                }
            }, token);
        }
Exemplo n.º 2
0
        public void TestDischarge()
        {
            //Arrange
            Init();

            //Act
            for (var i = 0; i < 102; i++)
            {
                _phone.Discharge();
            }

            //Assert
            Assert.AreEqual(_phone.BatteryBase.Charge, 0);
        }
Exemplo n.º 3
0
 public ChargeThread(MobilePhone phone, Action ShowBattery)
 {
     ChargingThread = new Thread(
         () => {
         while (IsCharging)
         {
             // for unit test only — to stop thread after certain number of iterations
             if (IsTestCharge)
             {
                 Endless++;
                 if (Endless >= TestIterations)
                 {
                     IsCharging = false;
                 }
             }
             // end for unit test
             Thread.Sleep(ChargeInterval);
             lock (phone.BatteryBase) {
                 phone.Charge();
             }
         }
     })
     {
         IsBackground = true
     };
     ChargingThread.Start();
     ChargingThread.Suspend();
     DischargingThread = new Thread(
         () => {
         // if not in unit test — discharging process is always active
         while (Endless < TestIterations)
         {
             if (IsTestDischarge)
             {
                 Endless++;
             }
             ShowBattery();
             Thread.Sleep(DischargeInterval);
             lock (phone.BatteryBase) {
                 phone.Discharge();
             }
         }
     })
     {
         IsBackground = true
     };
     DischargingThread.Start();
 }