public override void Start()
        {
            _tokenCancelCharge = new CancellationTokenSource();
            var token = _tokenCancelCharge.Token;

            ChargingTask = Task.Run(
                async() => {
                while (IsCharging)
                {
                    // for unit test only — to stop task after certain number of iterations
                    if (IsTestCharge)
                    {
                        Endless++;
                        if (Endless >= TestIterations)
                        {
                            IsCharging = false;
                        }
                    }
                    // end for unit test
                    await Task.Delay(ChargeInterval, token);
                    lock (_phone.BatteryBase) {
                        _phone.Charge();
                    }
                    if (token.IsCancellationRequested)
                    {
                        break;
                    }
                }
            }, token);
        }
Exemplo n.º 2
0
        public void TestCharge()
        {
            //Arrange
            Init();

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

            //Assert
            Assert.AreEqual(_phone.BatteryBase.Charge, 101);
        }
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();
 }
Exemplo n.º 4
0
        private void ApplyButton_Click(object sender, EventArgs e)
        {
            _clickNum++;
            switch (_clickNum)
            {
            case 1:
                IPlayback playback;
                if (Option1.Checked)
                {
                    playback = new MeizuHeadset(output);
                }
                else if (Option2.Checked)
                {
                    playback = new SamsungHeadset(output);
                }
                else if (Option3.Checked)
                {
                    playback = new UnofficialiPhoneHeadset(output);
                }
                else if (Option4.Checked)
                {
                    playback = new PortableSpeaker(output);
                }
                else
                {
                    throw new ArgumentOutOfRangeException();
                }
                phone.PlaybackComponent = playback;
                output.WriteLine($"{playback.GetType().Name} playback selected");
                output.WriteLine("Set playback to Mobile...");
                output.WriteLine("Play sound in Mobile:");
                phone.Play("Song");
                FormTopLabel    = "Select charger component (specify index):";
                FormOption1     = "1 - SamsungCharger";
                FormOption2     = "2 - MeizuCharger";
                FormOption3     = "3 - OfficialiPhoneCharger";
                FormOption4     = "";
                Option1.Checked = true;
                break;

            case 2:
                output.WriteLine("");
                ICharger charger;
                if (Option1.Checked)
                {
                    charger = new SamsungCharger(output);
                }
                else if (Option2.Checked)
                {
                    charger = new MeizuCharger(output);
                }
                else if (Option3.Checked)
                {
                    charger = new OfficialiPhoneCharger(output);
                }
                else
                {
                    throw new ArgumentOutOfRangeException();
                }
                phone.ChargerComponent = charger;
                output.WriteLine($"{charger.GetType().Name} selected");
                output.WriteLine("Plug in charger to Mobile:");
                phone.Charge(null);
                FormTopLabel    = "Select protector component (specify index):";
                FormOption1     = "1 - LeatherProtector";
                FormOption2     = "2 - SteelProtector";
                FormOption3     = "";
                FormOption4     = "";
                Option1.Checked = true;
                break;

            case 3:
                output.WriteLine("");
                IProtector protector;
                if (Option1.Checked)
                {
                    protector = new LeatherProtector(output);
                }
                else if (Option2.Checked)
                {
                    protector = new SteelProtector(output);
                }
                else
                {
                    throw new ArgumentOutOfRangeException();
                }
                phone.ProtectorComponent = protector;
                output.WriteLine($"{protector.GetType().Name} selected");
                output.WriteLine("Put protector on Mobile:");
                phone.Put(protector);
                break;
            }
        }