Commit 608166e7 authored by Saurabh Nijhara's avatar Saurabh Nijhara Committed by Commit Bot

Extract test base class for force installed extensions

This CL is pre-work for extracting ForceInstalledTracker related unit
tests from force_installed_metrics_unittest.cc.
This CL extracts the common elements in a test base class which could
be used for unit tests of both ForceInstalledMetrics and
ForceInstalledTracker.

Bug: 1086430
Change-Id: I60a190012b640d87f1dc5f8d3e4e19fcc683dd53
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2416588Reviewed-by: default avatarOleg Davydov <burunduk@chromium.org>
Commit-Queue: Saurabh Nijhara <snijhara@google.com>
Cr-Commit-Position: refs/heads/master@{#808854}
parent 325c4040
......@@ -7,21 +7,15 @@
#include "base/memory/ptr_util.h"
#include "base/optional.h"
#include "base/scoped_observer.h"
#include "base/strings/strcat.h"
#include "base/strings/utf_string_conversions.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/time/time.h"
#include "base/timer/mock_timer.h"
#include "base/values.h"
#include "chrome/browser/extensions/external_provider_impl.h"
#include "chrome/browser/extensions/forced_extensions/force_installed_test_base.h"
#include "chrome/browser/extensions/forced_extensions/force_installed_tracker.h"
#include "chrome/browser/extensions/forced_extensions/install_stage_tracker.h"
#include "chrome/test/base/testing_browser_process.h"
#include "chrome/test/base/testing_profile.h"
#include "chrome/test/base/testing_profile_manager.h"
#include "components/policy/core/common/mock_configuration_policy_provider.h"
#include "components/policy/core/common/policy_service_impl.h"
#include "components/prefs/pref_service.h"
#include "components/sync_preferences/testing_pref_service_syncable.h"
#include "content/public/test/browser_task_environment.h"
#include "extensions/browser/disable_reason.h"
......@@ -48,15 +42,8 @@
namespace {
// The extension ids used here should be valid extension ids.
constexpr char kExtensionId1[] = "abcdefghijklmnopabcdefghijklmnop";
constexpr char kExtensionId2[] = "bcdefghijklmnopabcdefghijklmnopa";
// Intentionally invalid extension id.
constexpr char kExtensionId3[] = "cdefghijklmnopqrstuvwxyzabcdefgh";
constexpr char kExtensionName1[] = "name1";
constexpr char kExtensionName2[] = "name2";
constexpr char kExtensionUpdateUrl[] =
"https://clients2.google.com/service/update2/crx"; // URL of Chrome Web
// Store backend.
const int kFetchTries = 5;
// HTTP_UNAUTHORIZED
......@@ -130,7 +117,7 @@ namespace extensions {
using testing::_;
using testing::Return;
class ForceInstalledMetricsTest : public testing::Test,
class ForceInstalledMetricsTest : public ForceInstalledTestBase,
public ForceInstalledTracker::Observer {
public:
ForceInstalledMetricsTest() = default;
......@@ -140,59 +127,14 @@ class ForceInstalledMetricsTest : public testing::Test,
delete;
void SetUp() override {
EXPECT_CALL(policy_provider_, IsInitializationComplete(_))
.WillRepeatedly(Return(false));
auto policy_service = std::make_unique<policy::PolicyServiceImpl>(
std::vector<policy::ConfigurationPolicyProvider*>{&policy_provider_});
profile_manager_ = std::make_unique<TestingProfileManager>(
TestingBrowserProcess::GetGlobal());
ASSERT_TRUE(profile_manager_->SetUp());
profile_ = profile_manager_->CreateTestingProfile(
"p1", nullptr, base::UTF8ToUTF16("p1"), 0, "",
TestingProfile::TestingFactories(), base::nullopt,
std::move(policy_service));
prefs_ = profile_->GetTestingPrefService();
registry_ = ExtensionRegistry::Get(profile_);
install_stage_tracker_ = InstallStageTracker::Get(profile_);
ForceInstalledTestBase::SetUp();
auto fake_timer = std::make_unique<base::MockOneShotTimer>();
fake_timer_ = fake_timer.get();
tracker_ = std::make_unique<ForceInstalledTracker>(registry_, profile_);
scoped_observer_.Add(tracker_.get());
metrics_ = std::make_unique<ForceInstalledMetrics>(
registry_, profile_, tracker_.get(), std::move(fake_timer));
}
void SetupForceList() {
base::Value list(base::Value::Type::LIST);
list.Append(base::StrCat({kExtensionId1, ";", kExtensionUpdateUrl}));
list.Append(base::StrCat({kExtensionId2, ";", kExtensionUpdateUrl}));
std::unique_ptr<base::Value> dict =
DictionaryBuilder()
.Set(kExtensionId1,
DictionaryBuilder()
.Set(ExternalProviderImpl::kExternalUpdateUrl,
kExtensionUpdateUrl)
.Build())
.Set(kExtensionId2,
DictionaryBuilder()
.Set(ExternalProviderImpl::kExternalUpdateUrl,
kExtensionUpdateUrl)
.Build())
.Build();
prefs_->SetManagedPref(pref_names::kInstallForceList, std::move(dict));
EXPECT_CALL(policy_provider_, IsInitializationComplete(_))
.WillRepeatedly(Return(true));
policy::PolicyMap map;
map.Set("ExtensionInstallForcelist", policy::POLICY_LEVEL_MANDATORY,
policy::POLICY_SCOPE_MACHINE, policy::POLICY_SOURCE_PLATFORM,
std::move(list), nullptr);
policy_provider_.UpdateChromePolicy(map);
base::RunLoop().RunUntilIdle();
}
void SetupExtensionManagementPref() {
std::unique_ptr<base::DictionaryValue> extension_entry =
DictionaryBuilder()
......@@ -205,17 +147,6 @@ class ForceInstalledMetricsTest : public testing::Test,
.Build());
}
void SetupEmptyForceList() {
std::unique_ptr<base::Value> dict = DictionaryBuilder().Build();
prefs_->SetManagedPref(pref_names::kInstallForceList, std::move(dict));
EXPECT_CALL(policy_provider_, IsInitializationComplete(_))
.WillRepeatedly(Return(true));
policy::PolicyMap map;
policy_provider_.UpdateChromePolicy(std::move(map));
base::RunLoop().RunUntilIdle();
}
// Report downloading manifest stage for both the extensions.
void ReportDownloadingManifestStage() {
install_stage_tracker_->ReportDownloadingStage(
......@@ -244,18 +175,8 @@ class ForceInstalledMetricsTest : public testing::Test,
void OnForceInstalledExtensionsReady() override { ready_call_count_++; }
protected:
content::BrowserTaskEnvironment task_environment_{
base::test::TaskEnvironment::TimeSource::MOCK_TIME};
policy::MockConfigurationPolicyProvider policy_provider_;
std::unique_ptr<TestingProfileManager> profile_manager_;
TestingProfile* profile_;
sync_preferences::TestingPrefServiceSyncable* prefs_;
ExtensionRegistry* registry_;
InstallStageTracker* install_stage_tracker_;
base::HistogramTester histogram_tester_;
base::MockOneShotTimer* fake_timer_;
std::unique_ptr<ForceInstalledTracker> tracker_;
std::unique_ptr<ForceInstalledMetrics> metrics_;
ScopedObserver<ForceInstalledTracker, ForceInstalledTracker::Observer>
......
// Copyright 2020 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 "chrome/browser/extensions/forced_extensions/force_installed_test_base.h"
#include "base/strings/strcat.h"
#include "base/strings/utf_string_conversions.h"
#include "base/values.h"
#include "chrome/browser/extensions/external_provider_impl.h"
#include "chrome/browser/extensions/forced_extensions/force_installed_tracker.h"
#include "chrome/browser/extensions/forced_extensions/install_stage_tracker.h"
#include "chrome/test/base/testing_browser_process.h"
#include "chrome/test/base/testing_profile.h"
#include "components/policy/core/common/policy_service_impl.h"
#include "components/prefs/pref_service.h"
#include "components/sync_preferences/testing_pref_service_syncable.h"
#include "extensions/browser/extension_registry.h"
#include "extensions/browser/pref_names.h"
#include "extensions/common/extension.h"
#include "extensions/common/extension_builder.h"
#include "testing/gmock/include/gmock/gmock.h"
namespace extensions {
// The extension ids used here should be valid extension ids.
const char ForceInstalledTestBase::kExtensionId1[] =
"abcdefghijklmnopabcdefghijklmnop";
const char ForceInstalledTestBase::kExtensionId2[] =
"bcdefghijklmnopabcdefghijklmnopa";
const char ForceInstalledTestBase::kExtensionName1[] = "name1";
const char ForceInstalledTestBase::kExtensionName2[] = "name2";
const char ForceInstalledTestBase::kExtensionUpdateUrl[] =
"https://clients2.google.com/service/update2/crx"; // URL of Chrome Web
// Store backend.
ForceInstalledTestBase::ForceInstalledTestBase() = default;
ForceInstalledTestBase::~ForceInstalledTestBase() = default;
void ForceInstalledTestBase::SetUp() {
EXPECT_CALL(policy_provider_, IsInitializationComplete(testing::_))
.WillRepeatedly(testing::Return(false));
auto policy_service = std::make_unique<policy::PolicyServiceImpl>(
std::vector<policy::ConfigurationPolicyProvider*>{&policy_provider_});
profile_manager_ = std::make_unique<TestingProfileManager>(
TestingBrowserProcess::GetGlobal());
ASSERT_TRUE(profile_manager_->SetUp());
profile_ = profile_manager_->CreateTestingProfile(
"p1", nullptr, base::UTF8ToUTF16("p1"), 0, "",
TestingProfile::TestingFactories(), base::nullopt,
std::move(policy_service));
prefs_ = profile_->GetTestingPrefService();
registry_ = ExtensionRegistry::Get(profile_);
install_stage_tracker_ = InstallStageTracker::Get(profile_);
tracker_ = std::make_unique<ForceInstalledTracker>(registry_, profile_);
}
void ForceInstalledTestBase::SetupForceList() {
base::Value list(base::Value::Type::LIST);
list.Append(base::StrCat({kExtensionId1, ";", kExtensionUpdateUrl}));
list.Append(base::StrCat({kExtensionId2, ";", kExtensionUpdateUrl}));
std::unique_ptr<base::Value> dict =
DictionaryBuilder()
.Set(kExtensionId1, DictionaryBuilder()
.Set(ExternalProviderImpl::kExternalUpdateUrl,
kExtensionUpdateUrl)
.Build())
.Set(kExtensionId2, DictionaryBuilder()
.Set(ExternalProviderImpl::kExternalUpdateUrl,
kExtensionUpdateUrl)
.Build())
.Build();
prefs_->SetManagedPref(pref_names::kInstallForceList, std::move(dict));
EXPECT_CALL(policy_provider_, IsInitializationComplete(testing::_))
.WillRepeatedly(testing::Return(true));
policy::PolicyMap map;
map.Set("ExtensionInstallForcelist", policy::POLICY_LEVEL_MANDATORY,
policy::POLICY_SCOPE_MACHINE, policy::POLICY_SOURCE_PLATFORM,
std::move(list), nullptr);
policy_provider_.UpdateChromePolicy(map);
base::RunLoop().RunUntilIdle();
}
void ForceInstalledTestBase::SetupEmptyForceList() {
std::unique_ptr<base::Value> dict = DictionaryBuilder().Build();
prefs_->SetManagedPref(pref_names::kInstallForceList, std::move(dict));
EXPECT_CALL(policy_provider_, IsInitializationComplete(testing::_))
.WillRepeatedly(testing::Return(true));
policy::PolicyMap map;
policy_provider_.UpdateChromePolicy(std::move(map));
base::RunLoop().RunUntilIdle();
}
} // namespace extensions
// Copyright 2020 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.
#ifndef CHROME_BROWSER_EXTENSIONS_FORCED_EXTENSIONS_FORCE_INSTALLED_TEST_BASE_H_
#define CHROME_BROWSER_EXTENSIONS_FORCED_EXTENSIONS_FORCE_INSTALLED_TEST_BASE_H_
#include "chrome/test/base/testing_profile_manager.h"
#include "components/policy/core/common/mock_configuration_policy_provider.h"
#include "content/public/test/browser_task_environment.h"
#include "testing/gtest/include/gtest/gtest.h"
class TestingProfile;
namespace sync_preferences {
class TestingPrefServiceSyncable;
}
namespace extensions {
class ExtensionRegistry;
class InstallStageTracker;
class ForceInstalledTracker;
// This class is extended by tests to provide a setup for tracking installation
// of force extensions. It also provides helper functions for creating and
// setting ExtensionInstallForcelist policy value.
class ForceInstalledTestBase : public testing::Test {
public:
ForceInstalledTestBase();
~ForceInstalledTestBase() override;
ForceInstalledTestBase(const ForceInstalledTestBase&) = delete;
ForceInstalledTestBase& operator=(const ForceInstalledTestBase&) = delete;
protected:
void SetUp() override;
// Creates and sets value for ExtensionInstallForcelist policy and
// kInstallForceList preference.
void SetupForceList();
// Creates and sets empty value for ExtensionInstallForcelist policy and
// kInstallForceList preference.
void SetupEmptyForceList();
static const char kExtensionId1[];
static const char kExtensionId2[];
static const char kExtensionName1[];
static const char kExtensionName2[];
static const char kExtensionUpdateUrl[];
content::BrowserTaskEnvironment task_environment_{
base::test::TaskEnvironment::TimeSource::MOCK_TIME};
policy::MockConfigurationPolicyProvider policy_provider_;
std::unique_ptr<TestingProfileManager> profile_manager_;
TestingProfile* profile_;
sync_preferences::TestingPrefServiceSyncable* prefs_;
ExtensionRegistry* registry_;
InstallStageTracker* install_stage_tracker_;
std::unique_ptr<ForceInstalledTracker> tracker_;
};
} // namespace extensions
#endif // CHROME_BROWSER_EXTENSIONS_FORCED_EXTENSIONS_FORCE_INSTALLED_TEST_BASE_H_
......@@ -5060,6 +5060,8 @@ test("unit_tests") {
"../browser/extensions/external_provider_impl_chromeos_unittest.cc",
"../browser/extensions/external_provider_impl_unittest.cc",
"../browser/extensions/forced_extensions/force_installed_metrics_unittest.cc",
"../browser/extensions/forced_extensions/force_installed_test_base.cc",
"../browser/extensions/forced_extensions/force_installed_test_base.h",
"../browser/extensions/install_tracker_unittest.cc",
"../browser/extensions/install_verifier_unittest.cc",
"../browser/extensions/installed_loader_unittest.cc",
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment