| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef EXTENSIONS_BROWSER_API_TEST_UTILS_H_ | 5 #ifndef EXTENSIONS_BROWSER_API_TEST_UTILS_H_ |
| 6 #define EXTENSIONS_BROWSER_API_TEST_UTILS_H_ | 6 #define EXTENSIONS_BROWSER_API_TEST_UTILS_H_ |
| 7 | 7 |
| 8 #include <memory> | 8 #include <memory> |
| 9 #include <string> | 9 #include <string> |
| 10 | 10 |
| 11 #include "base/memory/ref_counted.h" | 11 #include "base/memory/ref_counted.h" |
| 12 #include "base/run_loop.h" |
| 13 #include "extensions/browser/extension_function.h" |
| 12 #include "extensions/common/manifest.h" | 14 #include "extensions/common/manifest.h" |
| 13 | 15 |
| 14 class UIThreadExtensionFunction; | |
| 15 | |
| 16 namespace base { | 16 namespace base { |
| 17 class DictionaryValue; | 17 class DictionaryValue; |
| 18 class ListValue; | 18 class ListValue; |
| 19 class Value; | 19 class Value; |
| 20 } | 20 } |
| 21 | 21 |
| 22 namespace content { | 22 namespace content { |
| 23 class BrowserContext; | 23 class BrowserContext; |
| 24 } | 24 } |
| 25 | 25 |
| 26 namespace extensions { | 26 namespace extensions { |
| 27 class Extension; | 27 class Extension; |
| 28 class ExtensionFunctionDispatcher; | 28 class ExtensionFunctionDispatcher; |
| 29 | 29 |
| 30 // TODO(yoz): crbug.com/394840: Remove duplicate functionality in | 30 // TODO(yoz): crbug.com/394840: Remove duplicate functionality in |
| 31 // chrome/browser/extensions/extension_function_test_utils.h. | 31 // chrome/browser/extensions/extension_function_test_utils.h. |
| 32 // | 32 // |
| 33 // TODO(ckehoe): Accept args as std::unique_ptr<base::Value>, | 33 // TODO(ckehoe): Accept args as std::unique_ptr<base::Value>, |
| 34 // and migrate existing users to the new API. | 34 // and migrate existing users to the new API. |
| 35 namespace api_test_utils { | 35 namespace api_test_utils { |
| 36 | 36 |
| 37 // A helper class to handle waiting for a function response. |
| 38 class SendResponseHelper { |
| 39 public: |
| 40 explicit SendResponseHelper(UIThreadExtensionFunction* function); |
| 41 ~SendResponseHelper(); |
| 42 |
| 43 bool has_response() { return response_.get() != nullptr; } |
| 44 |
| 45 // Asserts a response has been posted (has_response()) and returns the value. |
| 46 bool GetResponse(); |
| 47 |
| 48 // Waits until a response is posted. |
| 49 void WaitForResponse(); |
| 50 |
| 51 private: |
| 52 // Response handler. |
| 53 void OnResponse(ExtensionFunction::ResponseType response, |
| 54 const base::ListValue& results, |
| 55 const std::string& error, |
| 56 functions::HistogramValue histogram_value); |
| 57 |
| 58 base::RunLoop run_loop_; |
| 59 std::unique_ptr<bool> response_; |
| 60 |
| 61 DISALLOW_COPY_AND_ASSIGN(SendResponseHelper); |
| 62 }; |
| 63 |
| 37 enum RunFunctionFlags { NONE = 0, INCLUDE_INCOGNITO = 1 << 0 }; | 64 enum RunFunctionFlags { NONE = 0, INCLUDE_INCOGNITO = 1 << 0 }; |
| 38 | 65 |
| 39 // Parse JSON and return as the specified type, or NULL if the JSON is invalid | 66 // Parse JSON and return as the specified type, or NULL if the JSON is invalid |
| 40 // or not the specified type. | 67 // or not the specified type. |
| 41 std::unique_ptr<base::DictionaryValue> ParseDictionary(const std::string& data); | 68 std::unique_ptr<base::DictionaryValue> ParseDictionary(const std::string& data); |
| 42 | 69 |
| 43 // Get |key| from |val| as the specified type. If |key| does not exist, or is | 70 // Get |key| from |val| as the specified type. If |key| does not exist, or is |
| 44 // not of the specified type, adds a failure to the current test and returns | 71 // not of the specified type, adds a failure to the current test and returns |
| 45 // false, 0, empty string, etc. | 72 // false, 0, empty string, etc. |
| 46 bool GetBoolean(const base::DictionaryValue* val, const std::string& key); | 73 bool GetBoolean(const base::DictionaryValue* val, const std::string& key); |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 127 bool RunFunction(UIThreadExtensionFunction* function, | 154 bool RunFunction(UIThreadExtensionFunction* function, |
| 128 std::unique_ptr<base::ListValue> args, | 155 std::unique_ptr<base::ListValue> args, |
| 129 content::BrowserContext* context, | 156 content::BrowserContext* context, |
| 130 std::unique_ptr<ExtensionFunctionDispatcher> dispatcher, | 157 std::unique_ptr<ExtensionFunctionDispatcher> dispatcher, |
| 131 RunFunctionFlags flags); | 158 RunFunctionFlags flags); |
| 132 | 159 |
| 133 } // namespace api_test_utils | 160 } // namespace api_test_utils |
| 134 } // namespace extensions | 161 } // namespace extensions |
| 135 | 162 |
| 136 #endif // EXTENSIONS_BROWSER_API_TEST_UTILS_H_ | 163 #endif // EXTENSIONS_BROWSER_API_TEST_UTILS_H_ |
| OLD | NEW |