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 "components/pref_registry/testing_pref_service_syncable.h"
13 #include "extensions/browser/extensions_test.h"
17 class DictionaryValue
;
22 class NotificationService
;
23 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 ~ApiUnitTest() override
;
42 content::WebContents
* contents() { return contents_
.get(); }
43 const Extension
* extension() const { return extension_
.get(); }
44 scoped_refptr
<Extension
> extension_ref() { return extension_
; }
45 void set_extension(scoped_refptr
<Extension
> extension
) {
46 extension_
= extension
;
50 // SetUp creates and loads an empty, unpacked Extension.
51 void SetUp() override
;
53 // Creates a background page for |extension_|, and sets it for the WebContents
54 // to be used in API calls.
55 // If |contents_| is already set, this does nothing.
56 void CreateBackgroundPage();
58 // Various ways of running an API function. These methods take ownership of
59 // |function|. |args| should be in JSON format, wrapped in a list.
60 // See also the RunFunction* methods in extension_function_test_utils.h.
62 // Return the function result as a base::Value.
63 scoped_ptr
<base::Value
> RunFunctionAndReturnValue(
64 UIThreadExtensionFunction
* function
,
65 const std::string
& args
);
67 // Return the function result as a base::DictionaryValue, or NULL.
68 // This will EXPECT-fail if the result is not a DictionaryValue.
69 scoped_ptr
<base::DictionaryValue
> RunFunctionAndReturnDictionary(
70 UIThreadExtensionFunction
* function
,
71 const std::string
& args
);
73 // Return the function result as a base::ListValue, or NULL.
74 // This will EXPECT-fail if the result is not a ListValue.
75 scoped_ptr
<base::ListValue
> RunFunctionAndReturnList(
76 UIThreadExtensionFunction
* function
,
77 const std::string
& args
);
79 // Return an error thrown from the function, if one exists.
80 // This will EXPECT-fail if any result is returned from the function.
81 std::string
RunFunctionAndReturnError(UIThreadExtensionFunction
* function
,
82 const std::string
& args
);
84 // Run the function and ignore any result.
85 void RunFunction(UIThreadExtensionFunction
* function
,
86 const std::string
& args
);
89 scoped_ptr
<content::NotificationService
> notification_service_
;
91 scoped_ptr
<content::TestBrowserThreadBundle
> thread_bundle_
;
92 user_prefs::TestingPrefServiceSyncable testing_pref_service_
;
94 // The WebContents used to associate a RenderViewHost with API function calls,
96 scoped_ptr
<content::WebContents
> contents_
;
98 // The Extension used when running API function calls.
99 scoped_refptr
<Extension
> extension_
;
102 } // namespace extensions
104 #endif // EXTENSIONS_BROWSER_API_UNITTEST_H_