File: text_highlighter_manager_unittest.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (126 lines) | stat: -rw-r--r-- 4,968 bytes parent folder | download | duplicates (3)
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/companion/text_finder/text_highlighter_manager.h"

#include "base/strings/string_number_conversions.h"
#include "chrome/browser/companion/text_finder/text_finder_manager_base_test.h"
#include "chrome/browser/companion/text_finder/text_highlighter.h"
#include "chrome/test/base/testing_profile.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/page.h"
#include "content/public/browser/web_contents.h"
#include "content/public/test/navigation_simulator.h"
#include "content/public/test/test_renderer_host.h"
#include "content/public/test/web_contents_tester.h"
#include "mojo/public/cpp/bindings/receiver.h"
#include "services/service_manager/public/cpp/interface_provider.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/public/mojom/annotation/annotation.mojom.h"
#include "ui/gfx/geometry/rect.h"

namespace companion {

namespace {

constexpr char kBaseUrl[] = "https://www.example.com/";

}  // namespace

class TextHighlighterManagerTest : public content::RenderViewHostTestHarness {
 public:
  TextHighlighterManagerTest() = default;
  ~TextHighlighterManagerTest() override = default;

 protected:
  // Implements `content::RenderViewHostTestHarness`.
  void SetUp() override { content::RenderViewHostTestHarness::SetUp(); }

  void TearDown() override {
    // Owned web contentses must be destroyed before the test harness.
    web_contents_list_.clear();
    content::RenderViewHostTestHarness::TearDown();
  }

  std::unique_ptr<content::BrowserContext> CreateBrowserContext() override {
    return std::make_unique<TestingProfile>();
  }

  // Creates a new manager attached to mock pages.
  TextHighlighterManager* CreateManagerForTest(
      MockAnnotationAgentContainer* mock_container) {
    // Create a test frame and navigate it to a unique URL.
    std::unique_ptr<content::WebContents> wc = CreateTestWebContents();
    content::RenderFrameHostTester::For(wc->GetPrimaryMainFrame())
        ->InitializeRenderFrameIfNeeded();
    content::NavigationSimulator::NavigateAndCommitFromBrowser(
        wc.get(),
        GURL(kBaseUrl + base::NumberToString(web_contents_list_.size())));

    std::unique_ptr<service_manager::InterfaceProvider::TestApi> test_api;
    if (mock_container) {
      CHECK(!mock_container->is_bound());
      test_api = std::make_unique<service_manager::InterfaceProvider::TestApi>(
          wc->GetPrimaryMainFrame()->GetRemoteInterfaces());
      test_api->SetBinderForName(
          blink::mojom::AnnotationAgentContainer::Name_,
          base::BindRepeating(&MockAnnotationAgentContainer::Bind,
                              base::Unretained(mock_container)));
    }

    // Create and attach a `TextHighlighterManager` to the primary page.
    content::Page& page = wc->GetPrimaryPage();
    TextHighlighterManager::CreateForPage(page);
    TextHighlighterManager* manager = TextHighlighterManager::GetForPage(page);
    DCHECK(manager);
    web_contents_list_.emplace_back(std::move(wc));

    CHECK(!mock_container || mock_container->is_bound());

    return manager;
  }

  std::vector<std::unique_ptr<content::WebContents>> web_contents_list_;
};

TEST_F(TextHighlighterManagerTest, TextHighlighterTest) {
  // Set up a manager bound to the mock agent container.
  MockAnnotationAgentContainer mock_agent_container;
  TextHighlighterManager* manager = CreateManagerForTest(&mock_agent_container);

  // Create a new text highlighter.
  const std::string text_directive_1 = "ab,cd";
  manager->CreateTextHighlighterAndRemoveExistingInstance(text_directive_1);
  EXPECT_THAT(
      manager->get_text_highlighters_for_testing()[0]->GetTextDirective(),
      text_directive_1);

  // Create a second instance and automatically delete the existing one.
  const std::string text_directive_2 = "bcd";
  manager->CreateTextHighlighterAndRemoveExistingInstance(text_directive_2);
  EXPECT_THAT(
      manager->get_text_highlighters_for_testing()[0]->GetTextDirective(),
      text_directive_2);
}

TEST_F(TextHighlighterManagerTest, MultiTextHighlighters) {
  // Set up a manager bound to the mock agent container.
  MockAnnotationAgentContainer mock_agent_container;
  TextHighlighterManager* manager = CreateManagerForTest(&mock_agent_container);

  // Create two new text highlighters.
  const std::string text_directive_1 = "ab,cd";
  const std::string text_directive_2 = "bcd";
  manager->CreateTextHighlightersAndRemoveExisting(
      {text_directive_1, text_directive_2});
  EXPECT_THAT(
      manager->get_text_highlighters_for_testing()[0]->GetTextDirective(),
      text_directive_1);
  EXPECT_THAT(
      manager->get_text_highlighters_for_testing()[1]->GetTextDirective(),
      text_directive_2);
}

}  // namespace companion