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 127 128 129 130 131 132 133 134 135 136 137 138 139
|
// Copyright 2025 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/performance_manager/policies/keep_alive_dse_policy.h"
#include "chrome/browser/search_engines/template_url_service_factory.h"
#include "chrome/test/base/chrome_render_view_host_test_harness.h"
#include "components/performance_manager/public/performance_manager.h"
#include "components/performance_manager/test_support/test_harness_helper.h"
#include "components/search_engines/template_url_data.h"
#include "components/search_engines/template_url_service.h"
#include "content/public/browser/web_contents.h"
#include "content/public/test/mock_render_process_host.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace performance_manager::policies {
namespace {
const char* kDSEUrl = "https://foo.com/search?q={searchTerms}";
const char* kNewDSEUrl = "https://bar.com/search?q={searchTerms}";
const char* kNotADSEUrl = "https://not-a-dse.com";
} // namespace
class KeepAliveDSEPolicyTest : public ChromeRenderViewHostTestHarness {
public:
KeepAliveDSEPolicyTest() = default;
~KeepAliveDSEPolicyTest() override = default;
void SetUp() override {
ChromeRenderViewHostTestHarness::SetUp();
pm_harness_.SetUp();
SetContents(CreateTestWebContents());
// Get the TemplateURLService.
template_url_service_ = TemplateURLServiceFactory::GetForProfile(profile());
ASSERT_TRUE(template_url_service_);
template_url_service_->Load();
// Create the initial DSE TemplateURLData.
AddAndSetDSE("foo", "foo_keyword", kDSEUrl);
// Check to make sure it's set correctly
ASSERT_EQ(kDSEUrl,
template_url_service_->GetDefaultSearchProvider()->url());
PerformanceManager::GetGraph()->PassToGraph(
std::make_unique<KeepAliveDSEPolicy>());
}
void TearDown() override {
template_url_service_ = nullptr;
DeleteContents();
pm_harness_.TearDown();
ChromeRenderViewHostTestHarness::TearDown();
}
TestingProfile::TestingFactories GetTestingFactories() const override {
return TestingProfile::TestingFactory{
TemplateURLServiceFactory::GetInstance(),
base::BindRepeating(&TemplateURLServiceFactory::BuildInstanceFor)};
}
protected:
// Helper function to add and set a DSE to the TemplateURLService.
// Takes the necessary data to create the TemplateURLData and set the DSE.
void AddAndSetDSE(const std::string& short_name,
const std::string& keyword,
const std::string& url) {
// Create the TemplateURLData.
TemplateURLData data;
data.SetShortName(base::ASCIIToUTF16(short_name));
data.SetKeyword(base::ASCIIToUTF16(keyword));
data.SetURL(url);
auto template_url = std::make_unique<TemplateURL>(data);
template_url_service_->Add(std::move(template_url));
// Set the newly added TemplateURL as the default search provider.
template_url_service_->SetUserSelectedDefaultSearchProvider(
template_url_service_->GetTemplateURLForKeyword(
base::ASCIIToUTF16(keyword)));
}
private:
PerformanceManagerTestHarnessHelper pm_harness_;
raw_ptr<TemplateURLService> template_url_service_;
};
// Test that navigating to a non-DSE URL does *not* increment the
// pending reuse refcount.
TEST_F(KeepAliveDSEPolicyTest, NonDSENavigationDoesNotKeepAlive) {
NavigateAndCommit(GURL(kNotADSEUrl));
EXPECT_EQ(0, process()->GetPendingReuseRefCountForTesting());
}
// Test that navigating to the current DSE URL *does* increment the
// pending reuse refcount.
TEST_F(KeepAliveDSEPolicyTest, DSENavigationKeepsAlive) {
NavigateAndCommit(GURL(kDSEUrl));
EXPECT_EQ(1, process()->GetPendingReuseRefCountForTesting());
}
// Test that changing the DSE releases the keep-alive on the old DSE,
// and that navigating to the *new* DSE URL increments the refcount.
TEST_F(KeepAliveDSEPolicyTest, ChangeDSE) {
NavigateAndCommit(GURL(kDSEUrl));
EXPECT_EQ(1, process()->GetPendingReuseRefCountForTesting());
// Change the default search engine.
AddAndSetDSE("bar", "bar_keyword", kNewDSEUrl);
// The original process should no longer be kept alive.
EXPECT_EQ(0, process()->GetPendingReuseRefCountForTesting());
NavigateAndCommit(GURL(kNewDSEUrl));
EXPECT_EQ(1, process()->GetPendingReuseRefCountForTesting());
}
// Test that the DSE is kept alive even after navigating away from it.
TEST_F(KeepAliveDSEPolicyTest, KeepAliveAfterNavigationAway) {
// Navigate to the DSE page.
NavigateAndCommit(GURL(kDSEUrl));
auto* initial_rph = process();
EXPECT_EQ(1, initial_rph->GetPendingReuseRefCountForTesting());
// Navigate to a different, non-DSE page.
NavigateAndCommit(GURL(kNotADSEUrl));
// The original DSE process *should still* be kept alive.
EXPECT_EQ(0, process()->GetPendingReuseRefCountForTesting());
EXPECT_EQ(1, initial_rph->GetPendingReuseRefCountForTesting());
}
} // namespace performance_manager::policies
|