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
|
// 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_finder_manager_base_test.h"
#include <memory>
#include <string>
#include <vector>
#include "base/strings/string_number_conversions.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/render_frame_host.h"
#include "content/public/test/navigation_simulator.h"
#include "content/public/test/web_contents_tester.h"
#include "services/service_manager/public/cpp/interface_provider.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace companion {
namespace {
constexpr char kBaseUrl[] = "https://www.example.com/";
} // namespace
MockAnnotationAgentContainer::MockAnnotationAgentContainer() = default;
MockAnnotationAgentContainer::~MockAnnotationAgentContainer() = default;
void MockAnnotationAgentContainer::MockCreateAgent(
mojo::PendingRemote<blink::mojom::AnnotationAgentHost> host_remote,
mojo::PendingReceiver<blink::mojom::AnnotationAgent> agent_receiver,
blink::mojom::AnnotationType type,
const std::string& serialized_selector) {
return;
}
void MockAnnotationAgentContainer::Bind(mojo::ScopedMessagePipeHandle handle) {
is_bound_ = true;
receiver_.Bind(mojo::PendingReceiver<blink::mojom::AnnotationAgentContainer>(
std::move(handle)));
}
TextFinderManagerBaseTest::TextFinderManagerBaseTest() = default;
TextFinderManagerBaseTest::~TextFinderManagerBaseTest() = default;
void TextFinderManagerBaseTest::SetUp() {
content::RenderViewHostTestHarness::SetUp();
}
void TextFinderManagerBaseTest::TearDown() {
// Owned web contentses must be destroyed before the test harness.
web_contents_list_.clear();
content::RenderViewHostTestHarness::TearDown();
}
std::unique_ptr<content::BrowserContext>
TextFinderManagerBaseTest::CreateBrowserContext() {
return std::make_unique<TestingProfile>();
}
TextFinderManager* TextFinderManagerBaseTest::CreateTextFinderManagerForTest(
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 `TextFinderManager` to the primary page.
content::Page& page = wc->GetPrimaryPage();
TextFinderManager::CreateForPage(page);
TextFinderManager* text_finder_manager = TextFinderManager::GetForPage(page);
DCHECK(text_finder_manager);
web_contents_list_.emplace_back(std::move(wc));
CHECK(!mock_container || mock_container->is_bound());
return text_finder_manager;
}
} // namespace companion
|