blob: e9a1472b8ad60cf6925fea751a5f173d78f9ced2 [file] [log] [blame]
[email protected]63c64d12010-04-27 21:21:341// Copyright (c) 2010 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/extensions/test_extension_prefs.h"
6
7#include "base/file_util.h"
8#include "base/logging.h"
9#include "base/message_loop.h"
10#include "chrome/browser/chrome_thread.h"
11#include "chrome/browser/extensions/extension_prefs.h"
12#include "chrome/browser/json_pref_store.h"
13#include "chrome/browser/pref_service.h"
14#include "chrome/common/extensions/extension.h"
15#include "chrome/common/extensions/extension_constants.h"
16#include "testing/gtest/include/gtest/gtest.h"
17
18TestExtensionPrefs::TestExtensionPrefs() {
19 EXPECT_TRUE(temp_dir_.CreateUniqueTempDir());
20 preferences_file_ = temp_dir_.path().AppendASCII("Preferences");
21 extensions_dir_ = temp_dir_.path().AppendASCII("Extensions");
22 EXPECT_TRUE(file_util::CreateDirectory(extensions_dir_));
23
24 RecreateExtensionPrefs();
25}
26
27TestExtensionPrefs::~TestExtensionPrefs() {}
28
29void TestExtensionPrefs::RecreateExtensionPrefs() {
30 if (pref_service_.get()) {
31 // The PrefService writes its persistent file on the file thread, so we
32 // need to wait for any pending I/O to complete before creating a new
33 // PrefService.
34 MessageLoop file_loop;
35 ChromeThread file_thread(ChromeThread::FILE, &file_loop);
36 pref_service_->SavePersistentPrefs();
37 file_loop.RunAllPending();
38 }
39
40 pref_service_.reset(new PrefService(new JsonPrefStore(preferences_file_)));
41 ExtensionPrefs::RegisterUserPrefs(pref_service_.get());
42 prefs_.reset(new ExtensionPrefs(pref_service_.get(), temp_dir_.path()));
43}
44
45Extension* TestExtensionPrefs::AddExtension(std::string name) {
46 DictionaryValue dictionary;
47 dictionary.SetString(extension_manifest_keys::kName, name);
48 dictionary.SetString(extension_manifest_keys::kVersion, "0.1");
49 return AddExtensionWithManifest(dictionary);
50}
51
52Extension* TestExtensionPrefs::AddExtensionWithManifest(
53 const DictionaryValue& manifest) {
54 std::string name;
55 EXPECT_TRUE(manifest.GetString(extension_manifest_keys::kName, &name));
56 FilePath path = extensions_dir_.AppendASCII(name);
57 Extension* extension = new Extension(path);
58 std::string errors;
59 EXPECT_TRUE(extension->InitFromValue(manifest, false, &errors));
60 extension->set_location(Extension::INTERNAL);
61 EXPECT_TRUE(Extension::IdIsValid(extension->id()));
62 prefs_->OnExtensionInstalled(extension);
63 return extension;
64}
65
66std::string TestExtensionPrefs::AddExtensionAndReturnId(std::string name) {
67 scoped_ptr<Extension> extension(AddExtension(name));
68 return extension->id();
69}