0

[pdf] Migrate remaining permission unit tests

Migrates remaining PdfViewPluginBase tests that rely on PDFiumEngine's
HasPermission() API.

Bug: 1323307
Change-Id: I04ee984bd561126e2fc84de054c0a087cb84cf9d
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3696918
Reviewed-by: Lei Zhang <thestig@chromium.org>
Commit-Queue: K. Moon <kmoon@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1012251}
This commit is contained in:
K. Moon
2022-06-09 00:26:34 +00:00
committed by Chromium LUCI CQ
parent e4c147801d
commit 4ee171d2bf
2 changed files with 113 additions and 104 deletions

@ -15,7 +15,6 @@
#include "base/time/time.h"
#include "base/values.h"
#include "pdf/accessibility_structs.h"
#include "pdf/content_restriction.h"
#include "pdf/document_attachment_info.h"
#include "pdf/document_layout.h"
#include "pdf/document_metadata.h"
@ -192,24 +191,6 @@ class PdfViewPluginBaseWithEngineTest : public PdfViewPluginBaseTest {
})");
fake_plugin_.HandleMessage(message.GetDict());
}
void SetEnginePermissions(
const std::vector<DocumentPermission>& permissions) {
TestPDFiumEngine& engine =
*static_cast<TestPDFiumEngine*>(fake_plugin_.engine());
// TODO(crbug.com/1323307): Break up tests instead of "resetting" mocks.
for (DocumentPermission permission :
{DocumentPermission::kCopy, DocumentPermission::kCopyAccessible,
DocumentPermission::kPrintLowQuality,
DocumentPermission::kPrintHighQuality}) {
ON_CALL(engine, HasPermission(permission)).WillByDefault(Return(false));
}
for (DocumentPermission permission : permissions) {
ON_CALL(engine, HasPermission(permission)).WillByDefault(Return(true));
}
}
};
class PdfViewPluginBaseWithScopedLocaleTest
@ -584,89 +565,4 @@ TEST_F(PdfViewPluginBaseWithEngineTest, SelectionChangedScaled) {
EXPECT_EQ(gfx::Point(-300, -56), viewport_info.scroll);
}
TEST_F(PdfViewPluginBaseWithEngineTest, GetContentRestrictions) {
static constexpr int kContentRestrictionCutPaste =
kContentRestrictionCut | kContentRestrictionPaste;
// Test engine without any permissions.
SetEnginePermissions({});
int content_restrictions = fake_plugin_.GetContentRestrictions();
EXPECT_EQ(kContentRestrictionCutPaste | kContentRestrictionCopy |
kContentRestrictionPrint,
content_restrictions);
// Test engine with only copy permission.
SetEnginePermissions({DocumentPermission::kCopy});
content_restrictions = fake_plugin_.GetContentRestrictions();
EXPECT_EQ(kContentRestrictionCutPaste | kContentRestrictionPrint,
content_restrictions);
// Test engine with only print low quality permission.
SetEnginePermissions({DocumentPermission::kPrintLowQuality});
content_restrictions = fake_plugin_.GetContentRestrictions();
EXPECT_EQ(kContentRestrictionCutPaste | kContentRestrictionCopy,
content_restrictions);
// Test engine with both copy and print low quality permissions.
SetEnginePermissions(
{DocumentPermission::kCopy, DocumentPermission::kPrintLowQuality});
content_restrictions = fake_plugin_.GetContentRestrictions();
EXPECT_EQ(kContentRestrictionCutPaste, content_restrictions);
// Test engine with print high and low quality permissions.
SetEnginePermissions({DocumentPermission::kPrintHighQuality,
DocumentPermission::kPrintLowQuality});
content_restrictions = fake_plugin_.GetContentRestrictions();
EXPECT_EQ(kContentRestrictionCutPaste | kContentRestrictionCopy,
content_restrictions);
// Test engine with copy, print high and low quality permissions.
SetEnginePermissions({DocumentPermission::kCopy,
DocumentPermission::kPrintHighQuality,
DocumentPermission::kPrintLowQuality});
content_restrictions = fake_plugin_.GetContentRestrictions();
EXPECT_EQ(kContentRestrictionCutPaste, content_restrictions);
}
TEST_F(PdfViewPluginBaseWithEngineTest, GetAccessibilityDocInfo) {
// Test engine without any permissions.
SetEnginePermissions({});
AccessibilityDocInfo doc_info = fake_plugin_.GetAccessibilityDocInfo();
EXPECT_EQ(TestPDFiumEngine::kPageNumber, doc_info.page_count);
EXPECT_FALSE(doc_info.text_accessible);
EXPECT_FALSE(doc_info.text_copyable);
// Test engine with only copy permission.
SetEnginePermissions({DocumentPermission::kCopy});
doc_info = fake_plugin_.GetAccessibilityDocInfo();
EXPECT_EQ(TestPDFiumEngine::kPageNumber, doc_info.page_count);
EXPECT_FALSE(doc_info.text_accessible);
EXPECT_TRUE(doc_info.text_copyable);
// Test engine with only copy accessible permission.
SetEnginePermissions({DocumentPermission::kCopyAccessible});
doc_info = fake_plugin_.GetAccessibilityDocInfo();
EXPECT_EQ(TestPDFiumEngine::kPageNumber, doc_info.page_count);
EXPECT_TRUE(doc_info.text_accessible);
EXPECT_FALSE(doc_info.text_copyable);
// Test engine with both copy and copy accessible permission.
SetEnginePermissions(
{DocumentPermission::kCopy, DocumentPermission::kCopyAccessible});
doc_info = fake_plugin_.GetAccessibilityDocInfo();
EXPECT_EQ(TestPDFiumEngine::kPageNumber, doc_info.page_count);
EXPECT_TRUE(doc_info.text_accessible);
EXPECT_TRUE(doc_info.text_copyable);
}
} // namespace chrome_pdf

@ -28,6 +28,7 @@
#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/mojom/pdf.mojom.h"
@ -823,6 +824,118 @@ TEST_F(PdfViewWebPluginTest,
plugin_->EnableAccessibility();
}
TEST_F(PdfViewWebPluginTest, GetContentRestrictionsWithNoPermissions) {
EXPECT_EQ(kContentRestrictionCopy | kContentRestrictionCut |
kContentRestrictionPaste | kContentRestrictionPrint,
plugin_->GetContentRestrictions());
}
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());
}
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(