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