OLD | NEW |
(Empty) | |
| 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 #include "extensions/browser/api_test_utils.h" |
| 6 |
| 7 #include "base/json/json_reader.h" |
| 8 #include "base/memory/scoped_ptr.h" |
| 9 #include "base/values.h" |
| 10 #include "content/public/browser/browser_context.h" |
| 11 #include "content/public/test/test_utils.h" |
| 12 #include "extensions/browser/extension_function.h" |
| 13 #include "extensions/browser/extension_function_dispatcher.h" |
| 14 #include "testing/gtest/include/gtest/gtest.h" |
| 15 |
| 16 namespace { |
| 17 |
| 18 base::Value* ParseJSON(const std::string& data) { |
| 19 return base::JSONReader::Read(data); |
| 20 } |
| 21 |
| 22 base::ListValue* ParseList(const std::string& data) { |
| 23 base::Value* result = ParseJSON(data); |
| 24 base::ListValue* list = NULL; |
| 25 result->GetAsList(&list); |
| 26 return list; |
| 27 } |
| 28 |
| 29 // This helps us be able to wait until an UIThreadExtensionFunction calls |
| 30 // SendResponse. |
| 31 class SendResponseDelegate |
| 32 : public UIThreadExtensionFunction::DelegateForTests { |
| 33 public: |
| 34 SendResponseDelegate() : should_post_quit_(false) {} |
| 35 |
| 36 virtual ~SendResponseDelegate() {} |
| 37 |
| 38 void set_should_post_quit(bool should_quit) { |
| 39 should_post_quit_ = should_quit; |
| 40 } |
| 41 |
| 42 bool HasResponse() { return response_.get() != NULL; } |
| 43 |
| 44 bool GetResponse() { |
| 45 EXPECT_TRUE(HasResponse()); |
| 46 return *response_.get(); |
| 47 } |
| 48 |
| 49 virtual void OnSendResponse(UIThreadExtensionFunction* function, |
| 50 bool success, |
| 51 bool bad_message) OVERRIDE { |
| 52 ASSERT_FALSE(bad_message); |
| 53 ASSERT_FALSE(HasResponse()); |
| 54 response_.reset(new bool); |
| 55 *response_ = success; |
| 56 if (should_post_quit_) { |
| 57 base::MessageLoopForUI::current()->Quit(); |
| 58 } |
| 59 } |
| 60 |
| 61 private: |
| 62 scoped_ptr<bool> response_; |
| 63 bool should_post_quit_; |
| 64 }; |
| 65 |
| 66 } // namespace |
| 67 |
| 68 namespace extensions { |
| 69 |
| 70 namespace api_test_utils { |
| 71 |
| 72 base::Value* RunFunctionAndReturnSingleResult( |
| 73 UIThreadExtensionFunction* function, |
| 74 const std::string& args, |
| 75 content::BrowserContext* context, |
| 76 scoped_ptr<extensions::ExtensionFunctionDispatcher> dispatcher) { |
| 77 return RunFunctionAndReturnSingleResult( |
| 78 function, args, context, dispatcher.Pass(), NONE); |
| 79 } |
| 80 |
| 81 base::Value* RunFunctionAndReturnSingleResult( |
| 82 UIThreadExtensionFunction* function, |
| 83 const std::string& args, |
| 84 content::BrowserContext* context, |
| 85 scoped_ptr<extensions::ExtensionFunctionDispatcher> dispatcher, |
| 86 RunFunctionFlags flags) { |
| 87 scoped_refptr<ExtensionFunction> function_owner(function); |
| 88 // Without a callback the function will not generate a result. |
| 89 function->set_has_callback(true); |
| 90 RunFunction(function, args, context, dispatcher.Pass(), flags); |
| 91 EXPECT_TRUE(function->GetError().empty()) |
| 92 << "Unexpected error: " << function->GetError(); |
| 93 const base::Value* single_result = NULL; |
| 94 if (function->GetResultList() != NULL && |
| 95 function->GetResultList()->Get(0, &single_result)) { |
| 96 return single_result->DeepCopy(); |
| 97 } |
| 98 return NULL; |
| 99 } |
| 100 |
| 101 bool RunFunction(UIThreadExtensionFunction* function, |
| 102 const std::string& args, |
| 103 content::BrowserContext* context, |
| 104 scoped_ptr<extensions::ExtensionFunctionDispatcher> dispatcher, |
| 105 RunFunctionFlags flags) { |
| 106 SendResponseDelegate response_delegate; |
| 107 function->set_test_delegate(&response_delegate); |
| 108 scoped_ptr<base::ListValue> parsed_args(ParseList(args)); |
| 109 EXPECT_TRUE(parsed_args.get()) |
| 110 << "Could not parse extension function arguments: " << args; |
| 111 function->SetArgs(parsed_args.get()); |
| 112 |
| 113 CHECK(dispatcher); |
| 114 function->set_dispatcher(dispatcher->AsWeakPtr()); |
| 115 |
| 116 function->set_browser_context(context); |
| 117 function->set_include_incognito(flags & INCLUDE_INCOGNITO); |
| 118 function->Run()->Execute(); |
| 119 |
| 120 // If the RunAsync of |function| didn't already call SendResponse, run the |
| 121 // message loop until they do. |
| 122 if (!response_delegate.HasResponse()) { |
| 123 response_delegate.set_should_post_quit(true); |
| 124 content::RunMessageLoop(); |
| 125 } |
| 126 |
| 127 EXPECT_TRUE(response_delegate.HasResponse()); |
| 128 return response_delegate.GetResponse(); |
| 129 } |
| 130 |
| 131 } // namespace api_test_utils |
| 132 } // namespace extensions |
OLD | NEW |