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.
5 #ifndef EXTENSIONS_BROWSER_API_UNITTEST_H_
6 #define EXTENSIONS_BROWSER_API_UNITTEST_H_
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"
18 class DictionaryValue
;
23 class NotificationService
;
24 class TestBrowserThreadBundle
;
27 class UIThreadExtensionFunction
;
29 namespace extensions
{
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.
37 class ApiUnitTest
: public ExtensionsTest
{
40 virtual ~ApiUnitTest();
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
;
49 // SetUp creates and loads an empty, unpacked Extension.
50 virtual void SetUp() OVERRIDE
;
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.
56 // Return the function result as a base::Value.
57 scoped_ptr
<base::Value
> RunFunctionAndReturnValue(
58 UIThreadExtensionFunction
* function
,
59 const std::string
& args
);
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
);
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
);
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
);
78 // Run the function and ignore any result.
79 void RunFunction(UIThreadExtensionFunction
* function
,
80 const std::string
& args
);
83 scoped_ptr
<content::NotificationService
> notification_service_
;
85 scoped_ptr
<content::TestBrowserThreadBundle
> thread_bundle_
;
86 TestingPrefServiceSimple testing_pref_service_
;
88 MockExtensionSystemFactory
<MockExtensionSystem
> extension_system_factory_
;
90 // The Extension used when running API function calls.
91 scoped_refptr
<Extension
> extension_
;
94 } // namespace extensions
96 #endif // EXTENSIONS_BROWSER_API_UNITTEST_H_