15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/download/download_request_limiter.h"
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/bind.h"
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/run_loop.h"
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/content_settings/host_content_settings_map.h"
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/download/download_request_infobar_delegate.h"
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/infobars/infobar_service.h"
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/ui/website_settings/permission_bubble_manager.h"
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/test/base/chrome_render_view_host_test_harness.h"
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/test/base/testing_profile.h"
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/public/browser/navigation_controller.h"
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/public/browser/web_contents.h"
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "testing/gtest/include/gtest/gtest.h"
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)using content::WebContents;
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class DownloadRequestLimiterTest;
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class FakePermissionBubbleView : public PermissionBubbleView {
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  explicit FakePermissionBubbleView(DownloadRequestLimiterTest *test)
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      : test_(test), delegate_(NULL) {}
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual ~FakePermissionBubbleView() {
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (delegate_)
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      delegate_->SetView(NULL);
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void Close() {
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (delegate_)
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      delegate_->Closing();
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // PermissionBubbleView:
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void SetDelegate(Delegate* delegate) OVERRIDE {
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    delegate_ = delegate;
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void Show(
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      const std::vector<PermissionBubbleRequest*>& requests,
455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      const std::vector<bool>& accept_state,
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      bool customization_mode) OVERRIDE;
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual bool CanAcceptRequestUpdate() OVERRIDE { return false; }
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void Hide() OVERRIDE {}
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual bool IsVisible() OVERRIDE { return false; }
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private:
545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DownloadRequestLimiterTest* test_;
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  Delegate* delegate_;
565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class DownloadRequestLimiterTest : public ChromeRenderViewHostTestHarness {
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  enum TestingAction {
615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    ACCEPT,
625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    CANCEL,
635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    WAIT
645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  };
655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void SetUp() {
675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    ChromeRenderViewHostTestHarness::SetUp();
685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    InfoBarService::CreateForWebContents(web_contents());
695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    PermissionBubbleManager::CreateForWebContents(web_contents());
715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    view_.reset(new FakePermissionBubbleView(this));
725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    PermissionBubbleManager* manager =
735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      PermissionBubbleManager::FromWebContents(web_contents());
745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    manager->SetView(view_.get());
755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    testing_action_ = ACCEPT;
775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    ask_allow_count_ = cancel_count_ = continue_count_ = 0;
785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    download_request_limiter_ = new DownloadRequestLimiter();
795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    fake_create_callback_ = base::Bind(
802a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        &DownloadRequestLimiterTest::FakeCreate, base::Unretained(this));
815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    DownloadRequestInfoBarDelegate::SetCallbackForTesting(
822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        &fake_create_callback_);
835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    content_settings_ = new HostContentSettingsMap(profile_.GetPrefs(), false);
845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    DownloadRequestLimiter::SetContentSettingsForTesting(
855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        content_settings_.get());
865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  int GetAction() {
892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return testing_action_;
902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void AskAllow() {
932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    ask_allow_count_++;
942a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void FakeCreate(
975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      InfoBarService* infobar_service,
985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      base::WeakPtr<DownloadRequestLimiter::TabDownloadState> host) {
995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    AskAllow();
1005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    switch (testing_action_) {
1015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      case ACCEPT:
1025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        host->Accept();
1035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        break;
1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      case CANCEL:
1055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        host->Cancel();
1065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        break;
1075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      case WAIT:
1085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        break;
1095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
1105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void TearDown() {
1135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    content_settings_->ShutdownOnUIThread();
1145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    content_settings_ = NULL;
1155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    UnsetDelegate();
1165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    ChromeRenderViewHostTestHarness::TearDown();
1175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void UnsetDelegate() {
1205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    DownloadRequestInfoBarDelegate::SetCallbackForTesting(NULL);
1215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void CanDownload() {
1245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    CanDownloadFor(web_contents());
1255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void CanDownloadFor(WebContents* web_contents) {
1285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    download_request_limiter_->CanDownloadImpl(
1292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        web_contents,
1305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        "GET",  // request method
1315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        base::Bind(&DownloadRequestLimiterTest::ContinueDownload,
1325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                   base::Unretained(this)));
1335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    base::RunLoop().RunUntilIdle();
1345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void OnUserGesture() {
1375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    OnUserGestureFor(web_contents());
1382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
1395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void OnUserGestureFor(WebContents* web_contents) {
1415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    DownloadRequestLimiter::TabDownloadState* state =
1425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        download_request_limiter_->GetDownloadState(web_contents, NULL, false);
1435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (state)
1445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      state->DidGetUserGesture();
1455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void AboutToNavigateRenderView() {
1485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    view_->Close();
1495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    DownloadRequestLimiter::TabDownloadState* state =
1505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        download_request_limiter_->GetDownloadState(
1512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)            web_contents(), NULL, false);
1525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    state->AboutToNavigateRenderView(NULL);
1535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void ExpectAndResetCounts(
1565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      int expect_continues,
1575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      int expect_cancels,
1585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      int expect_asks,
1595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      int line) {
1605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    EXPECT_EQ(expect_continues, continue_count_) << "line " << line;
1615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    EXPECT_EQ(expect_cancels, cancel_count_) << "line " << line;
1622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    EXPECT_EQ(expect_asks, ask_allow_count_) << "line " << line;
1635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    continue_count_ = cancel_count_ = ask_allow_count_ = 0;
1645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) protected:
1675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void ContinueDownload(bool allow) {
1685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (allow) {
1695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      continue_count_++;
1705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    } else {
1715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      cancel_count_++;
1725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
1735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void SetHostContentSetting(WebContents* contents, ContentSetting setting) {
1765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    content_settings_->SetContentSetting(
1775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        ContentSettingsPattern::FromURL(contents->GetURL()),
1785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        ContentSettingsPattern::Wildcard(),
1795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        CONTENT_SETTINGS_TYPE_AUTOMATIC_DOWNLOADS,
1805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        std::string(),
1815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        setting);
1825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1832a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  scoped_refptr<DownloadRequestLimiter> download_request_limiter_;
1855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The action that FakeCreate() should take.
1875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  TestingAction testing_action_;
1885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Number of times ContinueDownload was invoked.
1905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int continue_count_;
1915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Number of times CancelDownload was invoked.
1935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int cancel_count_;
1945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Number of times ShouldAllowDownload was invoked.
1965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int ask_allow_count_;
1975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1982a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  scoped_refptr<HostContentSettingsMap> content_settings_;
1995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private:
2015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DownloadRequestInfoBarDelegate::FakeCreateCallback fake_create_callback_;
2025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  TestingProfile profile_;
2035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  scoped_ptr<FakePermissionBubbleView> view_;
2045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
2055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void FakePermissionBubbleView::Show(
2075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const std::vector<PermissionBubbleRequest*>& requests,
2082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const std::vector<bool>& accept_state,
2095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    bool customization_mode) {
2102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  test_->AskAllow();
2115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int action = test_->GetAction();
2125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (action == DownloadRequestLimiterTest::ACCEPT) {
2135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    delegate_->Accept();
2145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  } else if (action == DownloadRequestLimiterTest::CANCEL) {
2155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    delegate_->Deny();
2165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  } else if (action == DownloadRequestLimiterTest::WAIT) {
2175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // do nothing.
2185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  } else {
2195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    delegate_->Closing();
2205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
2215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)TEST_F(DownloadRequestLimiterTest,
2245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)       DownloadRequestLimiter_Allow) {
2255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // All tabs should initially start at ALLOW_ONE_DOWNLOAD.
2265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ASSERT_EQ(DownloadRequestLimiter::ALLOW_ONE_DOWNLOAD,
2275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            download_request_limiter_->GetDownloadStatus(web_contents()));
2285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Ask if the tab can do a download. This moves to PROMPT_BEFORE_DOWNLOAD.
2305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  CanDownload();
2315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ASSERT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD,
2325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            download_request_limiter_->GetDownloadStatus(web_contents()));
2335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // We should have been told we can download.
2345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ExpectAndResetCounts(1, 0, 0, __LINE__);
2355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Ask again. This triggers asking the delegate for allow/disallow.
2372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  testing_action_ = ACCEPT;
2385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  CanDownload();
2395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // This should ask us if the download is allowed.
2405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // We should have been told we can download.
2415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ExpectAndResetCounts(1, 0, 1, __LINE__);
2425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ASSERT_EQ(DownloadRequestLimiter::ALLOW_ALL_DOWNLOADS,
2435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            download_request_limiter_->GetDownloadStatus(web_contents()));
2445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Ask again and make sure continue is invoked.
2465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  CanDownload();
2475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The state is at allow_all, which means the delegate shouldn't be asked.
2485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // We should have been told we can download.
2495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ExpectAndResetCounts(1, 0, 0, __LINE__);
2505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ASSERT_EQ(DownloadRequestLimiter::ALLOW_ALL_DOWNLOADS,
2515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            download_request_limiter_->GetDownloadStatus(web_contents()));
2525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)TEST_F(DownloadRequestLimiterTest,
2555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)       DownloadRequestLimiter_ResetOnNavigation) {
2565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  NavigateAndCommit(GURL("http://foo.com/bar"));
2575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Do two downloads, allowing the second so that we end up with allow all.
2595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  CanDownload();
2602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  ExpectAndResetCounts(1, 0, 0, __LINE__);
2615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ASSERT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD,
2625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            download_request_limiter_->GetDownloadStatus(web_contents()));
2635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  testing_action_ = ACCEPT;
2655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  CanDownload();
266  ExpectAndResetCounts(1, 0, 1, __LINE__);
267  ASSERT_EQ(DownloadRequestLimiter::ALLOW_ALL_DOWNLOADS,
268            download_request_limiter_->GetDownloadStatus(web_contents()));
269
270  // Navigate to a new URL with the same host, which shouldn't reset the allow
271  // all state.
272  NavigateAndCommit(GURL("http://foo.com/bar2"));
273  CanDownload();
274  ExpectAndResetCounts(1, 0, 0, __LINE__);
275  ASSERT_EQ(DownloadRequestLimiter::ALLOW_ALL_DOWNLOADS,
276            download_request_limiter_->GetDownloadStatus(web_contents()));
277
278  // Do a user gesture, because we're at allow all, this shouldn't change the
279  // state.
280  OnUserGesture();
281  ASSERT_EQ(DownloadRequestLimiter::ALLOW_ALL_DOWNLOADS,
282            download_request_limiter_->GetDownloadStatus(web_contents()));
283
284  // Navigate to a completely different host, which should reset the state.
285  NavigateAndCommit(GURL("http://fooey.com"));
286  ASSERT_EQ(DownloadRequestLimiter::ALLOW_ONE_DOWNLOAD,
287            download_request_limiter_->GetDownloadStatus(web_contents()));
288
289  // Do two downloads, allowing the second so that we end up with allow all.
290  CanDownload();
291  ExpectAndResetCounts(1, 0, 0, __LINE__);
292  ASSERT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD,
293            download_request_limiter_->GetDownloadStatus(web_contents()));
294
295  testing_action_ = CANCEL;
296  CanDownload();
297  ExpectAndResetCounts(0, 1, 1, __LINE__);
298  ASSERT_EQ(DownloadRequestLimiter::DOWNLOADS_NOT_ALLOWED,
299            download_request_limiter_->GetDownloadStatus(web_contents()));
300
301  // Navigate to a new URL with the same host, which shouldn't reset the allow
302  // all state.
303  NavigateAndCommit(GURL("http://fooey.com/bar2"));
304  CanDownload();
305  ExpectAndResetCounts(0, 1, 0, __LINE__);
306  ASSERT_EQ(DownloadRequestLimiter::DOWNLOADS_NOT_ALLOWED,
307            download_request_limiter_->GetDownloadStatus(web_contents()));
308}
309
310TEST_F(DownloadRequestLimiterTest,
311       DownloadRequestLimiter_ResetOnUserGesture) {
312  NavigateAndCommit(GURL("http://foo.com/bar"));
313
314  // Do one download, which should change to prompt before download.
315  CanDownload();
316  ExpectAndResetCounts(1, 0, 0, __LINE__);
317  ASSERT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD,
318            download_request_limiter_->GetDownloadStatus(web_contents()));
319
320  // Do a user gesture, which should reset back to allow one.
321  OnUserGesture();
322  ASSERT_EQ(DownloadRequestLimiter::ALLOW_ONE_DOWNLOAD,
323            download_request_limiter_->GetDownloadStatus(web_contents()));
324
325  // Ask twice, which triggers calling the delegate. Don't allow the download
326  // so that we end up with not allowed.
327  CanDownload();
328  ExpectAndResetCounts(1, 0, 0, __LINE__);
329  ASSERT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD,
330            download_request_limiter_->GetDownloadStatus(web_contents()));
331
332  testing_action_ = CANCEL;
333  CanDownload();
334  ASSERT_EQ(DownloadRequestLimiter::DOWNLOADS_NOT_ALLOWED,
335            download_request_limiter_->GetDownloadStatus(web_contents()));
336  ExpectAndResetCounts(0, 1, 1, __LINE__);
337
338  // A user gesture now should NOT change the state.
339  OnUserGesture();
340  ASSERT_EQ(DownloadRequestLimiter::DOWNLOADS_NOT_ALLOWED,
341            download_request_limiter_->GetDownloadStatus(web_contents()));
342  // And make sure we really can't download.
343  CanDownload();
344  ExpectAndResetCounts(0, 1, 0, __LINE__);
345  // And the state shouldn't have changed.
346  ASSERT_EQ(DownloadRequestLimiter::DOWNLOADS_NOT_ALLOWED,
347            download_request_limiter_->GetDownloadStatus(web_contents()));
348}
349
350TEST_F(DownloadRequestLimiterTest,
351       DownloadRequestLimiter_ResetOnReload) {
352  NavigateAndCommit(GURL("http://foo.com/bar"));
353  ASSERT_EQ(DownloadRequestLimiter::ALLOW_ONE_DOWNLOAD,
354            download_request_limiter_->GetDownloadStatus(web_contents()));
355
356  // If the user refreshes the page without responding to the infobar, pretend
357  // like the refresh is the initial load: they get 1 free download (probably
358  // the same as the actual initial load), then an infobar.
359  testing_action_ = WAIT;
360
361  CanDownload();
362  ExpectAndResetCounts(1, 0, 0, __LINE__);
363  ASSERT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD,
364            download_request_limiter_->GetDownloadStatus(web_contents()));
365
366  CanDownload();
367  ExpectAndResetCounts(0, 0, 1, __LINE__);
368  ASSERT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD,
369            download_request_limiter_->GetDownloadStatus(web_contents()));
370
371  AboutToNavigateRenderView();
372  base::RunLoop().RunUntilIdle();
373  ExpectAndResetCounts(0, 1, 0, __LINE__);
374  ASSERT_EQ(DownloadRequestLimiter::ALLOW_ONE_DOWNLOAD,
375            download_request_limiter_->GetDownloadStatus(web_contents()));
376
377  CanDownload();
378  ASSERT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD,
379            download_request_limiter_->GetDownloadStatus(web_contents()));
380  ExpectAndResetCounts(1, 0, 0, __LINE__);
381
382  testing_action_ = CANCEL;
383  CanDownload();
384  ASSERT_EQ(DownloadRequestLimiter::DOWNLOADS_NOT_ALLOWED,
385            download_request_limiter_->GetDownloadStatus(web_contents()));
386  ExpectAndResetCounts(0, 1, 1, __LINE__);
387
388  AboutToNavigateRenderView();
389  base::RunLoop().RunUntilIdle();
390  ASSERT_EQ(DownloadRequestLimiter::DOWNLOADS_NOT_ALLOWED,
391            download_request_limiter_->GetDownloadStatus(web_contents()));
392  CanDownload();
393  ExpectAndResetCounts(0, 1, 0, __LINE__);
394  ASSERT_EQ(DownloadRequestLimiter::DOWNLOADS_NOT_ALLOWED,
395            download_request_limiter_->GetDownloadStatus(web_contents()));
396}
397
398TEST_F(DownloadRequestLimiterTest,
399       DownloadRequestLimiter_RawWebContents) {
400  scoped_ptr<WebContents> web_contents(CreateTestWebContents());
401
402  // DownloadRequestLimiter won't try to make a permission bubble if there's
403  // no permission bubble manager, so don't put one on the test WebContents.
404
405  // DownloadRequestLimiter won't try to make an infobar if it doesn't have an
406  // InfoBarService, and we want to test that it will Cancel() instead of
407  // prompting when it doesn't have a InfoBarService, so unset the delegate.
408  UnsetDelegate();
409  ExpectAndResetCounts(0, 0, 0, __LINE__);
410  EXPECT_EQ(DownloadRequestLimiter::ALLOW_ONE_DOWNLOAD,
411            download_request_limiter_->GetDownloadStatus(web_contents.get()));
412  // You get one freebie.
413  CanDownloadFor(web_contents.get());
414  ExpectAndResetCounts(1, 0, 0, __LINE__);
415  EXPECT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD,
416            download_request_limiter_->GetDownloadStatus(web_contents.get()));
417  OnUserGestureFor(web_contents.get());
418  EXPECT_EQ(DownloadRequestLimiter::ALLOW_ONE_DOWNLOAD,
419            download_request_limiter_->GetDownloadStatus(web_contents.get()));
420  CanDownloadFor(web_contents.get());
421  ExpectAndResetCounts(1, 0, 0, __LINE__);
422  EXPECT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD,
423            download_request_limiter_->GetDownloadStatus(web_contents.get()));
424  CanDownloadFor(web_contents.get());
425  ExpectAndResetCounts(0, 1, 0, __LINE__);
426  EXPECT_EQ(DownloadRequestLimiter::DOWNLOADS_NOT_ALLOWED,
427            download_request_limiter_->GetDownloadStatus(web_contents.get()));
428  OnUserGestureFor(web_contents.get());
429  EXPECT_EQ(DownloadRequestLimiter::ALLOW_ONE_DOWNLOAD,
430            download_request_limiter_->GetDownloadStatus(web_contents.get()));
431  CanDownloadFor(web_contents.get());
432  ExpectAndResetCounts(1, 0, 0, __LINE__);
433  EXPECT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD,
434            download_request_limiter_->GetDownloadStatus(web_contents.get()));
435}
436
437TEST_F(DownloadRequestLimiterTest,
438       DownloadRequestLimiter_SetHostContentSetting) {
439  NavigateAndCommit(GURL("http://foo.com/bar"));
440  SetHostContentSetting(web_contents(), CONTENT_SETTING_ALLOW);
441
442  CanDownload();
443  ExpectAndResetCounts(1, 0, 0, __LINE__);
444  ASSERT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD,
445            download_request_limiter_->GetDownloadStatus(web_contents()));
446
447  CanDownload();
448  ExpectAndResetCounts(1, 0, 0, __LINE__);
449  ASSERT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD,
450            download_request_limiter_->GetDownloadStatus(web_contents()));
451
452  SetHostContentSetting(web_contents(), CONTENT_SETTING_BLOCK);
453
454  CanDownload();
455  ExpectAndResetCounts(0, 1, 0, __LINE__);
456  ASSERT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD,
457            download_request_limiter_->GetDownloadStatus(web_contents()));
458
459  CanDownload();
460  ExpectAndResetCounts(0, 1, 0, __LINE__);
461  ASSERT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD,
462            download_request_limiter_->GetDownloadStatus(web_contents()));
463}
464