public static void TestPropertiesAndMethods() { using (MemoryStream memoryStream = FakeRuntimeFileInfo.ExpandableMemoryStream(new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 })) { string kilroy = String.Empty; using (FakeStream testStream = new FakeStream(memoryStream, (string wasHere) => { kilroy += wasHere; })) { using (ProgressStream progressStream = new ProgressStream(testStream, new ProgressContext())) { kilroy = String.Empty; Assert.That(progressStream.CanRead, Is.True, "The underlying stream is readable."); Assert.That(kilroy, Is.EqualTo("CanRead"), "ProgressStream should delegate to the underlying stream."); kilroy = String.Empty; Assert.That(progressStream.CanWrite, Is.True, "The underlying stream is writable."); Assert.That(kilroy, Is.EqualTo("CanWrite"), "ProgressStream should delegate to the underlying stream."); kilroy = String.Empty; Assert.That(progressStream.CanSeek, Is.True, "The underlying stream is seekable."); Assert.That(kilroy, Is.EqualTo("CanSeek"), "ProgressStream should delegate to the underlying stream."); kilroy = String.Empty; progressStream.Flush(); Assert.That(kilroy, Is.EqualTo("Flush"), "ProgressStream should delegate to the underlying stream."); kilroy = String.Empty; Assert.That(progressStream.Length, Is.EqualTo(10), "There are 10 bytes in the underlying stream."); Assert.That(kilroy, Is.EqualTo("Length"), "ProgressStream should delegate to the underlying stream."); kilroy = String.Empty; Assert.That(progressStream.Seek(-4, SeekOrigin.End), Is.EqualTo(6), "4 bytes from the end of 10 should be 6."); Assert.That(kilroy, Is.EqualTo("Seek"), "ProgressStream should delegate to the underlying stream."); kilroy = String.Empty; Assert.That(progressStream.Position, Is.EqualTo(6), "The position should still be at 6."); Assert.That(kilroy, Is.EqualTo("getPosition"), "ProgressStream should delegate to the underlying stream."); kilroy = String.Empty; progressStream.Position = 0; Assert.That(kilroy, Is.EqualTo("setPosition"), "ProgressStream should delegate to the underlying stream."); kilroy = String.Empty; progressStream.Write(new byte[] { 13 }, 0, 1); Assert.That(kilroy.Contains("Write"), Is.True, "ProgressStream should delegate to the underlying stream."); kilroy = String.Empty; progressStream.Position = 0; byte[] firstByte = new byte[1]; progressStream.Read(firstByte, 0, 1); Assert.That(kilroy.Contains("Read"), Is.True, "ProgressStream should delegate to the underlying stream."); kilroy = String.Empty; Assert.That(firstByte[0], Is.EqualTo(13), "13 was just written to the first position."); progressStream.SetLength(5); Assert.That(kilroy, Is.EqualTo("SetLength"), "ProgressStream should delegate to the underlying stream."); } } } }
public static void TestInvalidArguments() { Stream nullStream = null; ProgressContext nullProgress = null; ProgressStream progressStream; Assert.Throws <ArgumentNullException>(() => { progressStream = new ProgressStream(nullStream, new ProgressContext()); }); Assert.Throws <ArgumentNullException>(() => { progressStream = new ProgressStream(new MemoryStream(), nullProgress); }); progressStream = new ProgressStream(new MemoryStream(), new ProgressContext()); byte[] nullBuffer = null; Assert.Throws <ArgumentNullException>(() => { progressStream.Write(nullBuffer, 0, 0); }); Assert.Throws <ArgumentNullException>(() => { progressStream.Read(nullBuffer, 0, 0); }); }
public void Inner_Stream_Calls_Write() { string key = "test"; var mockStream = new Mock <Stream>(); var mockProgress = new Mock <IProgress <IWriteProgress> >(); var progressStream = new ProgressStream(key, mockStream.Object, null, mockProgress.Object); byte[] buffer = new byte[0]; int offset = 0; int count = 0; mockProgress.Setup(p => p.Report(It.IsAny <IWriteProgress>())); progressStream.Write(buffer, offset, count); mockProgress.Verify(p => p.Report(It.IsAny <IWriteProgress>()), Times.Once); mockStream.Verify(s => s.Write(buffer, offset, count), Times.Once); }
public void TestWrite() { using (var stream = new ProgressStream(new DummyNetworkStream(), Update)) { var buffer = new byte[1024]; int expected = 517; progress = 0; stream.Write(buffer, 0, expected); stream.Flush(); Assert.AreEqual(expected, progress, "progress"); } using (var stream = new ProgressStream(new DummyNetworkStream(), Update)) { var buffer = new byte[1024]; int expected = 517; progress = 0; stream.Write(buffer, 0, expected, CancellationToken.None); stream.Flush(CancellationToken.None); Assert.AreEqual(expected, progress, "progress"); } }
public void WriteTest() { using (Stream stream = new MemoryStream()) { FileTransmissionEvent transmissionEvent = new FileTransmissionEvent(this.transmissionType, this.filename); transmissionEvent.TransmissionStatus += delegate(object sender, TransmissionProgressEventArgs args) { if (args.ActualPosition != null) { this.positionCalls++; this.position = (long)args.ActualPosition; this.percent = (double)args.Percent; } }; byte[] buffer = new byte[10]; using (ProgressStream progress = new ProgressStream(stream, transmissionEvent)) { progress.SetLength(buffer.Length * 10); progress.Write(buffer, 0, buffer.Length); Assert.AreEqual(buffer.Length, this.position); Assert.AreEqual(10, this.percent); progress.Write(buffer, 0, buffer.Length); Assert.AreEqual(buffer.Length * 2, this.position); Assert.AreEqual(20, this.percent); progress.Write(buffer, 0, buffer.Length); Assert.AreEqual(buffer.Length * 3, this.position); Assert.AreEqual(30, this.percent); progress.Write(buffer, 0, buffer.Length); Assert.AreEqual(buffer.Length * 4, this.position); Assert.AreEqual(40, this.percent); progress.Write(buffer, 0, buffer.Length / 2); Assert.AreEqual((buffer.Length * 4) + (buffer.Length / 2), this.position); Assert.AreEqual(45, this.percent); progress.Write(buffer, 0, buffer.Length); Assert.AreEqual((buffer.Length * 5) + (buffer.Length / 2), this.position); Assert.AreEqual(55, this.percent); progress.SetLength(buffer.Length * 100); progress.Write(buffer, 0, buffer.Length); Assert.AreEqual((buffer.Length * 6) + (buffer.Length / 2), this.position); Assert.AreEqual(6.5, this.percent); } } }
public void Write() { using (var stream = new MemoryStream()) { byte[] buffer = new byte[10]; using (var progress = new ProgressStream(stream)) { progress.PropertyChanged += delegate(object sender, System.ComponentModel.PropertyChangedEventArgs e) { var t = sender as ProgressStream; if (e.PropertyName == Utils.NameOf(() => t.Position)) { this.positionCalls++; this.position = (long)t.Position; this.percent = (double)t.Percent.GetValueOrDefault(); } }; progress.SetLength(buffer.Length * 10); progress.Write(buffer, 0, buffer.Length); Assert.AreEqual(buffer.Length, this.position); Assert.AreEqual(10, this.percent); progress.Write(buffer, 0, buffer.Length); Assert.AreEqual(buffer.Length * 2, this.position); Assert.AreEqual(20, this.percent); progress.Write(buffer, 0, buffer.Length); Assert.AreEqual(buffer.Length * 3, this.position); Assert.AreEqual(30, this.percent); progress.Write(buffer, 0, buffer.Length); Assert.AreEqual(buffer.Length * 4, this.position); Assert.AreEqual(40, this.percent); progress.Write(buffer, 0, buffer.Length / 2); Assert.AreEqual((buffer.Length * 4) + (buffer.Length / 2), this.position); Assert.AreEqual(45, this.percent); progress.Write(buffer, 0, buffer.Length); Assert.AreEqual((buffer.Length * 5) + (buffer.Length / 2), this.position); Assert.AreEqual(55, this.percent); progress.SetLength(buffer.Length * 100); progress.Write(buffer, 0, buffer.Length); Assert.AreEqual((buffer.Length * 6) + (buffer.Length / 2), this.position); Assert.AreEqual(6.5, this.percent); } } }
public void ResumeTest() { byte[] inputContent = new byte[100]; long offset = 100; using (Stream stream = new MemoryStream(inputContent)) using (OffsetStream offsetstream = new OffsetStream(stream, offset)) { FileTransmissionEvent transmissionEvent = new FileTransmissionEvent(this.transmissionType, this.filename); transmissionEvent.TransmissionStatus += delegate(object sender, TransmissionProgressEventArgs args) { if (args.ActualPosition != null && args.Percent != null) { this.position = (long)args.ActualPosition; this.percent = (double)args.Percent; } }; using (ProgressStream progress = new ProgressStream(offsetstream, transmissionEvent)) { progress.Seek(0, SeekOrigin.Begin); Assert.AreEqual(offset, this.position); Assert.AreEqual(50, this.percent); progress.Seek(10, SeekOrigin.Current); Assert.AreEqual(offset + 10, this.position); progress.Seek(0, SeekOrigin.End); Assert.AreEqual(100, this.percent); Assert.AreEqual(offset + inputContent.Length, this.position); progress.Seek(0, SeekOrigin.Begin); progress.WriteByte(0); Assert.AreEqual(offset + 1, this.position); Assert.AreEqual(50.5, this.percent); progress.WriteByte(0); Assert.AreEqual(offset + 2, this.position); Assert.AreEqual(51, this.percent); progress.Write(new byte[10], 0, 10); Assert.AreEqual(56, this.percent); } } }
public void ResumeTest() { byte[] inputContent = new byte[100]; long offset = 100; using (var stream = new MemoryStream(inputContent)) using (var offsetstream = new OffsetStream(stream, offset)) { using (var progress = new ProgressStream(offsetstream)) { progress.PropertyChanged += delegate(object sender, System.ComponentModel.PropertyChangedEventArgs e) { var p = sender as ProgressStream; if (e.PropertyName == Utils.NameOf(() => p.Position)) { this.position = (long)p.Position; this.percent = p.Percent.GetValueOrDefault(); } }; progress.Seek(0, SeekOrigin.Begin); Assert.AreEqual(offset, this.position); Assert.AreEqual(50, this.percent); progress.Seek(10, SeekOrigin.Current); Assert.AreEqual(offset + 10, this.position); progress.Seek(0, SeekOrigin.End); Assert.AreEqual(100, this.percent); Assert.AreEqual(offset + inputContent.Length, this.position); progress.Seek(0, SeekOrigin.Begin); progress.WriteByte(0); Assert.AreEqual(offset + 1, this.position); Assert.AreEqual(50.5, this.percent); progress.WriteByte(0); Assert.AreEqual(offset + 2, this.position); Assert.AreEqual(51, this.percent); progress.Write(new byte[10], 0, 10); Assert.AreEqual(56, this.percent); } } }