| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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 <algorithm> | 5 #include <algorithm> |
| 6 #include <vector> | 6 #include <vector> |
| 7 | 7 |
| 8 #include "base/message_loop.h" | 8 #include "base/message_loop.h" |
| 9 #include "base/thread.h" | 9 #include "base/thread.h" |
| 10 #include "chrome/browser/chrome_thread.h" | 10 #include "chrome/browser/chrome_thread.h" |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 58 | 58 |
| 59 scoped_ptr<NotificationService> service_; | 59 scoped_ptr<NotificationService> service_; |
| 60 }; | 60 }; |
| 61 | 61 |
| 62 // A class to help with making and handling resource requests. | 62 // A class to help with making and handling resource requests. |
| 63 class ResourceDispatcherHostTester | 63 class ResourceDispatcherHostTester |
| 64 : public ResourceDispatcherHost::Receiver, | 64 : public ResourceDispatcherHost::Receiver, |
| 65 public URLRequest::Interceptor, | 65 public URLRequest::Interceptor, |
| 66 public base::RefCountedThreadSafe<ResourceDispatcherHostTester> { | 66 public base::RefCountedThreadSafe<ResourceDispatcherHostTester> { |
| 67 public: | 67 public: |
| 68 explicit ResourceDispatcherHostTester(MessageLoop* io_loop) | 68 explicit ResourceDispatcherHostTester() |
| 69 : Receiver(ChildProcessInfo::RENDER_PROCESS, -1), | 69 : Receiver(ChildProcessInfo::RENDER_PROCESS, -1) { |
| 70 host_(io_loop), | |
| 71 ui_loop_(MessageLoop::current()), | |
| 72 io_loop_(io_loop) { | |
| 73 URLRequest::RegisterRequestInterceptor(this); | 70 URLRequest::RegisterRequestInterceptor(this); |
| 74 } | 71 } |
| 75 virtual ~ResourceDispatcherHostTester() { | 72 virtual ~ResourceDispatcherHostTester() { |
| 76 URLRequest::UnregisterRequestInterceptor(this); | 73 URLRequest::UnregisterRequestInterceptor(this); |
| 77 } | 74 } |
| 78 | 75 |
| 79 void MakeTestRequest(int request_id, const GURL& url) { | 76 void MakeTestRequest(int request_id, const GURL& url) { |
| 80 io_loop_->PostTask(FROM_HERE, NewRunnableMethod( | 77 ChromeThread::PostTask( |
| 81 this, &ResourceDispatcherHostTester::MakeTestRequestOnIOThread, | 78 ChromeThread::IO, FROM_HERE, |
| 82 request_id, url)); | 79 NewRunnableMethod( |
| 80 this, &ResourceDispatcherHostTester::MakeTestRequestOnIOThread, |
| 81 request_id, url)); |
| 83 MessageLoop::current()->Run(); // wait for Quit from IO thread | 82 MessageLoop::current()->Run(); // wait for Quit from IO thread |
| 84 } | 83 } |
| 85 | 84 |
| 86 void WaitForScan(MockUserScriptMaster* master) { | 85 void WaitForScan(MockUserScriptMaster* master) { |
| 87 master->TestStartScan(); | 86 master->TestStartScan(); |
| 88 MessageLoop::current()->RunAllPending(); // run the scan | 87 MessageLoop::current()->RunAllPending(); // run the scan |
| 89 io_loop_->PostTask(FROM_HERE, NewRunnableMethod( | 88 ChromeThread::PostTask( |
| 90 this, &ResourceDispatcherHostTester::RunPending)); | 89 ChromeThread::IO, FROM_HERE, |
| 90 NewRunnableMethod(this, &ResourceDispatcherHostTester::RunPending)); |
| 91 MessageLoop::current()->Run(); // wait for Quit from IO thread | 91 MessageLoop::current()->Run(); // wait for Quit from IO thread |
| 92 } | 92 } |
| 93 | 93 |
| 94 bool IsRequestStarted(int request_id) { | 94 bool IsRequestStarted(int request_id) { |
| 95 return std::find(started_requests_.begin(), started_requests_.end(), | 95 return std::find(started_requests_.begin(), started_requests_.end(), |
| 96 request_id) != started_requests_.end(); | 96 request_id) != started_requests_.end(); |
| 97 } | 97 } |
| 98 | 98 |
| 99 bool IsRequestComplete(int request_id) { | 99 bool IsRequestComplete(int request_id) { |
| 100 return std::find(completed_requests_.begin(), completed_requests_.end(), | 100 return std::find(completed_requests_.begin(), completed_requests_.end(), |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 142 request.appcache_host_id = appcache::kNoHostId; | 142 request.appcache_host_id = appcache::kNoHostId; |
| 143 return request; | 143 return request; |
| 144 } | 144 } |
| 145 | 145 |
| 146 void RunPending() { | 146 void RunPending() { |
| 147 MessageLoop::current()->SetNestableTasksAllowed(true); | 147 MessageLoop::current()->SetNestableTasksAllowed(true); |
| 148 MessageLoop::current()->RunAllPending(); | 148 MessageLoop::current()->RunAllPending(); |
| 149 MessageLoop::current()->SetNestableTasksAllowed(false); | 149 MessageLoop::current()->SetNestableTasksAllowed(false); |
| 150 | 150 |
| 151 // return control to UI thread. | 151 // return control to UI thread. |
| 152 ui_loop_->PostTask(FROM_HERE, new MessageLoop::QuitTask()); | 152 ChromeThread::PostTask( |
| 153 ChromeThread::UI, FROM_HERE, new MessageLoop::QuitTask()); |
| 153 } | 154 } |
| 154 | 155 |
| 155 void MakeTestRequestOnIOThread(int request_id, const GURL& url) { | 156 void MakeTestRequestOnIOThread(int request_id, const GURL& url) { |
| 156 ViewHostMsg_Resource_Request request = CreateResourceRequest("GET", url); | 157 ViewHostMsg_Resource_Request request = CreateResourceRequest("GET", url); |
| 157 ViewHostMsg_RequestResource msg(0, request_id, request); | 158 ViewHostMsg_RequestResource msg(0, request_id, request); |
| 158 bool msg_was_ok; | 159 bool msg_was_ok; |
| 159 host_.OnMessageReceived(msg, this, &msg_was_ok); | 160 host_.OnMessageReceived(msg, this, &msg_was_ok); |
| 160 RunPending(); | 161 RunPending(); |
| 161 } | 162 } |
| 162 | 163 |
| 163 void OnReceivedResponse(int request_id, | 164 void OnReceivedResponse(int request_id, |
| 164 const ResourceResponseHead& response_head) { | 165 const ResourceResponseHead& response_head) { |
| 165 started_requests_.push_back(request_id); | 166 started_requests_.push_back(request_id); |
| 166 } | 167 } |
| 167 | 168 |
| 168 void OnRequestComplete(int request_id, | 169 void OnRequestComplete(int request_id, |
| 169 const URLRequestStatus& status, | 170 const URLRequestStatus& status, |
| 170 const std::string& security_info) { | 171 const std::string& security_info) { |
| 171 completed_requests_.push_back(request_id); | 172 completed_requests_.push_back(request_id); |
| 172 } | 173 } |
| 173 | 174 |
| 174 ResourceDispatcherHost host_; | 175 ResourceDispatcherHost host_; |
| 175 MessageLoop* ui_loop_; | |
| 176 MessageLoop* io_loop_; | |
| 177 | 176 |
| 178 // Note: these variables are accessed on both threads, but since we only | 177 // Note: these variables are accessed on both threads, but since we only |
| 179 // one thread executes at a time, they are safe. | 178 // one thread executes at a time, they are safe. |
| 180 std::vector<int> started_requests_; | 179 std::vector<int> started_requests_; |
| 181 std::vector<int> completed_requests_; | 180 std::vector<int> completed_requests_; |
| 182 }; | 181 }; |
| 183 | 182 |
| 184 class UserScriptListenerTest : public testing::Test { | 183 class UserScriptListenerTest : public testing::Test { |
| 185 public: | 184 public: |
| 186 virtual void SetUp() { | 185 virtual void SetUp() { |
| 187 ui_thread_.reset(new ChromeThread(ChromeThread::UI, &loop_)); | 186 ui_thread_.reset(new ChromeThread(ChromeThread::UI, &loop_)); |
| 188 file_thread_.reset(new ChromeThread(ChromeThread::FILE, &loop_)); | 187 file_thread_.reset(new ChromeThread(ChromeThread::FILE, &loop_)); |
| 189 io_thread_.reset(new MockIOThread()); | 188 io_thread_.reset(new MockIOThread()); |
| 190 base::Thread::Options options(MessageLoop::TYPE_IO, 0); | 189 base::Thread::Options options(MessageLoop::TYPE_IO, 0); |
| 191 io_thread_->StartWithOptions(options); | 190 io_thread_->StartWithOptions(options); |
| 192 DCHECK(io_thread_->message_loop()); | 191 DCHECK(io_thread_->message_loop()); |
| 193 DCHECK(io_thread_->IsRunning()); | 192 DCHECK(io_thread_->IsRunning()); |
| 194 | 193 |
| 195 FilePath install_dir = profile_.GetPath() | 194 FilePath install_dir = profile_.GetPath() |
| 196 .AppendASCII(ExtensionsService::kInstallDirectoryName); | 195 .AppendASCII(ExtensionsService::kInstallDirectoryName); |
| 197 | 196 |
| 198 resource_tester_ = | 197 resource_tester_ = new ResourceDispatcherHostTester(); |
| 199 new ResourceDispatcherHostTester(io_thread_->message_loop()); | |
| 200 | 198 |
| 201 master_ = new MockUserScriptMaster(install_dir); | 199 master_ = new MockUserScriptMaster(install_dir); |
| 202 | 200 |
| 203 service_ = new ExtensionsService(&profile_, | 201 service_ = new ExtensionsService(&profile_, |
| 204 CommandLine::ForCurrentProcess(), | 202 CommandLine::ForCurrentProcess(), |
| 205 profile_.GetPrefs(), | 203 profile_.GetPrefs(), |
| 206 install_dir, | 204 install_dir, |
| 207 false); | 205 false); |
| 208 service_->set_extensions_enabled(true); | 206 service_->set_extensions_enabled(true); |
| 209 service_->set_show_extensions_prompts(false); | 207 service_->set_show_extensions_prompts(false); |
| (...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 304 | 302 |
| 305 // Make the same requests, and they should complete instantly. | 303 // Make the same requests, and they should complete instantly. |
| 306 resource_tester_->MakeTestRequest(2, GURL("http://google.com/")); | 304 resource_tester_->MakeTestRequest(2, GURL("http://google.com/")); |
| 307 resource_tester_->MakeTestRequest(3, GURL("http://yahoo.com/")); | 305 resource_tester_->MakeTestRequest(3, GURL("http://yahoo.com/")); |
| 308 | 306 |
| 309 EXPECT_TRUE(resource_tester_->IsRequestStarted(2)); | 307 EXPECT_TRUE(resource_tester_->IsRequestStarted(2)); |
| 310 EXPECT_TRUE(resource_tester_->IsRequestComplete(2)); | 308 EXPECT_TRUE(resource_tester_->IsRequestComplete(2)); |
| 311 EXPECT_TRUE(resource_tester_->IsRequestStarted(3)); | 309 EXPECT_TRUE(resource_tester_->IsRequestStarted(3)); |
| 312 EXPECT_TRUE(resource_tester_->IsRequestComplete(3)); | 310 EXPECT_TRUE(resource_tester_->IsRequestComplete(3)); |
| 313 } | 311 } |
| OLD | NEW |