Index: chrome/browser/prefs/profile_pref_store_manager_unittest.cc |
diff --git a/chrome/browser/prefs/profile_pref_store_manager_unittest.cc b/chrome/browser/prefs/profile_pref_store_manager_unittest.cc |
index 62d22de8fd07e71ff35780716525a3f4323729e5..d7c0c0181c2cff603528522b613ac751c019590d 100644 |
--- a/chrome/browser/prefs/profile_pref_store_manager_unittest.cc |
+++ b/chrome/browser/prefs/profile_pref_store_manager_unittest.cc |
@@ -10,6 +10,7 @@ |
#include <utility> |
#include <vector> |
+#include "base/callback_helpers.h" |
#include "base/compiler_specific.h" |
#include "base/files/file_enumerator.h" |
#include "base/files/file_util.h" |
@@ -19,7 +20,11 @@ |
#include "base/memory/ref_counted.h" |
#include "base/run_loop.h" |
#include "base/strings/string_util.h" |
+#include "base/test/scoped_feature_list.h" |
+#include "base/test/sequenced_worker_pool_owner.h" |
+#include "base/threading/sequenced_worker_pool.h" |
#include "base/values.h" |
+#include "chrome/common/chrome_features.h" |
#include "components/pref_registry/pref_registry_syncable.h" |
#include "components/prefs/json_pref_store.h" |
#include "components/prefs/persistent_pref_store.h" |
@@ -30,6 +35,10 @@ |
#include "components/user_prefs/tracked/mock_validation_delegate.h" |
#include "components/user_prefs/tracked/pref_hash_filter.h" |
#include "components/user_prefs/tracked/pref_names.h" |
+#include "content/public/common/service_names.mojom.h" |
+#include "services/preferences/public/cpp/pref_store_manager_impl.h" |
+#include "services/service_manager/public/cpp/connector.h" |
+#include "services/service_manager/public/cpp/service_context.h" |
#include "testing/gtest/include/gtest/gtest.h" |
namespace { |
@@ -68,6 +77,89 @@ class RegistryVerifier : public PrefStore::Observer { |
scoped_refptr<PrefRegistry> pref_registry_; |
}; |
+class PrefStoreReadObserver : public PrefStore::Observer { |
+ public: |
+ explicit PrefStoreReadObserver(scoped_refptr<PersistentPrefStore> pref_store) |
+ : pref_store_(std::move(pref_store)) { |
+ pref_store_->AddObserver(this); |
+ } |
+ |
+ ~PrefStoreReadObserver() override { pref_store_->RemoveObserver(this); } |
+ |
+ PersistentPrefStore::PrefReadError Read() { |
+ base::RunLoop run_loop; |
+ stop_waiting_ = run_loop.QuitClosure(); |
+ pref_store_->ReadPrefsAsync(nullptr); |
+ run_loop.Run(); |
+ return pref_store_->GetReadError(); |
+ } |
+ |
+ // PrefStore::Observer implementation |
+ void OnPrefValueChanged(const std::string& key) override {} |
+ |
+ void OnInitializationCompleted(bool succeeded) override { |
+ if (!stop_waiting_.is_null()) { |
+ base::ResetAndReturn(&stop_waiting_).Run(); |
+ } |
+ } |
+ |
+ private: |
+ scoped_refptr<PersistentPrefStore> pref_store_; |
+ base::Closure stop_waiting_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(PrefStoreReadObserver); |
+}; |
+ |
+class MockConnector : public service_manager::Connector { |
+ public: |
+ explicit MockConnector(service_manager::mojom::ServicePtr service_ptr) |
+ : service_(std::move(service_ptr)) {} |
+ |
+ void StartService(const service_manager::Identity& identity, |
+ service_manager::mojom::ServicePtr service, |
+ service_manager::mojom::PIDReceiverRequest |
+ pid_receiver_request) override { |
+ NOTREACHED(); |
+ } |
+ |
+ std::unique_ptr<service_manager::Connection> Connect( |
+ const std::string& name) override { |
+ NOTREACHED(); |
+ return nullptr; |
+ } |
+ std::unique_ptr<service_manager::Connection> Connect( |
+ const service_manager::Identity& target) override { |
+ NOTREACHED(); |
+ return nullptr; |
+ } |
+ void BindInterface(const service_manager::Identity& target, |
+ const std::string& interface_name, |
+ mojo::ScopedMessagePipeHandle interface_pipe) override { |
+ service_manager::ServiceInfo source( |
+ service_manager::Identity(content::mojom::kBrowserServiceName, |
+ service_manager::mojom::kRootUserID), |
+ service_manager::InterfaceProviderSpecMap()); |
+ service_->OnBindInterface(source, interface_name, std::move(interface_pipe), |
+ base::Bind(&base::DoNothing)); |
+ } |
+ |
+ std::unique_ptr<service_manager::Connector> Clone() override { |
+ NOTREACHED(); |
+ return nullptr; |
+ } |
+ |
+ // Binds a Connector request to the other end of this Connector. |
+ void BindConnectorRequest( |
+ service_manager::mojom::ConnectorRequest request) override { |
+ NOTREACHED(); |
+ } |
+ |
+ private: |
+ service_manager::mojom::ServicePtr service_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(MockConnector); |
+}; |
+ |
const char kUnprotectedPref[] = "unprotected_pref"; |
const char kTrackedAtomic[] = "tracked_atomic"; |
const char kProtectedAtomic[] = "protected_atomic"; |
@@ -88,7 +180,7 @@ const size_t kReportingIdCount = 3u; |
} // namespace |
-class ProfilePrefStoreManagerTest : public testing::Test { |
+class ProfilePrefStoreManagerTest : public testing::TestWithParam<bool> { |
public: |
ProfilePrefStoreManagerTest() |
: configuration_(kConfiguration, |
@@ -99,9 +191,21 @@ class ProfilePrefStoreManagerTest : public testing::Test { |
reset_recorded_(false) {} |
void SetUp() override { |
+ worker_pool_ = base::MakeUnique<base::SequencedWorkerPoolOwner>( |
+ 2, "ProfilePrefStoreManagerTest"); |
+ if (GetParam()) { |
+ feature_list_.InitAndEnableFeature(features::kPrefService); |
+ service_manager::mojom::ServicePtr service_ptr; |
+ pref_service_context_ = base::MakeUnique<service_manager::ServiceContext>( |
+ base::MakeUnique<prefs::PrefStoreManagerImpl>( |
+ prefs::PrefStoreManagerImpl::PrefStoreTypes(), |
+ worker_pool_->pool()), |
+ mojo::MakeRequest(&service_ptr)); |
+ connector_ = base::MakeUnique<MockConnector>(std::move(service_ptr)); |
+ } else { |
+ feature_list_.InitAndDisableFeature(features::kPrefService); |
+ } |
mock_validation_delegate_record_ = new MockValidationDelegateRecord; |
- mock_validation_delegate_ = base::MakeUnique<MockValidationDelegate>( |
- mock_validation_delegate_record_); |
ProfilePrefStoreManager::RegisterProfilePrefs(profile_pref_registry_.get()); |
for (const PrefHashFilter::TrackedPreferenceMetadata* it = kConfiguration; |
it != kConfiguration + arraysize(kConfiguration); |
@@ -131,14 +235,35 @@ class ProfilePrefStoreManagerTest : public testing::Test { |
} |
void ReloadConfiguration() { |
+ RelaunchPrefService(); |
manager_.reset(new ProfilePrefStoreManager( |
profile_dir_.GetPath(), configuration_, kReportingIdCount, seed_, |
"device_id", &local_state_)); |
} |
- void TearDown() override { DestroyPrefStore(); } |
+ void TearDown() override { |
+ DestroyPrefStore(); |
+ if (GetParam()) { |
+ connector_.reset(); |
+ pref_service_context_.reset(); |
+ } |
+ worker_pool_.reset(); |
+ } |
protected: |
+ void RelaunchPrefService() { |
+ if (!GetParam()) |
+ return; |
+ |
+ service_manager::mojom::ServicePtr service_ptr; |
+ pref_service_context_ = base::MakeUnique<service_manager::ServiceContext>( |
+ base::MakeUnique<prefs::PrefStoreManagerImpl>( |
+ prefs::PrefStoreManagerImpl::PrefStoreTypes(), |
+ worker_pool_->pool()), |
+ mojo::MakeRequest(&service_ptr)); |
+ connector_ = base::MakeUnique<MockConnector>(std::move(service_ptr)); |
+ } |
+ |
// Verifies whether a reset was reported via the RecordReset() hook. Also |
// verifies that GetResetTime() was set (or not) accordingly. |
void VerifyResetRecorded(bool reset_expected) { |
@@ -170,12 +295,14 @@ class ProfilePrefStoreManagerTest : public testing::Test { |
void InitializePrefs() { |
// According to the implementation of ProfilePrefStoreManager, this is |
// actually a SegregatedPrefStore backed by two underlying pref stores. |
+ mock_validation_delegate_ = base::MakeUnique<MockValidationDelegate>( |
+ mock_validation_delegate_record_); |
scoped_refptr<PersistentPrefStore> pref_store = |
manager_->CreateProfilePrefStore( |
main_message_loop_.task_runner(), |
base::Bind(&ProfilePrefStoreManagerTest::RecordReset, |
base::Unretained(this)), |
- mock_validation_delegate_.get()); |
+ &mock_validation_delegate_, connector_.get()); |
InitializePrefStore(pref_store.get()); |
pref_store = NULL; |
base::RunLoop().RunUntilIdle(); |
@@ -188,6 +315,12 @@ class ProfilePrefStoreManagerTest : public testing::Test { |
// while our RegistryVerifier is watching. |
pref_store_->CommitPendingWrite(); |
base::RunLoop().RunUntilIdle(); |
+ base::RunLoop run_loop; |
+ JsonPrefStore::GetTaskRunnerForFile(profile_dir_.GetPath(), |
+ worker_pool_->pool().get()) |
+ ->PostTaskAndReply(FROM_HERE, base::Bind(&base::DoNothing), |
+ run_loop.QuitClosure()); |
+ run_loop.Run(); |
pref_store_->RemoveObserver(®istry_verifier_); |
pref_store_ = NULL; |
@@ -195,11 +328,13 @@ class ProfilePrefStoreManagerTest : public testing::Test { |
// case... |
base::RunLoop().RunUntilIdle(); |
} |
+ RelaunchPrefService(); |
} |
void InitializePrefStore(PersistentPrefStore* pref_store) { |
pref_store->AddObserver(®istry_verifier_); |
- PersistentPrefStore::PrefReadError error = pref_store->ReadPrefs(); |
+ PrefStoreReadObserver read_observer(pref_store); |
+ PersistentPrefStore::PrefReadError error = read_observer.Read(); |
EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_NO_FILE, error); |
pref_store->SetValue(kTrackedAtomic, base::MakeUnique<base::Value>(kFoobar), |
WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); |
@@ -212,17 +347,27 @@ class ProfilePrefStoreManagerTest : public testing::Test { |
pref_store->RemoveObserver(®istry_verifier_); |
pref_store->CommitPendingWrite(); |
base::RunLoop().RunUntilIdle(); |
+ base::RunLoop run_loop; |
+ JsonPrefStore::GetTaskRunnerForFile(profile_dir_.GetPath(), |
+ worker_pool_->pool().get()) |
+ ->PostTaskAndReply(FROM_HERE, base::Bind(&base::DoNothing), |
+ run_loop.QuitClosure()); |
+ run_loop.Run(); |
} |
void LoadExistingPrefs() { |
DestroyPrefStore(); |
+ std::unique_ptr<prefs::mojom::TrackedPreferenceValidationDelegate> |
+ validation_delegate; |
pref_store_ = manager_->CreateProfilePrefStore( |
- main_message_loop_.task_runner(), |
+ JsonPrefStore::GetTaskRunnerForFile(profile_dir_.GetPath(), |
+ worker_pool_->pool().get()), |
base::Bind(&ProfilePrefStoreManagerTest::RecordReset, |
base::Unretained(this)), |
- NULL); |
+ &validation_delegate, connector_.get()); |
pref_store_->AddObserver(®istry_verifier_); |
- pref_store_->ReadPrefs(); |
+ PrefStoreReadObserver read_observer(pref_store_); |
+ read_observer.Read(); |
} |
void ReplaceStringInPrefs(const std::string& find, |
@@ -269,7 +414,8 @@ class ProfilePrefStoreManagerTest : public testing::Test { |
scoped_refptr<user_prefs::PrefRegistrySyncable> profile_pref_registry_; |
RegistryVerifier registry_verifier_; |
scoped_refptr<MockValidationDelegateRecord> mock_validation_delegate_record_; |
- std::unique_ptr<MockValidationDelegate> mock_validation_delegate_; |
+ std::unique_ptr<prefs::mojom::TrackedPreferenceValidationDelegate> |
+ mock_validation_delegate_; |
std::unique_ptr<ProfilePrefStoreManager> manager_; |
scoped_refptr<PersistentPrefStore> pref_store_; |
@@ -284,10 +430,14 @@ class ProfilePrefStoreManagerTest : public testing::Test { |
reset_recorded_ = true; |
} |
+ base::test::ScopedFeatureList feature_list_; |
bool reset_recorded_; |
+ std::unique_ptr<base::SequencedWorkerPoolOwner> worker_pool_; |
+ std::unique_ptr<service_manager::ServiceContext> pref_service_context_; |
+ std::unique_ptr<service_manager::Connector> connector_; |
}; |
-TEST_F(ProfilePrefStoreManagerTest, StoreValues) { |
+TEST_P(ProfilePrefStoreManagerTest, StoreValues) { |
InitializePrefs(); |
LoadExistingPrefs(); |
@@ -299,7 +449,7 @@ TEST_F(ProfilePrefStoreManagerTest, StoreValues) { |
ExpectValidationObserved(kProtectedAtomic); |
} |
-TEST_F(ProfilePrefStoreManagerTest, ProtectValues) { |
+TEST_P(ProfilePrefStoreManagerTest, ProtectValues) { |
InitializePrefs(); |
ReplaceStringInPrefs(kFoobar, kBarfoo); |
@@ -322,7 +472,7 @@ TEST_F(ProfilePrefStoreManagerTest, ProtectValues) { |
ExpectValidationObserved(kProtectedAtomic); |
} |
-TEST_F(ProfilePrefStoreManagerTest, InitializePrefsFromMasterPrefs) { |
+TEST_P(ProfilePrefStoreManagerTest, InitializePrefsFromMasterPrefs) { |
auto master_prefs = base::MakeUnique<base::DictionaryValue>(); |
master_prefs->Set(kTrackedAtomic, new base::Value(kFoobar)); |
master_prefs->Set(kProtectedAtomic, new base::Value(kHelloWorld)); |
@@ -338,7 +488,7 @@ TEST_F(ProfilePrefStoreManagerTest, InitializePrefsFromMasterPrefs) { |
VerifyResetRecorded(false); |
} |
-TEST_F(ProfilePrefStoreManagerTest, UnprotectedToProtected) { |
+TEST_P(ProfilePrefStoreManagerTest, UnprotectedToProtected) { |
InitializePrefs(); |
ExpectValidationObserved(kTrackedAtomic); |
@@ -385,7 +535,7 @@ TEST_F(ProfilePrefStoreManagerTest, UnprotectedToProtected) { |
ProfilePrefStoreManager::kPlatformSupportsPreferenceTracking); |
} |
-TEST_F(ProfilePrefStoreManagerTest, NewPrefWhenFirstProtecting) { |
+TEST_P(ProfilePrefStoreManagerTest, NewPrefWhenFirstProtecting) { |
std::vector<PrefHashFilter::TrackedPreferenceMetadata> |
original_configuration = configuration_; |
for (std::vector<PrefHashFilter::TrackedPreferenceMetadata>::iterator it = |
@@ -425,7 +575,7 @@ TEST_F(ProfilePrefStoreManagerTest, NewPrefWhenFirstProtecting) { |
VerifyResetRecorded(false); |
} |
-TEST_F(ProfilePrefStoreManagerTest, UnprotectedToProtectedWithoutTrust) { |
+TEST_P(ProfilePrefStoreManagerTest, UnprotectedToProtectedWithoutTrust) { |
InitializePrefs(); |
ExpectValidationObserved(kTrackedAtomic); |
@@ -453,7 +603,7 @@ TEST_F(ProfilePrefStoreManagerTest, UnprotectedToProtectedWithoutTrust) { |
// This test verifies that preference values are correctly maintained when a |
// preference's protection state changes from protected to unprotected. |
-TEST_F(ProfilePrefStoreManagerTest, ProtectedToUnprotected) { |
+TEST_P(ProfilePrefStoreManagerTest, ProtectedToUnprotected) { |
InitializePrefs(); |
ExpectValidationObserved(kTrackedAtomic); |
@@ -495,3 +645,7 @@ TEST_F(ProfilePrefStoreManagerTest, ProtectedToUnprotected) { |
ExpectStringValueEquals(kProtectedAtomic, kGoodbyeWorld); |
VerifyResetRecorded(false); |
} |
+ |
+INSTANTIATE_TEST_CASE_P(ProfilePrefStoreManagerTest, |
+ ProfilePrefStoreManagerTest, |
+ testing::Bool()); |