OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 <set> | 5 #include <set> |
6 #include <string> | 6 #include <string> |
7 #include <utility> | 7 #include <utility> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
71 namespace extensions { | 71 namespace extensions { |
72 | 72 |
73 namespace { | 73 namespace { |
74 | 74 |
75 namespace errors = identity_constants; | 75 namespace errors = identity_constants; |
76 namespace utils = extension_function_test_utils; | 76 namespace utils = extension_function_test_utils; |
77 | 77 |
78 static const char kAccessToken[] = "auth_token"; | 78 static const char kAccessToken[] = "auth_token"; |
79 static const char kExtensionId[] = "ext_id"; | 79 static const char kExtensionId[] = "ext_id"; |
80 | 80 |
81 // This helps us be able to wait until an UIThreadExtensionFunction calls | |
82 // SendResponse. | |
83 class SendResponseDelegate | |
84 : public UIThreadExtensionFunction::DelegateForTests { | |
85 public: | |
86 SendResponseDelegate() : should_post_quit_(false) {} | |
87 | |
88 virtual ~SendResponseDelegate() {} | |
89 | |
90 void set_should_post_quit(bool should_quit) { | |
91 should_post_quit_ = should_quit; | |
92 } | |
93 | |
94 bool HasResponse() { | |
95 return response_.get() != NULL; | |
96 } | |
97 | |
98 bool GetResponse() { | |
99 EXPECT_TRUE(HasResponse()); | |
100 return *response_; | |
101 } | |
102 | |
103 void OnSendResponse(UIThreadExtensionFunction* function, | |
104 bool success, | |
105 bool bad_message) override { | |
106 ASSERT_FALSE(bad_message); | |
107 ASSERT_FALSE(HasResponse()); | |
108 response_.reset(new bool); | |
109 *response_ = success; | |
110 if (should_post_quit_) { | |
111 base::MessageLoopForUI::current()->QuitWhenIdle(); | |
112 } | |
113 } | |
114 | |
115 private: | |
116 std::unique_ptr<bool> response_; | |
117 bool should_post_quit_; | |
118 }; | |
119 | |
120 class AsyncExtensionBrowserTest : public ExtensionBrowserTest { | 81 class AsyncExtensionBrowserTest : public ExtensionBrowserTest { |
121 protected: | 82 protected: |
122 // Asynchronous function runner allows tests to manipulate the browser window | 83 // Asynchronous function runner allows tests to manipulate the browser window |
123 // after the call happens. | 84 // after the call happens. |
124 void RunFunctionAsync( | 85 void RunFunctionAsync( |
125 UIThreadExtensionFunction* function, | 86 UIThreadExtensionFunction* function, |
126 const std::string& args) { | 87 const std::string& args) { |
127 response_delegate_.reset(new SendResponseDelegate); | 88 response_delegate_.reset(new api_test_utils::SendResponseHelper(function)); |
128 function->set_test_delegate(response_delegate_.get()); | |
129 std::unique_ptr<base::ListValue> parsed_args(utils::ParseList(args)); | 89 std::unique_ptr<base::ListValue> parsed_args(utils::ParseList(args)); |
130 EXPECT_TRUE(parsed_args.get()) << | 90 EXPECT_TRUE(parsed_args.get()) << |
131 "Could not parse extension function arguments: " << args; | 91 "Could not parse extension function arguments: " << args; |
132 function->SetArgs(parsed_args.get()); | 92 function->SetArgs(parsed_args.get()); |
133 | 93 |
134 if (!function->extension()) { | 94 if (!function->extension()) { |
135 scoped_refptr<Extension> empty_extension( | 95 scoped_refptr<Extension> empty_extension( |
136 test_util::CreateEmptyExtension()); | 96 test_util::CreateEmptyExtension()); |
137 function->set_extension(empty_extension.get()); | 97 function->set_extension(empty_extension.get()); |
138 } | 98 } |
139 | 99 |
140 function->set_browser_context(browser()->profile()); | 100 function->set_browser_context(browser()->profile()); |
141 function->set_has_callback(true); | 101 function->set_has_callback(true); |
142 function->RunWithValidation()->Execute(); | 102 function->RunWithValidation()->Execute(); |
143 } | 103 } |
144 | 104 |
145 std::string WaitForError(UIThreadExtensionFunction* function) { | 105 std::string WaitForError(UIThreadExtensionFunction* function) { |
146 RunMessageLoopUntilResponse(); | 106 RunMessageLoopUntilResponse(); |
147 EXPECT_FALSE(function->GetResultList()) << "Did not expect a result"; | 107 CHECK(function->response_type()); |
| 108 EXPECT_EQ(ExtensionFunction::FAILED, *function->response_type()); |
148 return function->GetError(); | 109 return function->GetError(); |
149 } | 110 } |
150 | 111 |
151 base::Value* WaitForSingleResult(UIThreadExtensionFunction* function) { | 112 base::Value* WaitForSingleResult(UIThreadExtensionFunction* function) { |
152 RunMessageLoopUntilResponse(); | 113 RunMessageLoopUntilResponse(); |
153 EXPECT_TRUE(function->GetError().empty()) << "Unexpected error: " | 114 EXPECT_TRUE(function->GetError().empty()) << "Unexpected error: " |
154 << function->GetError(); | 115 << function->GetError(); |
155 const base::Value* single_result = NULL; | 116 const base::Value* single_result = NULL; |
156 if (function->GetResultList() != NULL && | 117 if (function->GetResultList() != NULL && |
157 function->GetResultList()->Get(0, &single_result)) { | 118 function->GetResultList()->Get(0, &single_result)) { |
158 return single_result->DeepCopy(); | 119 return single_result->DeepCopy(); |
159 } | 120 } |
160 return NULL; | 121 return NULL; |
161 } | 122 } |
162 | 123 |
163 private: | 124 private: |
164 void RunMessageLoopUntilResponse() { | 125 void RunMessageLoopUntilResponse() { |
165 // If the RunAsync of |function| didn't already call SendResponse, run the | 126 response_delegate_->WaitForResponse(); |
166 // message loop until they do. | 127 EXPECT_TRUE(response_delegate_->has_response()); |
167 if (!response_delegate_->HasResponse()) { | |
168 response_delegate_->set_should_post_quit(true); | |
169 content::RunMessageLoop(); | |
170 } | |
171 EXPECT_TRUE(response_delegate_->HasResponse()); | |
172 } | 128 } |
173 | 129 |
174 std::unique_ptr<SendResponseDelegate> response_delegate_; | 130 std::unique_ptr<api_test_utils::SendResponseHelper> response_delegate_; |
175 }; | 131 }; |
176 | 132 |
177 class TestHangOAuth2MintTokenFlow : public OAuth2MintTokenFlow { | 133 class TestHangOAuth2MintTokenFlow : public OAuth2MintTokenFlow { |
178 public: | 134 public: |
179 TestHangOAuth2MintTokenFlow() | 135 TestHangOAuth2MintTokenFlow() |
180 : OAuth2MintTokenFlow(NULL, OAuth2MintTokenFlow::Parameters()) {} | 136 : OAuth2MintTokenFlow(NULL, OAuth2MintTokenFlow::Parameters()) {} |
181 | 137 |
182 void Start(net::URLRequestContextGetter* context, | 138 void Start(net::URLRequestContextGetter* context, |
183 const std::string& access_token) override { | 139 const std::string& access_token) override { |
184 // Do nothing, simulating a hanging network call. | 140 // Do nothing, simulating a hanging network call. |
(...skipping 1713 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1898 EXPECT_EQ(std::string("https://abcdefghij.chromiumapp.org/callback#test"), | 1854 EXPECT_EQ(std::string("https://abcdefghij.chromiumapp.org/callback#test"), |
1899 url); | 1855 url); |
1900 } | 1856 } |
1901 | 1857 |
1902 } // namespace extensions | 1858 } // namespace extensions |
1903 | 1859 |
1904 // Tests the chrome.identity API implemented by custom JS bindings . | 1860 // Tests the chrome.identity API implemented by custom JS bindings . |
1905 IN_PROC_BROWSER_TEST_F(ExtensionApiTest, ChromeIdentityJsBindings) { | 1861 IN_PROC_BROWSER_TEST_F(ExtensionApiTest, ChromeIdentityJsBindings) { |
1906 ASSERT_TRUE(RunExtensionTest("identity/js_bindings")) << message_; | 1862 ASSERT_TRUE(RunExtensionTest("identity/js_bindings")) << message_; |
1907 } | 1863 } |
OLD | NEW |