Skip to content

Commit

Permalink
Fix persistent setttings tests. (#2673)
Browse files Browse the repository at this point in the history
b/330747684
  • Loading branch information
aee-google authored Mar 25, 2024
1 parent df6d914 commit 0d29761
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 15 deletions.
4 changes: 2 additions & 2 deletions cobalt/build/cobalt_configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,9 @@ def GetTestTargets(self):
'media_test',
'memory_store_test',
'metrics_test',
'network_test',
'overlay_info_test',
'persistent_settings_test',
'png_utils_test',
'render_tree_test',
'renderer_test',
Expand All @@ -155,9 +157,7 @@ def GetTestTargets(self):
]
# return [
# 'layout_tests',
# 'network_test',
# 'net_unittests',
# 'persistent_settings_test',
# ]

def GetTestBlackBoxTargets(self):
Expand Down
12 changes: 6 additions & 6 deletions cobalt/persistent_storage/persistent_settings_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ TEST_F(PersistentSettingTest, GetSetList) {
base::Value list(base::Value::Type::LIST);
list.GetList().Append("hello");
persistent_settings->SetPersistentSetting(
"key", base::Value::ToUniquePtrValue(std::move(list)), std::move(closure),
"key", base::Value::ToUniquePtrValue(list.Clone()), std::move(closure),
true);
test_done_.Wait();
test_done_.Reset();
Expand All @@ -339,7 +339,7 @@ TEST_F(PersistentSettingTest, GetSetList) {

list.GetList().Append("there");
persistent_settings->SetPersistentSetting(
"key", base::Value::ToUniquePtrValue(std::move(list)), std::move(closure),
"key", base::Value::ToUniquePtrValue(list.Clone()), std::move(closure),
true);
test_done_.Wait();
test_done_.Reset();
Expand All @@ -362,7 +362,7 @@ TEST_F(PersistentSettingTest, GetSetList) {

list.GetList().Append(42);
persistent_settings->SetPersistentSetting(
"key", base::Value::ToUniquePtrValue(std::move(list)), std::move(closure),
"key", base::Value::ToUniquePtrValue(list.Clone()), std::move(closure),
true);
test_done_.Wait();
test_done_.Reset();
Expand All @@ -389,7 +389,7 @@ TEST_F(PersistentSettingTest, GetSetDictionary) {
base::Value dict(base::Value::Type::DICT);
dict.GetDict().Set("key_string", "hello");
persistent_settings->SetPersistentSetting(
"key", base::Value::ToUniquePtrValue(std::move(dict)), std::move(closure),
"key", base::Value::ToUniquePtrValue(dict.Clone()), std::move(closure),
true);
test_done_.Wait();
test_done_.Reset();
Expand All @@ -411,7 +411,7 @@ TEST_F(PersistentSettingTest, GetSetDictionary) {

dict.GetDict().Set("key_int", 42);
persistent_settings->SetPersistentSetting(
"key", base::Value::ToUniquePtrValue(std::move(dict)), std::move(closure),
"key", base::Value::ToUniquePtrValue(dict.Clone()), std::move(closure),
true);
test_done_.Wait();
test_done_.Reset();
Expand Down Expand Up @@ -444,7 +444,7 @@ TEST_F(PersistentSettingTest, URLAsKey) {
base::Value dict(base::Value::Type::DICT);
dict.GetDict().Set("http://127.0.0.1:45019/", "Dictionary URL Key Works!");
persistent_settings->SetPersistentSetting(
"http://127.0.0.1:45019/", base::Value::ToUniquePtrValue(std::move(dict)),
"http://127.0.0.1:45019/", base::Value::ToUniquePtrValue(dict.Clone()),
std::move(closure), true);
test_done_.Wait();
test_done_.Reset();
Expand Down
17 changes: 10 additions & 7 deletions cobalt/worker/service_worker_persistent_settings.cc
Original file line number Diff line number Diff line change
Expand Up @@ -302,11 +302,12 @@ void ServiceWorkerPersistentSettings::

// Add key_string to the registered keys and write to persistent settings.
key_set_.insert(key_string);
std::vector<base::Value> key_list;
base::Value::List key_list;
for (auto& key : key_set_) {
key_list.emplace_back(key);
key_list.Append(key);
}
persistent_settings_->SetPersistentSetting(kSettingsKeyList, nullptr);
persistent_settings_->SetPersistentSetting(
kSettingsKeyList, std::make_unique<base::Value>(std::move(key_list)));

// Persist ServiceWorkerRegistrationObject's fields.
dict.Set(kSettingsStorageKeyKey, registration->storage_key().GetURL().spec());
Expand All @@ -323,7 +324,8 @@ void ServiceWorkerPersistentSettings::
.ToDeltaSinceWindowsEpoch()
.InMicroseconds()));

persistent_settings_->SetPersistentSetting(key_string, nullptr);
persistent_settings_->SetPersistentSetting(
key_string, std::make_unique<base::Value>(std::move(dict)));
}

base::Value::Dict
Expand Down Expand Up @@ -390,11 +392,12 @@ void ServiceWorkerPersistentSettings::

// Remove registration key string.
key_set_.erase(key_string);
std::vector<base::Value> key_list;
base::Value::List key_list;
for (auto& key : key_set_) {
key_list.emplace_back(key);
key_list.Append(key);
}
persistent_settings_->SetPersistentSetting(kSettingsKeyList, nullptr);
persistent_settings_->SetPersistentSetting(
kSettingsKeyList, std::make_unique<base::Value>(std::move(key_list)));

// Remove the registration dictionary.
persistent_settings_->RemovePersistentSetting(key_string);
Expand Down
32 changes: 32 additions & 0 deletions components/prefs/json_pref_store.cc
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,11 @@ bool JsonPrefStore::GetValue(const std::string& key,
const base::Value** result) const {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);

#if defined(STARBOARD)
const base::Value* tmp = prefs_.Find(key);
#else
const base::Value* tmp = prefs_.FindByDottedPath(key);
#endif
if (!tmp)
return false;

Expand Down Expand Up @@ -193,7 +197,11 @@ bool JsonPrefStore::GetMutableValue(const std::string& key,
base::Value** result) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);

#if defined(STARBOARD)
base::Value* tmp = prefs_.Find(key);
#else
base::Value* tmp = prefs_.FindByDottedPath(key);
#endif
if (!tmp)
return false;

Expand All @@ -207,9 +215,17 @@ void JsonPrefStore::SetValue(const std::string& key,
uint32_t flags) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);

#if defined(STARBOARD)
base::Value* old_value = prefs_.Find(key);
#else
base::Value* old_value = prefs_.FindByDottedPath(key);
#endif
if (!old_value || value != *old_value) {
#if defined(STARBOARD)
prefs_.Set(key, std::move(value));
#else
prefs_.SetByDottedPath(key, std::move(value));
#endif
ReportValueChanged(key, flags);
}
}
Expand All @@ -219,17 +235,29 @@ void JsonPrefStore::SetValueSilently(const std::string& key,
uint32_t flags) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);

#if defined(STARBOARD)
base::Value* old_value = prefs_.Find(key);
#else
base::Value* old_value = prefs_.FindByDottedPath(key);
#endif
if (!old_value || value != *old_value) {
#if defined(STARBOARD)
prefs_.Set(key, std::move(value));
#else
prefs_.SetByDottedPath(key, std::move(value));
#endif
ScheduleWrite(flags);
}
}

void JsonPrefStore::RemoveValue(const std::string& key, uint32_t flags) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);

#if defined(STARBOARD)
if (prefs_.Remove(key)) {
#else
if (prefs_.RemoveByDottedPath(key)) {
#endif
ReportValueChanged(key, flags);
}
}
Expand All @@ -238,7 +266,11 @@ void JsonPrefStore::RemoveValueSilently(const std::string& key,
uint32_t flags) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);

#if defined(STARBOARD)
prefs_.Remove(key);
#else
prefs_.RemoveByDottedPath(key);
#endif
ScheduleWrite(flags);
}

Expand Down

0 comments on commit 0d29761

Please sign in to comment.