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 3 // found in the LICENSE file. 4 5 #include "chrome/browser/sync/test/integration/extension_settings_helper.h" 6 7 #include "base/bind.h" 8 #include "base/json/json_writer.h" 9 #include "base/logging.h" 10 #include "base/memory/scoped_ptr.h" 11 #include "base/synchronization/waitable_event.h" 12 #include "base/values.h" 13 #include "chrome/browser/extensions/api/storage/settings_frontend.h" 14 #include "chrome/browser/extensions/extension_service.h" 15 #include "chrome/browser/profiles/profile.h" 16 #include "chrome/browser/sync/test/integration/extensions_helper.h" 17 #include "chrome/browser/sync/test/integration/sync_datatype_helper.h" 18 #include "chrome/browser/sync/test/integration/sync_extension_helper.h" 19 #include "chrome/browser/value_store/value_store.h" 20 #include "chrome/common/extensions/extension_set.h" 21 #include "content/public/browser/browser_thread.h" 22 #include "extensions/common/extension.h" 23 24 using content::BrowserThread; 25 using sync_datatype_helper::test; 26 27 namespace extension_settings_helper { 28 29 namespace { 30 31 std::string ToJson(const Value& value) { 32 std::string json; 33 base::JSONWriter::WriteWithOptions(&value, 34 base::JSONWriter::OPTIONS_PRETTY_PRINT, 35 &json); 36 return json; 37 } 38 39 void GetAllSettingsOnFileThread(DictionaryValue* out, 40 base::WaitableEvent* signal, 41 ValueStore* storage) { 42 CHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); 43 out->Swap(&storage->Get()->settings()); 44 signal->Signal(); 45 } 46 47 scoped_ptr<DictionaryValue> GetAllSettings( 48 Profile* profile, const std::string& id) { 49 base::WaitableEvent signal(false, false); 50 scoped_ptr<DictionaryValue> settings(new DictionaryValue()); 51 profile->GetExtensionService()->settings_frontend()->RunWithStorage( 52 id, 53 extensions::settings_namespace::SYNC, 54 base::Bind(&GetAllSettingsOnFileThread, settings.get(), &signal)); 55 signal.Wait(); 56 return settings.Pass(); 57 } 58 59 bool AreSettingsSame(Profile* expected_profile, Profile* actual_profile) { 60 const ExtensionSet* extensions = 61 expected_profile->GetExtensionService()->extensions(); 62 if (extensions->size() != 63 actual_profile->GetExtensionService()->extensions()->size()) { 64 ADD_FAILURE(); 65 return false; 66 } 67 68 bool same = true; 69 for (ExtensionSet::const_iterator it = extensions->begin(); 70 it != extensions->end(); ++it) { 71 const std::string& id = (*it)->id(); 72 scoped_ptr<DictionaryValue> expected(GetAllSettings(expected_profile, id)); 73 scoped_ptr<DictionaryValue> actual(GetAllSettings(actual_profile, id)); 74 if (!expected->Equals(actual.get())) { 75 ADD_FAILURE() << 76 "Expected " << ToJson(*expected) << " got " << ToJson(*actual); 77 same = false; 78 } 79 } 80 return same; 81 } 82 83 void SetSettingsOnFileThread( 84 const DictionaryValue* settings, 85 base::WaitableEvent* signal, 86 ValueStore* storage) { 87 CHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); 88 storage->Set(ValueStore::DEFAULTS, *settings); 89 signal->Signal(); 90 } 91 92 } // namespace 93 94 void SetExtensionSettings( 95 Profile* profile, const std::string& id, const DictionaryValue& settings) { 96 base::WaitableEvent signal(false, false); 97 profile->GetExtensionService()->settings_frontend()->RunWithStorage( 98 id, 99 extensions::settings_namespace::SYNC, 100 base::Bind(&SetSettingsOnFileThread, &settings, &signal)); 101 signal.Wait(); 102 } 103 104 void SetExtensionSettingsForAllProfiles( 105 const std::string& id, const DictionaryValue& settings) { 106 for (int i = 0; i < test()->num_clients(); ++i) 107 SetExtensionSettings(test()->GetProfile(i), id, settings); 108 SetExtensionSettings(test()->verifier(), id, settings); 109 } 110 111 bool AllExtensionSettingsSameAsVerifier() { 112 bool all_profiles_same = true; 113 for (int i = 0; i < test()->num_clients(); ++i) { 114 // &= so that all profiles are tested; analogous to EXPECT over ASSERT. 115 all_profiles_same &= 116 AreSettingsSame(test()->verifier(), test()->GetProfile(i)); 117 } 118 return all_profiles_same; 119 } 120 121 } // namespace extension_settings_helper 122