summaryrefslogtreecommitdiff
path: root/chromium/components/language/content/browser/ulp_language_code_locator/ulp_language_code_locator_unittest.cc
blob: 3bdc553b0100c81fb64f5335cb989d8cce31ba99 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// Copyright 2018 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.

#include "components/language/content/browser/ulp_language_code_locator/ulp_language_code_locator.h"

#include <bitset>
#include <memory>
#include <string>
#include <vector>

#include "base/logging.h"
#include "components/language/content/browser/ulp_language_code_locator/s2langquadtree.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/s2cellid/src/s2/s2cellid.h"
#include "third_party/s2cellid/src/s2/s2latlng.h"

namespace language {

std::vector<std::unique_ptr<S2LangQuadTreeNode>> GetTestTrees() {
  const std::vector<std::string> languages_rank0{"fr", "en"};
  // |tree_rank0| is a two level quadtree with the second level being all leaves
  // with language indices 0, 0, 0, 1.
  const std::bitset<13> tree_rank0(
      "0111011011010");  // String is in reverse order.

  const std::vector<std::string> languages_rank1{"en", "de"};
  // |tree_rank1| is a two level quadtree with the second level being all leaves
  // with language indices 1, 0, 0, 0.
  const std::bitset<13> tree_rank1(
      "1011011010110");  // String is in reverse order.

  std::vector<std::unique_ptr<S2LangQuadTreeNode>> roots;
  roots.reserve(2);
  roots.push_back(std::make_unique<S2LangQuadTreeNode>(
      S2LangQuadTreeNode::Deserialize(languages_rank0, tree_rank0)));
  roots.push_back(std::make_unique<S2LangQuadTreeNode>(
      S2LangQuadTreeNode::Deserialize(languages_rank1, tree_rank1)));
  return roots;
}

void ExpectLatLngHasLanguages(const UlpLanguageCodeLocator& locator,
                              S2CellId cell,
                              std::vector<std::string> languages_expected) {
  const S2LatLng latlng = cell.ToLatLng();
  const std::vector<std::string> languages =
      locator.GetLanguageCodes(latlng.lat().degrees(), latlng.lng().degrees());
  EXPECT_THAT(languages, ::testing::ElementsAreArray(languages_expected));
}

TEST(UlpLanguageCodeLocatorTest, QuadrantMatchLanguages) {
  std::vector<std::unique_ptr<S2LangQuadTreeNode>> roots = GetTestTrees();
  const UlpLanguageCodeLocator locator(std::move(roots));
  const S2CellId face = S2CellId::FromFace(0);

  ExpectLatLngHasLanguages(locator, face.child(0), {"fr", "de"});
  ExpectLatLngHasLanguages(locator, face.child(1), {"fr", "en"});
  ExpectLatLngHasLanguages(locator, face.child(2), {"fr", "en"});
  ExpectLatLngHasLanguages(locator, face.child(3), {"en", "en"});
}
}  // namespace language