예제 #1
0
        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
        }
예제 #2
0
        public async Task GetBytesAlreadyCached()
        {
            var dc = new dummyCache();
            await dc.Insert("hi", new byte[] { 0, 1, 2 }).ToArray();

            var r = await dc.GetOrFetch("hi", () => Observable.Return(new byte[0]));

            Assert.AreEqual(3, r.Length);
            Assert.AreEqual((byte)1, r[1]);
        }