0

[unseasoned-pdf] Implement SearchString() in PdfViewWebPlugin

Use RepeatingStringSearch in base::i18n to implement SearchString()
in PdfViewWebPlugin.

Bug: 1199999
Change-Id: Ic476e8ccef3dc9a74ad7fdc706fb804ab1cbccc3
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2877174
Commit-Queue: Ankit Kumar 🌪️ <ankk@microsoft.com>
Reviewed-by: Daniel Hosseinian <dhoss@chromium.org>
Reviewed-by: Lei Zhang <thestig@chromium.org>
Cr-Commit-Position: refs/heads/master@{#886248}
This commit is contained in:
Ankit Kumar 🌪️
2021-05-25 11:55:17 +00:00
committed by Chromium LUCI CQ
parent 796bfde1d3
commit ace80b2186
3 changed files with 40 additions and 1 deletions

@ -331,6 +331,7 @@ if (enable_pdf) {
":internal",
":ppapi_migration",
"//base",
"//base:i18n",
"//cc/paint",
"//content/public/renderer",
"//gin",

@ -12,6 +12,7 @@
#include <vector>
#include "base/check_op.h"
#include "base/i18n/string_search.h"
#include "base/metrics/user_metrics.h"
#include "base/metrics/user_metrics_action.h"
#include "base/no_destructor.h"
@ -400,7 +401,14 @@ std::vector<PDFEngine::Client::SearchStringResult>
PdfViewWebPlugin::SearchString(const char16_t* string,
const char16_t* term,
bool case_sensitive) {
return {};
base::i18n::RepeatingStringSearch searcher(
/*find_this=*/term, /*in_this=*/string, case_sensitive);
std::vector<SearchStringResult> results;
int match_index;
int match_length;
while (searcher.NextMatchResult(match_index, match_length))
results.push_back({.start_index = match_index, .length = match_length});
return results;
}
pp::Instance* PdfViewWebPlugin::GetPluginInstance() {

@ -12,6 +12,7 @@
#include "cc/test/pixel_test_utils.h"
#include "pdf/ppapi_migration/bitmap.h"
#include "pdf/test/test_helpers.h"
#include "testing/gmock/include/gmock/gmock-matchers.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/public/platform/web_string.h"
@ -25,6 +26,8 @@
#include "ui/gfx/geometry/size.h"
#include "ui/gfx/skia_util.h"
using testing::Pointwise;
namespace chrome_pdf {
namespace {
@ -51,6 +54,12 @@ struct PaintParams {
gfx::Rect paint_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;
}
// Generates the expected `SkBitmap` with `paint_color` filled in the expected
// clipped area and `kDefaultColor` as the background color.
SkBitmap GenerateExpectedBitmapForPaint(float device_scale,
@ -321,4 +330,25 @@ TEST_F(PdfViewWebPluginTest, FormTextFieldFocusChangeUpdatesTextInputType) {
wrapper_ptr_->widget_text_input_type());
}
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));
}
}
} // namespace chrome_pdf