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 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
|
// Copyright 2016 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/browsing_data/counters/hosted_apps_counter.h"
#include <algorithm>
#include <memory>
#include <optional>
#include <string>
#include <vector>
#include "base/functional/bind.h"
#include "base/memory/raw_ptr.h"
#include "base/uuid.h"
#include "base/values.h"
#include "chrome/test/base/testing_profile.h"
#include "components/browsing_data/core/browsing_data_utils.h"
#include "components/browsing_data/core/pref_names.h"
#include "components/crx_file/id_util.h"
#include "components/prefs/pref_service.h"
#include "content/public/test/browser_task_environment.h"
#include "extensions/browser/extension_registry.h"
#include "extensions/common/extension_builder.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace {
class HostedAppsCounterTest : public testing::Test {
public:
void SetUp() override {
profile_ = std::make_unique<TestingProfile>();
extension_registry_ = extensions::ExtensionRegistry::Get(profile_.get());
SetHostedAppsDeletionPref(true);
SetDeletionPeriodPref(browsing_data::TimePeriod::ALL_TIME);
}
// Adding and removing apps and extensions. ----------------------------------
std::string AddExtension() {
return AddItem(base::Uuid::GenerateRandomV4().AsLowercaseString(),
/*app_manifest=*/std::nullopt);
}
std::string AddPackagedApp() {
return AddItem(
base::Uuid::GenerateRandomV4().AsLowercaseString(),
base::Value::Dict().Set(
"launch", base::Value::Dict().Set("local_path", "index.html")));
}
std::string AddHostedApp() {
return AddHostedAppWithName(
base::Uuid::GenerateRandomV4().AsLowercaseString());
}
std::string AddHostedAppWithName(const std::string& name) {
return AddItem(
name,
base::Value::Dict()
.Set("urls", base::Value::List().Append("https://example.com"))
.Set("launch",
base::Value::Dict().Set("web_url", "https://example.com")));
}
std::string AddItem(const std::string& name,
std::optional<base::Value::Dict> app_manifest) {
auto manifest_builder = base::Value::Dict()
.Set("manifest_version", 2)
.Set("name", name)
.Set("version", "1");
if (app_manifest) {
manifest_builder.Set("app", std::move(*app_manifest));
}
scoped_refptr<const extensions::Extension> item =
extensions::ExtensionBuilder()
.SetManifest(std::move(manifest_builder))
.SetID(crx_file::id_util::GenerateId(name))
.Build();
extension_registry_->AddEnabled(item.get());
return item->id();
}
void RemoveItem(const std::string& id) {
extension_registry_->RemoveEnabled(id);
}
// Setting preferences. ------------------------------------------------------
void SetHostedAppsDeletionPref(bool value) {
GetProfile()->GetPrefs()->SetBoolean(
browsing_data::prefs::kDeleteHostedAppsData, value);
}
void SetDeletionPeriodPref(browsing_data::TimePeriod period) {
GetProfile()->GetPrefs()->SetInteger(
browsing_data::prefs::kDeleteTimePeriod, static_cast<int>(period));
}
// Retrieving counter results. -----------------------------------------------
browsing_data::BrowsingDataCounter::ResultInt GetNumHostedApps() {
DCHECK(finished_);
return num_apps_;
}
const std::vector<std::string>& GetExamples() {
DCHECK(finished_);
return examples_;
}
void Callback(
std::unique_ptr<browsing_data::BrowsingDataCounter::Result> result) {
finished_ = result->Finished();
if (finished_) {
HostedAppsCounter::HostedAppsResult* hosted_apps_result =
static_cast<HostedAppsCounter::HostedAppsResult*>(result.get());
num_apps_ = hosted_apps_result->Value();
examples_ = hosted_apps_result->examples();
}
}
// Miscellaneous. ------------------------------------------------------------
Profile* GetProfile() {
return profile_.get();
}
private:
content::BrowserTaskEnvironment task_environment_;
std::unique_ptr<TestingProfile> profile_;
raw_ptr<extensions::ExtensionRegistry> extension_registry_;
bool finished_;
browsing_data::BrowsingDataCounter::ResultInt num_apps_;
std::vector<std::string> examples_;
};
// Tests that we count the total number of hosted apps correctly.
TEST_F(HostedAppsCounterTest, Count) {
Profile* profile = GetProfile();
HostedAppsCounter counter(profile);
counter.Init(profile->GetPrefs(),
browsing_data::ClearBrowsingDataTab::ADVANCED,
base::BindRepeating(&HostedAppsCounterTest::Callback,
base::Unretained(this)));
counter.Restart();
EXPECT_EQ(0u, GetNumHostedApps());
std::string first_app = AddHostedApp();
AddHostedApp();
std::string last_app = AddHostedApp();
counter.Restart();
EXPECT_EQ(3u, GetNumHostedApps());
RemoveItem(last_app);
RemoveItem(first_app);
counter.Restart();
EXPECT_EQ(1u, GetNumHostedApps());
AddHostedApp();
counter.Restart();
EXPECT_EQ(2u, GetNumHostedApps());
}
// Tests that we only count hosted apps, not packaged apps or extensions.
TEST_F(HostedAppsCounterTest, OnlyHostedApps) {
Profile* profile = GetProfile();
HostedAppsCounter counter(profile);
counter.Init(profile->GetPrefs(),
browsing_data::ClearBrowsingDataTab::ADVANCED,
base::BindRepeating(&HostedAppsCounterTest::Callback,
base::Unretained(this)));
AddHostedApp(); // 1
AddExtension();
AddPackagedApp();
AddExtension();
counter.Restart();
EXPECT_EQ(1u, GetNumHostedApps());
AddHostedApp(); // 2
AddHostedApp(); // 3
AddExtension();
counter.Restart();
EXPECT_EQ(3u, GetNumHostedApps());
AddPackagedApp();
AddExtension();
counter.Restart();
EXPECT_EQ(3u, GetNumHostedApps());
AddHostedApp(); // 4
AddPackagedApp();
AddHostedApp(); // 5
AddExtension();
AddExtension();
AddExtension();
AddPackagedApp();
counter.Restart();
EXPECT_EQ(5u, GetNumHostedApps());
}
// Tests that the counter results contain names of the first two hosted apps
// in lexicographic ordering.
TEST_F(HostedAppsCounterTest, Examples) {
Profile* profile = GetProfile();
HostedAppsCounter counter(profile);
counter.Init(profile->GetPrefs(),
browsing_data::ClearBrowsingDataTab::ADVANCED,
base::BindRepeating(&HostedAppsCounterTest::Callback,
base::Unretained(this)));
counter.Restart();
EXPECT_EQ(0u, GetExamples().size());
AddHostedAppWithName("App 1");
counter.Restart();
EXPECT_EQ(1u, GetExamples().size());
EXPECT_EQ("App 1", GetExamples().front());
AddHostedAppWithName("App 2");
counter.Restart();
EXPECT_EQ(2u, GetExamples().size());
EXPECT_EQ("App 1", GetExamples().front());
EXPECT_EQ("App 2", GetExamples().back());
AddHostedAppWithName("App 3");
counter.Restart();
EXPECT_EQ(2u, GetExamples().size());
EXPECT_EQ("App 1", GetExamples().front());
EXPECT_EQ("App 2", GetExamples().back());
}
} // namespace
|