| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2011 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/test/live_sync/live_extensions_sync_test_base.h" | |
| 6 | |
| 7 #include <string> | |
| 8 | |
| 9 #include "base/file_path.h" | |
| 10 #include "base/file_util.h" | |
| 11 #include "base/logging.h" | |
| 12 #include "base/memory/ref_counted.h" | |
| 13 #include "base/string_number_conversions.h" | |
| 14 #include "base/values.h" | |
| 15 #include "chrome/browser/extensions/extension_service.h" | |
| 16 #include "chrome/browser/profiles/profile.h" | |
| 17 #include "chrome/common/extensions/extension.h" | |
| 18 #include "chrome/common/extensions/extension_constants.h" | |
| 19 | |
| 20 LiveExtensionsSyncTestBase::LiveExtensionsSyncTestBase(TestType test_type) | |
| 21 : LiveSyncTest(test_type) {} | |
| 22 | |
| 23 LiveExtensionsSyncTestBase::~LiveExtensionsSyncTestBase() {} | |
| 24 | |
| 25 namespace { | |
| 26 | |
| 27 // TODO(akalin): Somehow unify this with MakeExtension() in | |
| 28 // extension_util_unittest.cc. | |
| 29 scoped_refptr<Extension> CreateExtension( | |
| 30 const ScopedTempDir& scoped_temp_dir, | |
| 31 bool is_theme, | |
| 32 int index) { | |
| 33 DictionaryValue source; | |
| 34 std::string name_prefix = is_theme ? "faketheme" : "fakeextension"; | |
| 35 source.SetString( | |
| 36 extension_manifest_keys::kName, | |
| 37 name_prefix + base::IntToString(index)); | |
| 38 source.SetString(extension_manifest_keys::kVersion, "0.0.0.0"); | |
| 39 if (is_theme) { | |
| 40 source.Set(extension_manifest_keys::kTheme, new DictionaryValue()); | |
| 41 } | |
| 42 FilePath extension_dir; | |
| 43 if (!file_util::CreateTemporaryDirInDir( | |
| 44 scoped_temp_dir.path(), FILE_PATH_LITERAL("fakeextension"), | |
| 45 &extension_dir)) { | |
| 46 return NULL; | |
| 47 } | |
| 48 std::string error; | |
| 49 scoped_refptr<Extension> extension = | |
| 50 Extension::Create(extension_dir, Extension::INTERNAL, | |
| 51 source, Extension::STRICT_ERROR_CHECKS, &error); | |
| 52 if (!error.empty()) { | |
| 53 LOG(WARNING) << error; | |
| 54 return NULL; | |
| 55 } | |
| 56 return extension; | |
| 57 } | |
| 58 | |
| 59 } // namespace | |
| 60 | |
| 61 bool LiveExtensionsSyncTestBase::SetupClients() { | |
| 62 if (!LiveSyncTest::SetupClients()) | |
| 63 return false; | |
| 64 | |
| 65 for (int i = 0; i < num_clients(); ++i) { | |
| 66 GetProfile(i)->InitExtensions(true); | |
| 67 } | |
| 68 verifier()->InitExtensions(true); | |
| 69 | |
| 70 if (!extension_base_dir_.CreateUniqueTempDir()) | |
| 71 return false; | |
| 72 | |
| 73 return true; | |
| 74 } | |
| 75 | |
| 76 scoped_refptr<Extension> LiveExtensionsSyncTestBase::GetExtensionHelper( | |
| 77 bool is_theme, int index) { | |
| 78 std::pair<bool, int> type_index = std::make_pair(is_theme, index); | |
| 79 ExtensionTypeIndexMap::const_iterator it = | |
| 80 extensions_by_type_index_.find(type_index); | |
| 81 if (it != extensions_by_type_index_.end()) { | |
| 82 return it->second; | |
| 83 } | |
| 84 scoped_refptr<Extension> extension = | |
| 85 CreateExtension(extension_base_dir_, is_theme, index); | |
| 86 if (!extension.get()) | |
| 87 return NULL; | |
| 88 extensions_by_type_index_[type_index] = extension; | |
| 89 extensions_by_id_[extension->id()] = extension; | |
| 90 return extension; | |
| 91 } | |
| 92 | |
| 93 scoped_refptr<Extension> LiveExtensionsSyncTestBase::GetTheme(int index) { | |
| 94 return GetExtensionHelper(true, index); | |
| 95 } | |
| 96 | |
| 97 scoped_refptr<Extension> LiveExtensionsSyncTestBase::GetExtension( | |
| 98 int index) { | |
| 99 return GetExtensionHelper(false, index); | |
| 100 } | |
| 101 | |
| 102 void LiveExtensionsSyncTestBase::InstallExtension( | |
| 103 Profile* profile, scoped_refptr<Extension> extension) { | |
| 104 CHECK(profile); | |
| 105 CHECK(extension.get()); | |
| 106 profile->GetExtensionService()->OnExtensionInstalled(extension); | |
| 107 } | |
| 108 | |
| 109 void LiveExtensionsSyncTestBase::InstallAllPendingExtensions( | |
| 110 Profile* profile) { | |
| 111 // TODO(akalin): Mock out the servers that the extensions auto-update | |
| 112 // mechanism talk to so as to more closely match what actually happens. | |
| 113 // Background networking will need to be re-enabled for extensions tests. | |
| 114 | |
| 115 // We make a copy here since InstallExtension() removes the | |
| 116 // extension from the extensions service's copy. | |
| 117 const PendingExtensionManager* pending_extension_manager = | |
| 118 profile->GetExtensionService()->pending_extension_manager(); | |
| 119 PendingExtensionManager::const_iterator it; | |
| 120 for (it = pending_extension_manager->begin(); | |
| 121 it != pending_extension_manager->end(); ++it) { | |
| 122 ExtensionIdMap::const_iterator it2 = extensions_by_id_.find(it->first); | |
| 123 CHECK(it2 != extensions_by_id_.end()); | |
| 124 InstallExtension(profile, it2->second); | |
| 125 } | |
| 126 } | |
| OLD | NEW |