diff options
author | Artem Strygin <art-snake@yandex-team.ru> | 2017-09-28 18:46:03 +0300 |
---|---|---|
committer | Chromium commit bot <commit-bot@chromium.org> | 2017-09-28 15:59:08 +0000 |
commit | 0e60b9ef2b79de52ef62101abae2af7292e879b7 (patch) | |
tree | 9f71519f2a364ee6702219a44de7fdb1def18cc7 /fpdfsdk/fpdf_dataavail_embeddertest.cpp | |
parent | 1ca7173cd85adcd58766fc89f95c3dc163efa17a (diff) | |
download | pdfium-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 'fpdfsdk/fpdf_dataavail_embeddertest.cpp')
-rw-r--r-- | fpdfsdk/fpdf_dataavail_embeddertest.cpp | 72 |
1 files changed, 25 insertions, 47 deletions
diff --git a/fpdfsdk/fpdf_dataavail_embeddertest.cpp b/fpdfsdk/fpdf_dataavail_embeddertest.cpp index c40f8579b6..2084153852 100644 --- a/fpdfsdk/fpdf_dataavail_embeddertest.cpp +++ b/fpdfsdk/fpdf_dataavail_embeddertest.cpp @@ -12,10 +12,25 @@ #include "public/fpdfview.h" #include "testing/embedder_test.h" #include "testing/gtest/include/gtest/gtest.h" +#include "testing/range_set.h" #include "testing/test_support.h" #include "testing/utils/path_service.h" namespace { + +class MockDownloadHints : public FX_DOWNLOADHINTS { + public: + static void SAddSegment(FX_DOWNLOADHINTS* pThis, size_t offset, size_t size) { + } + + MockDownloadHints() { + FX_DOWNLOADHINTS::version = 1; + FX_DOWNLOADHINTS::AddSegment = SAddSegment; + } + + ~MockDownloadHints() {} +}; + class TestAsyncLoader : public FX_DOWNLOADHINTS, FX_FILEAVAIL { public: explicit TestAsyncLoader(const std::string& file_name) { @@ -60,7 +75,9 @@ class TestAsyncLoader : public FX_DOWNLOADHINTS, FX_FILEAVAIL { } size_t max_already_available_bound() const { - return available_ranges_.empty() ? 0 : available_ranges_.rbegin()->second; + return available_ranges_.IsEmpty() + ? 0 + : available_ranges_.ranges().rbegin()->second; } void FlushRequestedData() { @@ -72,45 +89,11 @@ class TestAsyncLoader : public FX_DOWNLOADHINTS, FX_FILEAVAIL { private: void SetDataAvailable(size_t start, size_t size) { - if (size == 0) - return; - const auto range = std::make_pair(start, start + size); - if (available_ranges_.empty()) { - available_ranges_.insert(range); - return; - } - auto start_it = available_ranges_.upper_bound(range); - if (start_it != available_ranges_.begin()) - --start_it; // start now points to the key equal or lower than offset. - if (start_it->second < range.first) - ++start_it; // start element is entirely before current range, skip it. - - auto end_it = available_ranges_.upper_bound( - std::make_pair(range.second, range.second)); - if (start_it == end_it) { // No ranges to merge. - available_ranges_.insert(range); - return; - } - - --end_it; - - size_t new_start = std::min<size_t>(start_it->first, range.first); - size_t new_end = std::max(end_it->second, range.second); - - available_ranges_.erase(start_it, ++end_it); - available_ranges_.insert(std::make_pair(new_start, new_end)); + available_ranges_.Union(RangeSet::Range(start, start + size)); } bool CheckDataAlreadyAvailable(size_t start, size_t size) const { - if (size == 0) - return false; - const auto range = std::make_pair(start, start + size); - auto it = available_ranges_.upper_bound(range); - if (it == available_ranges_.begin()) - return false; // No ranges includes range.start(). - - --it; // Now it starts equal or before range.start(). - return it->second >= range.second; + return available_ranges_.Contains(RangeSet::Range(start, start + size)); } int GetBlockImpl(unsigned long pos, unsigned char* pBuf, unsigned long size) { @@ -165,14 +148,7 @@ class TestAsyncLoader : public FX_DOWNLOADHINTS, FX_FILEAVAIL { size_t max_requested_bound_ = 0; bool is_new_data_available_ = true; - using Range = std::pair<size_t, size_t>; - struct range_compare { - bool operator()(const Range& lval, const Range& rval) const { - return lval.first < rval.first; - } - }; - using RangesContainer = std::set<Range, range_compare>; - RangesContainer available_ranges_; + RangeSet available_ranges_; }; } // namespace @@ -182,13 +158,15 @@ class FPDFDataAvailEmbeddertest : public EmbedderTest {}; TEST_F(FPDFDataAvailEmbeddertest, TrailerUnterminated) { // Document must load without crashing but is too malformed to be available. EXPECT_FALSE(OpenDocument("trailer_unterminated.pdf")); - EXPECT_FALSE(FPDFAvail_IsDocAvail(avail_, &hints_)); + MockDownloadHints hints; + EXPECT_FALSE(FPDFAvail_IsDocAvail(avail_, &hints)); } TEST_F(FPDFDataAvailEmbeddertest, TrailerAsHexstring) { // Document must load without crashing but is too malformed to be available. EXPECT_FALSE(OpenDocument("trailer_as_hexstring.pdf")); - EXPECT_FALSE(FPDFAvail_IsDocAvail(avail_, &hints_)); + MockDownloadHints hints; + EXPECT_FALSE(FPDFAvail_IsDocAvail(avail_, &hints)); } TEST_F(FPDFDataAvailEmbeddertest, LoadUsingHintTables) { |