Skip to content

Commit

Permalink
[unseasoned-pdf] Implement SearchString() in PdfViewWebPlugin
Browse files Browse the repository at this point in the history
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}
  • Loading branch information
ankk0294 authored and Chromium LUCI CQ committed May 25, 2021
1 parent 796bfde commit ace80b2
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 1 deletion.
1 change: 1 addition & 0 deletions pdf/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,7 @@ if (enable_pdf) {
":internal",
":ppapi_migration",
"//base",
"//base:i18n",
"//cc/paint",
"//content/public/renderer",
"//gin",
Expand Down
10 changes: 9 additions & 1 deletion pdf/pdf_view_web_plugin.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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() {
Expand Down
30 changes: 30 additions & 0 deletions pdf/pdf_view_web_plugin_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -25,6 +26,8 @@
#include "ui/gfx/geometry/size.h"
#include "ui/gfx/skia_util.h"

using testing::Pointwise;

namespace chrome_pdf {

namespace {
Expand All @@ -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,
Expand Down Expand Up @@ -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

0 comments on commit ace80b2

Please sign in to comment.