public void SetLength() { var mockedStream = new Mock<Stream>(); mockedStream.Setup(s => s.SetLength(It.IsAny<long>())).Callback<long>((l) => mockedStream.Setup(mock => mock.Length).Returns(l)); using (ProgressStream progress = new ProgressStream(mockedStream.Object)) { progress.PropertyChanged += delegate(object sender, System.ComponentModel.PropertyChangedEventArgs args) { if (args.PropertyName == Utils.NameOf((ProgressStream s) => s.Length)) { this.lengthCalls++; } }; progress.SetLength(100); progress.SetLength(100); Assert.AreEqual(1, this.lengthCalls); Assert.That(progress.Length, Is.EqualTo(100)); } }
public void Position() { var mockedStream = new Mock<Stream>(); mockedStream.Setup(s => s.SetLength(It.IsAny<long>())); mockedStream.SetupProperty(s => s.Position); using (var underTest = new ProgressStream(mockedStream.Object)) { underTest.PropertyChanged += delegate(object sender, System.ComponentModel.PropertyChangedEventArgs e) { var p = sender as ProgressStream; if (e.PropertyName == Utils.NameOf(() => p.Length)) { this.lengthCalls++; } if (e.PropertyName == Utils.NameOf(() => p.Position)) { this.positionCalls++; } }; underTest.SetLength(100); Assert.AreEqual(1, this.lengthCalls); Assert.AreEqual(0, this.positionCalls); underTest.Position = 50; underTest.Position = 50; Assert.AreEqual(1, this.positionCalls); underTest.Position = 55; Assert.AreEqual(2, this.positionCalls); Assert.AreEqual(1, this.lengthCalls); } }
public void SeekTest() { using (var stream = new MemoryStream()) { using (var progress = new ProgressStream(stream)) { progress.PropertyChanged += delegate(object sender, System.ComponentModel.PropertyChangedEventArgs e) { var p = sender as ProgressStream; if (e.PropertyName == Utils.NameOf(() => p.Position)) { this.positionCalls++; this.position = (long)p.Position; this.percent = (double)p.Percent; } }; progress.SetLength(100); progress.Seek(10, SeekOrigin.Begin); Assert.AreEqual(10, this.position); Assert.AreEqual(10, this.percent); progress.Seek(10, SeekOrigin.Current); Assert.AreEqual(20, this.position); Assert.AreEqual(20, this.percent); progress.Seek(10, SeekOrigin.Current); Assert.AreEqual(30, this.position); Assert.AreEqual(30, this.percent); progress.Seek(10, SeekOrigin.Current); Assert.AreEqual(40, this.position); Assert.AreEqual(40, this.percent); progress.Seek(5, SeekOrigin.Current); Assert.AreEqual(45, this.position); Assert.AreEqual(45, this.percent); progress.Seek(10, SeekOrigin.Current); Assert.AreEqual(55, this.position); Assert.AreEqual(55, this.percent); progress.SetLength(1000); progress.Seek(10, SeekOrigin.Current); Assert.AreEqual(65, this.position); Assert.AreEqual(6.5, this.percent); progress.Seek(0, SeekOrigin.End); Assert.AreEqual(100, this.percent); Assert.AreEqual(1000, this.position); } } }
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); } } }