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