public async Task MonitorPageUpdateLateRenderRequest() { // In this test we make sure not to access the # of pages. var f = new dummyFile(); var data = await TestUtils.GetFileAsBytes("test.pdf"); f.GetStream = () => { return(Observable.Return(new StreamReader(new MemoryStream(data)))); }; var dc = new dummyCache(); var vm = new FileDownloadController(f, dc); var pf = new PDFFile(vm); // Setup the look before we setup the render. var pupdatetmp = pf.GetPageStreamAndCacheInfo(5).Timeout(TimeSpan.FromSeconds(5)).FirstAsync(); vm.DownloadOrUpdate.Execute(null); var pupdate = await pupdatetmp; // Make sure what came back looks ok! Assert.IsNotNull(pupdate); var page = await pupdate.Item2.FirstAsync(); Assert.AreEqual(5, (int)page.Index); }
public async Task MonitorPageUpdateEarlyRenderRequest() { // In this test we make sure not to access the # of pages. var f = new dummyFile(); var data = await TestUtils.GetFileAsBytes("test.pdf"); f.GetStream = () => { return(Observable.Return(new StreamReader(new MemoryStream(data)))); }; var dc = new dummyCache(); var vm = new FileDownloadController(f, dc); var pf = new PDFFile(vm); // Run the download (which will complete synchronously here), and then schedule the watch // for new items. vm.DownloadOrUpdate.Execute(null); var pupdate = await pf.GetPageStreamAndCacheInfo(5).Timeout(TimeSpan.FromSeconds(5)).FirstAsync(); // Make sure all that came back is ok. Assert.IsNotNull(pupdate); var page = await pupdate.Item2.FirstAsync(); Assert.AreEqual(5, (int)page.Index); }
public async Task LoadVMButNoImageLoad() { // WHen this VM isn't attached to a actual View, we should not // trigger any loading or similar. All should remain very quiet. // (e.g. lazy loading). // Get the infrastructure setup var f = new dummyFile(); var data = await TestUtils.GetFileAsBytes("test.pdf"); int timesLoaded = 0; f.GetStream = () => { timesLoaded++; return(Observable.Return(new StreamReader(new MemoryStream(data)))); }; var dc = new dummyCache(); var vm = new FileDownloadController(f, dc); vm.DownloadOrUpdate.Execute(null); var pf = new PDFFile(vm); // Now, build the VM var pdfVM = new PDFPageViewModel(pf.GetPageStreamAndCacheInfo(1), dc); await TestUtils.SpinWait(() => timesLoaded != 0, 1000); Assert.AreEqual(1, timesLoaded); Assert.AreEqual(0, dc.NumberTimesGetCalled); }
public async Task SizePerpareCausesErrorsIfCalledEarly() { // Get the size stuff ready, and then call it to make sure // there are no issues with doing the size calculation. // Get the infrastructure setup var f = new dummyFile(); var data = await TestUtils.GetFileAsBytes("test.pdf"); f.GetStream = () => { return(Observable.Return(new StreamReader(new MemoryStream(data)))); }; var dc = new dummyCache(); var vm = new FileDownloadController(f, dc); var pf = new PDFFile(vm); vm.DownloadOrUpdate.Execute(null); // Now, build the VM var pdfVM = new PDFPageViewModel(pf.GetPageStreamAndCacheInfo(1), dc); // Next, fire off the size thing. var r = pdfVM.CalcRenderingSize(IWalker.ViewModels.PDFPageViewModel.RenderingDimension.Horizontal, (double)100, (double)100); Assert.IsNull(r); }
public async Task MakeSureNothingRenderedWhenImageCached() { // The exact image we need is in the cache. So we should never make a // request to load the PDF file or PdfDocument. // Get the infrastructure setup var f = new dummyFile(); var data = await TestUtils.GetFileAsBytes("test.pdf"); int loaderCalled = 0; f.GetStream = () => { loaderCalled++; throw new InvalidOperationException(); }; // Create the cache, and add everything into it that the system should need. var dc = new dummyCache(); await f.SaveFileInCache(f.DateToReturn, data, dc); var dt = await f.GetCacheCreateTime(dc); var pageSize = new IWalkerSize() { Width = 1280, Height = 720 }; await dc.InsertObject(string.Format("{0}-{1}-p1-DefaultPageSize", f.UniqueKey, dt.Value.ToString()), pageSize); var imageData = new byte[] { 0, 1, 2, 3, 4 }; await dc.Insert(string.Format("{0}-{1}-p1-w100-h56", f.UniqueKey, dt.Value), imageData); Debug.WriteLine("Setup is done, and data has been inserted into the cache. Testing starting"); // Create the rest of the infrastructure. var vm = new FileDownloadController(f, dc); var pf = new PDFFile(vm); // Now, build the VM var pdfVM = new PDFPageViewModel(pf.GetPageStreamAndCacheInfo(1), dc); // Subscribe so we can "get" the image. MemoryStream lastImage = null; pdfVM.ImageStream.Subscribe(img => lastImage = img); Assert.IsNull(lastImage); // Render, and make sure things "worked" pdfVM.RenderImage.Execute(Tuple.Create(IWalker.ViewModels.PDFPageViewModel.RenderingDimension.Horizontal, (double)100, (double)100)); vm.DownloadOrUpdate.Execute(null); await TestUtils.SpinWait(() => lastImage != null, 2000); Assert.AreEqual(0, loaderCalled); Assert.IsNotNull(lastImage); Assert.AreEqual(4, dc.NumberTimesInsertCalled); // Nothing new should have happened }
public async Task RenderNormalRenderEarlyTrigger() { // Normal sequence of things when there is no image cached. // WHen this VM isn't attached to a actual View, we should not // trigger any loading or similar. All should remain very quiet. // (e.g. lazy loading). // Get the infrastructure setup var f = new dummyFile(); var data = await TestUtils.GetFileAsBytes("test.pdf"); int timesLoaded = 0; f.GetStream = () => { timesLoaded++; return(Observable.Return(new StreamReader(new MemoryStream(data)))); }; var dc = new dummyCache(); var vm = new FileDownloadController(f, dc); // It shouldn't matter where the download is triggered from - let it happen early // here before other things are hooked up. vm.DownloadOrUpdate.Execute(null); var pf = new PDFFile(vm); // Now, build the VM var pdfVM = new PDFPageViewModel(pf.GetPageStreamAndCacheInfo(1), dc); // Subscribe so we can "get" the image. MemoryStream lastImage = null; Debug.WriteLine("Subscribing to ImageStream"); pdfVM.ImageStream.Subscribe(img => { lastImage = img; Debug.WriteLine("Just got an image."); }); Assert.IsNull(lastImage); // Render, and make sure things "worked" Debug.WriteLine("Going to fire off a render request"); pdfVM.RenderImage.Execute(Tuple.Create(IWalker.ViewModels.PDFPageViewModel.RenderingDimension.Horizontal, (double)100, (double)100)); await TestUtils.SpinWait(() => timesLoaded != 0, 1000); await TestUtils.SpinWait(() => dc.NumberTimesGetCalled == 3, 1000); await TestUtils.SpinWait(() => lastImage != null, 1000); Assert.AreEqual(1, timesLoaded); Assert.AreEqual(3, dc.NumberTimesGetCalled); // Once for data, once for size cache, and once again for data file. Assert.IsNotNull(lastImage); }
public async Task RenderAlreadyCachedFile() { // If the file has already been downloaded and installed locally (on a previous // look) then PDF rendering should happen automatically this time, even if // the download isn't triggered. // Get the infrastructure setup var f = new dummyFile(); var data = await TestUtils.GetFileAsBytes("test.pdf"); int loaderCalled = 0; f.GetStream = () => { loaderCalled++; throw new InvalidOperationException(); }; var dc = new dummyCache(); await f.SaveFileInCache(f.DateToReturn, data, dc); var vm = new FileDownloadController(f, dc); var pf = new PDFFile(vm); // Now, build the VM var pdfVM = new PDFPageViewModel(pf.GetPageStreamAndCacheInfo(1), dc); // Subscribe so we can "get" the image. MemoryStream lastImage = null; pdfVM.ImageStream.Subscribe(img => lastImage = img); Assert.IsNull(lastImage); // Render, and make sure things "worked" pdfVM.RenderImage.Execute(Tuple.Create(IWalker.ViewModels.PDFPageViewModel.RenderingDimension.Horizontal, (double)100, (double)100)); vm.DownloadOrUpdate.Execute(null); await TestUtils.SpinWait(() => lastImage != null, 2000); Assert.AreEqual(0, loaderCalled); Assert.IsNotNull(lastImage); }
public async Task GetFileViaCacheSequenceTwice() { // Pretend a cache miss, and fetch the file to do the render. ANd then // do it again. var f = new dummyFile(); var data = await TestUtils.GetFileAsBytes("test.pdf"); f.GetStream = () => { return(Observable.Return(new StreamReader(new MemoryStream(data)))); }; var dc = new dummyCache(); var vm = new FileDownloadController(f, dc); var pf = new PDFFile(vm); var dummy1 = pf.NumberOfPages; vm.DownloadOrUpdate.Execute(null); await pf.WhenAny(x => x.NumberOfPages, y => y.Value) .Where(y => y != 0) .Take(1) .Timeout(TimeSpan.FromSeconds(1), Observable.Return(0)) .FirstAsync(); Assert.AreEqual(10, pf.NumberOfPages); var pupdate = await pf.GetPageStreamAndCacheInfo(5).Timeout(TimeSpan.FromSeconds(1)).FirstAsync(); // First rendering var page = await pupdate.Item2.FirstAsync(); // Get the # of times we've done a data lookup. And this shouldn't change when we get it again. var getCalls = dc.NumberTimesGetCalled; Debug.WriteLine("Get was called {0} times after first page was rendered.", getCalls); var pageAgain = await pupdate.Item2.FirstAsync(); Assert.AreEqual(getCalls, dc.NumberTimesGetCalled); }
public async Task GetImagesViaCacheSequence() { // We want to make sure that if there is a cache image we never try to load // the file. This involves just getting the cache key, and if that doesn't // cause a fetch, we are good. var f = new dummyFile(); var data = await TestUtils.GetFileAsBytes("test.pdf"); f.GetStream = () => { throw new InvalidOperationException(); }; // For this to work, we need the # of pages in the cache already. var dc = new dummyCache(); await f.SaveFileInCache("old date", data, dc); var dtc = await f.GetCacheCreateTime(dc); var cacheStem = string.Format("talk.pdf-{0}", dtc.Value.ToString()); await dc.InsertObject(cacheStem + "-NumberOfPages", 10).FirstAsync(); // Create VM's and hook them up. var vm = new FileDownloadController(f, dc); var pf = new PDFFile(vm); var dummy1 = pf.NumberOfPages; // Get the cache info and the items from it, we are really only // interested in the first item. var cacheInfo = await pf .GetPageStreamAndCacheInfo(5) .Timeout(TimeSpan.FromSeconds(2), Observable.Return <Tuple <string, IObservable <PdfPage> > >(null)) .FirstAsync(); Assert.IsNotNull(cacheInfo); Assert.AreEqual(cacheStem + "-p5", cacheInfo.Item1); Assert.AreEqual(1, dc.NumberTimesGetCalled); }