public void Play_pause() { var ctx = new ContextFor <ChannelsViewModel>(); var waveFormat = WaveFormat.CreateIeeeFloatWaveFormat(8000, 1); var mixer = ctx.For <IChannelMixer>(); mixer.SampleProvider.WaveFormat.Returns(waveFormat); var player = ctx.For <IWavePlayer>(); player.When(x => x.Play()).Do(x => player.PlaybackState.Returns(PlaybackState.Playing)); player.When(x => x.Pause()).Do(x => player.PlaybackState.Returns(PlaybackState.Paused)); var sut = ctx.BuildSut(); sut.IsPlaying.Should().Be(sut.IsActive); ((IActivate)sut).Activate(); sut.IsPlaying.Should().Be(sut.IsActive); sut.Pause(); sut.IsPlaying.Should().BeFalse(); sut.Play(); sut.IsPlaying.Should().BeTrue(); ((IDeactivate)sut).Deactivate(false); sut.IsPlaying.Should().Be(sut.IsActive); }
public void Log_and_rethrow() { var context = new ContextFor <NuPlugPackageManager>(); var sut = context.BuildSut(); var foo = "foo".CreatePackage("0.1.0"); var expExc = new InvalidOperationException("test"); context.For <IPackageRepository>(nameof(sut.LocalRepository)) .GetPackages() .Returns(x => { throw expExc; }); using (var tl = new TestTraceListener()) { sut.Logger = new TraceLogger(); try { sut.InstallPackage(foo, true, false); } catch (Exception ex) { ex.Should().Be(expExc); } tl.MessagesFor(TraceLevel.Warning) .Single() .Should() .StartWith($"Could not install '{foo.Id} {foo.Version}': "); } }
public void Use_full_volume_and_center_balance_on_sampling() { var ctx = new ContextFor <NAudioGetWaveform>(); var peakProvider = new PeakProvider(); ctx.Use <IPeakProvider>(peakProvider); var sut = ctx.BuildSut(); var waveFormat = WaveFormat.CreateIeeeFloatWaveFormat(8000, 1); using (var buffer = new BufferedWaveStream(waveFormat, TimeSpan.FromSeconds(1))) using (var waveStream = new WaveProviderEx(buffer)) { var source = new Uri("my://source/"); ctx.For <IWaveProviderFactory>().Create(source).Returns(waveStream); waveStream.Position = 40; waveStream.Volume = 0.5f; waveStream.Pan = -1f; var samples = waveFormat.Generate(buffer.BufferDuration, SignalGeneratorType.Square); buffer.AddSamples(samples, 0, samples.Length); var waveForm = sut.For(source); waveForm.Duration.Should().Be(buffer.BufferDuration); waveForm.Peaks.Should().HaveCount((int)(peakProvider.PeaksPerSecond * buffer.BufferDuration.TotalSeconds)); waveForm.Peaks.All(p => p.Max == 1f && p.Min == -1f).Should().BeTrue(); waveStream.Position.Should().Be(40, "should be restored after sampling"); waveStream.Volume.Should().Be(0.5f); waveStream.Pan.Should().Be(-1f); } }
public void Lookup_packages() { var ctx = new ContextFor <NuPlugPackageRepository>(); var sut = ctx.BuildSut(); var lookup = ctx.For <IPackageLookup>(); var registry = ctx.For <IPackageRegistry>(); var package = "foo".CreatePackage("0.1.0"); sut.Exists(package).Should().BeFalse(); Received.InOrder(() => { registry.Exists(package.Id, package.Version); lookup.Exists(package.Id, package.Version); }); registry.FindPackage(package.Id, package.Version).Returns(package); sut.FindPackage(package.Id, package.Version).Should().Be(package); registry.FindPackage(package.Id, package.Version).Returns((IPackage)null); lookup.FindPackage(package.Id, package.Version).Returns(package); sut.FindPackage(package.Id, package.Version).Should().Be(package); lookup.FindPackage(package.Id, package.Version).Returns((IPackage)null); sut.FindPackage(package.Id, package.Version).Should().BeNull(); sut.FindPackagesById(package.Id).Should().BeEmpty(); var packages = new[] { package }; registry.FindPackagesById(package.Id).Returns(packages); sut.FindPackagesById(package.Id).ShouldAllBeEquivalentTo(packages); var empty = Enumerable.Empty <IPackage>().ToList(); registry.FindPackagesById(package.Id).Returns(empty); lookup.FindPackagesById(package.Id).Returns(packages); sut.FindPackagesById(package.Id).ShouldAllBeEquivalentTo(packages); lookup.FindPackagesById(package.Id).Returns(empty); sut.FindPackagesById(package.Id).Should().BeEmpty(); }
public void Return_players_Source_when_getting_source() { var uri = new Uri("http://some/uri/audio.wav"); var ctx = new ContextFor <WaveformPlayerViewModel>(); ctx.For <IMediaPlayer>().Source.Returns(uri); var sut = ctx.BuildSut(); sut.Source.Should().Be(uri); }
public void Cleanup() { var ctx = new ContextFor <WaveformViewModel>(); WaveformViewModel sut; var events = ctx.For <IEventAggregator>(); using (sut = ctx.BuildSut()) events.Received().Subscribe(sut); events.Received().Unsubscribe(sut); }
public void Get_lib_dirs_for(string framework) { var context = new ContextFor <NuGetPackageContainer <string> >(); var foo = "foo".CreatePackage("0.1.0"); var packages = new[] { foo }; var pm = context.For <IPackageManager>(); pm.LocalRepository.GetPackages().Returns(packages.AsQueryable()); var path = foo.Id; pm.PathResolver.GetInstallPath(foo).Returns(path); var expectedPath = MockFileSystem(pm.FileSystem, foo, framework); var sut = context.BuildSut(); var asmResolver = context.For <IResolveAssembly>(); asmResolver.Received().Directories.Add(expectedPath); }
public void Ignore_AlreadyInstalled_Packages() { var ctx = new ContextFor <NuPlugPackageRepository>(); var sut = ctx.BuildSut(); var package = "foo".CreatePackage("0.1.0"); sut.Exists(package.Id, package.Version).Should().BeFalse(); ctx.For <IPackageRegistry>().Exists(package.Id, Arg.Any <SemanticVersion>()).Returns(true); sut.Exists(package.Id, package.Version).Should().BeTrue(); }
public void Handle_samples_and_publish_peaks() { var ctx = new ContextFor <SamplesHandlerPeakPublisher>(); var sut = ctx.BuildSut(); sut.Should().BeAssignableTo <IHandle <SamplesReceivedEvent> >(); var events = ctx.For <IEventAggregator>(); events.Received().Subscribe(sut); var waveFormat = new WaveFormat(8000, 1); var start = TimeSpan.Zero; var duration = TimeSpan.FromSeconds(1); var data = waveFormat.Generate(duration); var samples = new SamplesReceivedEvent(new Uri("some://uri/"), start, waveFormat, data); PeaksReceivedEvent actualPeaks = null; events.When(x => x.PublishOnCurrentThread(Arg.Any <PeaksReceivedEvent>())).Do(x => actualPeaks = x.Arg <PeaksReceivedEvent>()); var peakProvider = ctx.For <IPeakProvider>(); WaveFormat actualWaveformat = null; byte[] actualData = null; peakProvider.Sample(Arg.Any <WaveFormat>(), Arg.Any <byte[]>()).Returns(x => { actualWaveformat = x.Arg <WaveFormat>(); actualData = x.Arg <byte[]>(); return(new[] { new PeakInfo(-1, 1) }); }); sut.Handle(samples); actualPeaks.Source.Should().Be(samples.Source); actualPeaks.Start.Should().Be(samples.Start.TotalSeconds); actualPeaks.End.Should().Be((samples.Start + duration).TotalSeconds); actualWaveformat.Should().BeSameAs(waveFormat); actualData.Should().BeSameAs(samples.Data); }
public void Initialize_selection_menu_on() { var ctx = new ContextFor <WaveformPlayerViewModel>(); var menu = new MenuViewModel(new[] { new MenuItemViewModel { Header = "test" } }); ctx.For <IAudioSelectionMenuProvider>().Menu.Returns(menu); var sut = ctx.BuildSut(); sut.Waveform.SelectionMenu.Should().Be(menu); }
public void Sync_directories_with_installed_packages() { var context = new ContextFor <NuGetPackageContainer <string> >(); var foo = "foo".CreatePackage("0.1.0"); var packages = new[] { foo }; var pm = context.For <IPackageManager>(); pm.LocalRepository.GetPackages().Returns(packages.AsQueryable()); var path = foo.Id; pm.PathResolver.GetInstallPath(foo).Returns(path); var expectedPath = MockFileSystem(pm.FileSystem, foo); var sut = context.BuildSut(); pm.LocalRepository.Received().GetPackages(); var asmResolver = context.For <IResolveAssembly>(); asmResolver.Received().Directories.Add(expectedPath); var bar = "bar".CreatePackage("1.42.0"); var fs2 = Substitute.For <IFileSystem>(); expectedPath = MockFileSystem(fs2, bar); var args = new PackageOperationEventArgs(bar, fs2, "otherPath"); pm.PackageInstalled += Raise.EventWith(args); asmResolver.Received().Directories.Add(expectedPath); pm.PackageUninstalled += Raise.EventWith(args); asmResolver.Received().Directories.Remove(expectedPath); }
public void Support_start_time_with_shifts() { var uri = new Uri("http://some/uri/audio.wav"); var ctx = new ContextFor <WaveformPlayerViewModel>(); var d = DateTimeOffset.UtcNow; var sut = ctx.BuildSut(); sut.Should().BeAssignableTo <IHandle <AudioShiftedEvent> >(); ctx.For <IEventAggregator>().Received().Subscribe(sut); var getTime = ctx.For <IGetTimeStamp>(); getTime.For(uri).Returns(d); sut.Source = uri; sut.HasCurrentTime.Should().BeTrue(); sut.StartTime.Should().Be(d); var shiftedEvent = new AudioShiftedEvent(uri, TimeSpan.FromSeconds(3), sut.StartTime); sut.Handle(shiftedEvent); sut.StartTime.Should().Be(d + shiftedEvent.Shift); }
public void Support_an_Api_to_ignore_packages() { var ctx = new ContextFor <NuPlugPackageRepository>(); var sut = ctx.BuildSut(); sut.Should().BeAssignableTo <ISkipPackages>(); var bar = "bar".CreatePackage("0.1.1"); var packagesToSkip = new[] { bar }.ToList(); sut.SkipPackages(packagesToSkip); var packageRegistry = ctx.For <IPackageRegistry>(); packagesToSkip.ForEach(p => packageRegistry.Received().Add(p.Id, p.Version)); }
public void Cleanup_on_dispose() { var ctx = new ContextFor <WaveformDisplayViewModel>(); var events = ctx.For <IEventAggregator>(); WaveformDisplayViewModel sut; using (sut = ctx.BuildSut()) { sut.WaveformImage.Should().NotBeNull(); events.Received().Subscribe(sut); } events.Received().Unsubscribe(sut); sut.WaveformImage.Should().BeNull(); }
public void Update_waveform_when_changing_source() { var ctx = new ContextFor <WaveformDisplayViewModel>(); var sut = ctx.BuildSut(); var uri = new Uri("my://source/"); var getWaveform = ctx.For <IGetWaveform>(); var waveform = new WaveformData(TimeSpan.FromSeconds(2), new[] { new PeakInfo(-1f, 1) }); getWaveform.For(uri).Returns(waveform); sut.Source = uri; getWaveform.Received().For(uri); sut.CurrentStreamTime.Should().BeCloseTo(DateTime.UtcNow, 200); sut.Duration.Should().Be(waveform.Duration.TotalSeconds); }
public void Query_On_For() { var ctx = new ContextFor <ElasticSearchSuggestions>(); ctx.Use("http://127.0.0.1:9200", "connectionPrefix"); ctx.For <IRestClient>() .Request(HttpMethod.Post, "_search", Arg.Any <JToken>()) .Returns(TestData.Hits); var sut = ctx.BuildSut(); var items = sut.For("Tho", "elasticsearch://nzazu/autocomplete|director").ToArray(); items.Should().Equal("Thomate", "Thomas", "Thomas Smith"); items.Should().NotContain("Horst"); items = sut.For("Tho", "elasticsearch://nzazu/autocomplete|director").ToArray(); items.Should().Equal("Thomate", "Thomas", "Thomas Smith"); items.Should().NotContain("Horst"); }
public void Delegate_to_decorated_repo() { var ctx = new ContextFor <NuPlugPackageRepository>(); var sut = ctx.BuildSut(); sut.Should().BeAssignableTo <IPackageLookup>(); var lookup = ctx.For <IPackageLookup>(); sut.PackageSaveMode.Should().Be(lookup.PackageSaveMode); // ReSharper disable once RedundantAssignment var mode = lookup.Received().PackageSaveMode; mode = PackageSaveModes.Nupkg; sut.PackageSaveMode = mode; lookup.Received().PackageSaveMode = mode; lookup.SupportsPrereleasePackages.Returns(true); sut.SupportsPrereleasePackages.Should().BeTrue(); // ReSharper disable once UnusedVariable var b = lookup.Received().SupportsPrereleasePackages; lookup.Source.Returns("source"); sut.Source.Should().Be(lookup.Source); // ReSharper disable once UnusedVariable var s = lookup.Received().Source; sut.GetPackages().Should().BeEmpty(); lookup.Received().GetPackages(); var package = "foo".CreatePackage("0.1.0"); sut.AddPackage(package); lookup.Received().AddPackage(package); sut.RemovePackage(package); lookup.Received().RemovePackage(package); }
public void Handle_live_sample_updates() { var ctx = new ContextFor <WaveformDisplayViewModel>(); var sut = ctx.BuildSut(); sut.Should().BeAssignableTo <IHandle <PeaksReceivedEvent> >(); ctx.For <IEventAggregator>().Received().Subscribe(sut); // |---------- | // |-------------------| // | ---------| sut.BackgroundBrush = new SolidColorBrush(Colors.Black); sut.LeftBrush = new SolidColorBrush(Colors.Red); sut.RightBrush = new SolidColorBrush(Colors.Blue); sut.WaveformImage = BitmapFactory.New(20, 20); var w2 = (int)(sut.WaveformImage.Width / 2); var h2 = (int)(sut.WaveformImage.Height / 2); var peaks = Enumerable.Repeat(1f, 2 * w2).Concat(Enumerable.Repeat(0f, 2 * w2)) .Select(m => new PeakInfo(m - 1f, m)).ToArray(); var e = new PeaksReceivedEvent(new Uri("source://test/"), 0, 2, peaks); sut.Duration = e.End; sut.HandlePeaks(e); // assert the image rendered sut.WaveformImage.RectShouldHaveColor(1, 1, w2, h2, sut.LeftBrush.Color); sut.WaveformImage.RectShouldHaveColor(w2 + 1, h2 + 1, 2 * w2, 2 * h2, sut.RightBrush.Color); sut.WaveformImage.RectShouldHaveColor(1, h2 + 1, w2 - 1, 2 * h2 - 1, sut.BackgroundBrush.Color); sut.WaveformImage.RectShouldHaveColor(w2 + 1, 1, 2 * w2, h2, sut.BackgroundBrush.Color); // assert the points are filled sut.LeftChannel.Take(10).Should().AllBeEquivalentTo(0); sut.RightChannel.Take(10).Should().AllBeEquivalentTo(h2); sut.LeftChannel.Skip(10).Take(10).Should().AllBeEquivalentTo(h2); sut.RightChannel.Skip(10).Take(10).Should().AllBeEquivalentTo(2 * h2); }