From b8777a2c5f38ea4d6fc74ea01a114d3e056c0fdb Mon Sep 17 00:00:00 2001 From: Dan Sinclair Date: Wed, 20 Sep 2017 16:21:31 -0400 Subject: Add embeddertest for CFWL_Edit This CL adds two mouse selection tests for CFWL_Edit. In order to do so the needed selection code was added to the XFA widget handler and plumbed down to the CFWL_Edit field as needed. Bug: pdfium:840 Change-Id: Ia3b5f5d191494a4579c01524df8fb35b24cc0085 Reviewed-on: https://pdfium-review.googlesource.com/14530 Reviewed-by: Ryan Harrison Commit-Queue: dsinclair --- xfa/fwl/cfwl_edit.cpp | 39 +++++++++++----------- xfa/fwl/cfwl_edit.h | 2 +- xfa/fwl/cfwl_edit_embeddertest.cpp | 67 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 88 insertions(+), 20 deletions(-) create mode 100644 xfa/fwl/cfwl_edit_embeddertest.cpp (limited to 'xfa/fwl') diff --git a/xfa/fwl/cfwl_edit.cpp b/xfa/fwl/cfwl_edit.cpp index 426d46e761..d1f0c99a39 100644 --- a/xfa/fwl/cfwl_edit.cpp +++ b/xfa/fwl/cfwl_edit.cpp @@ -1120,7 +1120,7 @@ void CFWL_Edit::OnProcessMessage(CFWL_Message* pMessage) { OnMouseMove(pMsg); break; case FWL_MouseCommand::RightButtonDown: - DoButtonDown(pMsg); + DoRButtonDown(pMsg); break; default: break; @@ -1159,17 +1159,11 @@ void CFWL_Edit::OnDrawWidget(CXFA_Graphics* pGraphics, DrawWidget(pGraphics, matrix); } -void CFWL_Edit::DoButtonDown(CFWL_MessageMouse* pMsg) { +void CFWL_Edit::DoRButtonDown(CFWL_MessageMouse* pMsg) { if ((m_pProperties->m_dwStates & FWL_WGTSTATE_Focused) == 0) SetFocus(true); - // TODO(dsinclair): Handle DoButtonDown - // bool bBefore = true; - // int32_t nIndex = - // std::max(0, pPage->GetCharIndex(DeviceToEngine(pMsg->m_pos), - // bBefore)); - - // SetCursorPosition(nIndex); + m_CursorPosition = m_EdtEngine.GetIndexForPoint(DeviceToEngine(pMsg->m_pos)); } void CFWL_Edit::OnFocusChanged(CFWL_Message* pMsg, bool bSet) { @@ -1206,7 +1200,9 @@ void CFWL_Edit::OnLButtonDown(CFWL_MessageMouse* pMsg) { m_bLButtonDown = true; SetGrab(true); - DoButtonDown(pMsg); + + if ((m_pProperties->m_dwStates & FWL_WGTSTATE_Focused) == 0) + SetFocus(true); bool bRepaint = false; if (m_EdtEngine.HasSelection()) { @@ -1216,13 +1212,16 @@ void CFWL_Edit::OnLButtonDown(CFWL_MessageMouse* pMsg) { size_t index_at_click = m_EdtEngine.GetIndexForPoint(DeviceToEngine(pMsg->m_pos)); + if (index_at_click != m_CursorPosition && !!(pMsg->m_dwFlags & FWL_KEYFLAG_Shift)) { size_t start = std::min(m_CursorPosition, index_at_click); size_t end = std::max(m_CursorPosition, index_at_click); - m_EdtEngine.SetSelection(start, end); + m_EdtEngine.SetSelection(start, end - start); bRepaint = true; + } else { + m_CursorPosition = index_at_click; } if (bRepaint) @@ -1259,14 +1258,16 @@ void CFWL_Edit::OnMouseMove(CFWL_MessageMouse* pMsg) { if (m_CursorPosition > length) SetCursorPosition(length); - size_t sel_start; - size_t count; - std::tie(sel_start, count) = m_EdtEngine.GetSelection(); - size_t original_end = sel_start + count; - sel_start = std::min(sel_start, m_CursorPosition); - m_EdtEngine.SetSelection( - std::min(sel_start, m_CursorPosition), - std::max(original_end, m_CursorPosition) - sel_start); + size_t sel_start = 0; + size_t count = 0; + if (m_EdtEngine.HasSelection()) + std::tie(sel_start, count) = m_EdtEngine.GetSelection(); + else + sel_start = old_cursor_pos; + + size_t start_pos = std::min(sel_start, m_CursorPosition); + size_t end_pos = std::max(sel_start, m_CursorPosition); + m_EdtEngine.SetSelection(start_pos, end_pos - start_pos); } void CFWL_Edit::OnKeyDown(CFWL_MessageKey* pMsg) { diff --git a/xfa/fwl/cfwl_edit.h b/xfa/fwl/cfwl_edit.h index cfebb25146..b7c252668b 100644 --- a/xfa/fwl/cfwl_edit.h +++ b/xfa/fwl/cfwl_edit.h @@ -142,7 +142,7 @@ class CFWL_Edit : public CFWL_Widget, public CFDE_TextEditEngine::Delegate { void SetCursorPosition(size_t position); void UpdateCursorRect(); - void DoButtonDown(CFWL_MessageMouse* pMsg); + void DoRButtonDown(CFWL_MessageMouse* pMsg); void OnFocusChanged(CFWL_Message* pMsg, bool bSet); void OnLButtonDown(CFWL_MessageMouse* pMsg); void OnLButtonUp(CFWL_MessageMouse* pMsg); diff --git a/xfa/fwl/cfwl_edit_embeddertest.cpp b/xfa/fwl/cfwl_edit_embeddertest.cpp new file mode 100644 index 0000000000..c1575689eb --- /dev/null +++ b/xfa/fwl/cfwl_edit_embeddertest.cpp @@ -0,0 +1,67 @@ +// Copyright 2017 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "core/fxcrt/widestring.h" +#include "public/fpdf_formfill.h" +#include "public/fpdf_fwlevent.h" +#include "testing/embedder_test.h" +#include "testing/gtest/include/gtest/gtest.h" + +class CFWLEditEmbeddertest : public EmbedderTest { + protected: + void SetUp() override { + EmbedderTest::SetUp(); + CreateAndInitializeFormPDF(); + } + + void TearDown() override { + UnloadPage(page()); + EmbedderTest::TearDown(); + } + + void CreateAndInitializeFormPDF() { + EXPECT_TRUE(OpenDocument("xfa/email_recommended.pdf")); + page_ = LoadPage(0); + ASSERT_TRUE(page_); + } + + FPDF_PAGE page() const { return page_; } + + private: + FPDF_PAGE page_; +}; + +TEST_F(CFWLEditEmbeddertest, LeftClickMouseSelection) { + FORM_OnLButtonDown(form_handle(), page(), 0, 115, 58); + for (size_t i = 0; i < 10; ++i) + FORM_OnChar(form_handle(), page(), 'a' + i, 0); + + // Mouse selection + FORM_OnLButtonDown(form_handle(), page(), 0, 128, 58); + FORM_OnLButtonDown(form_handle(), page(), FWL_EVENTFLAG_ShiftKey, 152, 58); + + // 12 == (2 * strlen(defgh)) + 2 (for \0\0) + EXPECT_EQ(12UL, FORM_GetSelectedText(form_handle(), page(), nullptr, 0)); + + unsigned short buf[128]; + unsigned long len = FORM_GetSelectedText(form_handle(), page(), &buf, 128); + EXPECT_STREQ(L"defgh", WideString::FromUTF16LE(buf, len).c_str()); +} + +TEST_F(CFWLEditEmbeddertest, DragMouseSelection) { + FORM_OnLButtonDown(form_handle(), page(), 0, 115, 58); + for (size_t i = 0; i < 10; ++i) + FORM_OnChar(form_handle(), page(), 'a' + i, 0); + + // Mouse selection + FORM_OnLButtonDown(form_handle(), page(), 0, 128, 58); + FORM_OnMouseMove(form_handle(), page(), FWL_EVENTFLAG_ShiftKey, 152, 58); + + // 12 == (2 * strlen(defgh)) + 2 (for \0\0) + EXPECT_EQ(12UL, FORM_GetSelectedText(form_handle(), page(), nullptr, 0)); + + unsigned short buf[128]; + unsigned long len = FORM_GetSelectedText(form_handle(), page(), &buf, 128); + EXPECT_STREQ(L"defgh", WideString::FromUTF16LE(buf, len).c_str()); +} -- cgit v1.2.3