This source file includes following definitions.
- delegate_
- Close
- SetDelegate
- CanAcceptRequestUpdate
- Hide
- SetUp
- GetAction
- AskAllow
- FakeCreate
- TearDown
- UnsetDelegate
- CanDownload
- CanDownloadFor
- OnUserGesture
- OnUserGestureFor
- AboutToNavigateRenderView
- ExpectAndResetCounts
- ContinueDownload
- SetHostContentSetting
- Show
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
#include "chrome/browser/download/download_request_limiter.h"
#include "base/bind.h"
#include "base/run_loop.h"
#include "chrome/browser/content_settings/host_content_settings_map.h"
#include "chrome/browser/download/download_request_infobar_delegate.h"
#include "chrome/browser/infobars/infobar_service.h"
#include "chrome/browser/ui/website_settings/permission_bubble_manager.h"
#include "chrome/test/base/chrome_render_view_host_test_harness.h"
#include "chrome/test/base/testing_profile.h"
#include "content/public/browser/navigation_controller.h"
#include "content/public/browser/web_contents.h"
#include "testing/gtest/include/gtest/gtest.h"
using content::WebContents;
class DownloadRequestLimiterTest;
class FakePermissionBubbleView : public PermissionBubbleView {
public:
explicit FakePermissionBubbleView(DownloadRequestLimiterTest *test)
: test_(test), delegate_(NULL) {}
virtual ~FakePermissionBubbleView() {
if (delegate_)
delegate_->SetView(NULL);
}
void Close() {
if (delegate_)
delegate_->Closing();
}
virtual void SetDelegate(Delegate* delegate) OVERRIDE {
delegate_ = delegate;
}
virtual void Show(
const std::vector<PermissionBubbleRequest*>& requests,
const std::vector<bool>& accept_state,
bool customization_mode) OVERRIDE;
virtual bool CanAcceptRequestUpdate() OVERRIDE { return false; }
virtual void Hide() OVERRIDE {}
private:
DownloadRequestLimiterTest* test_;
Delegate* delegate_;
};
class DownloadRequestLimiterTest : public ChromeRenderViewHostTestHarness {
public:
enum TestingAction {
ACCEPT,
CANCEL,
WAIT
};
virtual void SetUp() {
ChromeRenderViewHostTestHarness::SetUp();
InfoBarService::CreateForWebContents(web_contents());
PermissionBubbleManager::CreateForWebContents(web_contents());
view_.reset(new FakePermissionBubbleView(this));
PermissionBubbleManager* manager =
PermissionBubbleManager::FromWebContents(web_contents());
manager->SetView(view_.get());
manager->SetCoalesceIntervalForTesting(0);
testing_action_ = ACCEPT;
ask_allow_count_ = cancel_count_ = continue_count_ = 0;
download_request_limiter_ = new DownloadRequestLimiter();
fake_create_callback_ = base::Bind(
&DownloadRequestLimiterTest::FakeCreate, base::Unretained(this));
DownloadRequestInfoBarDelegate::SetCallbackForTesting(
&fake_create_callback_);
content_settings_ = new HostContentSettingsMap(profile_.GetPrefs(), false);
DownloadRequestLimiter::SetContentSettingsForTesting(
content_settings_.get());
}
int GetAction() {
return testing_action_;
}
void AskAllow() {
ask_allow_count_++;
}
void FakeCreate(
InfoBarService* infobar_service,
base::WeakPtr<DownloadRequestLimiter::TabDownloadState> host) {
AskAllow();
switch (testing_action_) {
case ACCEPT:
host->Accept();
break;
case CANCEL:
host->Cancel();
break;
case WAIT:
break;
}
}
virtual void TearDown() {
content_settings_->ShutdownOnUIThread();
content_settings_ = NULL;
UnsetDelegate();
ChromeRenderViewHostTestHarness::TearDown();
}
virtual void UnsetDelegate() {
DownloadRequestInfoBarDelegate::SetCallbackForTesting(NULL);
}
void CanDownload() {
CanDownloadFor(web_contents());
}
void CanDownloadFor(WebContents* web_contents) {
download_request_limiter_->CanDownloadImpl(
web_contents,
-1,
"GET",
base::Bind(&DownloadRequestLimiterTest::ContinueDownload,
base::Unretained(this)));
base::RunLoop().RunUntilIdle();
}
void OnUserGesture() {
OnUserGestureFor(web_contents());
}
void OnUserGestureFor(WebContents* web_contents) {
DownloadRequestLimiter::TabDownloadState* state =
download_request_limiter_->GetDownloadState(web_contents, NULL, false);
if (state)
state->DidGetUserGesture();
}
void AboutToNavigateRenderView() {
view_->Close();
DownloadRequestLimiter::TabDownloadState* state =
download_request_limiter_->GetDownloadState(
web_contents(), NULL, false);
state->AboutToNavigateRenderView(NULL);
}
void ExpectAndResetCounts(
int expect_continues,
int expect_cancels,
int expect_asks,
int line) {
EXPECT_EQ(expect_continues, continue_count_) << "line " << line;
EXPECT_EQ(expect_cancels, cancel_count_) << "line " << line;
EXPECT_EQ(expect_asks, ask_allow_count_) << "line " << line;
continue_count_ = cancel_count_ = ask_allow_count_ = 0;
}
protected:
void ContinueDownload(bool allow) {
if (allow) {
continue_count_++;
} else {
cancel_count_++;
}
}
void SetHostContentSetting(WebContents* contents, ContentSetting setting) {
content_settings_->SetContentSetting(
ContentSettingsPattern::FromURL(contents->GetURL()),
ContentSettingsPattern::Wildcard(),
CONTENT_SETTINGS_TYPE_AUTOMATIC_DOWNLOADS,
std::string(),
setting);
}
scoped_refptr<DownloadRequestLimiter> download_request_limiter_;
TestingAction testing_action_;
int continue_count_;
int cancel_count_;
int ask_allow_count_;
scoped_refptr<HostContentSettingsMap> content_settings_;
private:
DownloadRequestInfoBarDelegate::FakeCreateCallback fake_create_callback_;
TestingProfile profile_;
scoped_ptr<FakePermissionBubbleView> view_;
};
void FakePermissionBubbleView::Show(
const std::vector<PermissionBubbleRequest*>& requests,
const std::vector<bool>& accept_state,
bool customization_mode) {
test_->AskAllow();
int action = test_->GetAction();
if (action == DownloadRequestLimiterTest::ACCEPT) {
delegate_->Accept();
} else if (action == DownloadRequestLimiterTest::CANCEL) {
delegate_->Deny();
} else if (action == DownloadRequestLimiterTest::WAIT) {
} else {
delegate_->Closing();
}
}
TEST_F(DownloadRequestLimiterTest,
DownloadRequestLimiter_Allow) {
ASSERT_EQ(DownloadRequestLimiter::ALLOW_ONE_DOWNLOAD,
download_request_limiter_->GetDownloadStatus(web_contents()));
CanDownload();
ASSERT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD,
download_request_limiter_->GetDownloadStatus(web_contents()));
ExpectAndResetCounts(1, 0, 0, __LINE__);
testing_action_ = ACCEPT;
CanDownload();
ExpectAndResetCounts(1, 0, 1, __LINE__);
ASSERT_EQ(DownloadRequestLimiter::ALLOW_ALL_DOWNLOADS,
download_request_limiter_->GetDownloadStatus(web_contents()));
CanDownload();
ExpectAndResetCounts(1, 0, 0, __LINE__);
ASSERT_EQ(DownloadRequestLimiter::ALLOW_ALL_DOWNLOADS,
download_request_limiter_->GetDownloadStatus(web_contents()));
}
TEST_F(DownloadRequestLimiterTest,
DownloadRequestLimiter_ResetOnNavigation) {
NavigateAndCommit(GURL("http://foo.com/bar"));
CanDownload();
ExpectAndResetCounts(1, 0, 0, __LINE__);
ASSERT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD,
download_request_limiter_->GetDownloadStatus(web_contents()));
testing_action_ = ACCEPT;
CanDownload();
ExpectAndResetCounts(1, 0, 1, __LINE__);
ASSERT_EQ(DownloadRequestLimiter::ALLOW_ALL_DOWNLOADS,
download_request_limiter_->GetDownloadStatus(web_contents()));
NavigateAndCommit(GURL("http://foo.com/bar2"));
CanDownload();
ExpectAndResetCounts(1, 0, 0, __LINE__);
ASSERT_EQ(DownloadRequestLimiter::ALLOW_ALL_DOWNLOADS,
download_request_limiter_->GetDownloadStatus(web_contents()));
OnUserGesture();
ASSERT_EQ(DownloadRequestLimiter::ALLOW_ALL_DOWNLOADS,
download_request_limiter_->GetDownloadStatus(web_contents()));
NavigateAndCommit(GURL("http://fooey.com"));
ASSERT_EQ(DownloadRequestLimiter::ALLOW_ONE_DOWNLOAD,
download_request_limiter_->GetDownloadStatus(web_contents()));
CanDownload();
ExpectAndResetCounts(1, 0, 0, __LINE__);
ASSERT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD,
download_request_limiter_->GetDownloadStatus(web_contents()));
testing_action_ = CANCEL;
CanDownload();
ExpectAndResetCounts(0, 1, 1, __LINE__);
ASSERT_EQ(DownloadRequestLimiter::DOWNLOADS_NOT_ALLOWED,
download_request_limiter_->GetDownloadStatus(web_contents()));
NavigateAndCommit(GURL("http://fooey.com/bar2"));
CanDownload();
ExpectAndResetCounts(0, 1, 0, __LINE__);
ASSERT_EQ(DownloadRequestLimiter::DOWNLOADS_NOT_ALLOWED,
download_request_limiter_->GetDownloadStatus(web_contents()));
}
TEST_F(DownloadRequestLimiterTest,
DownloadRequestLimiter_ResetOnUserGesture) {
NavigateAndCommit(GURL("http://foo.com/bar"));
CanDownload();
ExpectAndResetCounts(1, 0, 0, __LINE__);
ASSERT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD,
download_request_limiter_->GetDownloadStatus(web_contents()));
OnUserGesture();
ASSERT_EQ(DownloadRequestLimiter::ALLOW_ONE_DOWNLOAD,
download_request_limiter_->GetDownloadStatus(web_contents()));
CanDownload();
ExpectAndResetCounts(1, 0, 0, __LINE__);
ASSERT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD,
download_request_limiter_->GetDownloadStatus(web_contents()));
testing_action_ = CANCEL;
CanDownload();
ASSERT_EQ(DownloadRequestLimiter::DOWNLOADS_NOT_ALLOWED,
download_request_limiter_->GetDownloadStatus(web_contents()));
ExpectAndResetCounts(0, 1, 1, __LINE__);
OnUserGesture();
ASSERT_EQ(DownloadRequestLimiter::DOWNLOADS_NOT_ALLOWED,
download_request_limiter_->GetDownloadStatus(web_contents()));
CanDownload();
ExpectAndResetCounts(0, 1, 0, __LINE__);
ASSERT_EQ(DownloadRequestLimiter::DOWNLOADS_NOT_ALLOWED,
download_request_limiter_->GetDownloadStatus(web_contents()));
}
TEST_F(DownloadRequestLimiterTest,
DownloadRequestLimiter_ResetOnReload) {
NavigateAndCommit(GURL("http://foo.com/bar"));
ASSERT_EQ(DownloadRequestLimiter::ALLOW_ONE_DOWNLOAD,
download_request_limiter_->GetDownloadStatus(web_contents()));
testing_action_ = WAIT;
CanDownload();
ExpectAndResetCounts(1, 0, 0, __LINE__);
ASSERT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD,
download_request_limiter_->GetDownloadStatus(web_contents()));
CanDownload();
ExpectAndResetCounts(0, 0, 1, __LINE__);
ASSERT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD,
download_request_limiter_->GetDownloadStatus(web_contents()));
AboutToNavigateRenderView();
base::RunLoop().RunUntilIdle();
ExpectAndResetCounts(0, 1, 0, __LINE__);
ASSERT_EQ(DownloadRequestLimiter::ALLOW_ONE_DOWNLOAD,
download_request_limiter_->GetDownloadStatus(web_contents()));
CanDownload();
ASSERT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD,
download_request_limiter_->GetDownloadStatus(web_contents()));
ExpectAndResetCounts(1, 0, 0, __LINE__);
testing_action_ = CANCEL;
CanDownload();
ASSERT_EQ(DownloadRequestLimiter::DOWNLOADS_NOT_ALLOWED,
download_request_limiter_->GetDownloadStatus(web_contents()));
ExpectAndResetCounts(0, 1, 1, __LINE__);
AboutToNavigateRenderView();
base::RunLoop().RunUntilIdle();
ASSERT_EQ(DownloadRequestLimiter::DOWNLOADS_NOT_ALLOWED,
download_request_limiter_->GetDownloadStatus(web_contents()));
CanDownload();
ExpectAndResetCounts(0, 1, 0, __LINE__);
ASSERT_EQ(DownloadRequestLimiter::DOWNLOADS_NOT_ALLOWED,
download_request_limiter_->GetDownloadStatus(web_contents()));
}
TEST_F(DownloadRequestLimiterTest,
DownloadRequestLimiter_RawWebContents) {
scoped_ptr<WebContents> web_contents(CreateTestWebContents());
UnsetDelegate();
ExpectAndResetCounts(0, 0, 0, __LINE__);
EXPECT_EQ(DownloadRequestLimiter::ALLOW_ONE_DOWNLOAD,
download_request_limiter_->GetDownloadStatus(web_contents.get()));
CanDownloadFor(web_contents.get());
ExpectAndResetCounts(1, 0, 0, __LINE__);
EXPECT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD,
download_request_limiter_->GetDownloadStatus(web_contents.get()));
OnUserGestureFor(web_contents.get());
EXPECT_EQ(DownloadRequestLimiter::ALLOW_ONE_DOWNLOAD,
download_request_limiter_->GetDownloadStatus(web_contents.get()));
CanDownloadFor(web_contents.get());
ExpectAndResetCounts(1, 0, 0, __LINE__);
EXPECT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD,
download_request_limiter_->GetDownloadStatus(web_contents.get()));
CanDownloadFor(web_contents.get());
ExpectAndResetCounts(0, 1, 0, __LINE__);
EXPECT_EQ(DownloadRequestLimiter::DOWNLOADS_NOT_ALLOWED,
download_request_limiter_->GetDownloadStatus(web_contents.get()));
OnUserGestureFor(web_contents.get());
EXPECT_EQ(DownloadRequestLimiter::ALLOW_ONE_DOWNLOAD,
download_request_limiter_->GetDownloadStatus(web_contents.get()));
CanDownloadFor(web_contents.get());
ExpectAndResetCounts(1, 0, 0, __LINE__);
EXPECT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD,
download_request_limiter_->GetDownloadStatus(web_contents.get()));
}
TEST_F(DownloadRequestLimiterTest,
DownloadRequestLimiter_SetHostContentSetting) {
NavigateAndCommit(GURL("http://foo.com/bar"));
SetHostContentSetting(web_contents(), CONTENT_SETTING_ALLOW);
CanDownload();
ExpectAndResetCounts(1, 0, 0, __LINE__);
ASSERT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD,
download_request_limiter_->GetDownloadStatus(web_contents()));
CanDownload();
ExpectAndResetCounts(1, 0, 0, __LINE__);
ASSERT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD,
download_request_limiter_->GetDownloadStatus(web_contents()));
SetHostContentSetting(web_contents(), CONTENT_SETTING_BLOCK);
CanDownload();
ExpectAndResetCounts(0, 1, 0, __LINE__);
ASSERT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD,
download_request_limiter_->GetDownloadStatus(web_contents()));
CanDownload();
ExpectAndResetCounts(0, 1, 0, __LINE__);
ASSERT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD,
download_request_limiter_->GetDownloadStatus(web_contents()));
}