15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2010 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/history/snippet.h"
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <algorithm>
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
92a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/strings/string_split.h"
10868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "base/strings/string_util.h"
11868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "base/strings/utf_string_conversions.h"
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "testing/gtest/include/gtest/gtest.h"
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace {
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// A sample document to compute snippets of.
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// The \x bits after the first "Google" are UTF-8 of U+2122 TRADE MARK SIGN,
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// and are useful for verifying we don't screw up in UTF-8/UTF-16 conversion.
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)const char* kSampleDocument = "Google\xe2\x84\xa2 Terms of Service "
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)"Welcome to Google! "
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)"1. Your relationship with Google "
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)"1.1 Your use of Google's products, software, services and web sites "
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)"(referred to collectively as the \"Services\" in this document and excluding "
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)"any services provided to you by Google under a separate written agreement) "
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)"is subject to the terms of a legal agreement between you and Google. "
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)"\"Google\" means Google Inc., whose principal place of business is at 1600 "
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)"Amphitheatre Parkway, Mountain View, CA 94043, United States. This document "
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)"explains how the agreement is made up, and sets out some of the terms of "
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)"that agreement.";
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Thai sample taken from http://www.google.co.th/intl/th/privacy.html
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// TODO(jungshik) : Add more samples (e.g. Hindi) after porting
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// ICU 4.0's character iterator changes to our copy of ICU 3.8 to get
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// grapheme clusters in Indic scripts handled more reasonably.
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)const char* kThaiSample = "Google \xE0\xB9\x80\xE0\xB8\x81\xE0\xB9\x87"
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)"\xE0\xB8\x9A\xE0\xB8\xA3\xE0\xB8\xA7\xE0\xB8\x9A\xE0\xB8\xA3\xE0\xB8\xA7"
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)"\xE0\xB8\xA1 \xE0\xB8\x82\xE0\xB9\x89\xE0\xB8\xAD\xE0\xB8\xA1\xE0\xB8\xB9"
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)"\xE0\xB8\xA5\xE0\xB8\xAA\xE0\xB9\x88\xE0\xB8\xA7\xE0\xB8\x99\xE0\xB8\x9A"
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)"\xE0\xB8\xB8\xE0\xB8\x84\xE0\xB8\x84\xE0\xB8\xA5 \xE0\xB9\x80\xE0\xB8\xA1"
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)"\xE0\xB8\xB7\xE0\xB9\x88\xE0\xB8\xAD\xE0\xB8\x84\xE0\xB8\xB8\xE0\xB8\x93"
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)"\xE0\xB8\xA5\xE0\xB8\x87\xE0\xB8\x97\xE0\xB8\xB0\xE0\xB9\x80\xE0\xB8\x9A"
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)"\xE0\xB8\xB5\xE0\xB8\xA2\xE0\xB8\x99\xE0\xB9\x80\xE0\xB8\x9E\xE0\xB8\xB7"
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)"\xE0\xB9\x88\xE0\xB8\xAD\xE0\xB9\x83\xE0\xB8\x8A\xE0\xB9\x89\xE0\xB8\x9A"
455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)"\xE0\xB8\xA3\xE0\xB8\xB4\xE0\xB8\x81\xE0\xB8\xB2\xE0\xB8\xA3\xE0\xB8\x82"
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)"\xE0\xB8\xAD\xE0\xB8\x87 Google \xE0\xB8\xAB\xE0\xB8\xA3\xE0\xB8\xB7"
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)"\xE0\xB8\xAD\xE0\xB9\x83\xE0\xB8\xAB\xE0\xB9\x89\xE0\xB8\x82\xE0\xB9\x89"
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)"\xE0\xB8\xAD\xE0\xB8\xA1\xE0\xB8\xB9\xE0\xB8\xA5\xE0\xB8\x94\xE0\xB8\xB1"
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)"\xE0\xB8\x87\xE0\xB8\x81\xE0\xB8\xA5\xE0\xB9\x88\xE0\xB8\xB2\xE0\xB8\xA7"
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)"\xE0\xB9\x82\xE0\xB8\x94\xE0\xB8\xA2\xE0\xB8\xAA\xE0\xB8\xA1\xE0\xB8\xB1"
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)"\xE0\xB8\x84\xE0\xB8\xA3\xE0\xB9\x83\xE0\xB8\x88 \xE0\xB9\x80\xE0\xB8\xA3"
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)"\xE0\xB8\xB2\xE0\xB8\xAD\xE0\xB8\xB2\xE0\xB8\x88\xE0\xB8\xA3\xE0\xB8\xA7"
535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)"\xE0\xB8\xA1\xE0\xB8\x82\xE0\xB9\x89\xE0\xB8\xAD\xE0\xB8\xA1\xE0\xB8\xB9"
545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)"\xE0\xB8\xA5\xE0\xB8\xAA\xE0\xB9\x88\xE0\xB8\xA7\xE0\xB8\x99\xE0\xB8\x9A"
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)"\xE0\xB8\xB8\xE0\xB8\x84\xE0\xB8\x84\xE0\xB8\xA5\xE0\xB8\x97\xE0\xB8\xB5"
565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)"\xE0\xB9\x88\xE0\xB9\x80\xE0\xB8\x81\xE0\xB9\x87\xE0\xB8\x9A\xE0\xB8\xA3"
575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)"\xE0\xB8\xA7\xE0\xB8\x9A\xE0\xB8\xA3\xE0\xB8\xA7\xE0\xB8\xA1\xE0\xB8\x88"
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)"\xE0\xB8\xB2\xE0\xB8\x81\xE0\xB8\x84\xE0\xB8\xB8\xE0\xB8\x93\xE0\xB9\x80"
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)"\xE0\xB8\x82\xE0\xB9\x89\xE0\xB8\xB2\xE0\xB8\x81\xE0\xB8\xB1\xE0\xB8\x9A"
605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)"\xE0\xB8\x82\xE0\xB9\x89\xE0\xB8\xAD\xE0\xB8\xA1\xE0\xB8\xB9\xE0\xB8\xA5"
615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)"\xE0\xB8\x88\xE0\xB8\xB2\xE0\xB8\x81\xE0\xB8\x9A\xE0\xB8\xA3\xE0\xB8\xB4"
625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)"\xE0\xB8\x81\xE0\xB8\xB2\xE0\xB8\xA3\xE0\xB8\xAD\xE0\xB8\xB7\xE0\xB9\x88"
635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)"\xE0\xB8\x99\xE0\xB8\x82\xE0\xB8\xAD\xE0\xB8\x87 Google \xE0\xB8\xAB"
645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)"\xE0\xB8\xA3\xE0\xB8\xB7\xE0\xB8\xAD\xE0\xB8\x9A\xE0\xB8\xB8\xE0\xB8\x84"
655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)"\xE0\xB8\x84\xE0\xB8\xA5\xE0\xB8\x97\xE0\xB8\xB5\xE0\xB9\x88\xE0\xB8\xAA"
665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)"\xE0\xB8\xB2\xE0\xB8\xA1 \xE0\xB9\x80\xE0\xB8\x9E\xE0\xB8\xB7\xE0\xB9\x88"
675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)"\xE0\xB8\xAD\xE0\xB9\x83\xE0\xB8\xAB\xE0\xB9\x89\xE0\xB8\x9C\xE0\xB8\xB9"
685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)"\xE0\xB9\x89\xE0\xB9\x83\xE0\xB8\x8A\xE0\xB9\x89\xE0\xB9\x84\xE0\xB8\x94"
695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)"\xE0\xB9\x89\xE0\xB8\xA3\xE0\xB8\xB1\xE0\xB8\x9A\xE0\xB8\x9B\xE0\xB8\xA3"
705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)"\xE0\xB8\xB0\xE0\xB8\xAA\xE0\xB8\x9A\xE0\xB8\x81\xE0\xB8\xB2\xE0\xB8\xA3"
715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)"\xE0\xB8\x93\xE0\xB9\x8C\xE0\xB8\x97\xE0\xB8\xB5\xE0\xB9\x88\xE0\xB8\x94"
725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)"\xE0\xB8\xB5\xE0\xB8\x82\xE0\xB8\xB6\xE0\xB9\x89\xE0\xB8\x99 \xE0\xB8\xA3"
735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)"\xE0\xB8\xA7\xE0\xB8\xA1\xE0\xB8\x97\xE0\xB8\xB1\xE0\xB9\x89\xE0\xB8\x87"
745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)"\xE0\xB8\x9B\xE0\xB8\xA3\xE0\xB8\xB1\xE0\xB8\x9A\xE0\xB9\x81\xE0\xB8\x95"
755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)"\xE0\xB9\x88\xE0\xB8\x87\xE0\xB9\x80\xE0\xB8\x99\xE0\xB8\xB7\xE0\xB9\x89"
765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)"\xE0\xB8\xAD\xE0\xB8\xAB\xE0\xB8\xB2\xE0\xB9\x83\xE0\xB8\xAB\xE0\xB9\x89"
775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)"\xE0\xB9\x80\xE0\xB8\xAB\xE0\xB8\xA1\xE0\xB8\xB2\xE0\xB8\xB0\xE0\xB8\xAA"
785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)"\xE0\xB8\xB3\xE0\xB8\xAB\xE0\xB8\xA3\xE0\xB8\xB1\xE0\xB8\x9A\xE0\xB8\x84"
795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)"\xE0\xB8\xB8\xE0\xB8\x93";
805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Comparator for sorting by the first element in a pair.
825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool ComparePair1st(const Snippet::MatchPosition& a,
835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                    const Snippet::MatchPosition& b) {
845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return a.first < b.first;
855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// For testing, we'll compute the match positions manually instead of using
885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// sqlite's FTS matching.  BuildSnippet returns the snippet for matching
895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// |query| against |document|.  Matches are surrounded by "**".
90d57369da7c6519fef57db42085f7b42d4c8845c1Torne (Richard Coles)base::string16 BuildSnippet(const std::string& document,
915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                      const std::string& query) {
925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // This function assumes that |document| does not contain
935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // any character for which lowercasing changes its length. Further,
945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // it's assumed that lowercasing only the ASCII-portion works for
955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // |document|. We need to add more test cases and change this function
965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // to be more generic depending on how we deal with 'folding for match'
975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // in history.
985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const std::string document_folded = StringToLowerASCII(std::string(document));
995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::vector<std::string> query_words;
1015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  base::SplitString(query, ' ', &query_words);
1025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Manually construct match_positions of the document.
1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  Snippet::MatchPositions match_positions;
1055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  match_positions.clear();
1065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  for (std::vector<std::string>::iterator qw = query_words.begin();
1075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)       qw != query_words.end(); ++qw) {
1085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // Insert all instances of this word into match_pairs.
1095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    size_t ofs = 0;
1105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    while ((ofs = document_folded.find(*qw, ofs)) != std::string::npos) {
1115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      match_positions.push_back(std::make_pair(ofs, ofs + qw->size()));
1125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      ofs += qw->size();
1135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
1145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Sort match_positions in order of increasing offset.
1165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::sort(match_positions.begin(), match_positions.end(), ComparePair1st);
1175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Compute the snippet.
1195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  Snippet snippet;
1205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  snippet.ComputeSnippet(match_positions, document);
1215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Now "highlight" all matches in the snippet with **.
123a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  base::string16 star_snippet;
1245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  Snippet::MatchPositions::const_iterator match;
1255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  size_t pos = 0;
1265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  for (match = snippet.matches().begin();
1275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)       match != snippet.matches().end(); ++match) {
1285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    star_snippet += snippet.text().substr(pos, match->first - pos);
1295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    star_snippet += UTF8ToUTF16("**");
1305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    star_snippet += snippet.text().substr(match->first,
1315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                          match->second - match->first);
1325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    star_snippet += UTF8ToUTF16("**");
1335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    pos = match->second;
1345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  star_snippet += snippet.text().substr(pos);
1365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return star_snippet;
1385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)TEST(Snippets, SimpleQuery) {
1415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ASSERT_EQ(" ... eferred to collectively as the \"Services\" in this "
1425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            "**document** and excluding any services provided to you by "
1435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            "Goo ...  ... way, Mountain View, CA 94043, United States. This "
1445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            "**document** explains how the agreement is made up, and sets "
1455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            "o ... ",
1465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            UTF16ToUTF8(BuildSnippet(kSampleDocument, "document")));
1475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Test that two words that are near each other don't produce two elided bits.
1505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)TEST(Snippets, NearbyWords) {
1515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ASSERT_EQ(" ... lace of business is at 1600 Amphitheatre Parkway, "
1525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            "**Mountain** **View**, CA 94043, United States. This "
1535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            "document explains  ... ",
1545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            UTF16ToUTF8(BuildSnippet(kSampleDocument, "mountain view")));
1555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// The above tests already test that we get byte offsets correct, but here's
1585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// one that gets the "TM" in its snippet.
1595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)TEST(Snippets, UTF8) {
1605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ASSERT_EQ(" ... ogle\xe2\x84\xa2 Terms of Service Welcome to Google! "
1615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            "1. Your **relationship** with Google 1.1 Your use of Google's "
1625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            "products, so ... ",
1635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            UTF16ToUTF8(BuildSnippet(kSampleDocument, "relationship")));
1645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)TEST(Snippets, ThaiUTF8) {
1675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // There are 3 instances of '\u0E43\u0E2B\u0E49'
1685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // (\xE0\xB9\x83\xE0\xB8\xAB\xE0\xB9\x89) in kThaiSample.
1695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The 1st is more than |kSniipetContext| graphemes away from the
1705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // 2nd while the 2nd and 3rd are within that window. However, with
1715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // the 2nd match added, the snippet goes over the size limit so that
1725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // the snippet ends right before the 3rd match.
1735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ASSERT_EQ(" ... "
1745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            "\xE0\xB8\xA3\xE0\xB8\xA7\xE0\xB8\x9A\xE0\xB8\xA3\xE0\xB8\xA7"
1755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            "\xE0\xB8\xA1 \xE0\xB8\x82\xE0\xB9\x89\xE0\xB8\xAD\xE0\xB8"
1765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            "\xA1\xE0\xB8\xB9\xE0\xB8\xA5\xE0\xB8\xAA\xE0\xB9\x88\xE0\xB8"
1775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            "\xA7\xE0\xB8\x99\xE0\xB8\x9A\xE0\xB8\xB8\xE0\xB8\x84\xE0\xB8"
1785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            "\x84\xE0\xB8\xA5 \xE0\xB9\x80\xE0\xB8\xA1\xE0\xB8\xB7\xE0"
1795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            "\xB9\x88\xE0\xB8\xAD\xE0\xB8\x84\xE0\xB8\xB8\xE0\xB8\x93\xE0"
1805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            "\xB8\xA5\xE0\xB8\x87\xE0\xB8\x97\xE0\xB8\xB0\xE0\xB9\x80\xE0"
1815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            "\xB8\x9A\xE0\xB8\xB5\xE0\xB8\xA2\xE0\xB8\x99\xE0\xB9\x80\xE0"
1825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            "\xB8\x9E\xE0\xB8\xB7\xE0\xB9\x88\xE0\xB8\xAD\xE0\xB9\x83\xE0"
1835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            "\xB8\x8A\xE0\xB9\x89\xE0\xB8\x9A\xE0\xB8\xA3\xE0\xB8\xB4\xE0"
1845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            "\xB8\x81\xE0\xB8\xB2\xE0\xB8\xA3\xE0\xB8\x82\xE0\xB8\xAD\xE0"
1855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            "\xB8\x87 Google \xE0\xB8\xAB\xE0\xB8\xA3\xE0\xB8\xB7\xE0\xB8"
1865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            "\xAD**\xE0\xB9\x83\xE0\xB8\xAB\xE0\xB9\x89**\xE0\xB8\x82\xE0"
1875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            "\xB9\x89\xE0\xB8\xAD\xE0\xB8\xA1\xE0\xB8\xB9\xE0\xB8\xA5\xE0"
1885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            "\xB8\x94\xE0\xB8\xB1\xE0\xB8\x87\xE0\xB8\x81\xE0\xB8\xA5\xE0"
1895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            "\xB9\x88\xE0\xB8\xB2\xE0\xB8\xA7\xE0\xB9\x82\xE0\xB8\x94\xE0"
1905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            "\xB8\xA2\xE0\xB8\xAA\xE0\xB8\xA1\xE0\xB8\xB1\xE0\xB8\x84\xE0"
1915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            "\xB8\xA3\xE0\xB9\x83\xE0\xB8\x88 \xE0\xB9\x80\xE0\xB8\xA3"
1925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            "\xE0\xB8\xB2\xE0\xB8\xAD\xE0\xB8\xB2\xE0\xB8\x88\xE0\xB8\xA3"
1935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            "\xE0\xB8\xA7\xE0\xB8\xA1\xE0\xB8\x82\xE0\xB9\x89\xE0\xB8\xAD"
1945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            "\xE0\xB8\xA1\xE0\xB8\xB9\xE0\xB8\xA5\xE0\xB8\xAA\xE0\xB9\x88"
1955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            "\xE0\xB8\xA7\xE0\xB8\x99\xE0\xB8\x9A\xE0\xB8\xB8\xE0\xB8\x84"
1965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            "\xE0\xB8\x84\xE0\xB8\xA5\xE0\xB8\x97\xE0\xB8\xB5\xE0\xB9\x88"
1975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            "\xE0\xB9\x80\xE0\xB8\x81\xE0\xB9\x87\xE0\xB8\x9A\xE0\xB8\xA3"
1985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            "\xE0\xB8\xA7\xE0\xB8\x9A\xE0\xB8\xA3\xE0\xB8\xA7\xE0\xB8\xA1"
1995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            "\xE0\xB8\x88\xE0\xB8\xB2\xE0\xB8\x81\xE0\xB8\x84\xE0\xB8\xB8"
2005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            "\xE0\xB8\x93\xE0\xB9\x80\xE0\xB8\x82\xE0\xB9\x89\xE0\xB8\xB2"
2015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            "\xE0\xB8\x81\xE0\xB8\xB1\xE0\xB8\x9A ...  ... \xE0\xB8\x82"
2025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            "\xE0\xB9\x89\xE0\xB8\xAD\xE0\xB8\xA1\xE0\xB8\xB9\xE0\xB8\xA5"
2035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            "\xE0\xB8\x88\xE0\xB8\xB2\xE0\xB8\x81\xE0\xB8\x9A\xE0\xB8\xA3"
2045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            "\xE0\xB8\xB4\xE0\xB8\x81\xE0\xB8\xB2\xE0\xB8\xA3\xE0\xB8\xAD"
2055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            "\xE0\xB8\xB7\xE0\xB9\x88\xE0\xB8\x99\xE0\xB8\x82\xE0\xB8\xAD"
2065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            "\xE0\xB8\x87 Google \xE0\xB8\xAB\xE0\xB8\xA3\xE0\xB8\xB7\xE0"
2075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            "\xB8\xAD\xE0\xB8\x9A\xE0\xB8\xB8\xE0\xB8\x84\xE0\xB8\x84\xE0"
2085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            "\xB8\xA5\xE0\xB8\x97\xE0\xB8\xB5\xE0\xB9\x88\xE0\xB8\xAA\xE0"
2095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            "\xB8\xB2\xE0\xB8\xA1 \xE0\xB9\x80\xE0\xB8\x9E\xE0\xB8\xB7"
2105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            "\xE0\xB9\x88\xE0\xB8\xAD**\xE0\xB9\x83\xE0\xB8\xAB\xE0\xB9"
2115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            "\x89**\xE0\xB8\x9C\xE0\xB8\xB9\xE0\xB9\x89\xE0\xB9\x83\xE0"
2125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            "\xB8\x8A\xE0\xB9\x89\xE0\xB9\x84\xE0\xB8\x94\xE0\xB9\x89\xE0"
2135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            "\xB8\xA3\xE0\xB8\xB1\xE0\xB8\x9A\xE0\xB8\x9B\xE0\xB8\xA3\xE0"
2145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            "\xB8\xB0\xE0\xB8\xAA\xE0\xB8\x9A\xE0\xB8\x81\xE0\xB8\xB2\xE0"
2155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            "\xB8\xA3\xE0\xB8\x93\xE0\xB9\x8C\xE0\xB8\x97\xE0\xB8\xB5\xE0"
2165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            "\xB9\x88\xE0\xB8\x94\xE0\xB8\xB5\xE0\xB8\x82\xE0\xB8\xB6\xE0"
2175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            "\xB9\x89\xE0\xB8\x99 \xE0\xB8\xA3\xE0\xB8\xA7\xE0\xB8\xA1"
2185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            "\xE0\xB8\x97\xE0\xB8\xB1\xE0\xB9\x89\xE0\xB8\x87\xE0\xB8\x9B"
2195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            "\xE0\xB8\xA3\xE0\xB8\xB1\xE0\xB8\x9A\xE0\xB9\x81\xE0\xB8\x95"
2205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            "\xE0\xB9\x88\xE0\xB8\x87\xE0\xB9\x80\xE0\xB8\x99\xE0\xB8\xB7"
2215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            "\xE0\xB9\x89\xE0\xB8\xAD\xE0\xB8\xAB\xE0\xB8\xB2",
2225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            UTF16ToUTF8(BuildSnippet(kThaiSample,
2235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                     "\xE0\xB9\x83\xE0\xB8\xAB\xE0\xB9\x89")));
2245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)TEST(Snippets, ExtractMatchPositions) {
2275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  struct TestData {
2285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const std::string offsets_string;
2295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const size_t expected_match_count;
2305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const size_t expected_matches[10];
2315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  } data[] = {
2325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    { "0 0 1 2 0 0 4 1 0 0 1 5",            1,     { 1, 6 } },
2335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    { "0 0 1 4 0 0 2 1",                    1,     { 1, 5 } },
2345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    { "0 0 4 1 0 0 2 1",                    2,     { 2, 3, 4, 5 } },
2355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    { "0 0 0 1",                            1,     { 0, 1 } },
2365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    { "0 0 0 1 0 0 0 2",                    1,     { 0, 2 } },
2375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    { "0 0 1 1 0 0 1 2",                    1,     { 1, 3 } },
2385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    { "0 0 1 2 0 0 4 3 0 0 3 1",            1,     { 1, 7 } },
2395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    { "0 0 1 4 0 0 2 5",                    1,     { 1, 7 } },
2405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    { "0 0 1 2 0 0 1 1",                    1,     { 1, 3 } },
2415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    { "0 0 1 1 0 0 5 2 0 0 10 1 0 0 3 10",  2,     { 1, 2, 3, 13 } },
2425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  };
2435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  for (size_t i = 0; i < ARRAYSIZE_UNSAFE(data); ++i) {
2445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    Snippet::MatchPositions matches;
2455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    Snippet::ExtractMatchPositions(data[i].offsets_string, "0", &matches);
2465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    EXPECT_EQ(data[i].expected_match_count, matches.size());
2475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    for (size_t j = 0; j < data[i].expected_match_count; ++j) {
2485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      EXPECT_EQ(data[i].expected_matches[2 * j], matches[j].first);
2495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      EXPECT_EQ(data[i].expected_matches[2 * j + 1], matches[j].second);
2505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
2515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
2525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
253