summaryrefslogtreecommitdiff
path: root/testing/fake_file_access.cpp
diff options
context:
space:
mode:
authorArtem Strygin <art-snake@yandex-team.ru>2017-09-28 18:46:03 +0300
committerChromium commit bot <commit-bot@chromium.org>2017-09-28 15:59:08 +0000
commit0e60b9ef2b79de52ef62101abae2af7292e879b7 (patch)
tree9f71519f2a364ee6702219a44de7fdb1def18cc7 /testing/fake_file_access.cpp
parent1ca7173cd85adcd58766fc89f95c3dc163efa17a (diff)
downloadpdfium-0e60b9ef2b79de52ef62101abae2af7292e879b7.tar.xz
Implement FakeFileAccess.
Update embedder tests to simulate unavailable data and download requests. Change-Id: I634fa89d2a0c859243e849752936da87568909f4 Reviewed-on: https://pdfium-review.googlesource.com/11890 Commit-Queue: Art Snake <art-snake@yandex-team.ru> Reviewed-by: dsinclair <dsinclair@chromium.org>
Diffstat (limited to 'testing/fake_file_access.cpp')
-rw-r--r--testing/fake_file_access.cpp115
1 files changed, 115 insertions, 0 deletions
diff --git a/testing/fake_file_access.cpp b/testing/fake_file_access.cpp
new file mode 100644
index 0000000000..c69f278102
--- /dev/null
+++ b/testing/fake_file_access.cpp
@@ -0,0 +1,115 @@
+// Copyright 2017 PDFium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "testing/fake_file_access.h"
+
+#include <algorithm>
+#include <set>
+#include <utility>
+#include <vector>
+
+#include "core/fxcrt/fx_system.h"
+#include "third_party/base/ptr_util.h"
+
+namespace {
+
+class FileAccessWrapper : public FPDF_FILEACCESS {
+ public:
+ explicit FileAccessWrapper(FakeFileAccess* simulator)
+ : simulator_(simulator) {
+ m_FileLen = simulator_->GetFileSize();
+ m_GetBlock = &GetBlockImpl;
+ m_Param = this;
+ }
+
+ static int GetBlockImpl(void* param,
+ unsigned long position,
+ unsigned char* pBuf,
+ unsigned long size) {
+ return static_cast<FileAccessWrapper*>(param)->simulator_->GetBlock(
+ position, pBuf, size);
+ }
+
+ private:
+ fxcrt::UnownedPtr<FakeFileAccess> simulator_;
+};
+
+class FileAvailImpl : public FX_FILEAVAIL {
+ public:
+ explicit FileAvailImpl(FakeFileAccess* simulator) : simulator_(simulator) {
+ version = 1;
+ IsDataAvail = &IsDataAvailImpl;
+ }
+
+ static FPDF_BOOL IsDataAvailImpl(FX_FILEAVAIL* pThis,
+ size_t offset,
+ size_t size) {
+ return static_cast<FileAvailImpl*>(pThis)->simulator_->IsDataAvail(offset,
+ size);
+ }
+
+ private:
+ fxcrt::UnownedPtr<FakeFileAccess> simulator_;
+};
+
+class DownloadHintsImpl : public FX_DOWNLOADHINTS {
+ public:
+ explicit DownloadHintsImpl(FakeFileAccess* simulator)
+ : simulator_(simulator) {
+ version = 1;
+ AddSegment = &AddSegmentImpl;
+ }
+
+ static void AddSegmentImpl(FX_DOWNLOADHINTS* pThis,
+ size_t offset,
+ size_t size) {
+ return static_cast<DownloadHintsImpl*>(pThis)->simulator_->AddSegment(
+ offset, size);
+ }
+
+ private:
+ fxcrt::UnownedPtr<FakeFileAccess> simulator_;
+};
+
+} // namespace
+
+FakeFileAccess::FakeFileAccess(FPDF_FILEACCESS* file_access)
+ : file_access_(file_access),
+ file_access_wrapper_(pdfium::MakeUnique<FileAccessWrapper>(this)),
+ file_avail_(pdfium::MakeUnique<FileAvailImpl>(this)),
+ download_hints_(pdfium::MakeUnique<DownloadHintsImpl>(this)) {
+ ASSERT(file_access_);
+}
+
+FakeFileAccess::~FakeFileAccess() {}
+
+FPDF_BOOL FakeFileAccess::IsDataAvail(size_t offset, size_t size) const {
+ return available_data_.Contains(RangeSet::Range(offset, offset + size));
+}
+
+void FakeFileAccess::AddSegment(size_t offset, size_t size) {
+ requested_data_.Union(RangeSet::Range(offset, offset + size));
+}
+
+unsigned long FakeFileAccess::GetFileSize() {
+ return file_access_->m_FileLen;
+}
+
+int FakeFileAccess::GetBlock(unsigned long position,
+ unsigned char* pBuf,
+ unsigned long size) {
+ if (!IsDataAvail(static_cast<size_t>(position), static_cast<size_t>(size)))
+ return false;
+ return file_access_->m_GetBlock(file_access_->m_Param, position, pBuf, size);
+}
+
+void FakeFileAccess::SetRequestedDataAvailable() {
+ available_data_.Union(requested_data_);
+ requested_data_.Clear();
+}
+
+void FakeFileAccess::SetWholeFileAvailable() {
+ available_data_.Union(RangeSet::Range(0, static_cast<size_t>(GetFileSize())));
+ requested_data_.Clear();
+}