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_UNITTEST_H_
6#define EXTENSIONS_BROWSER_API_UNITTEST_H_
7
8#include <string>
9
10#include "base/memory/ref_counted.h"
11#include "base/memory/scoped_ptr.h"
12#include "base/prefs/testing_pref_service.h"
13#include "extensions/browser/extensions_test.h"
14#include "extensions/browser/mock_extension_system.h"
15
16namespace base {
17class Value;
18class DictionaryValue;
19class ListValue;
20}
21
22namespace content {
23class NotificationService;
24class TestBrowserThreadBundle;
25}
26
27class UIThreadExtensionFunction;
28
29namespace extensions {
30
31// Use this class to enable calling API functions in a unittest.
32// By default, this class will create and load an empty unpacked |extension_|,
33// which will be used in all API function calls. This extension can be
34// overridden using set_extension().
35// When calling RunFunction[AndReturn*], |args| should be in JSON format,
36// wrapped in a list. See also RunFunction* in api_test_utils.h.
37class ApiUnitTest : public ExtensionsTest {
38 public:
39  ApiUnitTest();
40  virtual ~ApiUnitTest();
41
42  const Extension* extension() const { return extension_.get(); }
43  scoped_refptr<Extension> extension_ref() { return extension_; }
44  void set_extension(scoped_refptr<Extension> extension) {
45    extension_ = extension;
46  }
47
48 protected:
49  // SetUp creates and loads an empty, unpacked Extension.
50  virtual void SetUp() OVERRIDE;
51
52  // Various ways of running an API function. These methods take ownership of
53  // |function|. |args| should be in JSON format, wrapped in a list.
54  // See also the RunFunction* methods in extension_function_test_utils.h.
55
56  // Return the function result as a base::Value.
57  scoped_ptr<base::Value> RunFunctionAndReturnValue(
58      UIThreadExtensionFunction* function,
59      const std::string& args);
60
61  // Return the function result as a base::DictionaryValue, or NULL.
62  // This will EXPECT-fail if the result is not a DictionaryValue.
63  scoped_ptr<base::DictionaryValue> RunFunctionAndReturnDictionary(
64      UIThreadExtensionFunction* function,
65      const std::string& args);
66
67  // Return the function result as a base::ListValue, or NULL.
68  // This will EXPECT-fail if the result is not a ListValue.
69  scoped_ptr<base::ListValue> RunFunctionAndReturnList(
70      UIThreadExtensionFunction* function,
71      const std::string& args);
72
73  // Return an error thrown from the function, if one exists.
74  // This will EXPECT-fail if any result is returned from the function.
75  std::string RunFunctionAndReturnError(UIThreadExtensionFunction* function,
76                                        const std::string& args);
77
78  // Run the function and ignore any result.
79  void RunFunction(UIThreadExtensionFunction* function,
80                   const std::string& args);
81
82 private:
83  scoped_ptr<content::NotificationService> notification_service_;
84
85  scoped_ptr<content::TestBrowserThreadBundle> thread_bundle_;
86  TestingPrefServiceSimple testing_pref_service_;
87
88  MockExtensionSystemFactory<MockExtensionSystem> extension_system_factory_;
89
90  // The Extension used when running API function calls.
91  scoped_refptr<Extension> extension_;
92};
93
94}  // namespace extensions
95
96#endif  // EXTENSIONS_BROWSER_API_UNITTEST_H_
97