public void TestNormalCase()
        {
            SampleFrameDisplayerImpl displayer
                = new SampleFrameDisplayerImpl(mockDrawer.Object, 10, 3);
            ushort[] samples = new ushort[21];
            for(int i = 0; i < 21; i++)
            {
                samples[i] = (ushort)(i * 2);
            }

            displayer.SetNumSamplesToDisplay(11);
            mockDrawer.Setup(x => x.getCanvasWidth()).Returns(31);
            displayer.DisplaySampleFrameFromStartIndex(10, 21, samples);

            List<LineCoordinates> expectedLines = new List<LineCoordinates>();
            // Test that all of the expected lines got called to be drawn
            for(int i = 0; i < 10; i++)
            {
                int expX1 = i * 3;
                int expY1 = samples[i + 10];
                int expX2 = i * 3 + 3;
                int expY2 = samples[i + 11];
                LineCoordinates expectedLine = CreateLineCoordinates(expX1, expY1, expX2, expY2);
                expectedLines.Add(expectedLine);
            }
            // The canvas should be cleared every time before drawing
            mockDrawer.Verify(x => x.drawLinesOnOscope(It.Is<List<LineCoordinates>>(
                lines => ListOfLineCoordinatesStructsEqual(lines, expectedLines))), Times.Once);
        }
        public MainWindow()
        {
            InitializeComponent();

            Array.ForEach<string>(SerialPort.GetPortNames(), name => this.COM_port_comboBox.Items.Add(name));

            // Initialize PSOC sample receiving chain.
            OscopeWindowClient oscopeWindowClient
                = new OscopeWindowClientImpl(this.oscope_window_canvas, this, (int)this.oscope_window_canvas.Width);
            SampleFrameDisplayer sampleFrameDisplayer = new SampleFrameDisplayerImpl(oscopeWindowClient, this);
            SampleFrameReceiver sampleFrameReceiver = new RisingEdgeTriggeringFrameReceiver(sampleFrameDisplayer, this);
            SampleFrameAssembler sampleFrameAssembler = new SampleFrameAssemblerImpl(sampleFrameReceiver);
            SampleAssembler sampleAssembler = new HighByteFirstSampleAssemblerImpl(sampleFrameAssembler, this);
            ByteReceiverImpl byteReceiver = new ByteReceiverImpl(sampleAssembler, sampleFrameAssembler);
            serialPortClient = new SerialPortClient(byteReceiver, this);

            byteReceiver.PsocReadyEvent += PSOC_ready;

            TriggerLevelChangedEvent(this, new TriggerLevelChangedEventArgs(100));
            TriggerScanLengthChangedEvent(this, new TriggerScanLengthChangedEventArgs(300));
            TriggerScanStartChangedEvent(this, new TriggerScanStartIndexChangedEventArgs(0));
            TriggerRelativeDisplayStartChangedEvent(this, new TriggerRelativeDisplayStartChangedEventArgs(0));
            SampleSpacingChangedEvent(this, new SampleSpacingChangedEventArgs(10));
            NumSamplesToDisplayChangedEvent(this, new NumSamplesToDisplayChangedEventArgs(300));
            OscopeHeightChangedEvent(this, new OscopeHeightChangedEventArgs(
                    (int)this.oscope_window_canvas.Height));
            OscopeWidthChangedEvent(this, new OscopeWidthChangedEventArgs(
                    (int)this.oscope_window_canvas.Width));
            MaxSampleSizeChangedEvent(this, new MaxSampleSizeChangedEventArgs(
                    4095));
            SampleScalerChangedEvent(this, new SampleScalerChangedEventArgs(
                    1));
            SampleOffsetChangedEvent(this, new SampleOffsetChangedEventArgs(
                    0.0));
        }
        public void testNumSamplesAvailableLessThanNumSamplesToDisplay()
        {
            Mock<OscopeWindowClient> mockDrawer = new Mock<OscopeWindowClient>();
            SampleFrameDisplayerImpl displayer
                = new SampleFrameDisplayerImpl(mockDrawer.Object, 10, 3);
            ushort[] samples = new ushort[21];
            for (int i = 0; i < 21; i++)
            {
                samples[i] = (ushort)(i * 2);
            }

            displayer.SetNumSamplesToDisplay(11);
            mockDrawer.Setup(x => x.getCanvasWidth()).Returns(20);

            // calling display samples with a start of 11 and 21 total, so 10 samples available.
            displayer.DisplaySampleFrameFromStartIndex(11, 21, samples);
        }
        public void TestSpacingOfTwo()
        {
            Mock<OscopeWindowClient> mockDrawer = new Mock<OscopeWindowClient>();
            SampleFrameDisplayerImpl displayer
                = new SampleFrameDisplayerImpl(mockDrawer.Object, 10, 2);
            ushort[] samples = new ushort[] { 0, 1, 2, 3 };

            displayer.SetNumSamplesToDisplay(3);
            mockDrawer.Setup(x => x.getCanvasWidth()).Returns(4);
            displayer.DisplaySampleFrameFromStartIndex(1, 4, samples);

            List<LineCoordinates> expLines = new List<LineCoordinates>();
            expLines.Add(CreateLineCoordinates(0, 1, 2, 2));
            expLines.Add(CreateLineCoordinates(2, 2, 4, 3));

            mockDrawer.Verify(x => x.drawLinesOnOscope(It.Is<List<LineCoordinates>>
                (lines => ListOfLineCoordinatesStructsEqual(lines, expLines)))
                , Times.Once);
        }
        public void testSpacingTimesNumSamplesToDisplayNotEnoughToCoverCanvas()
        {
            Mock<OscopeWindowClient> mockDrawer = new Mock<OscopeWindowClient>();
            SampleFrameDisplayerImpl displayer
                = new SampleFrameDisplayerImpl(mockDrawer.Object, 10, 3);
            ushort[] samples = new ushort[21];
            for (int i = 0; i < 21; i++)
            {
                samples[i] = (ushort)(i * 2);
            }

            displayer.SetNumSamplesToDisplay(11);
            // Setting canvas width to 17. The line beginning at 15 and going to 18 should
            // still be drawn though.
            mockDrawer.Setup(x => x.getCanvasWidth()).Returns(32);

            displayer.DisplaySampleFrameFromStartIndex(10, 21, samples);
        }
        public void testSpacingTimesNumSamplesToDisplayGreatherThanCanvasWidth()
        {
            Mock<OscopeWindowClient> mockDrawer = new Mock<OscopeWindowClient>();
            SampleFrameDisplayerImpl displayer
                = new SampleFrameDisplayerImpl(mockDrawer.Object, 10, 3);
            ushort[] samples = new ushort[21];
            for (int i = 0; i < 21; i++)
            {
                samples[i] = (ushort)(i * 2);
            }

            displayer.SetNumSamplesToDisplay(11);
            // Setting canvas width to 17. The line beginning at 15 and going to 18 should
            // still be drawn though.
            mockDrawer.Setup(x => x.getCanvasWidth()).Returns(17);
            displayer.DisplaySampleFrameFromStartIndex(10, 21, samples);

            // The canvas should be cleared every time before drawing
            List<LineCoordinates> expLines = new List<LineCoordinates>();

            // Test that all of the expected lines got called to be drawn
            for (int i = 0; i < 6; i++)
            {
                int expX1 = i * 3;
                int expY1 = samples[i + 10];
                int expX2 = i * 3 + 3;
                int expY2 = samples[i + 11];
                expLines.Add(CreateLineCoordinates(expX1, expY1, expX2, expY2));
            }

            mockDrawer.Verify(x => x.drawLinesOnOscope(It.Is<List<LineCoordinates>>
                (lines => ListOfLineCoordinatesStructsEqual(lines, expLines)))
                , Times.Once);
        }