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 "base/file_path.h" | 5 #include "base/file_path.h" |
6 #include "base/memory/ref_counted.h" | 6 #include "base/memory/ref_counted.h" |
7 #include "base/memory/scoped_ptr.h" | 7 #include "base/memory/scoped_ptr.h" |
8 #include "base/synchronization/waitable_event.h" | 8 #include "base/synchronization/waitable_event.h" |
9 #include "chrome/browser/safe_browsing/browser_feature_extractor.h" | 9 #include "chrome/browser/safe_browsing/browser_feature_extractor.h" |
10 #include "chrome/browser/safe_browsing/client_side_detection_host.h" | 10 #include "chrome/browser/safe_browsing/client_side_detection_host.h" |
11 #include "chrome/browser/safe_browsing/client_side_detection_service.h" | 11 #include "chrome/browser/safe_browsing/client_side_detection_service.h" |
12 #include "chrome/browser/safe_browsing/safe_browsing_service.h" | 12 #include "chrome/browser/safe_browsing/safe_browsing_service.h" |
13 #include "chrome/browser/ui/tab_contents/tab_contents.h" | |
14 #include "chrome/browser/ui/tab_contents/test_tab_contents.h" | |
15 #include "chrome/common/chrome_switches.h" | 13 #include "chrome/common/chrome_switches.h" |
16 #include "chrome/common/safe_browsing/csd.pb.h" | 14 #include "chrome/common/safe_browsing/csd.pb.h" |
17 #include "chrome/common/safe_browsing/safebrowsing_messages.h" | 15 #include "chrome/common/safe_browsing/safebrowsing_messages.h" |
| 16 #include "chrome/test/base/chrome_render_view_host_test_harness.h" |
18 #include "chrome/test/base/testing_profile.h" | 17 #include "chrome/test/base/testing_profile.h" |
19 #include "content/public/browser/web_contents.h" | 18 #include "content/public/browser/web_contents.h" |
20 #include "content/public/test/mock_render_process_host.h" | 19 #include "content/public/test/mock_render_process_host.h" |
21 #include "content/public/test/test_browser_thread.h" | 20 #include "content/public/test/test_browser_thread.h" |
22 #include "content/public/test/test_renderer_host.h" | 21 #include "content/public/test/test_renderer_host.h" |
23 #include "googleurl/src/gurl.h" | 22 #include "googleurl/src/gurl.h" |
24 #include "ipc/ipc_test_sink.h" | 23 #include "ipc/ipc_test_sink.h" |
25 #include "testing/gmock/include/gmock/gmock.h" | 24 #include "testing/gmock/include/gmock/gmock.h" |
26 #include "testing/gtest/include/gtest/gtest.h" | 25 #include "testing/gtest/include/gtest/gtest.h" |
27 | 26 |
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
147 | 146 |
148 // Helper function which quits the UI message loop from the IO message loop. | 147 // Helper function which quits the UI message loop from the IO message loop. |
149 void QuitUIMessageLoopFromIO() { | 148 void QuitUIMessageLoopFromIO() { |
150 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 149 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
151 BrowserThread::PostTask(BrowserThread::UI, | 150 BrowserThread::PostTask(BrowserThread::UI, |
152 FROM_HERE, | 151 FROM_HERE, |
153 MessageLoop::QuitClosure()); | 152 MessageLoop::QuitClosure()); |
154 } | 153 } |
155 } // namespace | 154 } // namespace |
156 | 155 |
157 class ClientSideDetectionHostTest : public TabContentsTestHarness { | 156 class ClientSideDetectionHostTest : public ChromeRenderViewHostTestHarness { |
158 public: | 157 public: |
159 virtual void SetUp() { | 158 virtual void SetUp() { |
160 // Set custom profile object so that we can mock calls to IsOffTheRecord. | 159 // Set custom profile object so that we can mock calls to IsOffTheRecord. |
161 // This needs to happen before we call the parent SetUp() function. We use | 160 // This needs to happen before we call the parent SetUp() function. We use |
162 // a nice mock because other parts of the code are calling IsOffTheRecord. | 161 // a nice mock because other parts of the code are calling IsOffTheRecord. |
163 mock_profile_ = new NiceMock<MockTestingProfile>(); | 162 mock_profile_ = new NiceMock<MockTestingProfile>(); |
164 browser_context_.reset(mock_profile_); | 163 browser_context_.reset(mock_profile_); |
165 | 164 |
166 ui_thread_.reset(new content::TestBrowserThread(BrowserThread::UI, | 165 ui_thread_.reset(new content::TestBrowserThread(BrowserThread::UI, |
167 &message_loop_)); | 166 &message_loop_)); |
168 file_user_blocking_thread_.reset( | 167 file_user_blocking_thread_.reset( |
169 new content::TestBrowserThread(BrowserThread::FILE_USER_BLOCKING, | 168 new content::TestBrowserThread(BrowserThread::FILE_USER_BLOCKING, |
170 &message_loop_)); | 169 &message_loop_)); |
171 // Note: we're starting a real IO thread to make sure our DCHECKs that | 170 // Note: we're starting a real IO thread to make sure our DCHECKs that |
172 // verify which thread is running are actually tested. | 171 // verify which thread is running are actually tested. |
173 io_thread_.reset(new content::TestBrowserThread(BrowserThread::IO)); | 172 io_thread_.reset(new content::TestBrowserThread(BrowserThread::IO)); |
174 ASSERT_TRUE(io_thread_->Start()); | 173 ASSERT_TRUE(io_thread_->Start()); |
175 | 174 |
176 TabContentsTestHarness::SetUp(); | 175 ChromeRenderViewHostTestHarness::SetUp(); |
177 | 176 |
178 // Inject service classes. | 177 // Inject service classes. |
179 csd_service_.reset(new StrictMock<MockClientSideDetectionService>()); | 178 csd_service_.reset(new StrictMock<MockClientSideDetectionService>()); |
180 sb_service_ = new StrictMock<MockSafeBrowsingService>(); | 179 sb_service_ = new StrictMock<MockSafeBrowsingService>(); |
181 csd_host_.reset(safe_browsing::ClientSideDetectionHost::Create( | 180 csd_host_.reset(safe_browsing::ClientSideDetectionHost::Create( |
182 tab_contents()->web_contents())); | 181 web_contents())); |
183 csd_host_->set_client_side_detection_service(csd_service_.get()); | 182 csd_host_->set_client_side_detection_service(csd_service_.get()); |
184 csd_host_->set_safe_browsing_service(sb_service_.get()); | 183 csd_host_->set_safe_browsing_service(sb_service_.get()); |
185 // We need to create this here since we don't call | 184 // We need to create this here since we don't call |
186 // DidNavigateMainFramePostCommit in this test. | 185 // DidNavigateMainFramePostCommit in this test. |
187 csd_host_->browse_info_.reset(new BrowseInfo); | 186 csd_host_->browse_info_.reset(new BrowseInfo); |
188 } | 187 } |
189 | 188 |
190 static void RunAllPendingOnIO(base::WaitableEvent* event) { | 189 static void RunAllPendingOnIO(base::WaitableEvent* event) { |
191 MessageLoop::current()->RunAllPending(); | 190 MessageLoop::current()->RunAllPending(); |
192 event->Signal(); | 191 event->Signal(); |
193 } | 192 } |
194 | 193 |
195 virtual void TearDown() { | 194 virtual void TearDown() { |
196 // Delete the host object on the UI thread and release the | 195 // Delete the host object on the UI thread and release the |
197 // SafeBrowsingService. | 196 // SafeBrowsingService. |
198 BrowserThread::DeleteSoon(BrowserThread::UI, FROM_HERE, | 197 BrowserThread::DeleteSoon(BrowserThread::UI, FROM_HERE, |
199 csd_host_.release()); | 198 csd_host_.release()); |
200 sb_service_ = NULL; | 199 sb_service_ = NULL; |
201 message_loop_.RunAllPending(); | 200 message_loop_.RunAllPending(); |
202 TabContentsTestHarness::TearDown(); | 201 ChromeRenderViewHostTestHarness::TearDown(); |
203 | 202 |
204 // Let the tasks on the IO thread run to avoid memory leaks. | 203 // Let the tasks on the IO thread run to avoid memory leaks. |
205 base::WaitableEvent done(false, false); | 204 base::WaitableEvent done(false, false); |
206 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, | 205 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, |
207 base::Bind(RunAllPendingOnIO, &done)); | 206 base::Bind(RunAllPendingOnIO, &done)); |
208 done.Wait(); | 207 done.Wait(); |
209 io_thread_.reset(); | 208 io_thread_.reset(); |
210 message_loop_.RunAllPending(); | 209 message_loop_.RunAllPending(); |
211 file_user_blocking_thread_.reset(); | 210 file_user_blocking_thread_.reset(); |
212 ui_thread_.reset(); | 211 ui_thread_.reset(); |
(...skipping 547 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
760 EXPECT_TRUE(Mock::VerifyAndClear(sb_service_.get())); | 759 EXPECT_TRUE(Mock::VerifyAndClear(sb_service_.get())); |
761 EXPECT_EQ(url, resource.url); | 760 EXPECT_EQ(url, resource.url); |
762 EXPECT_EQ(url, resource.original_url); | 761 EXPECT_EQ(url, resource.original_url); |
763 resource.callback.Reset(); | 762 resource.callback.Reset(); |
764 msg = process()->sink().GetFirstMessageMatching( | 763 msg = process()->sink().GetFirstMessageMatching( |
765 SafeBrowsingMsg_StartPhishingDetection::ID); | 764 SafeBrowsingMsg_StartPhishingDetection::ID); |
766 ASSERT_FALSE(msg); | 765 ASSERT_FALSE(msg); |
767 } | 766 } |
768 | 767 |
769 } // namespace safe_browsing | 768 } // namespace safe_browsing |
OLD | NEW |