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