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"
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
{
32 std::string
ToJson(const base::Value
& value
) {
34 base::JSONWriter::WriteWithOptions(
35 value
, base::JSONWriter::OPTIONS_PRETTY_PRINT
, &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());
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
));
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()) {
69 for (extensions::ExtensionSet::const_iterator it
= extensions
.begin();
70 it
!= extensions
.end();
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())) {
79 "Expected " << ToJson(*expected
) << " got " << ToJson(*actual
);
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
);
97 void SetExtensionSettings(
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
));
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.
121 AreSettingsSame(test()->verifier(), test()->GetProfile(i
));
123 return all_profiles_same
;
126 } // namespace extension_settings_helper