Chromium Code Reviews| 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 #include "extensions/browser/api_test_utils.h" | 5 #include "extensions/browser/api_test_utils.h" |
| 6 | 6 |
| 7 #include "base/json/json_reader.h" | 7 #include "base/json/json_reader.h" |
| 8 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
| 9 #include "base/values.h" | 9 #include "base/values.h" |
| 10 #include "content/public/browser/browser_context.h" | 10 #include "content/public/browser/browser_context.h" |
| 11 #include "content/public/test/test_utils.h" | 11 #include "content/public/test/test_utils.h" |
| 12 #include "extensions/browser/extension_function.h" | 12 #include "extensions/browser/extension_function.h" |
| 13 #include "extensions/browser/extension_function_dispatcher.h" | 13 #include "extensions/browser/extension_function_dispatcher.h" |
| 14 #include "extensions/common/extension_builder.h" | |
| 14 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
| 15 | 16 |
| 17 using extensions::ExtensionFunctionDispatcher; | |
| 18 | |
| 16 namespace { | 19 namespace { |
| 17 | 20 |
| 21 class TestFunctionDispatcherDelegate | |
| 22 : public ExtensionFunctionDispatcher::Delegate { | |
| 23 public: | |
| 24 TestFunctionDispatcherDelegate() {} | |
| 25 virtual ~TestFunctionDispatcherDelegate() {} | |
| 26 | |
| 27 // NULL implementation. | |
| 28 private: | |
| 29 DISALLOW_COPY_AND_ASSIGN(TestFunctionDispatcherDelegate); | |
| 30 }; | |
| 31 | |
| 18 base::Value* ParseJSON(const std::string& data) { | 32 base::Value* ParseJSON(const std::string& data) { |
| 19 return base::JSONReader::Read(data); | 33 return base::JSONReader::Read(data); |
| 20 } | 34 } |
| 21 | 35 |
| 22 base::ListValue* ParseList(const std::string& data) { | 36 base::ListValue* ParseList(const std::string& data) { |
| 23 base::Value* result = ParseJSON(data); | 37 base::Value* result = ParseJSON(data); |
| 24 base::ListValue* list = NULL; | 38 base::ListValue* list = NULL; |
| 25 result->GetAsList(&list); | 39 result->GetAsList(&list); |
| 26 return list; | 40 return list; |
| 27 } | 41 } |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 62 scoped_ptr<bool> response_; | 76 scoped_ptr<bool> response_; |
| 63 bool should_post_quit_; | 77 bool should_post_quit_; |
| 64 }; | 78 }; |
| 65 | 79 |
| 66 } // namespace | 80 } // namespace |
| 67 | 81 |
| 68 namespace extensions { | 82 namespace extensions { |
| 69 | 83 |
| 70 namespace api_test_utils { | 84 namespace api_test_utils { |
| 71 | 85 |
| 86 scoped_refptr<Extension> CreateEmptyExtension() { | |
|
Yoyo Zhou
2014/08/08 01:28:04
I recently found extensions/common/test_util.h. I
Daniel Nishi
2014/08/08 17:24:08
I think it looks better, too.
Done.
| |
| 87 scoped_refptr<Extension> empty_extension( | |
| 88 ExtensionBuilder() | |
| 89 .SetManifest( | |
| 90 DictionaryBuilder().Set("name", "Test").Set("version", "1.0")) | |
| 91 .Build()); | |
| 92 return empty_extension; | |
| 93 } | |
| 94 | |
| 72 base::Value* RunFunctionAndReturnSingleResult( | 95 base::Value* RunFunctionAndReturnSingleResult( |
| 73 UIThreadExtensionFunction* function, | 96 UIThreadExtensionFunction* function, |
| 74 const std::string& args, | 97 const std::string& args, |
| 75 content::BrowserContext* context, | 98 content::BrowserContext* context, |
| 76 scoped_ptr<extensions::ExtensionFunctionDispatcher> dispatcher) { | 99 scoped_ptr<extensions::ExtensionFunctionDispatcher> dispatcher) { |
| 77 return RunFunctionAndReturnSingleResult( | 100 return RunFunctionAndReturnSingleResult( |
| 78 function, args, context, dispatcher.Pass(), NONE); | 101 function, args, context, dispatcher.Pass(), NONE); |
| 79 } | 102 } |
| 80 | 103 |
| 81 base::Value* RunFunctionAndReturnSingleResult( | 104 base::Value* RunFunctionAndReturnSingleResult( |
| 82 UIThreadExtensionFunction* function, | 105 UIThreadExtensionFunction* function, |
| 83 const std::string& args, | 106 const std::string& args, |
| 84 content::BrowserContext* context, | 107 content::BrowserContext* context, |
| 85 scoped_ptr<extensions::ExtensionFunctionDispatcher> dispatcher, | 108 scoped_ptr<extensions::ExtensionFunctionDispatcher> dispatcher, |
| 86 RunFunctionFlags flags) { | 109 RunFunctionFlags flags) { |
| 87 scoped_refptr<ExtensionFunction> function_owner(function); | 110 scoped_refptr<ExtensionFunction> function_owner(function); |
| 88 // Without a callback the function will not generate a result. | 111 // Without a callback the function will not generate a result. |
| 89 function->set_has_callback(true); | 112 function->set_has_callback(true); |
| 90 RunFunction(function, args, context, dispatcher.Pass(), flags); | 113 RunFunction(function, args, context, dispatcher.Pass(), flags); |
| 91 EXPECT_TRUE(function->GetError().empty()) | 114 EXPECT_TRUE(function->GetError().empty()) |
| 92 << "Unexpected error: " << function->GetError(); | 115 << "Unexpected error: " << function->GetError(); |
| 93 const base::Value* single_result = NULL; | 116 const base::Value* single_result = NULL; |
| 94 if (function->GetResultList() != NULL && | 117 if (function->GetResultList() != NULL && |
| 95 function->GetResultList()->Get(0, &single_result)) { | 118 function->GetResultList()->Get(0, &single_result)) { |
| 96 return single_result->DeepCopy(); | 119 return single_result->DeepCopy(); |
| 97 } | 120 } |
| 98 return NULL; | 121 return NULL; |
| 99 } | 122 } |
| 100 | 123 |
| 124 base::Value* RunDelegatelessFunctionAndReturnSingleResult( | |
|
Yoyo Zhou
2014/08/08 01:28:04
I think the default should be with no delegate, si
Daniel Nishi
2014/08/08 17:24:08
Done.
| |
| 125 UIThreadExtensionFunction* function, | |
| 126 const std::string& args, | |
| 127 content::BrowserContext* context) { | |
| 128 return RunDelegatelessFunctionAndReturnSingleResult( | |
| 129 function, args, context, NONE); | |
| 130 } | |
| 131 | |
| 132 base::Value* RunDelegatelessFunctionAndReturnSingleResult( | |
| 133 UIThreadExtensionFunction* function, | |
| 134 const std::string& args, | |
| 135 content::BrowserContext* context, | |
| 136 RunFunctionFlags flags) { | |
| 137 TestFunctionDispatcherDelegate delegate; | |
| 138 scoped_ptr<ExtensionFunctionDispatcher> dispatcher( | |
| 139 new ExtensionFunctionDispatcher(context, &delegate)); | |
| 140 | |
| 141 return RunFunctionAndReturnSingleResult( | |
| 142 function, args, context, dispatcher.Pass(), flags); | |
| 143 } | |
| 144 | |
| 101 bool RunFunction(UIThreadExtensionFunction* function, | 145 bool RunFunction(UIThreadExtensionFunction* function, |
| 102 const std::string& args, | 146 const std::string& args, |
| 103 content::BrowserContext* context, | 147 content::BrowserContext* context, |
| 104 scoped_ptr<extensions::ExtensionFunctionDispatcher> dispatcher, | 148 scoped_ptr<extensions::ExtensionFunctionDispatcher> dispatcher, |
| 105 RunFunctionFlags flags) { | 149 RunFunctionFlags flags) { |
| 106 SendResponseDelegate response_delegate; | 150 SendResponseDelegate response_delegate; |
| 107 function->set_test_delegate(&response_delegate); | 151 function->set_test_delegate(&response_delegate); |
| 108 scoped_ptr<base::ListValue> parsed_args(ParseList(args)); | 152 scoped_ptr<base::ListValue> parsed_args(ParseList(args)); |
| 109 EXPECT_TRUE(parsed_args.get()) | 153 EXPECT_TRUE(parsed_args.get()) |
| 110 << "Could not parse extension function arguments: " << args; | 154 << "Could not parse extension function arguments: " << args; |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 123 response_delegate.set_should_post_quit(true); | 167 response_delegate.set_should_post_quit(true); |
| 124 content::RunMessageLoop(); | 168 content::RunMessageLoop(); |
| 125 } | 169 } |
| 126 | 170 |
| 127 EXPECT_TRUE(response_delegate.HasResponse()); | 171 EXPECT_TRUE(response_delegate.HasResponse()); |
| 128 return response_delegate.GetResponse(); | 172 return response_delegate.GetResponse(); |
| 129 } | 173 } |
| 130 | 174 |
| 131 } // namespace api_test_utils | 175 } // namespace api_test_utils |
| 132 } // namespace extensions | 176 } // namespace extensions |
| OLD | NEW |