Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / sync / test / integration / extension_settings_helper.cc
blobe9414af1832959b60c5fbbc45a18232f82c63bc2
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.
5 #include "chrome/browser/sync/test/integration/extension_settings_helper.h"
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"
24 using content::BrowserThread;
25 using extensions::ExtensionRegistry;
26 using sync_datatype_helper::test;
28 namespace extension_settings_helper {
30 namespace {
32 std::string ToJson(const base::Value& value) {
33 std::string json;
34 base::JSONWriter::WriteWithOptions(
35 value, base::JSONWriter::OPTIONS_PRETTY_PRINT, &json);
36 return json;
39 void 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();
47 scoped_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 extensions::StorageFrontend::Get(profile)->RunWithStorage(
52 ExtensionRegistry::Get(profile)->enabled_extensions().GetByID(id),
53 extensions::settings_namespace::SYNC,
54 base::Bind(&GetAllSettingsOnFileThread, settings.get(), &signal));
55 signal.Wait();
56 return settings.Pass();
59 bool AreSettingsSame(Profile* expected_profile, Profile* actual_profile) {
60 const extensions::ExtensionSet& extensions =
61 ExtensionRegistry::Get(expected_profile)->enabled_extensions();
62 if (extensions.size() !=
63 ExtensionRegistry::Get(actual_profile)->enabled_extensions().size()) {
64 ADD_FAILURE();
65 return false;
68 bool same = true;
69 for (extensions::ExtensionSet::const_iterator it = extensions.begin();
70 it != extensions.end();
71 ++it) {
72 const std::string& id = (*it)->id();
73 scoped_ptr<base::DictionaryValue> expected(
74 GetAllSettings(expected_profile, id));
75 scoped_ptr<base::DictionaryValue> actual(
76 GetAllSettings(actual_profile, id));
77 if (!expected->Equals(actual.get())) {
78 ADD_FAILURE() <<
79 "Expected " << ToJson(*expected) << " got " << ToJson(*actual);
80 same = false;
83 return same;
86 void SetSettingsOnFileThread(
87 const base::DictionaryValue* settings,
88 base::WaitableEvent* signal,
89 ValueStore* storage) {
90 CHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
91 storage->Set(ValueStore::DEFAULTS, *settings);
92 signal->Signal();
95 } // namespace
97 void SetExtensionSettings(
98 Profile* profile,
99 const std::string& id,
100 const base::DictionaryValue& settings) {
101 base::WaitableEvent signal(false, false);
102 extensions::StorageFrontend::Get(profile)->RunWithStorage(
103 ExtensionRegistry::Get(profile)->enabled_extensions().GetByID(id),
104 extensions::settings_namespace::SYNC,
105 base::Bind(&SetSettingsOnFileThread, &settings, &signal));
106 signal.Wait();
109 void SetExtensionSettingsForAllProfiles(
110 const std::string& id, const base::DictionaryValue& settings) {
111 for (int i = 0; i < test()->num_clients(); ++i)
112 SetExtensionSettings(test()->GetProfile(i), id, settings);
113 SetExtensionSettings(test()->verifier(), id, settings);
116 bool AllExtensionSettingsSameAsVerifier() {
117 bool all_profiles_same = true;
118 for (int i = 0; i < test()->num_clients(); ++i) {
119 // &= so that all profiles are tested; analogous to EXPECT over ASSERT.
120 all_profiles_same &=
121 AreSettingsSame(test()->verifier(), test()->GetProfile(i));
123 return all_profiles_same;
126 } // namespace extension_settings_helper