123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325 |
- // Copyright 2021 The Chromium 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 "pdf/pdf_view_web_plugin.h"
- #include <stdint.h>
- #include <functional>
- #include <memory>
- #include <string>
- #include <utility>
- #include <vector>
- #include "base/containers/span.h"
- #include "base/location.h"
- #include "base/memory/raw_ptr.h"
- #include "base/memory/weak_ptr.h"
- #include "base/run_loop.h"
- #include "base/strings/string_piece.h"
- #include "base/test/bind.h"
- #include "base/test/values_test_util.h"
- #include "base/time/time.h"
- #include "base/values.h"
- #include "cc/paint/paint_canvas.h"
- #include "cc/test/pixel_comparator.h"
- #include "cc/test/pixel_test_utils.h"
- #include "mojo/public/cpp/bindings/associated_receiver.h"
- #include "mojo/public/cpp/bindings/associated_remote.h"
- #include "net/cookies/site_for_cookies.h"
- #include "pdf/accessibility_structs.h"
- #include "pdf/buildflags.h"
- #include "pdf/content_restriction.h"
- #include "pdf/document_layout.h"
- #include "pdf/mojom/pdf.mojom.h"
- #include "pdf/paint_ready_rect.h"
- #include "pdf/pdf_accessibility_data_handler.h"
- #include "pdf/pdf_view_plugin_base.h"
- #include "pdf/test/mock_web_associated_url_loader.h"
- #include "pdf/test/test_helpers.h"
- #include "pdf/test/test_pdfium_engine.h"
- #include "printing/metafile_skia.h"
- #include "services/network/public/mojom/referrer_policy.mojom-shared.h"
- #include "testing/gmock/include/gmock/gmock.h"
- #include "testing/gtest/include/gtest/gtest.h"
- #include "third_party/blink/public/common/input/web_coalesced_input_event.h"
- #include "third_party/blink/public/common/input/web_input_event.h"
- #include "third_party/blink/public/common/input/web_keyboard_event.h"
- #include "third_party/blink/public/common/input/web_mouse_event.h"
- #include "third_party/blink/public/common/loader/http_body_element_type.h"
- #include "third_party/blink/public/platform/web_data.h"
- #include "third_party/blink/public/platform/web_http_body.h"
- #include "third_party/blink/public/platform/web_http_header_visitor.h"
- #include "third_party/blink/public/platform/web_input_event_result.h"
- #include "third_party/blink/public/platform/web_string.h"
- #include "third_party/blink/public/platform/web_text_input_type.h"
- #include "third_party/blink/public/platform/web_url.h"
- #include "third_party/blink/public/platform/web_url_request.h"
- #include "third_party/blink/public/platform/web_url_response.h"
- #include "third_party/blink/public/web/web_associated_url_loader.h"
- #include "third_party/blink/public/web/web_associated_url_loader_client.h"
- #include "third_party/blink/public/web/web_plugin_container.h"
- #include "third_party/blink/public/web/web_plugin_params.h"
- #include "third_party/blink/public/web/web_print_params.h"
- #include "third_party/skia/include/core/SkBitmap.h"
- #include "third_party/skia/include/core/SkCanvas.h"
- #include "third_party/skia/include/core/SkColor.h"
- #include "third_party/skia/include/core/SkRefCnt.h"
- #include "third_party/skia/include/core/SkSurface.h"
- #include "ui/base/cursor/cursor.h"
- #include "ui/events/blink/blink_event_util.h"
- #include "ui/events/keycodes/dom/dom_code.h"
- #include "ui/events/keycodes/dom/dom_key.h"
- #include "ui/gfx/canvas.h"
- #include "ui/gfx/geometry/point.h"
- #include "ui/gfx/geometry/point_f.h"
- #include "ui/gfx/geometry/rect.h"
- #include "ui/gfx/geometry/size.h"
- #include "ui/gfx/geometry/skia_conversions.h"
- #include "ui/gfx/geometry/vector2d_f.h"
- #include "ui/gfx/range/range.h"
- #include "ui/latency/latency_info.h"
- #include "url/gurl.h"
- namespace chrome_pdf {
- namespace {
- using ::testing::AnyNumber;
- using ::testing::ElementsAre;
- using ::testing::ElementsAreArray;
- using ::testing::Eq;
- using ::testing::InSequence;
- using ::testing::Invoke;
- using ::testing::IsEmpty;
- using ::testing::IsFalse;
- using ::testing::IsTrue;
- using ::testing::MockFunction;
- using ::testing::NiceMock;
- using ::testing::Pointwise;
- using ::testing::Return;
- using ::testing::SaveArg;
- using ::testing::SizeIs;
- // `kCanvasSize` needs to be big enough to hold plugin's snapshots during
- // testing.
- constexpr gfx::Size kCanvasSize(100, 100);
- // Note: Make sure `kDefaultColor` is different from `kPaintColor` and the
- // plugin's background color. This will help identify bitmap changes after
- // painting.
- constexpr SkColor kDefaultColor = SK_ColorGREEN;
- constexpr SkColor kPaintColor = SK_ColorRED;
- struct PaintParams {
- // The plugin container's device scale.
- float device_scale;
- // The window area in CSS pixels.
- gfx::Rect window_rect;
- // The target painting area on the canvas in CSS pixels.
- gfx::Rect paint_rect;
- // The expected clipped area to be filled with paint color. The clipped area
- // should be the intersection of `paint_rect` and `window_rect`.
- gfx::Rect expected_clipped_rect;
- };
- MATCHER(SearchStringResultEq, "") {
- PDFEngine::Client::SearchStringResult l = std::get<0>(arg);
- PDFEngine::Client::SearchStringResult r = std::get<1>(arg);
- return l.start_index == r.start_index && l.length == r.length;
- }
- MATCHER_P(IsExpectedImeKeyEvent, expected_text, "") {
- if (arg.GetType() != blink::WebInputEvent::Type::kChar)
- return false;
- const auto& event = static_cast<const blink::WebKeyboardEvent&>(arg);
- return event.GetModifiers() == blink::WebInputEvent::kNoModifiers &&
- event.windows_key_code == expected_text[0] &&
- event.native_key_code == expected_text[0] &&
- event.dom_code == static_cast<int>(ui::DomCode::NONE) &&
- event.dom_key == ui::DomKey::NONE && !event.is_system_key &&
- !event.is_browser_shortcut && event.text == expected_text &&
- event.unmodified_text == expected_text;
- }
- base::Value::Dict ParseMessage(base::StringPiece json) {
- return std::move(base::test::ParseJson(json).GetDict());
- }
- // Generates the expected `SkBitmap` with `paint_color` filled in the expected
- // clipped area and `kDefaultColor` as the background color.
- SkBitmap GenerateExpectedBitmapForPaint(const gfx::Rect& expected_clipped_rect,
- SkColor paint_color) {
- sk_sp<SkSurface> expected_surface =
- CreateSkiaSurfaceForTesting(kCanvasSize, kDefaultColor);
- expected_surface->getCanvas()->clipIRect(
- gfx::RectToSkIRect(expected_clipped_rect));
- expected_surface->getCanvas()->clear(paint_color);
- SkBitmap expected_bitmap;
- expected_surface->makeImageSnapshot()->asLegacyBitmap(&expected_bitmap);
- return expected_bitmap;
- }
- class MockHeaderVisitor : public blink::WebHTTPHeaderVisitor {
- public:
- MOCK_METHOD(void,
- VisitHeader,
- (const blink::WebString&, const blink::WebString&),
- (override));
- };
- class MockPdfAccessibilityDataHandler : public PdfAccessibilityDataHandler {
- public:
- // PdfAccessibilityDataHandler:
- MOCK_METHOD(void,
- SetAccessibilityViewportInfo,
- (AccessibilityViewportInfo),
- (override));
- MOCK_METHOD(void,
- SetAccessibilityDocInfo,
- (AccessibilityDocInfo),
- (override));
- MOCK_METHOD(void,
- SetAccessibilityPageInfo,
- (AccessibilityPageInfo,
- std::vector<AccessibilityTextRunInfo>,
- std::vector<AccessibilityCharInfo>,
- AccessibilityPageObjects),
- (override));
- };
- class FakePdfViewWebPluginClient : public PdfViewWebPlugin::Client {
- public:
- FakePdfViewWebPluginClient() {
- ON_CALL(*this, CreateAssociatedURLLoader).WillByDefault([]() {
- auto associated_loader =
- std::make_unique<NiceMock<MockWebAssociatedURLLoader>>();
- ON_CALL(*associated_loader, LoadAsynchronously)
- .WillByDefault([](const blink::WebURLRequest& /*request*/,
- blink::WebAssociatedURLLoaderClient* client) {
- // TODO(crbug.com/1322928): Must trigger callback to free
- // `UrlLoader`.
- client->DidReceiveResponse(blink::WebURLResponse());
- client->DidFinishLoading();
- });
- return associated_loader;
- });
- ON_CALL(*this, GetEmbedderOriginString)
- .WillByDefault(
- Return("chrome-extension://mhjfbmdgcfjbbpaeojofohoefgiehjai/"));
- ON_CALL(*this, HasFrame).WillByDefault(Return(true));
- }
- // PdfViewWebPlugin::Client:
- MOCK_METHOD(std::unique_ptr<base::Value>,
- FromV8Value,
- (v8::Local<v8::Value>, v8::Local<v8::Context>),
- (override));
- MOCK_METHOD(base::WeakPtr<Client>, GetWeakPtr, (), (override));
- MOCK_METHOD(std::unique_ptr<PDFiumEngine>,
- CreateEngine,
- (PDFEngine::Client*, PDFiumFormFiller::ScriptOption),
- (override));
- MOCK_METHOD(void,
- SetPluginContainer,
- (blink::WebPluginContainer*),
- (override));
- MOCK_METHOD(blink::WebPluginContainer*, PluginContainer, (), (override));
- MOCK_METHOD(net::SiteForCookies, SiteForCookies, (), (const override));
- MOCK_METHOD(blink::WebURL,
- CompleteURL,
- (const blink::WebString&),
- (const override));
- MOCK_METHOD(void, PostMessage, (base::Value::Dict), (override));
- MOCK_METHOD(void, Invalidate, (), (override));
- MOCK_METHOD(void,
- RequestTouchEventType,
- (blink::WebPluginContainer::TouchEventRequestType),
- (override));
- MOCK_METHOD(void, ReportFindInPageMatchCount, (int, int, bool), (override));
- MOCK_METHOD(void, ReportFindInPageSelection, (int, int, bool), (override));
- MOCK_METHOD(void,
- ReportFindInPageTickmarks,
- (const std::vector<gfx::Rect>&),
- (override));
- MOCK_METHOD(float, DeviceScaleFactor, (), (override));
- MOCK_METHOD(gfx::PointF, GetScrollPosition, (), (override));
- MOCK_METHOD(void, UsePluginAsFindHandler, (), (override));
- MOCK_METHOD(void,
- SetReferrerForRequest,
- (blink::WebURLRequest&, const blink::WebURL&),
- (override));
- MOCK_METHOD(void, Alert, (const blink::WebString&), (override));
- MOCK_METHOD(bool, Confirm, (const blink::WebString&), (override));
- MOCK_METHOD(blink::WebString,
- Prompt,
- (const blink::WebString&, const blink::WebString&),
- (override));
- MOCK_METHOD(void,
- TextSelectionChanged,
- (const blink::WebString&, uint32_t, const gfx::Range&),
- (override));
- MOCK_METHOD(std::unique_ptr<blink::WebAssociatedURLLoader>,
- CreateAssociatedURLLoader,
- (const blink::WebAssociatedURLLoaderOptions&),
- (override));
- MOCK_METHOD(void, UpdateTextInputState, (), (override));
- MOCK_METHOD(void, UpdateSelectionBounds, (), (override));
- MOCK_METHOD(std::string, GetEmbedderOriginString, (), (override));
- MOCK_METHOD(bool, HasFrame, (), (const override));
- MOCK_METHOD(void, DidStartLoading, (), (override));
- MOCK_METHOD(void, DidStopLoading, (), (override));
- MOCK_METHOD(void, RecordComputedAction, (const std::string&), (override));
- MOCK_METHOD(std::unique_ptr<PdfAccessibilityDataHandler>,
- CreateAccessibilityDataHandler,
- (PdfAccessibilityActionHandler*),
- (override));
- };
- class FakePdfService : public pdf::mojom::PdfService {
- public:
- MOCK_METHOD(void,
- SetListener,
- (mojo::PendingRemote<pdf::mojom::PdfListener>),
- (override));
- MOCK_METHOD(void, UpdateContentRestrictions, (int32_t), (override));
- MOCK_METHOD(void, HasUnsupportedFeature, (), (override));
- MOCK_METHOD(void,
- SaveUrlAs,
- (const GURL&, network::mojom::ReferrerPolicy),
- (override));
- MOCK_METHOD(void,
- SelectionChanged,
- (const gfx::PointF&, int32_t, const gfx::PointF&, int32_t),
- (override));
- MOCK_METHOD(void, SetPluginCanSave, (bool), (override));
- };
- } // namespace
- class PdfViewWebPluginWithoutInitializeTest : public testing::Test {
- protected:
- // Custom deleter for `plugin_`. PdfViewWebPlugin must be destroyed by
- // PdfViewWebPlugin::Destroy() instead of its destructor.
- struct PluginDeleter {
- void operator()(PdfViewWebPlugin* ptr) { ptr->Destroy(); }
- };
- static void AddToPluginParams(base::StringPiece name,
- base::StringPiece value,
- blink::WebPluginParams& params) {
- params.attribute_names.push_back(
- blink::WebString::FromUTF8(name.data(), name.size()));
- params.attribute_values.push_back(
- blink::WebString::FromUTF8(value.data(), value.size()));
- }
- void SetUpPlugin(base::StringPiece document_url,
- const blink::WebPluginParams& params) {
- auto client = std::make_unique<NiceMock<FakePdfViewWebPluginClient>>();
- client_ptr_ = client.get();
- ON_CALL(*client_ptr_, CompleteURL)
- .WillByDefault([parsed_document_url = GURL(document_url)](
- const blink::WebString& partial_url) {
- return parsed_document_url.Resolve(partial_url.Utf8());
- });
- ON_CALL(*client_ptr_, CreateEngine)
- .WillByDefault([this](
- PDFEngine::Client* client,
- PDFiumFormFiller::ScriptOption /*script_option*/) {
- auto engine = std::make_unique<NiceMock<TestPDFiumEngine>>(client);
- engine_ptr_ = engine.get();
- return engine;
- });
- ON_CALL(*client_ptr_, CreateAccessibilityDataHandler)
- .WillByDefault([this]() {
- auto handler =
- std::make_unique<NiceMock<MockPdfAccessibilityDataHandler>>();
- accessibility_data_handler_ptr_ = handler.get();
- return handler;
- });
- SetUpClient();
- plugin_ =
- std::unique_ptr<PdfViewWebPlugin, PluginDeleter>(new PdfViewWebPlugin(
- std::move(client),
- mojo::AssociatedRemote<pdf::mojom::PdfService>(
- pdf_receiver_.BindNewEndpointAndPassDedicatedRemote()),
- params));
- }
- void SetUpPluginWithUrl(const std::string& url) {
- blink::WebPluginParams params;
- AddToPluginParams("src", url, params);
- SetUpPluginParams(params);
- SetUpPlugin(url, params);
- }
- // Allows derived classes to customize plugin parameters within
- // `SetUpPluginWithUrl()`.
- virtual void SetUpPluginParams(blink::WebPluginParams& params) {}
- // Allows derived classes to customize `client_ptr_` within `SetUpPlugin()`.
- virtual void SetUpClient() {}
- void TearDown() override { plugin_.reset(); }
- void ExpectUpdateTextInputState(
- blink::WebTextInputType expected_text_input_type) {
- EXPECT_CALL(*client_ptr_, UpdateTextInputState)
- .WillOnce([this, expected_text_input_type]() {
- EXPECT_EQ(expected_text_input_type,
- plugin_->GetPluginTextInputType());
- });
- }
- NiceMock<FakePdfService> pdf_service_;
- mojo::AssociatedReceiver<pdf::mojom::PdfService> pdf_receiver_{&pdf_service_};
- raw_ptr<FakePdfViewWebPluginClient> client_ptr_;
- std::unique_ptr<PdfViewWebPlugin, PluginDeleter> plugin_;
- raw_ptr<TestPDFiumEngine> engine_ptr_;
- raw_ptr<MockPdfAccessibilityDataHandler> accessibility_data_handler_ptr_;
- };
- class PdfViewWebPluginTest : public PdfViewWebPluginWithoutInitializeTest {
- protected:
- static constexpr char kPdfUrl[] = "http://localhost/example.pdf";
- void SetUp() override {
- SetUpPluginWithUrl(kPdfUrl);
- EXPECT_TRUE(plugin_->InitializeForTesting());
- }
- void SetDocumentDimensions(const gfx::Size& dimensions) {
- EXPECT_CALL(*engine_ptr_, ApplyDocumentLayout)
- .WillRepeatedly(Return(dimensions));
- SendViewportMessage(/*zoom=*/1.0);
- }
- void SendViewportMessage(double zoom) {
- base::Value::Dict message = ParseMessage(R"({
- "type": "viewport",
- "userInitiated": false,
- "zoom": 1,
- "layoutOptions": {
- "direction": 2,
- "defaultPageOrientation": 0,
- "twoUpViewEnabled": false,
- },
- "xOffset": 0,
- "yOffset": 0,
- "pinchPhase": 0,
- })");
- message.Set("zoom", zoom);
- plugin_->OnMessage(message);
- }
- void UpdatePluginGeometry(float device_scale, const gfx::Rect& window_rect) {
- UpdatePluginGeometryWithoutWaiting(device_scale, window_rect);
- // Waits for main thread callback scheduled by `PaintManager`.
- base::RunLoop run_loop;
- base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE,
- run_loop.QuitClosure());
- run_loop.Run();
- }
- void UpdatePluginGeometryWithoutWaiting(float device_scale,
- const gfx::Rect& window_rect) {
- // The plugin container's device scale must be set before calling
- // UpdateGeometry().
- EXPECT_CALL(*client_ptr_, DeviceScaleFactor)
- .WillRepeatedly(Return(device_scale));
- plugin_->UpdateGeometry(window_rect, window_rect, window_rect,
- /*is_visible=*/true);
- }
- void TestUpdateGeometrySetsPluginRect(float device_scale,
- const gfx::Rect& window_rect,
- float expected_device_scale,
- const gfx::Rect& expected_plugin_rect) {
- UpdatePluginGeometryWithoutWaiting(device_scale, window_rect);
- EXPECT_EQ(expected_device_scale, plugin_->GetDeviceScaleForTesting())
- << "Device scale comparison failure at device scale of "
- << device_scale;
- EXPECT_EQ(expected_plugin_rect, plugin_->GetPluginRectForTesting())
- << "Plugin rect comparison failure at device scale of " << device_scale
- << ", window rect of " << window_rect.ToString();
- }
- void TestPaintEmptySnapshots(float device_scale,
- const gfx::Rect& window_rect,
- const gfx::Rect& paint_rect,
- const gfx::Rect& expected_clipped_rect) {
- UpdatePluginGeometryWithoutWaiting(device_scale, window_rect);
- canvas_.DrawColor(kDefaultColor);
- plugin_->Paint(canvas_.sk_canvas(), paint_rect);
- // Expect the clipped area on canvas to be filled with plugin's background
- // color.
- SkBitmap expected_bitmap = GenerateExpectedBitmapForPaint(
- expected_clipped_rect, plugin_->GetBackgroundColor());
- EXPECT_TRUE(
- cc::MatchesBitmap(canvas_.GetBitmap(), expected_bitmap,
- cc::ExactPixelComparator(/*discard_alpha=*/false)))
- << "Failure at device scale of " << device_scale << ", window rect of "
- << window_rect.ToString();
- }
- void TestPaintSnapshots(float device_scale,
- const gfx::Rect& window_rect,
- const gfx::Rect& paint_rect,
- const gfx::Rect& expected_clipped_rect) {
- UpdatePluginGeometry(device_scale, window_rect);
- canvas_.DrawColor(kDefaultColor);
- // Paint the plugin with `kPaintColor`.
- plugin_->UpdateSnapshot(CreateSkiaImageForTesting(
- plugin_->GetPluginRectForTesting().size(), kPaintColor));
- plugin_->Paint(canvas_.sk_canvas(), paint_rect);
- // Expect the clipped area on canvas to be filled with `kPaintColor`.
- SkBitmap expected_bitmap =
- GenerateExpectedBitmapForPaint(expected_clipped_rect, kPaintColor);
- EXPECT_TRUE(
- cc::MatchesBitmap(canvas_.GetBitmap(), expected_bitmap,
- cc::ExactPixelComparator(/*discard_alpha=*/false)))
- << "Failure at device scale of " << device_scale << ", window rect of "
- << window_rect.ToString();
- }
- // Provides the cc::PaintCanvas for painting.
- gfx::Canvas canvas_{kCanvasSize, /*image_scale=*/1.0f, /*is_opaque=*/true};
- };
- class PdfViewWebPluginFullFrameTest : public PdfViewWebPluginTest {
- protected:
- void SetUpPluginParams(blink::WebPluginParams& params) override {
- AddToPluginParams("full-frame", "full-frame", params);
- }
- };
- TEST_F(PdfViewWebPluginWithoutInitializeTest, Initialize) {
- SetUpPluginWithUrl("http://localhost/example.pdf");
- EXPECT_CALL(*client_ptr_, CreateAssociatedURLLoader)
- .WillOnce([](const blink::WebAssociatedURLLoaderOptions& options) {
- EXPECT_TRUE(options.grant_universal_access);
- auto associated_loader =
- std::make_unique<NiceMock<MockWebAssociatedURLLoader>>();
- EXPECT_CALL(*associated_loader, LoadAsynchronously)
- .WillOnce([](const blink::WebURLRequest& request,
- blink::WebAssociatedURLLoaderClient* client) {
- EXPECT_EQ("http://localhost/example.pdf",
- request.Url().GetString().Utf8());
- EXPECT_EQ("GET", request.HttpMethod().Utf8());
- EXPECT_TRUE(request.HttpBody().IsNull());
- NiceMock<MockHeaderVisitor> header_visitor;
- EXPECT_CALL(header_visitor, VisitHeader).Times(0);
- request.VisitHttpHeaderFields(&header_visitor);
- EXPECT_FALSE(client->WillFollowRedirect(blink::WebURL(),
- blink::WebURLResponse()));
- client->DidReceiveResponse(blink::WebURLResponse());
- client->DidFinishLoading();
- });
- return associated_loader;
- });
- EXPECT_CALL(*client_ptr_, SetReferrerForRequest).Times(0);
- EXPECT_TRUE(plugin_->InitializeForTesting());
- }
- TEST_F(PdfViewWebPluginWithoutInitializeTest, InitializeWithEmptyUrl) {
- SetUpPluginWithUrl("");
- EXPECT_CALL(*client_ptr_, CreateAssociatedURLLoader).Times(0);
- EXPECT_FALSE(plugin_->InitializeForTesting());
- }
- TEST_F(PdfViewWebPluginWithoutInitializeTest, InitializeForPrintPreview) {
- SetUpPluginWithUrl("about:blank");
- EXPECT_CALL(*client_ptr_, GetEmbedderOriginString)
- .WillRepeatedly(Return("chrome://print/"));
- EXPECT_CALL(*client_ptr_, CreateAssociatedURLLoader).Times(0);
- EXPECT_TRUE(plugin_->InitializeForTesting());
- }
- TEST_F(PdfViewWebPluginTest, CreateUrlLoader) {
- EXPECT_CALL(*client_ptr_, DidStartLoading).Times(0);
- EXPECT_CALL(pdf_service_, UpdateContentRestrictions).Times(0);
- plugin_->CreateUrlLoader();
- EXPECT_EQ(PdfViewPluginBase::DocumentLoadState::kLoading,
- plugin_->document_load_state_for_testing());
- pdf_receiver_.FlushForTesting();
- }
- TEST_F(PdfViewWebPluginFullFrameTest, CreateUrlLoader) {
- EXPECT_CALL(*client_ptr_, DidStartLoading);
- EXPECT_CALL(pdf_service_,
- UpdateContentRestrictions(kContentRestrictionSave |
- kContentRestrictionPrint));
- plugin_->CreateUrlLoader();
- EXPECT_EQ(PdfViewPluginBase::DocumentLoadState::kLoading,
- plugin_->document_load_state_for_testing());
- pdf_receiver_.FlushForTesting();
- }
- TEST_F(PdfViewWebPluginFullFrameTest, CreateUrlLoaderMultipleTimes) {
- plugin_->CreateUrlLoader();
- EXPECT_CALL(*client_ptr_, DidStartLoading).Times(0);
- plugin_->CreateUrlLoader();
- }
- TEST_F(PdfViewWebPluginFullFrameTest, CreateUrlLoaderAfterDocumentLoadFailed) {
- plugin_->CreateUrlLoader();
- plugin_->DocumentLoadFailed();
- EXPECT_CALL(*client_ptr_, DidStartLoading);
- plugin_->CreateUrlLoader();
- }
- TEST_F(PdfViewWebPluginTest, DocumentLoadComplete) {
- plugin_->CreateUrlLoader();
- EXPECT_CALL(*client_ptr_, RecordComputedAction("PDF.LoadSuccess"));
- EXPECT_CALL(*client_ptr_, PostMessage);
- EXPECT_CALL(*client_ptr_, PostMessage(base::test::IsJson(R"({
- "type": "formFocusChange",
- "focused": false,
- })")));
- ExpectUpdateTextInputState(blink::WebTextInputType::kWebTextInputTypeNone);
- EXPECT_CALL(*client_ptr_, PostMessage(base::test::IsJson(R"({
- "type": "printPreviewLoaded",
- })")))
- .Times(0);
- EXPECT_CALL(*accessibility_data_handler_ptr_, SetAccessibilityDocInfo)
- .Times(0);
- EXPECT_CALL(*client_ptr_, DidStopLoading).Times(0);
- EXPECT_CALL(pdf_service_, UpdateContentRestrictions).Times(0);
- plugin_->DocumentLoadComplete();
- EXPECT_EQ(PdfViewPluginBase::DocumentLoadState::kComplete,
- plugin_->document_load_state_for_testing());
- pdf_receiver_.FlushForTesting();
- }
- TEST_F(PdfViewWebPluginFullFrameTest, DocumentLoadComplete) {
- // Must flush IPCs after `CreateUrlLoader()` in full-frame mode, otherwise
- // there's an unexpected `UpdateContentRestrictions()` call (see the
- // `PdfViewWebPluginFullFrameTest.CreateUrlLoader` test).
- plugin_->CreateUrlLoader();
- pdf_receiver_.FlushForTesting();
- EXPECT_CALL(*client_ptr_, RecordComputedAction("PDF.LoadSuccess"));
- EXPECT_CALL(*client_ptr_, PostMessage);
- EXPECT_CALL(*client_ptr_, PostMessage(base::test::IsJson(R"({
- "type": "formFocusChange",
- "focused": false,
- })")));
- ExpectUpdateTextInputState(blink::WebTextInputType::kWebTextInputTypeNone);
- EXPECT_CALL(*client_ptr_, PostMessage(base::test::IsJson(R"({
- "type": "printPreviewLoaded",
- })")))
- .Times(0);
- EXPECT_CALL(*accessibility_data_handler_ptr_, SetAccessibilityDocInfo)
- .Times(0);
- EXPECT_CALL(*client_ptr_, DidStopLoading);
- EXPECT_CALL(pdf_service_, UpdateContentRestrictions(kContentRestrictionPrint |
- kContentRestrictionPaste |
- kContentRestrictionCut |
- kContentRestrictionCopy));
- plugin_->DocumentLoadComplete();
- EXPECT_EQ(PdfViewPluginBase::DocumentLoadState::kComplete,
- plugin_->document_load_state_for_testing());
- pdf_receiver_.FlushForTesting();
- }
- TEST_F(PdfViewWebPluginTest, DocumentLoadFailed) {
- plugin_->CreateUrlLoader();
- EXPECT_CALL(*client_ptr_, RecordComputedAction("PDF.LoadFailure"));
- EXPECT_CALL(*client_ptr_, DidStopLoading).Times(0);
- plugin_->DocumentLoadFailed();
- EXPECT_EQ(PdfViewPluginBase::DocumentLoadState::kFailed,
- plugin_->document_load_state_for_testing());
- }
- TEST_F(PdfViewWebPluginFullFrameTest, DocumentLoadFailed) {
- plugin_->CreateUrlLoader();
- EXPECT_CALL(*client_ptr_, RecordComputedAction("PDF.LoadFailure"));
- EXPECT_CALL(*client_ptr_, DidStopLoading);
- plugin_->DocumentLoadFailed();
- EXPECT_EQ(PdfViewPluginBase::DocumentLoadState::kFailed,
- plugin_->document_load_state_for_testing());
- }
- TEST_F(PdfViewWebPluginTest, DocumentHasUnsupportedFeature) {
- EXPECT_CALL(*client_ptr_, RecordComputedAction).Times(AnyNumber());
- EXPECT_CALL(*client_ptr_, RecordComputedAction("PDF_Unsupported_feature1"));
- EXPECT_CALL(*client_ptr_, RecordComputedAction("PDF_Unsupported_feature2"));
- // `HasUnsupportedFeature()` is not called if the viewer is not full-frame.
- EXPECT_CALL(pdf_service_, HasUnsupportedFeature).Times(0);
- plugin_->DocumentHasUnsupportedFeature("feature1");
- plugin_->DocumentHasUnsupportedFeature("feature2");
- pdf_receiver_.FlushForTesting();
- }
- TEST_F(PdfViewWebPluginTest, DocumentHasUnsupportedFeatureWithRepeatedFeature) {
- // Metrics should only be recorded once per feature.
- EXPECT_CALL(*client_ptr_, RecordComputedAction).Times(AnyNumber());
- EXPECT_CALL(*client_ptr_, RecordComputedAction("PDF_Unsupported_feature"));
- // `HasUnsupportedFeature()` is not called if the viewer is not full-frame.
- EXPECT_CALL(pdf_service_, HasUnsupportedFeature).Times(0);
- plugin_->DocumentHasUnsupportedFeature("feature");
- plugin_->DocumentHasUnsupportedFeature("feature");
- pdf_receiver_.FlushForTesting();
- }
- TEST_F(PdfViewWebPluginFullFrameTest, DocumentHasUnsupportedFeature) {
- EXPECT_CALL(*client_ptr_, RecordComputedAction).Times(AnyNumber());
- EXPECT_CALL(*client_ptr_, RecordComputedAction("PDF_Unsupported_feature1"));
- EXPECT_CALL(*client_ptr_, RecordComputedAction("PDF_Unsupported_feature2"));
- // `HasUnsupportedFeature()` is called once for all features.
- EXPECT_CALL(pdf_service_, HasUnsupportedFeature);
- plugin_->DocumentHasUnsupportedFeature("feature1");
- plugin_->DocumentHasUnsupportedFeature("feature2");
- pdf_receiver_.FlushForTesting();
- }
- TEST_F(PdfViewWebPluginFullFrameTest,
- DocumentHasUnsupportedFeatureWithRepeatedFeature) {
- // Metrics should only be recorded once per feature.
- EXPECT_CALL(*client_ptr_, RecordComputedAction).Times(AnyNumber());
- EXPECT_CALL(*client_ptr_, RecordComputedAction("PDF_Unsupported_feature"));
- // `HasUnsupportedFeature()` is called once for all features.
- EXPECT_CALL(pdf_service_, HasUnsupportedFeature);
- plugin_->DocumentHasUnsupportedFeature("feature");
- plugin_->DocumentHasUnsupportedFeature("feature");
- pdf_receiver_.FlushForTesting();
- }
- TEST_F(PdfViewWebPluginTest, DocumentLoadProgress) {
- EXPECT_CALL(*client_ptr_, PostMessage(base::test::IsJson(R"({
- "type": "loadProgress",
- "progress": 5.0,
- })")));
- plugin_->DocumentLoadProgress(10, 200);
- }
- TEST_F(PdfViewWebPluginTest, DocumentLoadProgressIgnoreSmall) {
- plugin_->DocumentLoadProgress(2, 100);
- EXPECT_CALL(*client_ptr_, PostMessage).Times(0);
- plugin_->DocumentLoadProgress(3, 100);
- }
- TEST_F(PdfViewWebPluginTest, DocumentLoadProgressMultipleSmall) {
- plugin_->DocumentLoadProgress(2, 100);
- EXPECT_CALL(*client_ptr_, PostMessage(base::test::IsJson(R"({
- "type": "loadProgress",
- "progress": 4.0,
- })")));
- plugin_->DocumentLoadProgress(3, 100);
- plugin_->DocumentLoadProgress(4, 100);
- }
- TEST_F(PdfViewWebPluginTest, EnableAccessibilityBeforeDocumentLoadComplete) {
- EXPECT_CALL(*accessibility_data_handler_ptr_, SetAccessibilityDocInfo)
- .Times(0);
- plugin_->EnableAccessibility();
- EXPECT_CALL(*accessibility_data_handler_ptr_, SetAccessibilityDocInfo);
- plugin_->CreateUrlLoader();
- plugin_->DocumentLoadComplete();
- }
- TEST_F(PdfViewWebPluginTest,
- EnableAccessibilityBeforeDocumentLoadCompleteRepeated) {
- EXPECT_CALL(*accessibility_data_handler_ptr_, SetAccessibilityDocInfo)
- .Times(0);
- plugin_->EnableAccessibility();
- plugin_->EnableAccessibility();
- EXPECT_CALL(*accessibility_data_handler_ptr_, SetAccessibilityDocInfo);
- plugin_->CreateUrlLoader();
- plugin_->DocumentLoadComplete();
- }
- TEST_F(PdfViewWebPluginTest, EnableAccessibilityAfterDocumentLoadComplete) {
- EXPECT_CALL(*accessibility_data_handler_ptr_, SetAccessibilityDocInfo)
- .Times(0);
- plugin_->CreateUrlLoader();
- plugin_->DocumentLoadComplete();
- EXPECT_CALL(*accessibility_data_handler_ptr_, SetAccessibilityDocInfo);
- plugin_->EnableAccessibility();
- }
- TEST_F(PdfViewWebPluginTest,
- EnableAccessibilityAfterDocumentLoadCompleteRepeated) {
- plugin_->CreateUrlLoader();
- plugin_->DocumentLoadComplete();
- plugin_->EnableAccessibility();
- EXPECT_CALL(*accessibility_data_handler_ptr_, SetAccessibilityDocInfo)
- .Times(0);
- plugin_->EnableAccessibility();
- }
- TEST_F(PdfViewWebPluginTest, GetContentRestrictionsWithNoPermissions) {
- EXPECT_EQ(kContentRestrictionCopy | kContentRestrictionCut |
- kContentRestrictionPaste | kContentRestrictionPrint,
- plugin_->GetContentRestrictions());
- EXPECT_FALSE(plugin_->CanCopy());
- }
- TEST_F(PdfViewWebPluginTest, GetContentRestrictionsWithCopyAllowed) {
- EXPECT_CALL(*engine_ptr_, HasPermission).WillRepeatedly(Return(false));
- EXPECT_CALL(*engine_ptr_, HasPermission(DocumentPermission::kCopy))
- .WillRepeatedly(Return(true));
- EXPECT_EQ(kContentRestrictionCut | kContentRestrictionPaste |
- kContentRestrictionPrint,
- plugin_->GetContentRestrictions());
- EXPECT_TRUE(plugin_->CanCopy());
- }
- TEST_F(PdfViewWebPluginTest, GetContentRestrictionsWithPrintLowQualityAllowed) {
- EXPECT_CALL(*engine_ptr_, HasPermission).WillRepeatedly(Return(false));
- EXPECT_CALL(*engine_ptr_, HasPermission(DocumentPermission::kPrintLowQuality))
- .WillRepeatedly(Return(true));
- EXPECT_EQ(kContentRestrictionCopy | kContentRestrictionCut |
- kContentRestrictionPaste,
- plugin_->GetContentRestrictions());
- }
- TEST_F(PdfViewWebPluginTest,
- GetContentRestrictionsWithCopyAndPrintLowQualityAllowed) {
- EXPECT_CALL(*engine_ptr_, HasPermission).WillRepeatedly(Return(false));
- EXPECT_CALL(*engine_ptr_, HasPermission(DocumentPermission::kCopy))
- .WillRepeatedly(Return(true));
- EXPECT_CALL(*engine_ptr_, HasPermission(DocumentPermission::kPrintLowQuality))
- .WillRepeatedly(Return(true));
- EXPECT_EQ(kContentRestrictionCut | kContentRestrictionPaste,
- plugin_->GetContentRestrictions());
- }
- TEST_F(PdfViewWebPluginTest, GetContentRestrictionsWithPrintAllowed) {
- EXPECT_CALL(*engine_ptr_, HasPermission).WillRepeatedly(Return(false));
- EXPECT_CALL(*engine_ptr_,
- HasPermission(DocumentPermission::kPrintHighQuality))
- .WillRepeatedly(Return(true));
- EXPECT_CALL(*engine_ptr_, HasPermission(DocumentPermission::kPrintLowQuality))
- .WillRepeatedly(Return(true));
- EXPECT_EQ(kContentRestrictionCopy | kContentRestrictionCut |
- kContentRestrictionPaste,
- plugin_->GetContentRestrictions());
- }
- TEST_F(PdfViewWebPluginTest, GetContentRestrictionsWithCopyAndPrintAllowed) {
- EXPECT_CALL(*engine_ptr_, HasPermission).WillRepeatedly(Return(false));
- EXPECT_CALL(*engine_ptr_, HasPermission(DocumentPermission::kCopy))
- .WillRepeatedly(Return(true));
- EXPECT_CALL(*engine_ptr_,
- HasPermission(DocumentPermission::kPrintHighQuality))
- .WillRepeatedly(Return(true));
- EXPECT_CALL(*engine_ptr_, HasPermission(DocumentPermission::kPrintLowQuality))
- .WillRepeatedly(Return(true));
- EXPECT_EQ(kContentRestrictionCut | kContentRestrictionPaste,
- plugin_->GetContentRestrictions());
- }
- TEST_F(PdfViewWebPluginTest, GetAccessibilityDocInfoWithNoPermissions) {
- AccessibilityDocInfo doc_info = plugin_->GetAccessibilityDocInfo();
- EXPECT_EQ(TestPDFiumEngine::kPageNumber, doc_info.page_count);
- EXPECT_FALSE(doc_info.text_accessible);
- EXPECT_FALSE(doc_info.text_copyable);
- }
- TEST_F(PdfViewWebPluginTest, GetAccessibilityDocInfoWithCopyAllowed) {
- EXPECT_CALL(*engine_ptr_, HasPermission).WillRepeatedly(Return(false));
- EXPECT_CALL(*engine_ptr_, HasPermission(DocumentPermission::kCopy))
- .WillRepeatedly(Return(true));
- AccessibilityDocInfo doc_info = plugin_->GetAccessibilityDocInfo();
- EXPECT_EQ(TestPDFiumEngine::kPageNumber, doc_info.page_count);
- EXPECT_FALSE(doc_info.text_accessible);
- EXPECT_TRUE(doc_info.text_copyable);
- }
- TEST_F(PdfViewWebPluginTest, GetAccessibilityDocInfoWithCopyAccessibleAllowed) {
- EXPECT_CALL(*engine_ptr_, HasPermission).WillRepeatedly(Return(false));
- EXPECT_CALL(*engine_ptr_, HasPermission(DocumentPermission::kCopyAccessible))
- .WillRepeatedly(Return(true));
- AccessibilityDocInfo doc_info = plugin_->GetAccessibilityDocInfo();
- EXPECT_EQ(TestPDFiumEngine::kPageNumber, doc_info.page_count);
- EXPECT_TRUE(doc_info.text_accessible);
- EXPECT_FALSE(doc_info.text_copyable);
- }
- TEST_F(PdfViewWebPluginTest,
- GetAccessibilityDocInfoWithCopyAndCopyAccessibleAllowed) {
- EXPECT_CALL(*engine_ptr_, HasPermission).WillRepeatedly(Return(false));
- EXPECT_CALL(*engine_ptr_, HasPermission(DocumentPermission::kCopy))
- .WillRepeatedly(Return(true));
- EXPECT_CALL(*engine_ptr_, HasPermission(DocumentPermission::kCopyAccessible))
- .WillRepeatedly(Return(true));
- AccessibilityDocInfo doc_info = plugin_->GetAccessibilityDocInfo();
- EXPECT_EQ(TestPDFiumEngine::kPageNumber, doc_info.page_count);
- EXPECT_TRUE(doc_info.text_accessible);
- EXPECT_TRUE(doc_info.text_copyable);
- }
- TEST_F(PdfViewWebPluginTest, UpdateGeometrySetsPluginRect) {
- EXPECT_CALL(*engine_ptr_, ZoomUpdated(2.0f));
- TestUpdateGeometrySetsPluginRect(
- /*device_scale=*/2.0f, /*window_rect=*/gfx::Rect(4, 4, 12, 12),
- /*expected_device_scale=*/2.0f,
- /*expected_plugin_rect=*/gfx::Rect(4, 4, 12, 12));
- }
- TEST_F(PdfViewWebPluginTest,
- UpdateGeometrySetsPluginRectOnVariousDeviceScales) {
- struct UpdateGeometryParams {
- // The plugin container's device scale.
- float device_scale;
- // The window rect in CSS pixels.
- gfx::Rect window_rect;
- // The expected plugin device scale.
- float expected_device_scale;
- // The expected plugin rect in device pixels.
- gfx::Rect expected_plugin_rect;
- };
- static constexpr UpdateGeometryParams kUpdateGeometryParams[] = {
- {1.0f, gfx::Rect(3, 4, 5, 6), 1.0f, gfx::Rect(3, 4, 5, 6)},
- {2.0f, gfx::Rect(3, 4, 5, 6), 2.0f, gfx::Rect(3, 4, 5, 6)},
- };
- for (const auto& params : kUpdateGeometryParams) {
- TestUpdateGeometrySetsPluginRect(params.device_scale, params.window_rect,
- params.expected_device_scale,
- params.expected_plugin_rect);
- }
- }
- TEST_F(PdfViewWebPluginTest, UpdateGeometrySetsPluginRectWithEmptyWindow) {
- EXPECT_CALL(*engine_ptr_, ZoomUpdated).Times(0);
- TestUpdateGeometrySetsPluginRect(
- /*device_scale=*/2.0f, /*window_rect=*/gfx::Rect(2, 2, 0, 0),
- /*expected_device_scale=*/1.0f, /*expected_plugin_rect=*/gfx::Rect());
- }
- TEST_F(PdfViewWebPluginTest, UpdateGeometryScroll) {
- SetDocumentDimensions({100, 200});
- EXPECT_CALL(*client_ptr_, GetScrollPosition)
- .WillRepeatedly(Return(gfx::PointF(4.0f, 6.0f)));
- EXPECT_CALL(*engine_ptr_, ScrolledToXPosition(4));
- EXPECT_CALL(*engine_ptr_, ScrolledToYPosition(6));
- UpdatePluginGeometryWithoutWaiting(1.0f, gfx::Rect(3, 4, 5, 6));
- }
- TEST_F(PdfViewWebPluginTest, UpdateGeometryScrollStopped) {
- SetDocumentDimensions({100, 200});
- plugin_->OnMessage(ParseMessage(R"({
- "type": "stopScrolling",
- })"));
- EXPECT_CALL(*client_ptr_, GetScrollPosition)
- .WillRepeatedly(Return(gfx::PointF(4.0f, 6.0f)));
- EXPECT_CALL(*engine_ptr_, ScrolledToXPosition).Times(0);
- EXPECT_CALL(*engine_ptr_, ScrolledToYPosition).Times(0);
- UpdatePluginGeometryWithoutWaiting(1.0f, gfx::Rect(3, 4, 5, 6));
- }
- TEST_F(PdfViewWebPluginTest, UpdateGeometryScrollUnderflow) {
- SetDocumentDimensions({100, 200});
- EXPECT_CALL(*client_ptr_, GetScrollPosition)
- .WillRepeatedly(Return(gfx::PointF(-1.0f, -1.0f)));
- EXPECT_CALL(*engine_ptr_, ScrolledToXPosition(0));
- EXPECT_CALL(*engine_ptr_, ScrolledToYPosition(0));
- UpdatePluginGeometryWithoutWaiting(1.0f, gfx::Rect(3, 4, 5, 6));
- }
- TEST_F(PdfViewWebPluginTest, UpdateGeometryScrollOverflow) {
- SetDocumentDimensions({100, 200});
- EXPECT_CALL(*client_ptr_, GetScrollPosition)
- .WillRepeatedly(Return(gfx::PointF(96.0f, 195.0f)));
- EXPECT_CALL(*engine_ptr_, ScrolledToXPosition(95));
- EXPECT_CALL(*engine_ptr_, ScrolledToYPosition(194));
- UpdatePluginGeometryWithoutWaiting(1.0f, gfx::Rect(3, 4, 5, 6));
- }
- TEST_F(PdfViewWebPluginTest, UpdateGeometryScrollOverflowZoomed) {
- SetDocumentDimensions({100, 200});
- SendViewportMessage(/*zoom=*/2.0);
- EXPECT_CALL(*client_ptr_, GetScrollPosition)
- .WillRepeatedly(Return(gfx::PointF(196.0f, 395.0f)));
- EXPECT_CALL(*engine_ptr_, ScrolledToXPosition(195));
- EXPECT_CALL(*engine_ptr_, ScrolledToYPosition(394));
- UpdatePluginGeometryWithoutWaiting(1.0f, gfx::Rect(3, 4, 5, 6));
- }
- TEST_F(PdfViewWebPluginTest, UpdateGeometryScrollScaled) {
- SetDocumentDimensions({100, 200});
- EXPECT_CALL(*client_ptr_, GetScrollPosition)
- .WillRepeatedly(Return(gfx::PointF(4.0f, 6.0f)));
- EXPECT_CALL(*engine_ptr_, ScrolledToXPosition(4));
- EXPECT_CALL(*engine_ptr_, ScrolledToYPosition(6));
- UpdatePluginGeometryWithoutWaiting(2.0f, gfx::Rect(3, 4, 5, 6));
- }
- TEST_F(PdfViewWebPluginTest, UpdateGeometryScrollOverflowScaled) {
- SetDocumentDimensions({100, 200});
- EXPECT_CALL(*client_ptr_, GetScrollPosition)
- .WillRepeatedly(Return(gfx::PointF(195.0f, 395.0f)));
- EXPECT_CALL(*engine_ptr_, ScrolledToXPosition(194));
- EXPECT_CALL(*engine_ptr_, ScrolledToYPosition(394));
- UpdatePluginGeometryWithoutWaiting(2.0f, gfx::Rect(3, 4, 5, 6));
- }
- TEST_F(PdfViewWebPluginTest, SetCaretPosition) {
- SetDocumentDimensions({16, 9});
- UpdatePluginGeometryWithoutWaiting(1.0f, {10, 20, 20, 5});
- EXPECT_CALL(*engine_ptr_, SetCaretPosition(gfx::Point(2, 3)));
- plugin_->SetCaretPosition({4.0f, 3.0f});
- }
- TEST_F(PdfViewWebPluginTest, SetCaretPositionNegativeOrigin) {
- SetDocumentDimensions({16, 9});
- UpdatePluginGeometryWithoutWaiting(1.0f, {-10, -20, 20, 5});
- EXPECT_CALL(*engine_ptr_, SetCaretPosition(gfx::Point(2, 3)));
- plugin_->SetCaretPosition({4.0f, 3.0f});
- }
- TEST_F(PdfViewWebPluginTest, SetCaretPositionFractional) {
- SetDocumentDimensions({16, 9});
- UpdatePluginGeometryWithoutWaiting(1.0f, {10, 20, 20, 5});
- EXPECT_CALL(*engine_ptr_, SetCaretPosition(gfx::Point(1, 2)));
- plugin_->SetCaretPosition({3.9f, 2.9f});
- }
- TEST_F(PdfViewWebPluginTest, SetCaretPositionScaled) {
- SetDocumentDimensions({16, 9});
- UpdatePluginGeometryWithoutWaiting(2.0f, {20, 40, 40, 10});
- EXPECT_CALL(*engine_ptr_, SetCaretPosition(gfx::Point(4, 6)));
- plugin_->SetCaretPosition({4.0f, 3.0f});
- }
- TEST_F(PdfViewWebPluginTest, PaintEmptySnapshots) {
- TestPaintEmptySnapshots(/*device_scale=*/4.0f,
- /*window_rect=*/gfx::Rect(10, 10, 20, 20),
- /*paint_rect=*/gfx::Rect(5, 5, 15, 15),
- /*expected_clipped_rect=*/gfx::Rect(10, 10, 10, 10));
- }
- TEST_F(PdfViewWebPluginTest, PaintSnapshots) {
- TestPaintSnapshots(/*device_scale=*/4.0f,
- /*window_rect=*/gfx::Rect(10, 10, 20, 20),
- /*paint_rect=*/gfx::Rect(5, 5, 15, 15),
- /*expected_clipped_rect=*/gfx::Rect(10, 10, 10, 10));
- }
- TEST_F(PdfViewWebPluginTest, PaintSnapshotsWithVariousDeviceScales) {
- static constexpr PaintParams kPaintWithVariousScalesParams[] = {
- {0.4f, gfx::Rect(8, 8, 30, 30), gfx::Rect(10, 10, 30, 30),
- gfx::Rect(10, 10, 28, 28)},
- {1.0f, gfx::Rect(8, 8, 30, 30), gfx::Rect(10, 10, 30, 30),
- gfx::Rect(10, 10, 28, 28)},
- {4.0f, gfx::Rect(8, 8, 30, 30), gfx::Rect(10, 10, 30, 30),
- gfx::Rect(10, 10, 28, 28)},
- };
- for (const auto& params : kPaintWithVariousScalesParams) {
- TestPaintSnapshots(params.device_scale, params.window_rect,
- params.paint_rect, params.expected_clipped_rect);
- }
- }
- TEST_F(PdfViewWebPluginTest, PaintSnapshotsWithVariousRectPositions) {
- static constexpr PaintParams kPaintWithVariousPositionsParams[] = {
- // The window origin falls outside the `paint_rect` area.
- {4.0f, gfx::Rect(10, 10, 20, 20), gfx::Rect(5, 5, 15, 15),
- gfx::Rect(10, 10, 10, 10)},
- // The window origin falls within the `paint_rect` area.
- {4.0f, gfx::Rect(4, 4, 20, 20), gfx::Rect(8, 8, 15, 15),
- gfx::Rect(8, 8, 15, 15)},
- };
- for (const auto& params : kPaintWithVariousPositionsParams) {
- TestPaintSnapshots(params.device_scale, params.window_rect,
- params.paint_rect, params.expected_clipped_rect);
- }
- }
- TEST_F(PdfViewWebPluginTest, OnPaintWithMultiplePaintRects) {
- SetDocumentDimensions({100, 200});
- UpdatePluginGeometryWithoutWaiting(/*device_scale=*/1.0f,
- gfx::Rect(0, 0, 40, 40));
- EXPECT_CALL(*engine_ptr_, Paint)
- .WillRepeatedly(
- [](const gfx::Rect& rect, SkBitmap& /*image_data*/,
- std::vector<gfx::Rect>& ready,
- std::vector<gfx::Rect>& /*pending*/) { ready.push_back(rect); });
- std::vector<PaintReadyRect> ready;
- std::vector<gfx::Rect> pending;
- plugin_->OnPaint(
- /*paint_rects=*/{gfx::Rect(5, 5, 10, 10), gfx::Rect(20, 20, 10, 10)},
- ready, pending);
- // Expect three paints: an initial background-clearing paint, and one for each
- // requested paint rectangle.
- ASSERT_THAT(ready, SizeIs(3));
- EXPECT_THAT(pending, IsEmpty());
- EXPECT_EQ(gfx::Rect(0, 0, 90, 90), ready[0].rect());
- EXPECT_TRUE(ready[0].flush_now());
- EXPECT_EQ(gfx::Rect(5, 5, 10, 10), ready[1].rect());
- EXPECT_FALSE(ready[1].flush_now());
- EXPECT_EQ(gfx::Rect(20, 20, 10, 10), ready[2].rect());
- EXPECT_FALSE(ready[2].flush_now());
- // All the requested paints should share the same `SkImage`.
- EXPECT_NE(&ready[0].image(), &ready[1].image());
- EXPECT_EQ(&ready[1].image(), &ready[2].image());
- }
- TEST_F(PdfViewWebPluginTest, UpdateLayerTransformWithIdentity) {
- plugin_->UpdateLayerTransform(1.0f, gfx::Vector2dF());
- TestPaintSnapshots(/*device_scale=*/4.0f,
- /*window_rect=*/gfx::Rect(10, 10, 20, 20),
- /*paint_rect=*/gfx::Rect(10, 10, 20, 20),
- /*expected_clipped_rect=*/gfx::Rect(10, 10, 20, 20));
- }
- TEST_F(PdfViewWebPluginTest, UpdateLayerTransformWithScale) {
- plugin_->UpdateLayerTransform(0.5f, gfx::Vector2dF());
- TestPaintSnapshots(/*device_scale=*/4.0f,
- /*window_rect=*/gfx::Rect(10, 10, 20, 20),
- /*paint_rect=*/gfx::Rect(10, 10, 20, 20),
- /*expected_clipped_rect=*/gfx::Rect(10, 10, 10, 10));
- }
- TEST_F(PdfViewWebPluginTest, UpdateLayerTransformWithTranslate) {
- plugin_->UpdateLayerTransform(1.0f, gfx::Vector2dF(-1.25, 1.25));
- TestPaintSnapshots(/*device_scale=*/4.0f,
- /*window_rect=*/gfx::Rect(10, 10, 20, 20),
- /*paint_rect=*/gfx::Rect(10, 10, 20, 20),
- /*expected_clipped_rect=*/gfx::Rect(10, 15, 15, 15));
- }
- TEST_F(PdfViewWebPluginTest, UpdateLayerTransformWithScaleAndTranslate) {
- plugin_->UpdateLayerTransform(0.5f, gfx::Vector2dF(-1.25, 1.25));
- TestPaintSnapshots(/*device_scale=*/4.0f,
- /*window_rect=*/gfx::Rect(10, 10, 20, 20),
- /*paint_rect=*/gfx::Rect(10, 10, 20, 20),
- /*expected_clipped_rect=*/gfx::Rect(10, 15, 5, 10));
- }
- TEST_F(PdfViewWebPluginTest, HandleViewportMessageBeforeDocumentLoadComplete) {
- EXPECT_CALL(*engine_ptr_, ApplyDocumentLayout(DocumentLayout::Options()));
- EXPECT_CALL(*client_ptr_, PostMessage).Times(0);
- plugin_->OnMessage(ParseMessage(R"({
- "type": "viewport",
- "userInitiated": false,
- "zoom": 1,
- "layoutOptions": {
- "direction": 0,
- "defaultPageOrientation": 0,
- "twoUpViewEnabled": false,
- },
- "xOffset": 0,
- "yOffset": 0,
- "pinchPhase": 0,
- })"));
- }
- TEST_F(PdfViewWebPluginTest, HandleViewportMessageAfterDocumentLoadComplete) {
- plugin_->DocumentLoadComplete();
- EXPECT_CALL(*engine_ptr_, ApplyDocumentLayout(DocumentLayout::Options()));
- EXPECT_CALL(*client_ptr_, PostMessage(base::test::IsJson(R"({
- "type": "loadProgress",
- "progress": 100.0,
- })")));
- plugin_->OnMessage(ParseMessage(R"({
- "type": "viewport",
- "userInitiated": false,
- "zoom": 1,
- "layoutOptions": {
- "direction": 0,
- "defaultPageOrientation": 0,
- "twoUpViewEnabled": false,
- },
- "xOffset": 0,
- "yOffset": 0,
- "pinchPhase": 0,
- })"));
- }
- TEST_F(PdfViewWebPluginTest, HandleViewportMessageSubsequently) {
- plugin_->OnMessage(ParseMessage(R"({
- "type": "viewport",
- "userInitiated": false,
- "zoom": 1,
- "layoutOptions": {
- "direction": 0,
- "defaultPageOrientation": 0,
- "twoUpViewEnabled": false,
- },
- "xOffset": 0,
- "yOffset": 0,
- "pinchPhase": 0,
- })"));
- DocumentLayout::Options two_up_options;
- two_up_options.set_page_spread(DocumentLayout::PageSpread::kTwoUpOdd);
- EXPECT_CALL(*engine_ptr_, ApplyDocumentLayout(two_up_options));
- EXPECT_CALL(*client_ptr_, PostMessage).Times(0);
- plugin_->OnMessage(ParseMessage(R"({
- "type": "viewport",
- "userInitiated": false,
- "zoom": 1,
- "layoutOptions": {
- "direction": 0,
- "defaultPageOrientation": 0,
- "twoUpViewEnabled": true,
- },
- "xOffset": 0,
- "yOffset": 0,
- "pinchPhase": 0,
- })"));
- }
- TEST_F(PdfViewWebPluginTest, HandleViewportMessageScroll) {
- EXPECT_CALL(*engine_ptr_, ApplyDocumentLayout)
- .WillRepeatedly(Return(gfx::Size(16, 9)));
- EXPECT_CALL(*engine_ptr_, ScrolledToXPosition(2));
- EXPECT_CALL(*engine_ptr_, ScrolledToYPosition(3));
- plugin_->OnMessage(ParseMessage(R"({
- "type": "viewport",
- "userInitiated": false,
- "zoom": 1,
- "layoutOptions": {
- "direction": 2,
- "defaultPageOrientation": 0,
- "twoUpViewEnabled": false,
- },
- "xOffset": 2,
- "yOffset": 3,
- "pinchPhase": 0,
- })"));
- }
- TEST_F(PdfViewWebPluginTest, HandleViewportMessageScrollRightToLeft) {
- EXPECT_CALL(*engine_ptr_, ApplyDocumentLayout)
- .WillRepeatedly(Return(gfx::Size(16, 9)));
- EXPECT_CALL(*engine_ptr_, ScrolledToXPosition(2));
- EXPECT_CALL(*engine_ptr_, ScrolledToYPosition(3));
- plugin_->OnMessage(ParseMessage(R"({
- "type": "viewport",
- "userInitiated": false,
- "zoom": 1,
- "layoutOptions": {
- "direction": 1,
- "defaultPageOrientation": 0,
- "twoUpViewEnabled": false,
- },
- "xOffset": 2,
- "yOffset": 3,
- "pinchPhase": 0,
- })"));
- }
- TEST_F(PdfViewWebPluginTest, HandleSetBackgroundColorMessage) {
- ASSERT_NE(SK_ColorGREEN, plugin_->GetBackgroundColor());
- base::Value::Dict message;
- message.Set("type", "setBackgroundColor");
- message.Set("color", static_cast<double>(SK_ColorGREEN));
- plugin_->OnMessage(message);
- EXPECT_EQ(SK_ColorGREEN, plugin_->GetBackgroundColor());
- }
- TEST_F(PdfViewWebPluginTest, HandleInputEvent) {
- UpdatePluginGeometryWithoutWaiting(2.0f, {0, 0, 20, 20});
- EXPECT_CALL(*engine_ptr_, HandleInputEvent)
- .WillRepeatedly([](const blink::WebInputEvent& event) {
- if (!blink::WebInputEvent::IsMouseEventType(event.GetType())) {
- ADD_FAILURE() << "Unexpected event type: " << event.GetType();
- return false;
- }
- const auto& mouse_event =
- static_cast<const blink::WebMouseEvent&>(event);
- EXPECT_EQ(blink::WebInputEvent::Type::kMouseDown,
- mouse_event.GetType());
- EXPECT_EQ(gfx::PointF(10.0f, 40.0f), mouse_event.PositionInWidget());
- return true;
- });
- blink::WebMouseEvent mouse_event;
- mouse_event.SetType(blink::WebInputEvent::Type::kMouseDown);
- mouse_event.SetPositionInWidget(10.0f, 20.0f);
- ui::Cursor dummy_cursor;
- EXPECT_EQ(blink::WebInputEventResult::kHandledApplication,
- plugin_->HandleInputEvent(
- blink::WebCoalescedInputEvent(mouse_event, ui::LatencyInfo()),
- &dummy_cursor));
- }
- class PdfViewWebPluginImeTest : public PdfViewWebPluginTest {
- public:
- void TestImeSetCompositionForPlugin(const blink::WebString& text) {
- EXPECT_CALL(*engine_ptr_, HandleInputEvent).Times(0);
- plugin_->ImeSetCompositionForPlugin(text, std::vector<ui::ImeTextSpan>(),
- gfx::Range(),
- /*selection_start=*/0,
- /*selection_end=*/0);
- }
- void TestImeFinishComposingTextForPlugin(
- const blink::WebString& expected_text) {
- InSequence sequence;
- std::u16string expected_text16 = expected_text.Utf16();
- if (expected_text16.size()) {
- for (const auto& c : expected_text16) {
- base::StringPiece16 expected_key(&c, 1);
- EXPECT_CALL(*engine_ptr_,
- HandleInputEvent(IsExpectedImeKeyEvent(expected_key)))
- .WillOnce(Return(true));
- }
- } else {
- EXPECT_CALL(*engine_ptr_, HandleInputEvent).Times(0);
- }
- plugin_->ImeFinishComposingTextForPlugin(false);
- }
- void TestImeCommitTextForPlugin(const blink::WebString& text) {
- InSequence sequence;
- std::u16string expected_text16 = text.Utf16();
- if (expected_text16.size()) {
- for (const auto& c : expected_text16) {
- base::StringPiece16 event(&c, 1);
- EXPECT_CALL(*engine_ptr_,
- HandleInputEvent(IsExpectedImeKeyEvent(event)))
- .WillOnce(Return(true));
- }
- } else {
- EXPECT_CALL(*engine_ptr_, HandleInputEvent).Times(0);
- }
- plugin_->ImeCommitTextForPlugin(text, std::vector<ui::ImeTextSpan>(),
- gfx::Range(),
- /*relative_cursor_pos=*/0);
- }
- };
- TEST_F(PdfViewWebPluginImeTest, ImeSetCompositionAndFinishAscii) {
- const blink::WebString text = blink::WebString::FromASCII("input");
- TestImeSetCompositionForPlugin(text);
- TestImeFinishComposingTextForPlugin(text);
- }
- TEST_F(PdfViewWebPluginImeTest, ImeSetCompositionAndFinishUnicode) {
- const blink::WebString text = blink::WebString::FromUTF16(u"你好");
- TestImeSetCompositionForPlugin(text);
- TestImeFinishComposingTextForPlugin(text);
- // Calling ImeFinishComposingTextForPlugin() again is a no-op.
- TestImeFinishComposingTextForPlugin("");
- }
- TEST_F(PdfViewWebPluginImeTest, ImeSetCompositionAndFinishEmpty) {
- const blink::WebString text;
- TestImeSetCompositionForPlugin(text);
- TestImeFinishComposingTextForPlugin(text);
- }
- TEST_F(PdfViewWebPluginImeTest, ImeCommitTextForPluginAscii) {
- const blink::WebString text = blink::WebString::FromASCII("a b");
- TestImeCommitTextForPlugin(text);
- }
- TEST_F(PdfViewWebPluginImeTest, ImeCommitTextForPluginUnicode) {
- const blink::WebString text = blink::WebString::FromUTF16(u"さようなら");
- TestImeCommitTextForPlugin(text);
- }
- TEST_F(PdfViewWebPluginImeTest, ImeCommitTextForPluginEmpty) {
- const blink::WebString text;
- TestImeCommitTextForPlugin(text);
- }
- TEST_F(PdfViewWebPluginTest, SelectionChanged) {
- plugin_->EnableAccessibility();
- plugin_->DocumentLoadComplete();
- UpdatePluginGeometryWithoutWaiting(1.0f, {300, 56, 20, 5});
- SetDocumentDimensions({16, 9});
- AccessibilityViewportInfo viewport_info;
- EXPECT_CALL(pdf_service_, SelectionChanged(gfx::PointF(-8.0f, -20.0f), 40,
- gfx::PointF(52.0f, 60.0f), 80));
- EXPECT_CALL(*accessibility_data_handler_ptr_, SetAccessibilityViewportInfo)
- .WillOnce(SaveArg<0>(&viewport_info));
- plugin_->SelectionChanged({-10, -20, 30, 40}, {50, 60, 70, 80});
- EXPECT_EQ(gfx::Point(), viewport_info.scroll);
- pdf_receiver_.FlushForTesting();
- }
- TEST_F(PdfViewWebPluginTest, SelectionChangedNegativeOrigin) {
- plugin_->EnableAccessibility();
- plugin_->DocumentLoadComplete();
- UpdatePluginGeometryWithoutWaiting(1.0f, {-300, -56, 20, 5});
- SetDocumentDimensions({16, 9});
- AccessibilityViewportInfo viewport_info;
- EXPECT_CALL(pdf_service_, SelectionChanged(gfx::PointF(-8.0f, -20.0f), 40,
- gfx::PointF(52.0f, 60.0f), 80));
- EXPECT_CALL(*accessibility_data_handler_ptr_, SetAccessibilityViewportInfo)
- .WillOnce(SaveArg<0>(&viewport_info));
- plugin_->SelectionChanged({-10, -20, 30, 40}, {50, 60, 70, 80});
- EXPECT_EQ(gfx::Point(), viewport_info.scroll);
- pdf_receiver_.FlushForTesting();
- }
- TEST_F(PdfViewWebPluginTest, SelectionChangedScaled) {
- plugin_->EnableAccessibility();
- plugin_->DocumentLoadComplete();
- UpdatePluginGeometryWithoutWaiting(2.0f, {600, 112, 40, 10});
- SetDocumentDimensions({16, 9});
- AccessibilityViewportInfo viewport_info;
- EXPECT_CALL(pdf_service_, SelectionChanged(gfx::PointF(-8.0f, -20.0f), 40,
- gfx::PointF(52.0f, 60.0f), 80));
- EXPECT_CALL(*accessibility_data_handler_ptr_, SetAccessibilityViewportInfo)
- .WillOnce(SaveArg<0>(&viewport_info));
- plugin_->SelectionChanged({-20, -40, 60, 80}, {100, 120, 140, 160});
- EXPECT_EQ(gfx::Point(), viewport_info.scroll);
- pdf_receiver_.FlushForTesting();
- }
- TEST_F(PdfViewWebPluginTest, ChangeTextSelection) {
- ASSERT_FALSE(plugin_->HasSelection());
- ASSERT_TRUE(plugin_->SelectionAsText().IsEmpty());
- ASSERT_TRUE(plugin_->SelectionAsMarkup().IsEmpty());
- static constexpr char kSelectedText[] = "1234";
- EXPECT_CALL(*client_ptr_,
- TextSelectionChanged(blink::WebString::FromUTF8(kSelectedText), 0,
- gfx::Range(0, 4)));
- plugin_->SetSelectedText(kSelectedText);
- EXPECT_TRUE(plugin_->HasSelection());
- EXPECT_EQ(kSelectedText, plugin_->SelectionAsText().Utf8());
- EXPECT_EQ(kSelectedText, plugin_->SelectionAsMarkup().Utf8());
- static constexpr char kEmptyText[] = "";
- EXPECT_CALL(*client_ptr_,
- TextSelectionChanged(blink::WebString::FromUTF8(kEmptyText), 0,
- gfx::Range(0, 0)));
- plugin_->SetSelectedText(kEmptyText);
- EXPECT_FALSE(plugin_->HasSelection());
- EXPECT_TRUE(plugin_->SelectionAsText().IsEmpty());
- EXPECT_TRUE(plugin_->SelectionAsMarkup().IsEmpty());
- }
- TEST_F(PdfViewWebPluginTest, FormTextFieldFocusChangeUpdatesTextInputType) {
- ASSERT_EQ(blink::WebTextInputType::kWebTextInputTypeNone,
- plugin_->GetPluginTextInputType());
- ExpectUpdateTextInputState(blink::WebTextInputType::kWebTextInputTypeText);
- plugin_->FormFieldFocusChange(PDFEngine::FocusFieldType::kText);
- ExpectUpdateTextInputState(blink::WebTextInputType::kWebTextInputTypeNone);
- plugin_->FormFieldFocusChange(PDFEngine::FocusFieldType::kNoFocus);
- ExpectUpdateTextInputState(blink::WebTextInputType::kWebTextInputTypeText);
- plugin_->FormFieldFocusChange(PDFEngine::FocusFieldType::kText);
- ExpectUpdateTextInputState(blink::WebTextInputType::kWebTextInputTypeNone);
- plugin_->FormFieldFocusChange(PDFEngine::FocusFieldType::kNonText);
- }
- TEST_F(PdfViewWebPluginTest, SearchString) {
- static constexpr char16_t kPattern[] = u"fox";
- static constexpr char16_t kTarget[] =
- u"The quick brown fox jumped over the lazy Fox";
- {
- static constexpr PDFEngine::Client::SearchStringResult kExpectation[] = {
- {16, 3}};
- EXPECT_THAT(
- plugin_->SearchString(kTarget, kPattern, /*case_sensitive=*/true),
- Pointwise(SearchStringResultEq(), kExpectation));
- }
- {
- static constexpr PDFEngine::Client::SearchStringResult kExpectation[] = {
- {16, 3}, {41, 3}};
- EXPECT_THAT(
- plugin_->SearchString(kTarget, kPattern, /*case_sensitive=*/false),
- Pointwise(SearchStringResultEq(), kExpectation));
- }
- }
- TEST_F(PdfViewWebPluginTest, UpdateFocus) {
- MockFunction<void(int checkpoint_num)> checkpoint;
- {
- InSequence sequence;
- // Focus false -> true: Triggers updates.
- EXPECT_CALL(*client_ptr_, UpdateTextInputState);
- EXPECT_CALL(*client_ptr_, UpdateSelectionBounds);
- EXPECT_CALL(checkpoint, Call(1));
- // Focus true -> true: No updates.
- EXPECT_CALL(checkpoint, Call(2));
- // Focus true -> false: Triggers updates. `UpdateTextInputState` is called
- // twice because it also gets called due to
- // `PDFiumEngine::UpdateFocus(false)`.
- EXPECT_CALL(*client_ptr_, UpdateTextInputState).Times(2);
- EXPECT_CALL(*client_ptr_, UpdateSelectionBounds);
- EXPECT_CALL(checkpoint, Call(3));
- // Focus false -> false: No updates.
- EXPECT_CALL(checkpoint, Call(4));
- // Focus false -> true: Triggers updates.
- EXPECT_CALL(*client_ptr_, UpdateTextInputState);
- EXPECT_CALL(*client_ptr_, UpdateSelectionBounds);
- }
- // The focus type does not matter in this test.
- plugin_->UpdateFocus(/*focused=*/true, blink::mojom::FocusType::kNone);
- checkpoint.Call(1);
- plugin_->UpdateFocus(/*focused=*/true, blink::mojom::FocusType::kNone);
- checkpoint.Call(2);
- plugin_->UpdateFocus(/*focused=*/false, blink::mojom::FocusType::kNone);
- checkpoint.Call(3);
- plugin_->UpdateFocus(/*focused=*/false, blink::mojom::FocusType::kNone);
- checkpoint.Call(4);
- plugin_->UpdateFocus(/*focused=*/true, blink::mojom::FocusType::kNone);
- }
- TEST_F(PdfViewWebPluginTest, ShouldDispatchImeEventsToPlugin) {
- ASSERT_TRUE(plugin_->ShouldDispatchImeEventsToPlugin());
- }
- TEST_F(PdfViewWebPluginTest, CaretChange) {
- EXPECT_CALL(*engine_ptr_, ZoomUpdated(2.0f));
- UpdatePluginGeometry(
- /*device_scale=*/2.0f, /*window_rect=*/gfx::Rect(12, 24, 36, 48));
- plugin_->CaretChanged(gfx::Rect(10, 20, 30, 40));
- EXPECT_EQ(gfx::Rect(28, 20, 30, 40), plugin_->GetPluginCaretBounds());
- }
- TEST_F(PdfViewWebPluginTest, EnteredEditMode) {
- EXPECT_CALL(pdf_service_, SetPluginCanSave(true));
- EXPECT_CALL(*client_ptr_, PostMessage).Times(AnyNumber());
- EXPECT_CALL(*client_ptr_, PostMessage(base::test::IsJson(R"({
- "type": "setIsEditing",
- })")));
- plugin_->EnteredEditMode();
- pdf_receiver_.FlushForTesting();
- }
- TEST_F(PdfViewWebPluginTest, NotifyNumberOfFindResultsChanged) {
- plugin_->StartFind("x", /*case_sensitive=*/false, /*identifier=*/123);
- const std::vector<gfx::Rect> tickmarks = {gfx::Rect(1, 2), gfx::Rect(3, 4)};
- plugin_->UpdateTickMarks(tickmarks);
- EXPECT_CALL(*client_ptr_, ReportFindInPageTickmarks(tickmarks));
- EXPECT_CALL(*client_ptr_, ReportFindInPageMatchCount(123, 5, true));
- plugin_->NotifyNumberOfFindResultsChanged(/*total=*/5, /*final_result=*/true);
- }
- TEST_F(PdfViewWebPluginTest, OnDocumentLoadComplete) {
- base::Value::Dict metadata;
- metadata.Set("fileSize", "0 B");
- metadata.Set("linearized", false);
- metadata.Set("pageSize", "Varies");
- metadata.Set("canSerializeDocument", true);
- base::Value::Dict message;
- message.Set("type", "metadata");
- message.Set("metadataData", std::move(metadata));
- EXPECT_CALL(*client_ptr_, PostMessage);
- EXPECT_CALL(*client_ptr_, PostMessage(Eq(std::ref(message))));
- plugin_->DocumentLoadComplete();
- }
- class PdfViewWebPluginWithDocInfoTest : public PdfViewWebPluginTest {
- protected:
- class TestPDFiumEngineWithDocInfo : public TestPDFiumEngine {
- public:
- explicit TestPDFiumEngineWithDocInfo(PDFEngine::Client* client)
- : TestPDFiumEngine(client) {
- InitializeDocumentAttachments();
- InitializeDocumentMetadata();
- }
- base::Value::List GetBookmarks() override {
- // Create `bookmark1` which navigates to an in-doc position. This bookmark
- // will be in the top-level bookmark list.
- base::Value::Dict bookmark1;
- bookmark1.Set("title", "Bookmark 1");
- bookmark1.Set("page", 2);
- bookmark1.Set("x", 10);
- bookmark1.Set("y", 20);
- bookmark1.Set("zoom", 2.0);
- // Create `bookmark2` which navigates to a web page. This bookmark will be
- // a child of `bookmark1`.
- base::Value::Dict bookmark2;
- bookmark2.Set("title", "Bookmark 2");
- bookmark2.Set("uri", "test.com");
- base::Value::List children_of_bookmark1;
- children_of_bookmark1.Append(std::move(bookmark2));
- bookmark1.Set("children", std::move(children_of_bookmark1));
- // Create the top-level bookmark list.
- base::Value::List bookmarks;
- bookmarks.Append(std::move(bookmark1));
- return bookmarks;
- }
- absl::optional<gfx::Size> GetUniformPageSizePoints() override {
- return gfx::Size(1000, 1200);
- }
- private:
- void InitializeDocumentAttachments() {
- doc_attachment_info_list().resize(3);
- // A regular attachment.
- doc_attachment_info_list()[0].name = u"attachment1.txt";
- doc_attachment_info_list()[0].creation_date = u"D:20170712214438-07'00'";
- doc_attachment_info_list()[0].modified_date = u"D:20160115091400";
- doc_attachment_info_list()[0].is_readable = true;
- doc_attachment_info_list()[0].size_bytes = 13u;
- // An unreadable attachment.
- doc_attachment_info_list()[1].name = u"attachment2.pdf";
- doc_attachment_info_list()[1].is_readable = false;
- // A readable attachment that exceeds download size limit.
- doc_attachment_info_list()[2].name = u"attachment3.mov";
- doc_attachment_info_list()[2].is_readable = true;
- doc_attachment_info_list()[2].size_bytes =
- PdfViewPluginBase::kMaximumSavedFileSize + 1;
- }
- void InitializeDocumentMetadata() {
- metadata().version = PdfVersion::k1_7;
- metadata().size_bytes = 13u;
- metadata().page_count = 13u;
- metadata().linearized = true;
- metadata().has_attachments = true;
- metadata().form_type = FormType::kAcroForm;
- metadata().title = "Title";
- metadata().author = "Author";
- metadata().subject = "Subject";
- metadata().keywords = "Keywords";
- metadata().creator = "Creator";
- metadata().producer = "Producer";
- ASSERT_TRUE(base::Time::FromUTCString("2021-05-04 11:12:13",
- &metadata().creation_date));
- ASSERT_TRUE(base::Time::FromUTCString("2021-06-04 15:16:17",
- &metadata().mod_date));
- }
- };
- static base::Value::Dict CreateExpectedAttachmentsResponse() {
- base::Value::List attachments;
- {
- base::Value::Dict attachment;
- attachment.Set("name", "attachment1.txt");
- attachment.Set("size", 13);
- attachment.Set("readable", true);
- attachments.Append(std::move(attachment));
- }
- {
- base::Value::Dict attachment;
- attachment.Set("name", "attachment2.pdf");
- attachment.Set("size", 0);
- attachment.Set("readable", false);
- attachments.Append(std::move(attachment));
- }
- {
- base::Value::Dict attachment;
- attachment.Set("name", "attachment3.mov");
- attachment.Set("size", -1);
- attachment.Set("readable", true);
- attachments.Append(std::move(attachment));
- }
- base::Value::Dict message;
- message.Set("type", "attachments");
- message.Set("attachmentsData", std::move(attachments));
- return message;
- }
- static base::Value::Dict CreateExpectedBookmarksResponse(
- base::Value::List bookmarks) {
- base::Value::Dict message;
- message.Set("type", "bookmarks");
- message.Set("bookmarksData", std::move(bookmarks));
- return message;
- }
- static base::Value::Dict CreateExpectedMetadataResponse() {
- base::Value::Dict metadata;
- metadata.Set("version", "1.7");
- metadata.Set("fileSize", "13 B");
- metadata.Set("linearized", true);
- metadata.Set("title", "Title");
- metadata.Set("author", "Author");
- metadata.Set("subject", "Subject");
- metadata.Set("keywords", "Keywords");
- metadata.Set("creator", "Creator");
- metadata.Set("producer", "Producer");
- metadata.Set("creationDate", "5/4/21, 4:12:13 AM");
- metadata.Set("modDate", "6/4/21, 8:16:17 AM");
- metadata.Set("pageSize", "13.89 × 16.67 in (portrait)");
- metadata.Set("canSerializeDocument", true);
- base::Value::Dict message;
- message.Set("type", "metadata");
- message.Set("metadataData", std::move(metadata));
- return message;
- }
- void SetUpClient() override {
- EXPECT_CALL(*client_ptr_, CreateEngine).WillOnce([this]() {
- auto engine = std::make_unique<NiceMock<TestPDFiumEngineWithDocInfo>>(
- plugin_.get());
- engine_ptr_ = engine.get();
- return engine;
- });
- }
- };
- TEST_F(PdfViewWebPluginWithDocInfoTest, OnDocumentLoadComplete) {
- const base::Value::Dict expect_attachments =
- CreateExpectedAttachmentsResponse();
- const base::Value::Dict expect_bookmarks =
- CreateExpectedBookmarksResponse(engine_ptr_->GetBookmarks());
- const base::Value::Dict expect_metadata = CreateExpectedMetadataResponse();
- EXPECT_CALL(*client_ptr_, PostMessage);
- EXPECT_CALL(*client_ptr_, PostMessage(Eq(std::ref(expect_attachments))));
- EXPECT_CALL(*client_ptr_, PostMessage(Eq(std::ref(expect_bookmarks))));
- EXPECT_CALL(*client_ptr_, PostMessage(Eq(std::ref(expect_metadata))));
- plugin_->DocumentLoadComplete();
- }
- class PdfViewWebPluginSaveTest : public PdfViewWebPluginTest {
- protected:
- static void AddDataToValue(base::span<const uint8_t> data,
- base::Value& value) {
- value.GetDict().Set("dataToSave", base::Value(data));
- }
- void SetUpClient() override {
- // Ignore non-"saveData" `PdfViewWebPlugin::Client::PostMessage()` calls.
- EXPECT_CALL(*client_ptr_, PostMessage)
- .WillRepeatedly([](const base::Value::Dict& message) {
- EXPECT_NE("saveData", *message.FindString("type"));
- });
- }
- };
- #if BUILDFLAG(ENABLE_INK)
- TEST_F(PdfViewWebPluginSaveTest, AnnotationInNonEditMode) {
- base::Value expected_response = base::test::ParseJson(R"({
- "type": "saveData",
- "token": "annotation-in-non-edit-mode",
- "fileName": "example.pdf",
- "editModeForTesting": false,
- })");
- AddDataToValue(base::make_span(TestPDFiumEngine::kLoadedData),
- expected_response);
- EXPECT_CALL(pdf_service_, SetPluginCanSave(true));
- ExpectUpdateTextInputState(blink::WebTextInputType::kWebTextInputTypeNone);
- EXPECT_CALL(*client_ptr_, PostMessage(base::test::IsJson(expected_response)));
- plugin_->OnMessage(ParseMessage(R"({
- "type": "save",
- "saveRequestType": 0,
- "token": "annotation-in-non-edit-mode",
- })"));
- pdf_receiver_.FlushForTesting();
- }
- TEST_F(PdfViewWebPluginSaveTest, AnnotationInEditMode) {
- plugin_->EnteredEditMode();
- pdf_receiver_.FlushForTesting();
- base::Value expected_response = base::test::ParseJson(R"({
- "type": "saveData",
- "token": "annotation-in-edit-mode",
- "fileName": "example.pdf",
- "editModeForTesting": true,
- })");
- AddDataToValue(base::make_span(TestPDFiumEngine::kSaveData),
- expected_response);
- EXPECT_CALL(pdf_service_, SetPluginCanSave(true));
- ExpectUpdateTextInputState(blink::WebTextInputType::kWebTextInputTypeNone);
- EXPECT_CALL(*client_ptr_, PostMessage(base::test::IsJson(expected_response)));
- plugin_->OnMessage(ParseMessage(R"({
- "type": "save",
- "saveRequestType": 0,
- "token": "annotation-in-edit-mode",
- })"));
- pdf_receiver_.FlushForTesting();
- }
- #endif // BUILDFLAG(ENABLE_INK)
- TEST_F(PdfViewWebPluginSaveTest, OriginalInNonEditMode) {
- {
- InSequence pdf_service_sequence;
- EXPECT_CALL(pdf_service_, SetPluginCanSave(false));
- EXPECT_CALL(
- pdf_service_,
- SaveUrlAs(GURL(kPdfUrl), network::mojom::ReferrerPolicy::kDefault));
- EXPECT_CALL(pdf_service_, SetPluginCanSave(false));
- }
- ExpectUpdateTextInputState(blink::WebTextInputType::kWebTextInputTypeNone);
- EXPECT_CALL(*client_ptr_, PostMessage(base::test::IsJson(R"({
- "type": "consumeSaveToken",
- "token": "original-in-non-edit-mode",
- })")));
- plugin_->OnMessage(ParseMessage(R"({
- "type": "save",
- "saveRequestType": 1,
- "token": "original-in-non-edit-mode",
- })"));
- pdf_receiver_.FlushForTesting();
- }
- TEST_F(PdfViewWebPluginSaveTest, OriginalInEditMode) {
- plugin_->EnteredEditMode();
- pdf_receiver_.FlushForTesting();
- {
- InSequence pdf_service_sequence;
- EXPECT_CALL(pdf_service_, SetPluginCanSave(false));
- EXPECT_CALL(
- pdf_service_,
- SaveUrlAs(GURL(kPdfUrl), network::mojom::ReferrerPolicy::kDefault));
- EXPECT_CALL(pdf_service_, SetPluginCanSave(true));
- }
- ExpectUpdateTextInputState(blink::WebTextInputType::kWebTextInputTypeNone);
- EXPECT_CALL(*client_ptr_, PostMessage(base::test::IsJson(R"({
- "type": "consumeSaveToken",
- "token": "original-in-edit-mode",
- })")));
- plugin_->OnMessage(ParseMessage(R"({
- "type": "save",
- "saveRequestType": 1,
- "token": "original-in-edit-mode",
- })"));
- pdf_receiver_.FlushForTesting();
- }
- #if BUILDFLAG(ENABLE_INK)
- TEST_F(PdfViewWebPluginSaveTest, EditedInNonEditMode) {
- base::Value expected_response = base::test::ParseJson(R"({
- "type": "saveData",
- "token": "edited-in-non-edit-mode",
- "fileName": "example.pdf",
- "editModeForTesting": false,
- })");
- AddDataToValue(base::make_span(TestPDFiumEngine::kLoadedData),
- expected_response);
- ExpectUpdateTextInputState(blink::WebTextInputType::kWebTextInputTypeNone);
- EXPECT_CALL(*client_ptr_, PostMessage(base::test::IsJson(expected_response)));
- plugin_->OnMessage(ParseMessage(R"({
- "type": "save",
- "saveRequestType": 2,
- "token": "edited-in-non-edit-mode",
- })"));
- }
- #endif // BUILDFLAG(ENABLE_INK)
- TEST_F(PdfViewWebPluginSaveTest, EditedInEditMode) {
- plugin_->EnteredEditMode();
- base::Value expected_response = base::test::ParseJson(R"({
- "type": "saveData",
- "token": "edited-in-edit-mode",
- "fileName": "example.pdf",
- "editModeForTesting": true,
- })");
- AddDataToValue(base::make_span(TestPDFiumEngine::kSaveData),
- expected_response);
- ExpectUpdateTextInputState(blink::WebTextInputType::kWebTextInputTypeNone);
- EXPECT_CALL(*client_ptr_, PostMessage(base::test::IsJson(expected_response)));
- plugin_->OnMessage(ParseMessage(R"({
- "type": "save",
- "saveRequestType": 2,
- "token": "edited-in-edit-mode",
- })"));
- }
- class PdfViewWebPluginSubmitFormTest
- : public PdfViewWebPluginWithoutInitializeTest {
- protected:
- void SubmitForm(const std::string& url,
- base::StringPiece form_data = "data") {
- EXPECT_TRUE(plugin_->InitializeForTesting());
- EXPECT_CALL(*client_ptr_, CreateAssociatedURLLoader).WillOnce([this]() {
- auto associated_loader =
- std::make_unique<NiceMock<MockWebAssociatedURLLoader>>();
- EXPECT_CALL(*associated_loader, LoadAsynchronously)
- .WillOnce([this](const blink::WebURLRequest& request,
- blink::WebAssociatedURLLoaderClient* /*client*/) {
- // TODO(crbug.com/1322928): The `UrlLoader` created by `LoadUrl()`
- // and `SubmitForm()` shouldn't use different ownership semantics.
- // The loader created by `SubmitForm()` is owned by the plugin, and
- // cannot leak past the destruction of the plugin.
- request_.CopyFrom(request);
- });
- return associated_loader;
- });
- plugin_->SubmitForm(url, form_data.data(), form_data.size());
- }
- void SubmitFailingForm(const std::string& url) {
- EXPECT_TRUE(plugin_->InitializeForTesting());
- EXPECT_CALL(*client_ptr_, CreateAssociatedURLLoader).Times(0);
- constexpr base::StringPiece kFormData = "form data";
- plugin_->SubmitForm(url, kFormData.data(), kFormData.size());
- }
- blink::WebURLRequest request_;
- };
- TEST_F(PdfViewWebPluginSubmitFormTest, RequestMethod) {
- SetUpPluginWithUrl("https://www.example.com/path/to/the.pdf");
- SubmitForm(/*url=*/"");
- EXPECT_EQ(request_.HttpMethod().Utf8(), "POST");
- }
- TEST_F(PdfViewWebPluginSubmitFormTest, RequestBody) {
- SetUpPluginWithUrl("https://www.example.com/path/to/the.pdf");
- constexpr base::StringPiece kFormData = "form data";
- SubmitForm(/*url=*/"", kFormData);
- blink::WebHTTPBody::Element element;
- EXPECT_EQ(request_.HttpBody().ElementCount(), 1u);
- ASSERT_TRUE(request_.HttpBody().ElementAt(0, element));
- ASSERT_EQ(element.type, blink::HTTPBodyElementType::kTypeData);
- EXPECT_THAT(element.data.Copy(), testing::ElementsAreArray(kFormData));
- }
- TEST_F(PdfViewWebPluginSubmitFormTest, RelativeUrl) {
- SetUpPluginWithUrl("https://www.example.com/path/to/the.pdf");
- SubmitForm("relative_endpoint");
- EXPECT_EQ(request_.Url().GetString().Utf8(),
- "https://www.example.com/path/to/relative_endpoint");
- }
- TEST_F(PdfViewWebPluginSubmitFormTest, NoRelativeUrl) {
- SetUpPluginWithUrl("https://www.example.com/path/to/the.pdf");
- SubmitForm("");
- EXPECT_EQ(request_.Url().GetString().Utf8(),
- "https://www.example.com/path/to/the.pdf");
- }
- TEST_F(PdfViewWebPluginSubmitFormTest, AbsoluteUrl) {
- SetUpPluginWithUrl("https://a.example.com/path/to/the.pdf");
- SubmitForm("https://b.example.com/relative_endpoint");
- EXPECT_EQ(request_.Url().GetString().Utf8(),
- "https://b.example.com/relative_endpoint");
- }
- TEST_F(PdfViewWebPluginSubmitFormTest, RelativeUrlInvalidDocumentUrl) {
- SetUpPluginWithUrl("https://www.%B%Ad.com/path/to/the.pdf");
- SubmitFailingForm("relative_endpoint");
- }
- TEST_F(PdfViewWebPluginSubmitFormTest, AbsoluteUrlInvalidDocumentUrl) {
- SetUpPluginWithUrl("https://www.%B%Ad.com/path/to/the.pdf");
- SubmitFailingForm("https://wwww.example.com");
- }
- class PdfViewWebPluginPrintTest : public PdfViewWebPluginTest {
- protected:
- void SetUp() override {
- PdfViewWebPluginTest::SetUp();
- // Size must be at least 1 for conversion to `SkMemoryStream`.
- ON_CALL(*engine_ptr_, PrintPages)
- .WillByDefault(Return(std::vector<uint8_t>(1)));
- canvas_.sk_canvas()->SetPrintingMetafile(&metafile_);
- }
- printing::MetafileSkia metafile_;
- };
- TEST_F(PdfViewWebPluginPrintTest, HighQuality) {
- EXPECT_CALL(*engine_ptr_,
- HasPermission(DocumentPermission::kPrintHighQuality))
- .WillRepeatedly(Return(true));
- EXPECT_CALL(*engine_ptr_, HasPermission(DocumentPermission::kPrintLowQuality))
- .WillRepeatedly(Return(true));
- ASSERT_EQ(static_cast<int>(TestPDFiumEngine::kPageNumber),
- plugin_->PrintBegin(blink::WebPrintParams()));
- EXPECT_CALL(
- *engine_ptr_,
- PrintPages(ElementsAre(0),
- Field(&blink::WebPrintParams::rasterize_pdf, IsFalse())));
- plugin_->PrintPage(0, canvas_.sk_canvas());
- plugin_->PrintEnd();
- }
- TEST_F(PdfViewWebPluginPrintTest, HighQualityRasterized) {
- EXPECT_CALL(*engine_ptr_,
- HasPermission(DocumentPermission::kPrintHighQuality))
- .WillRepeatedly(Return(true));
- EXPECT_CALL(*engine_ptr_, HasPermission(DocumentPermission::kPrintLowQuality))
- .WillRepeatedly(Return(true));
- blink::WebPrintParams params;
- params.rasterize_pdf = true;
- ASSERT_EQ(static_cast<int>(TestPDFiumEngine::kPageNumber),
- plugin_->PrintBegin(params));
- EXPECT_CALL(
- *engine_ptr_,
- PrintPages(ElementsAre(0),
- Field(&blink::WebPrintParams::rasterize_pdf, IsTrue())));
- plugin_->PrintPage(0, canvas_.sk_canvas());
- plugin_->PrintEnd();
- }
- // Regression test for crbug.com/1307219.
- TEST_F(PdfViewWebPluginPrintTest, LowQuality) {
- EXPECT_CALL(*engine_ptr_,
- HasPermission(DocumentPermission::kPrintHighQuality))
- .WillRepeatedly(Return(false));
- EXPECT_CALL(*engine_ptr_, HasPermission(DocumentPermission::kPrintLowQuality))
- .WillRepeatedly(Return(true));
- ASSERT_EQ(static_cast<int>(TestPDFiumEngine::kPageNumber),
- plugin_->PrintBegin(blink::WebPrintParams()));
- EXPECT_CALL(
- *engine_ptr_,
- PrintPages(ElementsAre(0),
- Field(&blink::WebPrintParams::rasterize_pdf, IsTrue())));
- plugin_->PrintPage(0, canvas_.sk_canvas());
- plugin_->PrintEnd();
- }
- // Regression test for crbug.com/1307219.
- TEST_F(PdfViewWebPluginPrintTest, LowQualityRasterized) {
- EXPECT_CALL(*engine_ptr_,
- HasPermission(DocumentPermission::kPrintHighQuality))
- .WillRepeatedly(Return(false));
- EXPECT_CALL(*engine_ptr_, HasPermission(DocumentPermission::kPrintLowQuality))
- .WillRepeatedly(Return(true));
- blink::WebPrintParams params;
- params.rasterize_pdf = true;
- ASSERT_EQ(static_cast<int>(TestPDFiumEngine::kPageNumber),
- plugin_->PrintBegin(params));
- EXPECT_CALL(
- *engine_ptr_,
- PrintPages(ElementsAre(0),
- Field(&blink::WebPrintParams::rasterize_pdf, IsTrue())));
- plugin_->PrintPage(0, canvas_.sk_canvas());
- plugin_->PrintEnd();
- }
- TEST_F(PdfViewWebPluginPrintTest, Disabled) {
- EXPECT_EQ(0, plugin_->PrintBegin(blink::WebPrintParams()));
- }
- TEST_F(PdfViewWebPluginPrintTest, DisabledRasterized) {
- blink::WebPrintParams params;
- params.rasterize_pdf = true;
- EXPECT_EQ(0, plugin_->PrintBegin(params));
- }
- class PdfViewWebPluginPrintPreviewTest : public PdfViewWebPluginTest {
- protected:
- void SetUpClient() override {
- EXPECT_CALL(*client_ptr_, GetEmbedderOriginString)
- .WillRepeatedly(Return("chrome://print/"));
- }
- };
- TEST_F(PdfViewWebPluginPrintPreviewTest, HandleResetPrintPreviewModeMessage) {
- EXPECT_CALL(*client_ptr_, CreateEngine)
- .WillOnce([](PDFEngine::Client* client,
- PDFiumFormFiller::ScriptOption script_option) {
- EXPECT_EQ(PDFiumFormFiller::ScriptOption::kNoJavaScript, script_option);
- auto engine = std::make_unique<NiceMock<TestPDFiumEngine>>(client);
- EXPECT_CALL(*engine, ZoomUpdated);
- EXPECT_CALL(*engine, PageOffsetUpdated);
- EXPECT_CALL(*engine, PluginSizeUpdated);
- EXPECT_CALL(*engine, SetGrayscale(false));
- return engine;
- });
- plugin_->OnMessage(ParseMessage(R"({
- "type": "resetPrintPreviewMode",
- "url": "chrome-untrusted://print/0/0/print.pdf",
- "grayscale": false,
- "pageCount": 1,
- })"));
- }
- TEST_F(PdfViewWebPluginPrintPreviewTest,
- HandleResetPrintPreviewModeMessageSetGrayscale) {
- EXPECT_CALL(*client_ptr_, CreateEngine)
- .WillOnce([](PDFEngine::Client* client,
- PDFiumFormFiller::ScriptOption /*script_option*/) {
- auto engine = std::make_unique<NiceMock<TestPDFiumEngine>>(client);
- EXPECT_CALL(*engine, SetGrayscale(true));
- return engine;
- });
- plugin_->OnMessage(ParseMessage(R"({
- "type": "resetPrintPreviewMode",
- "url": "chrome-untrusted://print/0/0/print.pdf",
- "grayscale": true,
- "pageCount": 1,
- })"));
- }
- TEST_F(PdfViewWebPluginPrintPreviewTest, DocumentLoadComplete) {
- plugin_->OnMessage(ParseMessage(R"({
- "type": "resetPrintPreviewMode",
- "url": "chrome-untrusted://print/0/0/print.pdf",
- "grayscale": false,
- "pageCount": 1,
- })"));
- EXPECT_CALL(*client_ptr_, RecordComputedAction("PDF.LoadSuccess"));
- EXPECT_CALL(*client_ptr_, PostMessage);
- EXPECT_CALL(*client_ptr_, PostMessage(base::test::IsJson(R"({
- "type": "formFocusChange",
- "focused": false,
- })")));
- ExpectUpdateTextInputState(blink::WebTextInputType::kWebTextInputTypeNone);
- EXPECT_CALL(*client_ptr_, PostMessage(base::test::IsJson(R"({
- "type": "printPreviewLoaded",
- })")));
- EXPECT_CALL(*accessibility_data_handler_ptr_, SetAccessibilityDocInfo)
- .Times(0);
- EXPECT_CALL(*client_ptr_, DidStopLoading).Times(0);
- EXPECT_CALL(pdf_service_, UpdateContentRestrictions).Times(0);
- plugin_->DocumentLoadComplete();
- EXPECT_EQ(PdfViewPluginBase::DocumentLoadState::kComplete,
- plugin_->document_load_state_for_testing());
- pdf_receiver_.FlushForTesting();
- }
- TEST_F(PdfViewWebPluginPrintPreviewTest,
- DocumentLoadProgressResetByResetPrintPreviewModeMessage) {
- plugin_->DocumentLoadProgress(2, 100);
- plugin_->OnMessage(ParseMessage(R"({
- "type": "resetPrintPreviewMode",
- "url": "chrome-untrusted://print/123/0/print.pdf",
- "grayscale": false,
- "pageCount": 2,
- })"));
- EXPECT_CALL(*client_ptr_, PostMessage(base::test::IsJson(R"({
- "type": "loadProgress",
- "progress": 3.0,
- })")));
- plugin_->DocumentLoadProgress(3, 100);
- }
- TEST_F(PdfViewWebPluginPrintPreviewTest,
- DocumentLoadProgressNotResetByLoadPreviewPageMessage) {
- plugin_->OnMessage(ParseMessage(R"({
- "type": "resetPrintPreviewMode",
- "url": "chrome-untrusted://print/123/0/print.pdf",
- "grayscale": false,
- "pageCount": 2,
- })"));
- plugin_->DocumentLoadProgress(2, 100);
- plugin_->OnMessage(ParseMessage(R"({
- "type": "loadPreviewPage",
- "url": "chrome-untrusted://print/123/1/print.pdf",
- "index": 1,
- })"));
- EXPECT_CALL(*client_ptr_, PostMessage).Times(0);
- plugin_->DocumentLoadProgress(3, 100);
- }
- TEST_F(PdfViewWebPluginPrintPreviewTest,
- HandleViewportMessageScrollRightToLeft) {
- EXPECT_CALL(*engine_ptr_, ApplyDocumentLayout)
- .WillRepeatedly(Return(gfx::Size(16, 9)));
- EXPECT_CALL(*engine_ptr_, ScrolledToXPosition(14));
- EXPECT_CALL(*engine_ptr_, ScrolledToYPosition(3));
- plugin_->OnMessage(ParseMessage(R"({
- "type": "viewport",
- "userInitiated": false,
- "zoom": 1,
- "layoutOptions": {
- "direction": 1,
- "defaultPageOrientation": 0,
- "twoUpViewEnabled": false,
- },
- "xOffset": -2,
- "yOffset": 3,
- "pinchPhase": 0,
- })"));
- }
- } // namespace chrome_pdf
|