Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / sync / test / integration / extension_settings_helper.cc
blobf576abada01cb6356abe9a8ecb891b88dde2b39b
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/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"
24 using content::BrowserThread;
25 using sync_datatype_helper::test;
27 namespace extension_settings_helper {
29 namespace {
31 std::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;
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 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();
59 bool 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;
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;
82 return same;
85 void 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();
94 } // namespace
96 void 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(
103 extensions::settings_namespace::SYNC,
104 base::Bind(&SetSettingsOnFileThread, &settings, &signal));
105 signal.Wait();
108 void 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);
115 bool 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));
122 return all_profiles_same;
125 } // namespace extension_settings_helper