extension_settings_helper.cc revision 5d1f7b1de12d16ceb2c938c56701a3e8bfa558f7
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 "content/public/browser/browser_thread.h" 21#include "extensions/common/extension.h" 22#include "extensions/common/extension_set.h" 23 24using content::BrowserThread; 25using sync_datatype_helper::test; 26 27namespace extension_settings_helper { 28 29namespace { 30 31std::string ToJson(const base::Value& value) { 32 std::string json; 33 base::JSONWriter::WriteWithOptions(&value, 34 base::JSONWriter::OPTIONS_PRETTY_PRINT, 35 &json); 36 return json; 37} 38 39void GetAllSettingsOnFileThread(base::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 47scoped_ptr<base::DictionaryValue> GetAllSettings( 48 Profile* profile, const std::string& id) { 49 base::WaitableEvent signal(false, false); 50 scoped_ptr<base::DictionaryValue> settings(new base::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 59bool AreSettingsSame(Profile* expected_profile, Profile* actual_profile) { 60 const extensions::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 (extensions::ExtensionSet::const_iterator it = extensions->begin(); 70 it != extensions->end(); ++it) { 71 const std::string& id = (*it)->id(); 72 scoped_ptr<base::DictionaryValue> expected( 73 GetAllSettings(expected_profile, id)); 74 scoped_ptr<base::DictionaryValue> actual( 75 GetAllSettings(actual_profile, id)); 76 if (!expected->Equals(actual.get())) { 77 ADD_FAILURE() << 78 "Expected " << ToJson(*expected) << " got " << ToJson(*actual); 79 same = false; 80 } 81 } 82 return same; 83} 84 85void SetSettingsOnFileThread( 86 const base::DictionaryValue* settings, 87 base::WaitableEvent* signal, 88 ValueStore* storage) { 89 CHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); 90 storage->Set(ValueStore::DEFAULTS, *settings); 91 signal->Signal(); 92} 93 94} // namespace 95 96void SetExtensionSettings( 97 Profile* profile, 98 const std::string& id, 99 const base::DictionaryValue& settings) { 100 base::WaitableEvent signal(false, false); 101 profile->GetExtensionService()->settings_frontend()->RunWithStorage( 102 id, 103 extensions::settings_namespace::SYNC, 104 base::Bind(&SetSettingsOnFileThread, &settings, &signal)); 105 signal.Wait(); 106} 107 108void SetExtensionSettingsForAllProfiles( 109 const std::string& id, const base::DictionaryValue& settings) { 110 for (int i = 0; i < test()->num_clients(); ++i) 111 SetExtensionSettings(test()->GetProfile(i), id, settings); 112 SetExtensionSettings(test()->verifier(), id, settings); 113} 114 115bool AllExtensionSettingsSameAsVerifier() { 116 bool all_profiles_same = true; 117 for (int i = 0; i < test()->num_clients(); ++i) { 118 // &= so that all profiles are tested; analogous to EXPECT over ASSERT. 119 all_profiles_same &= 120 AreSettingsSame(test()->verifier(), test()->GetProfile(i)); 121 } 122 return all_profiles_same; 123} 124 125} // namespace extension_settings_helper 126