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 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); }