Chromium Code Reviews| Index: chrome/browser/safe_browsing/malware_details_unittest.cc |
| =================================================================== |
| --- chrome/browser/safe_browsing/malware_details_unittest.cc (revision 84048) |
| +++ chrome/browser/safe_browsing/malware_details_unittest.cc (working copy) |
| @@ -8,12 +8,15 @@ |
| #include "base/time.h" |
| #include "chrome/browser/profiles/profile.h" |
| #include "chrome/browser/safe_browsing/malware_details.h" |
| +#include "chrome/browser/safe_browsing/malware_details_history.h" |
| #include "chrome/browser/safe_browsing/report.pb.h" |
| #include "chrome/common/render_messages.h" |
| #include "chrome/common/safe_browsing/safebrowsing_messages.h" |
| #include "chrome/test/test_url_request_context_getter.h" |
| #include "chrome/test/testing_profile.h" |
| #include "content/browser/browser_thread.h" |
| +#include "chrome/browser/history/history.h" |
| +#include "chrome/browser/history/history_backend.h" |
| #include "content/browser/renderer_host/test_render_view_host.h" |
| #include "content/browser/tab_contents/navigation_entry.h" |
| #include "content/browser/tab_contents/test_tab_contents.h" |
| @@ -52,6 +55,24 @@ |
| namespace { |
| +// Used by WaitForHistory, see it for details. |
| +class WaitForHistoryTask : public HistoryDBTask { |
| + public: |
| + WaitForHistoryTask() {} |
| + |
| + virtual bool RunOnDBThread(history::HistoryBackend* backend, |
| + history::HistoryDatabase* db) { |
| + return true; |
| + } |
| + |
| + virtual void DoneRunOnMainThread() { |
| + MessageLoop::current()->Quit(); |
| + } |
| + |
| + private: |
| + DISALLOW_COPY_AND_ASSIGN(WaitForHistoryTask); |
| +}; |
| + |
| void WriteHeaders(disk_cache::Entry* entry, const std::string headers) { |
| net::HttpResponseInfo responseinfo; |
| std::string raw_headers = net::HttpUtil::AssembleRawHeaders( |
| @@ -175,9 +196,13 @@ |
| // The URLFetcher checks that the messageloop type is IO. |
| ASSERT_TRUE(io_thread_.StartWithOptions( |
| base::Thread::Options(MessageLoop::TYPE_IO, 0))); |
| + |
| + profile_.reset(new TestingProfile); |
| + profile_->CreateHistoryService(false, false); |
| } |
| virtual void TearDown() { |
| + profile_.reset(); |
| io_thread_.Stop(); |
| RenderViewHostTestHarness::TearDown(); |
| } |
| @@ -200,6 +225,18 @@ |
| return sb_service_->GetSerialized(); |
| } |
| + //TestingProfile* profile() {return profile_.get();} |
| + HistoryService* history_service() { |
| + return profile_->GetHistoryService(Profile::EXPLICIT_ACCESS); |
| + } |
| + |
| + // Blocks the caller until history processes a task. This is useful if you |
| + // need to wait until you know history has processed a task. |
| + void WaitForHistory() { |
|
Paweł Hajdan Jr.
2011/05/14 09:33:49
Isn't that TestingProfile::BlockUntilHistoryProces
kewang
2011/05/16 07:11:51
Good to know this! Fixed.
|
| + history_service()->ScheduleDBTask(new WaitForHistoryTask(), &consumer_); |
| + MessageLoop::current()->Run(); |
| + } |
| + |
| protected: |
| void InitResource(SafeBrowsingService::UnsafeResource* resource, |
| ResourceType::Type resource_type, |
| @@ -278,9 +315,19 @@ |
| } |
| } |
| + // Adds a page to history. |
| + void AddPageToHistory(const GURL& url, |
| + const history::RedirectList& redirects) { |
| + history_service()->AddPage( |
| + url, static_cast<void*>(this), 0, GURL(), PageTransition::TYPED, |
| + redirects, history::SOURCE_BROWSED, false); |
| + } |
| + |
| BrowserThread ui_thread_; |
| BrowserThread io_thread_; |
| scoped_refptr<MockSafeBrowsingService> sb_service_; |
| + scoped_ptr<TestingProfile> profile_; |
| + CancelableRequestConsumer consumer_; |
| }; |
| // Tests creating a simple malware report. |
| @@ -592,3 +639,59 @@ |
| VerifyResults(actual, expected); |
| } |
| + |
| +// Test getting redirects from history service |
| +TEST_F(MalwareDetailsTest, HistoryServiceUrls) { |
| + // Add content to history service |
| + GURL baseurl(kMalwareURL); |
| + history::RedirectList redirects; |
| + redirects.push_back(GURL(kFirstRedirectURL)); |
| + redirects.push_back(GURL(kSecondRedirectURL)); |
| + // the last item of the redirect chain has to be the final URL |
| + redirects.push_back(baseurl); |
| + AddPageToHistory(baseurl, redirects); |
| + WaitForHistory(); |
| + |
| + controller().LoadURL(GURL(kLandingURL), GURL(), PageTransition::TYPED); |
| + |
| + SafeBrowsingService::UnsafeResource resource; |
| + InitResource(&resource, ResourceType::SUB_RESOURCE, GURL(kMalwareURL)); |
| + scoped_refptr<MalwareDetailsWrap> report = new MalwareDetailsWrap( |
| + sb_service_.get(), contents(), resource, NULL); |
| + |
| + // Reset the history service pointer |
| + report->redirects_collector_->history_ = history_service(); |
| + |
| + // The redirects collection starts after the IPC from the DOM is fired. |
| + std::vector<SafeBrowsingHostMsg_MalwareDOMDetails_Node> params; |
| + report->OnReceivedMalwareDOMDetails(params); |
| + |
| + // Let the redirects callbacks complete |
| + MessageLoop::current()->RunAllPending(); |
| + |
| + std::string serialized = WaitForSerializedReport(report); |
| + ClientMalwareReportRequest actual; |
| + actual.ParseFromString(serialized); |
| + |
| + ClientMalwareReportRequest expected; |
| + expected.set_malware_url(kMalwareURL); |
| + expected.set_page_url(kLandingURL); |
| + expected.set_referrer_url(""); |
| + |
| + ClientMalwareReportRequest::Resource* pb_resource = expected.add_resources(); |
| + pb_resource->set_id(0); |
| + pb_resource->set_url(kLandingURL); |
| + pb_resource = expected.add_resources(); |
| + pb_resource->set_id(1); |
| + pb_resource->set_parent_id(2); |
| + pb_resource->set_url(kMalwareURL); |
| + pb_resource = expected.add_resources(); |
| + pb_resource->set_id(2); |
| + pb_resource->set_parent_id(3); |
| + pb_resource->set_url(kSecondRedirectURL); |
| + pb_resource = expected.add_resources(); |
| + pb_resource->set_id(3); |
| + pb_resource->set_url(kFirstRedirectURL); |
| + |
| + VerifyResults(actual, expected); |
| +} |