1// Copyright 2014 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#ifndef EXTENSIONS_BROWSER_API_STORAGE_SETTINGS_TEST_UTIL_H_
6#define EXTENSIONS_BROWSER_API_STORAGE_SETTINGS_TEST_UTIL_H_
7
8#include <set>
9#include <string>
10
11#include "base/compiler_specific.h"
12#include "base/memory/linked_ptr.h"
13#include "base/memory/ref_counted.h"
14#include "base/memory/scoped_ptr.h"
15#include "chrome/browser/extensions/test_extension_system.h"
16#include "chrome/test/base/testing_profile.h"
17#include "extensions/browser/api/storage/settings_namespace.h"
18#include "extensions/browser/api/storage/settings_storage_factory.h"
19#include "extensions/browser/event_router.h"
20#include "extensions/common/extension.h"
21
22class ValueStore;
23
24namespace extensions {
25
26class StorageFrontend;
27// Utilities for extension settings API tests.
28namespace settings_test_util {
29
30// Synchronously gets the storage area for an extension from |frontend|.
31ValueStore* GetStorage(scoped_refptr<const Extension> extension,
32                       settings_namespace::Namespace setting_namespace,
33                       StorageFrontend* frontend);
34
35// Synchronously gets the SYNC storage for an extension from |frontend|.
36ValueStore* GetStorage(scoped_refptr<const Extension> extension,
37                       StorageFrontend* frontend);
38
39// Creates an extension with |id| and adds it to the registry for |profile|.
40scoped_refptr<const Extension> AddExtensionWithId(Profile* profile,
41                                                  const std::string& id,
42                                                  Manifest::Type type);
43
44// Creates an extension with |id| with a set of |permissions| and adds it to
45// the registry for |profile|.
46scoped_refptr<const Extension> AddExtensionWithIdAndPermissions(
47    Profile* profile,
48    const std::string& id,
49    Manifest::Type type,
50    const std::set<std::string>& permissions);
51
52// A mock ExtensionSystem to serve an EventRouter.
53class MockExtensionSystem : public TestExtensionSystem {
54 public:
55  explicit MockExtensionSystem(Profile* profile);
56  virtual ~MockExtensionSystem();
57
58  virtual EventRouter* event_router() OVERRIDE;
59
60 private:
61  scoped_ptr<EventRouter> event_router_;
62
63  DISALLOW_COPY_AND_ASSIGN(MockExtensionSystem);
64};
65
66// A Profile which returns an ExtensionSystem with enough functionality for
67// the tests.
68class MockProfile : public TestingProfile {
69 public:
70  explicit MockProfile(const base::FilePath& file_path);
71  virtual ~MockProfile();
72};
73
74// SettingsStorageFactory which acts as a wrapper for other factories.
75class ScopedSettingsStorageFactory : public SettingsStorageFactory {
76 public:
77  ScopedSettingsStorageFactory();
78
79  explicit ScopedSettingsStorageFactory(
80      const scoped_refptr<SettingsStorageFactory>& delegate);
81
82  // Sets the delegate factory (equivalent to scoped_ptr::reset).
83  void Reset(const scoped_refptr<SettingsStorageFactory>& delegate);
84
85  // SettingsStorageFactory implementation.
86  virtual ValueStore* Create(const base::FilePath& base_path,
87                             const std::string& extension_id) OVERRIDE;
88  virtual void DeleteDatabaseIfExists(
89      const base::FilePath& base_path,
90      const std::string& extension_id) OVERRIDE;
91
92 private:
93  // SettingsStorageFactory is refcounted.
94  virtual ~ScopedSettingsStorageFactory();
95
96  scoped_refptr<SettingsStorageFactory> delegate_;
97};
98
99}  // namespace settings_test_util
100
101}  // namespace extensions
102
103#endif  // EXTENSIONS_BROWSER_API_STORAGE_SETTINGS_TEST_UTIL_H_
104