| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/sync/test/integration/extension_settings_helper.h" | 5 #include "chrome/browser/sync/test/integration/extension_settings_helper.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/json/json_writer.h" | 10 #include "base/json/json_writer.h" |
| 11 #include "base/logging.h" | 11 #include "base/logging.h" |
| 12 #include "base/synchronization/waitable_event.h" | 12 #include "base/synchronization/waitable_event.h" |
| 13 #include "base/values.h" | 13 #include "base/values.h" |
| 14 #include "chrome/browser/extensions/api/storage/backend_task_runner.h" |
| 14 #include "chrome/browser/profiles/profile.h" | 15 #include "chrome/browser/profiles/profile.h" |
| 15 #include "chrome/browser/sync/test/integration/extensions_helper.h" | 16 #include "chrome/browser/sync/test/integration/extensions_helper.h" |
| 16 #include "chrome/browser/sync/test/integration/sync_datatype_helper.h" | 17 #include "chrome/browser/sync/test/integration/sync_datatype_helper.h" |
| 17 #include "chrome/browser/sync/test/integration/sync_extension_helper.h" | 18 #include "chrome/browser/sync/test/integration/sync_extension_helper.h" |
| 18 #include "content/public/browser/browser_thread.h" | |
| 19 #include "extensions/browser/api/storage/storage_frontend.h" | 19 #include "extensions/browser/api/storage/storage_frontend.h" |
| 20 #include "extensions/browser/extension_registry.h" | 20 #include "extensions/browser/extension_registry.h" |
| 21 #include "extensions/browser/value_store/value_store.h" | 21 #include "extensions/browser/value_store/value_store.h" |
| 22 #include "extensions/common/extension.h" | 22 #include "extensions/common/extension.h" |
| 23 #include "extensions/common/extension_set.h" | 23 #include "extensions/common/extension_set.h" |
| 24 | 24 |
| 25 using content::BrowserThread; | |
| 26 using extensions::ExtensionRegistry; | 25 using extensions::ExtensionRegistry; |
| 27 using sync_datatype_helper::test; | 26 using sync_datatype_helper::test; |
| 28 | 27 |
| 29 namespace extension_settings_helper { | 28 namespace extension_settings_helper { |
| 30 | 29 |
| 31 namespace { | 30 namespace { |
| 32 | 31 |
| 33 std::string ToJson(const base::Value& value) { | 32 std::string ToJson(const base::Value& value) { |
| 34 std::string json; | 33 std::string json; |
| 35 base::JSONWriter::WriteWithOptions( | 34 base::JSONWriter::WriteWithOptions( |
| 36 value, base::JSONWriter::OPTIONS_PRETTY_PRINT, &json); | 35 value, base::JSONWriter::OPTIONS_PRETTY_PRINT, &json); |
| 37 return json; | 36 return json; |
| 38 } | 37 } |
| 39 | 38 |
| 40 void GetAllSettingsOnFileThread(base::DictionaryValue* out, | 39 void GetAllSettingsOnBackendSequence(base::DictionaryValue* out, |
| 41 base::WaitableEvent* signal, | 40 base::WaitableEvent* signal, |
| 42 ValueStore* storage) { | 41 ValueStore* storage) { |
| 43 CHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 42 EXPECT_TRUE(extensions::GetBackendTaskRunner()->RunsTasksInCurrentSequence()); |
| 44 out->Swap(&storage->Get()->settings()); | 43 out->Swap(&storage->Get()->settings()); |
| 45 signal->Signal(); | 44 signal->Signal(); |
| 46 } | 45 } |
| 47 | 46 |
| 48 std::unique_ptr<base::DictionaryValue> GetAllSettings(Profile* profile, | 47 std::unique_ptr<base::DictionaryValue> GetAllSettings(Profile* profile, |
| 49 const std::string& id) { | 48 const std::string& id) { |
| 50 base::WaitableEvent signal(base::WaitableEvent::ResetPolicy::AUTOMATIC, | 49 base::WaitableEvent signal(base::WaitableEvent::ResetPolicy::AUTOMATIC, |
| 51 base::WaitableEvent::InitialState::NOT_SIGNALED); | 50 base::WaitableEvent::InitialState::NOT_SIGNALED); |
| 52 std::unique_ptr<base::DictionaryValue> settings(new base::DictionaryValue()); | 51 std::unique_ptr<base::DictionaryValue> settings(new base::DictionaryValue()); |
| 53 extensions::StorageFrontend::Get(profile)->RunWithStorage( | 52 extensions::StorageFrontend::Get(profile)->RunWithStorage( |
| 54 ExtensionRegistry::Get(profile)->enabled_extensions().GetByID(id), | 53 ExtensionRegistry::Get(profile)->enabled_extensions().GetByID(id), |
| 55 extensions::settings_namespace::SYNC, | 54 extensions::settings_namespace::SYNC, |
| 56 base::Bind(&GetAllSettingsOnFileThread, settings.get(), &signal)); | 55 base::Bind(&GetAllSettingsOnBackendSequence, settings.get(), &signal)); |
| 57 signal.Wait(); | 56 signal.Wait(); |
| 58 return settings; | 57 return settings; |
| 59 } | 58 } |
| 60 | 59 |
| 61 bool AreSettingsSame(Profile* expected_profile, Profile* actual_profile) { | 60 bool AreSettingsSame(Profile* expected_profile, Profile* actual_profile) { |
| 62 const extensions::ExtensionSet& extensions = | 61 const extensions::ExtensionSet& extensions = |
| 63 ExtensionRegistry::Get(expected_profile)->enabled_extensions(); | 62 ExtensionRegistry::Get(expected_profile)->enabled_extensions(); |
| 64 if (extensions.size() != | 63 if (extensions.size() != |
| 65 ExtensionRegistry::Get(actual_profile)->enabled_extensions().size()) { | 64 ExtensionRegistry::Get(actual_profile)->enabled_extensions().size()) { |
| 66 ADD_FAILURE(); | 65 ADD_FAILURE(); |
| (...skipping 11 matching lines...) Expand all Loading... |
| 78 GetAllSettings(actual_profile, id)); | 77 GetAllSettings(actual_profile, id)); |
| 79 if (!expected->Equals(actual.get())) { | 78 if (!expected->Equals(actual.get())) { |
| 80 ADD_FAILURE() << | 79 ADD_FAILURE() << |
| 81 "Expected " << ToJson(*expected) << " got " << ToJson(*actual); | 80 "Expected " << ToJson(*expected) << " got " << ToJson(*actual); |
| 82 same = false; | 81 same = false; |
| 83 } | 82 } |
| 84 } | 83 } |
| 85 return same; | 84 return same; |
| 86 } | 85 } |
| 87 | 86 |
| 88 void SetSettingsOnFileThread( | 87 void SetSettingsOnBackendSequence(const base::DictionaryValue* settings, |
| 89 const base::DictionaryValue* settings, | 88 base::WaitableEvent* signal, |
| 90 base::WaitableEvent* signal, | 89 ValueStore* storage) { |
| 91 ValueStore* storage) { | 90 EXPECT_TRUE(extensions::GetBackendTaskRunner()->RunsTasksInCurrentSequence()); |
| 92 CHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | |
| 93 storage->Set(ValueStore::DEFAULTS, *settings); | 91 storage->Set(ValueStore::DEFAULTS, *settings); |
| 94 signal->Signal(); | 92 signal->Signal(); |
| 95 } | 93 } |
| 96 | 94 |
| 97 } // namespace | 95 } // namespace |
| 98 | 96 |
| 99 void SetExtensionSettings( | 97 void SetExtensionSettings( |
| 100 Profile* profile, | 98 Profile* profile, |
| 101 const std::string& id, | 99 const std::string& id, |
| 102 const base::DictionaryValue& settings) { | 100 const base::DictionaryValue& settings) { |
| 103 base::WaitableEvent signal(base::WaitableEvent::ResetPolicy::AUTOMATIC, | 101 base::WaitableEvent signal(base::WaitableEvent::ResetPolicy::AUTOMATIC, |
| 104 base::WaitableEvent::InitialState::NOT_SIGNALED); | 102 base::WaitableEvent::InitialState::NOT_SIGNALED); |
| 105 extensions::StorageFrontend::Get(profile)->RunWithStorage( | 103 extensions::StorageFrontend::Get(profile)->RunWithStorage( |
| 106 ExtensionRegistry::Get(profile)->enabled_extensions().GetByID(id), | 104 ExtensionRegistry::Get(profile)->enabled_extensions().GetByID(id), |
| 107 extensions::settings_namespace::SYNC, | 105 extensions::settings_namespace::SYNC, |
| 108 base::Bind(&SetSettingsOnFileThread, &settings, &signal)); | 106 base::Bind(&SetSettingsOnBackendSequence, &settings, &signal)); |
| 109 signal.Wait(); | 107 signal.Wait(); |
| 110 } | 108 } |
| 111 | 109 |
| 112 void SetExtensionSettingsForAllProfiles( | 110 void SetExtensionSettingsForAllProfiles( |
| 113 const std::string& id, const base::DictionaryValue& settings) { | 111 const std::string& id, const base::DictionaryValue& settings) { |
| 114 for (int i = 0; i < test()->num_clients(); ++i) | 112 for (int i = 0; i < test()->num_clients(); ++i) |
| 115 SetExtensionSettings(test()->GetProfile(i), id, settings); | 113 SetExtensionSettings(test()->GetProfile(i), id, settings); |
| 116 SetExtensionSettings(test()->verifier(), id, settings); | 114 SetExtensionSettings(test()->verifier(), id, settings); |
| 117 } | 115 } |
| 118 | 116 |
| 119 bool AllExtensionSettingsSameAsVerifier() { | 117 bool AllExtensionSettingsSameAsVerifier() { |
| 120 bool all_profiles_same = true; | 118 bool all_profiles_same = true; |
| 121 for (int i = 0; i < test()->num_clients(); ++i) { | 119 for (int i = 0; i < test()->num_clients(); ++i) { |
| 122 // &= so that all profiles are tested; analogous to EXPECT over ASSERT. | 120 // &= so that all profiles are tested; analogous to EXPECT over ASSERT. |
| 123 all_profiles_same &= | 121 all_profiles_same &= |
| 124 AreSettingsSame(test()->verifier(), test()->GetProfile(i)); | 122 AreSettingsSame(test()->verifier(), test()->GetProfile(i)); |
| 125 } | 123 } |
| 126 return all_profiles_same; | 124 return all_profiles_same; |
| 127 } | 125 } |
| 128 | 126 |
| 129 } // namespace extension_settings_helper | 127 } // namespace extension_settings_helper |
| OLD | NEW |