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/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
{
31 std::string
ToJson(const base::Value
& value
) {
33 base::JSONWriter::WriteWithOptions(&value
,
34 base::JSONWriter::OPTIONS_PRETTY_PRINT
,
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 profile
->GetExtensionService()->settings_frontend()->RunWithStorage(
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 expected_profile
->GetExtensionService()->extensions();
62 if (extensions
->size() !=
63 actual_profile
->GetExtensionService()->extensions()->size()) {
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())) {
78 "Expected " << ToJson(*expected
) << " got " << ToJson(*actual
);
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
);
96 void SetExtensionSettings(
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
));
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.
120 AreSettingsSame(test()->verifier(), test()->GetProfile(i
));
122 return all_profiles_same
;
125 } // namespace extension_settings_helper