71bb591a29f0334fd040543ce0b2b32bbe707bd2
[platform/framework/web/crosswalk.git] / src / chrome / browser / extensions / extension_api_unittest.h
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 CHROME_BROWSER_EXTENSIONS_EXTENSION_API_UNITTEST_H_
6 #define CHROME_BROWSER_EXTENSIONS_EXTENSION_API_UNITTEST_H_
7
8 #include <string>
9
10 #include "base/memory/ref_counted.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "chrome/test/base/browser_with_test_window_test.h"
13
14 namespace base {
15 class Value;
16 class DictionaryValue;
17 class ListValue;
18 }
19
20 namespace content {
21 class WebContents;
22 }
23
24 class UIThreadExtensionFunction;
25
26 namespace extensions {
27
28 // Use this class to enable calling API functions in a unittest.
29 // By default, this class will create and load an empty unpacked |extension_|,
30 // which will be used in all API function calls. This extension can be
31 // overridden using set_extension().
32 // By default, this class does not create a WebContents for the API functions.
33 // If a WebContents is needed, calling CreateBackgroundPage() will create a
34 // background page for the extension and use it in API function calls. (If
35 // needed, this could be expanded to allow for alternate WebContents).
36 // When calling RunFunction[AndReturn*], |args| should be in JSON format,
37 // wrapped in a list. See also RunFunction* in extension_function_test_utils.h.
38 class ExtensionApiUnittest : public BrowserWithTestWindowTest {
39  public:
40   ExtensionApiUnittest();
41   virtual ~ExtensionApiUnittest();
42
43   content::WebContents* contents() { return contents_; }
44
45   const Extension* extension() const { return extension_.get(); }
46   scoped_refptr<Extension> extension_ref() { return extension_; }
47   void set_extension(scoped_refptr<Extension> extension) {
48     extension_ = extension;
49   }
50
51  protected:
52   // SetUp creates and loads an empty, unpacked Extension.
53   virtual void SetUp() OVERRIDE;
54
55   // Creates a background page for |extension_|, and sets it for the WebContents
56   // to be used in API calls.
57   // If |contents_| is already set, this does nothing.
58   void CreateBackgroundPage();
59
60   // Various ways of running an API function. These methods take ownership of
61   // |function|. |args| should be in JSON format, wrapped in a list.
62   // See also the RunFunction* methods in extension_function_test_utils.h.
63
64   // Return the function result as a base::Value.
65   scoped_ptr<base::Value> RunFunctionAndReturnValue(
66       UIThreadExtensionFunction* function, const std::string& args);
67
68   // Return the function result as a base::DictionaryValue, or NULL.
69   // This will EXPECT-fail if the result is not a DictionaryValue.
70   scoped_ptr<base::DictionaryValue> RunFunctionAndReturnDictionary(
71       UIThreadExtensionFunction* function, const std::string& args);
72
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, const std::string& args);
77
78   // Return an error thrown from the function, if one exists.
79   // This will EXPECT-fail if any result is returned from the function.
80   std::string RunFunctionAndReturnError(
81       UIThreadExtensionFunction* function, const std::string& args);
82
83   // Run the function and ignore any result.
84   void RunFunction(
85       UIThreadExtensionFunction* function, const std::string& args);
86
87  private:
88   // The WebContents used to associate a RenderViewHost with API function calls,
89   // or NULL.
90   content::WebContents* contents_;
91
92   // The Extension used when running API function calls.
93   scoped_refptr<Extension> extension_;
94 };
95
96 }  // namespace extensions
97
98 #endif  // CHROME_BROWSER_EXTENSIONS_EXTENSION_API_UNITTEST_H_