test_extension_prefs.cc revision 21d179b334e59e9a3bfcaed4c4430bef1bc5759d
1// 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/message_loop.h"
9#include "base/message_loop_proxy.h"
10#include "base/scoped_ptr.h"
11#include "base/values.h"
12#include "chrome/browser/browser_thread.h"
13#include "chrome/browser/extensions/extension_pref_store.h"
14#include "chrome/browser/extensions/extension_prefs.h"
15#include "chrome/browser/prefs/pref_service.h"
16#include "chrome/browser/prefs/pref_service_mock_builder.h"
17#include "chrome/browser/prefs/pref_value_store.h"
18#include "chrome/common/extensions/extension.h"
19#include "chrome/common/extensions/extension_constants.h"
20#include "chrome/common/json_pref_store.h"
21#include "testing/gtest/include/gtest/gtest.h"
22
23namespace {
24
25// Mock ExtensionPrefs class with artificial clock to guarantee that no two
26// extensions get the same installation time stamp and we can reliably
27// assert the installation order in the tests below.
28class MockExtensionPrefs : public ExtensionPrefs {
29 public:
30  MockExtensionPrefs(PrefService* prefs,
31                     const FilePath& root_dir_,
32                     ExtensionPrefStore* pref_store)
33    : ExtensionPrefs(prefs, root_dir_, pref_store),
34      currentTime(base::Time::Now()) {}
35  ~MockExtensionPrefs() {}
36
37 protected:
38  mutable base::Time currentTime;
39
40  virtual base::Time GetCurrentTime() const {
41    currentTime += base::TimeDelta::FromSeconds(10);
42    return currentTime;
43  }
44};
45
46}  // namespace
47
48TestExtensionPrefs::TestExtensionPrefs() : pref_service_(NULL) {
49  EXPECT_TRUE(temp_dir_.CreateUniqueTempDir());
50  preferences_file_ = temp_dir_.path().AppendASCII("Preferences");
51  extensions_dir_ = temp_dir_.path().AppendASCII("Extensions");
52  EXPECT_TRUE(file_util::CreateDirectory(extensions_dir_));
53
54  RecreateExtensionPrefs();
55}
56
57TestExtensionPrefs::~TestExtensionPrefs() {}
58
59void TestExtensionPrefs::RecreateExtensionPrefs() {
60  // We persist and reload the PrefService's PrefStores because this process
61  // deletes all empty dictionaries. The ExtensionPrefs implementation
62  // needs to be able to handle this situation.
63  if (pref_service_.get()) {
64    // The PrefService writes its persistent file on the file thread, so we
65    // need to wait for any pending I/O to complete before creating a new
66    // PrefService.
67    MessageLoop file_loop;
68    BrowserThread file_thread(BrowserThread::FILE, &file_loop);
69    pref_service_->SavePersistentPrefs();
70    file_loop.RunAllPending();
71  }
72
73  ExtensionPrefStore* pref_store = new ExtensionPrefStore;
74  PrefServiceMockBuilder builder;
75  builder.WithUserFilePrefs(preferences_file_);
76  builder.WithExtensionPrefs(pref_store);
77  pref_service_.reset(builder.Create());
78  ExtensionPrefs::RegisterUserPrefs(pref_service_.get());
79  prefs_.reset(new MockExtensionPrefs(pref_service_.get(), temp_dir_.path(),
80                                      pref_store));
81}
82
83scoped_refptr<Extension> TestExtensionPrefs::AddExtension(std::string name) {
84  DictionaryValue dictionary;
85  dictionary.SetString(extension_manifest_keys::kName, name);
86  dictionary.SetString(extension_manifest_keys::kVersion, "0.1");
87  return AddExtensionWithManifest(dictionary, Extension::INTERNAL);
88}
89
90scoped_refptr<Extension> TestExtensionPrefs::AddExtensionWithManifest(
91    const DictionaryValue& manifest, Extension::Location location) {
92  std::string name;
93  EXPECT_TRUE(manifest.GetString(extension_manifest_keys::kName, &name));
94  FilePath path =  extensions_dir_.AppendASCII(name);
95  std::string errors;
96  scoped_refptr<Extension> extension = Extension::Create(
97      path, location, manifest, false, &errors);
98  EXPECT_TRUE(extension);
99  if (!extension)
100    return NULL;
101
102  EXPECT_TRUE(Extension::IdIsValid(extension->id()));
103  const bool kInitialIncognitoEnabled = false;
104  prefs_->OnExtensionInstalled(extension, Extension::ENABLED,
105                               kInitialIncognitoEnabled);
106  return extension;
107}
108
109std::string TestExtensionPrefs::AddExtensionAndReturnId(std::string name) {
110  scoped_refptr<Extension> extension(AddExtension(name));
111  return extension->id();
112}
113