PDF: Prevent left/right arrow shortcuts when a PDF text field is focused.
This fixes an issue where pressing left or right while editing a form would change page as well as move the caret. BUG=490004 Review URL: https://codereview.chromium.org/1359613002 Cr-Commit-Position: refs/heads/master@{#350322}
This commit is contained in:
chrome
pdf
@ -334,6 +334,10 @@ IN_PROC_BROWSER_TEST_F(PDFExtensionTest, WhitespaceTitle) {
|
||||
RunTestsInFile("whitespace_title_test.js", "test-whitespace-title.pdf");
|
||||
}
|
||||
|
||||
IN_PROC_BROWSER_TEST_F(PDFExtensionTest, PageChange) {
|
||||
RunTestsInFile("page_change_test.js", "test-bookmarks.pdf");
|
||||
}
|
||||
|
||||
// Ensure that the internal PDF plugin application/x-google-chrome-pdf won't be
|
||||
// loaded if it's not loaded in the chrome extension page.
|
||||
IN_PROC_BROWSER_TEST_F(PDFExtensionTest, EnsureInternalPluginDisabled) {
|
||||
@ -428,3 +432,7 @@ IN_PROC_BROWSER_TEST_F(MaterialPDFExtensionTest, Title) {
|
||||
IN_PROC_BROWSER_TEST_F(MaterialPDFExtensionTest, WhitespaceTitle) {
|
||||
RunTestsInFile("whitespace_title_test.js", "test-whitespace-title.pdf");
|
||||
}
|
||||
|
||||
IN_PROC_BROWSER_TEST_F(MaterialPDFExtensionTest, PageChange) {
|
||||
RunTestsInFile("page_change_test.js", "test-bookmarks.pdf");
|
||||
}
|
||||
|
@ -115,6 +115,7 @@ function PDFViewer(browserApi) {
|
||||
this.loadState_ = LoadState.LOADING;
|
||||
this.parentWindow_ = null;
|
||||
this.parentOrigin_ = null;
|
||||
this.isFormFieldFocused_ = false;
|
||||
|
||||
this.delayedScriptingMessages_ = [];
|
||||
|
||||
@ -325,8 +326,10 @@ PDFViewer.prototype = {
|
||||
return;
|
||||
case 37: // Left arrow key.
|
||||
if (!(e.altKey || e.ctrlKey || e.metaKey || e.shiftKey)) {
|
||||
// Go to the previous page if there are no horizontal scrollbars.
|
||||
if (!this.viewport_.documentHasScrollbars().horizontal) {
|
||||
// Go to the previous page if there are no horizontal scrollbars and
|
||||
// no form field is focused.
|
||||
if (!(this.viewport_.documentHasScrollbars().horizontal ||
|
||||
this.isFormFieldFocused_)) {
|
||||
this.viewport_.goToPage(this.viewport_.getMostVisiblePage() - 1);
|
||||
// Since we do the movement of the page.
|
||||
e.preventDefault();
|
||||
@ -344,8 +347,10 @@ PDFViewer.prototype = {
|
||||
return;
|
||||
case 39: // Right arrow key.
|
||||
if (!(e.altKey || e.ctrlKey || e.metaKey || e.shiftKey)) {
|
||||
// Go to the next page if there are no horizontal scrollbars.
|
||||
if (!this.viewport_.documentHasScrollbars().horizontal) {
|
||||
// Go to the next page if there are no horizontal scrollbars and no
|
||||
// form field is focused.
|
||||
if (!(this.viewport_.documentHasScrollbars().horizontal ||
|
||||
this.isFormFieldFocused_)) {
|
||||
this.viewport_.goToPage(this.viewport_.getMostVisiblePage() + 1);
|
||||
// Since we do the movement of the page.
|
||||
e.preventDefault();
|
||||
@ -673,6 +678,9 @@ PDFViewer.prototype = {
|
||||
this.paramsParser_.onNamedDestinationReceived(
|
||||
message.data.pageNumber);
|
||||
break;
|
||||
case 'formFocusChange':
|
||||
this.isFormFieldFocused_ = message.data.focused;
|
||||
break;
|
||||
}
|
||||
},
|
||||
|
||||
|
74
chrome/test/data/pdf/page_change_test.js
Normal file
74
chrome/test/data/pdf/page_change_test.js
Normal file
@ -0,0 +1,74 @@
|
||||
// Copyright 2015 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.
|
||||
|
||||
function resetDocument() {
|
||||
window.viewer.viewport.goToPage(0);
|
||||
window.viewer.viewport.setZoom(1);
|
||||
window.viewer.isFormFieldFocused_ = false;
|
||||
}
|
||||
|
||||
function getCurrentPage() {
|
||||
return window.viewer.viewport.getMostVisiblePage();
|
||||
}
|
||||
|
||||
var tests = [
|
||||
/**
|
||||
* Test that the left/right arrows change page back and forth.
|
||||
*/
|
||||
function testPageChangesWithArrows() {
|
||||
// Right arrow -> Go to page 2.
|
||||
MockInteractions.pressAndReleaseKeyOn(document, 39);
|
||||
chrome.test.assertEq(1, getCurrentPage());
|
||||
|
||||
// Left arrow -> Back to page 1.
|
||||
MockInteractions.pressAndReleaseKeyOn(document, 37);
|
||||
chrome.test.assertEq(0, getCurrentPage());
|
||||
|
||||
resetDocument();
|
||||
chrome.test.succeed();
|
||||
},
|
||||
|
||||
/**
|
||||
* Test that when a PDF form field is focused, the left/right shortcuts are
|
||||
* disabled. This doesn't test the plugin side of this feature.
|
||||
*/
|
||||
function testPageDoesntChangeWhenFormFocused() {
|
||||
// This should be set by a message from plugin -> page when a field is
|
||||
// focused.
|
||||
window.viewer.isFormFieldFocused_ = true;
|
||||
|
||||
// Page should not change when left/right are pressed.
|
||||
MockInteractions.pressAndReleaseKeyOn(document, 39);
|
||||
chrome.test.assertEq(0, getCurrentPage());
|
||||
|
||||
MockInteractions.pressAndReleaseKeyOn(document, 37);
|
||||
chrome.test.assertEq(0, getCurrentPage());
|
||||
|
||||
resetDocument();
|
||||
chrome.test.succeed();
|
||||
},
|
||||
|
||||
/**
|
||||
* Test that when the document is in fit to page, pressing page up/page down
|
||||
* changes page back/forth.
|
||||
*/
|
||||
function testPageDownInFitPage() {
|
||||
window.viewer.viewport.fitToPage();
|
||||
|
||||
// Page down -> Go to page 2.
|
||||
MockInteractions.pressAndReleaseKeyOn(document, 34);
|
||||
chrome.test.assertEq(1, getCurrentPage());
|
||||
|
||||
// Page up -> Back to page 1.
|
||||
MockInteractions.pressAndReleaseKeyOn(document, 33);
|
||||
chrome.test.assertEq(0, getCurrentPage());
|
||||
|
||||
resetDocument();
|
||||
chrome.test.succeed();
|
||||
}
|
||||
];
|
||||
|
||||
importTestHelpers().then(function() {
|
||||
chrome.test.runTests(tests);
|
||||
});
|
@ -144,6 +144,10 @@ const char kJSNamedDestinationPageNumber[] = "pageNumber";
|
||||
const char kJSSetIsSelectingType[] = "setIsSelecting";
|
||||
const char kJSIsSelecting[] = "isSelecting";
|
||||
|
||||
// Notify when a form field is focused (Plugin -> Page)
|
||||
const char kJSFieldFocusType[] = "formFocusChange";
|
||||
const char kJSFieldFocus[] = "focused";
|
||||
|
||||
const int kFindResultCooldownMs = 100;
|
||||
|
||||
const double kMinZoom = 0.01;
|
||||
@ -1264,6 +1268,12 @@ void OutOfProcessInstance::DocumentLoadProgress(uint32 available,
|
||||
void OutOfProcessInstance::FormTextFieldFocusChange(bool in_focus) {
|
||||
if (!text_input_.get())
|
||||
return;
|
||||
|
||||
pp::VarDictionary message;
|
||||
message.Set(pp::Var(kType), pp::Var(kJSFieldFocusType));
|
||||
message.Set(pp::Var(kJSFieldFocus), pp::Var(in_focus));
|
||||
PostMessage(message);
|
||||
|
||||
if (in_focus)
|
||||
text_input_->SetTextInputType(PP_TEXTINPUT_TYPE_DEV_TEXT);
|
||||
else
|
||||
|
Reference in New Issue
Block a user