1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #include "chrome/browser/download/download_request_infobar_delegate.h" 6 #include "chrome/browser/download/download_request_limiter.h" 7 #include "testing/gtest/include/gtest/gtest.h" 8 9 // MockTabDownloadState ------------------------------------------------------- 10 11 class MockTabDownloadState : public DownloadRequestLimiter::TabDownloadState { 12 public: 13 MockTabDownloadState(); 14 virtual ~MockTabDownloadState(); 15 16 // DownloadRequestLimiter::TabDownloadState 17 virtual void Cancel(); 18 virtual void Accept(); 19 infobar()20 ConfirmInfoBarDelegate* infobar() { 21 return infobar_->AsConfirmInfoBarDelegate(); 22 } close_infobar()23 void close_infobar() { 24 // TODO(pkasting): Right now InfoBarDelegates delete themselves via 25 // InfoBarClosed(); once InfoBars own their delegates, this can become a 26 // simple reset() call and ~MockTabDownloadState() will no longer need to 27 // call it. 28 if (infobar_ != NULL) 29 infobar_.release()->InfoBarClosed(); 30 } responded() const31 bool responded() const { return responded_; } accepted() const32 bool accepted() const { return accepted_; } 33 34 private: 35 // The actual infobar delegate we're listening to. 36 scoped_ptr<InfoBarDelegate> infobar_; 37 38 // True if we have gotten some sort of response. 39 bool responded_; 40 41 // True if we have gotten a Accept response. Meaningless if |responded_| is 42 // not true. 43 bool accepted_; 44 }; 45 MockTabDownloadState()46MockTabDownloadState::MockTabDownloadState() 47 : responded_(false), accepted_(false) { 48 infobar_.reset(new DownloadRequestInfoBarDelegate(NULL, this)); 49 } 50 ~MockTabDownloadState()51MockTabDownloadState::~MockTabDownloadState() { 52 close_infobar(); 53 EXPECT_TRUE(responded_); 54 } 55 Cancel()56void MockTabDownloadState::Cancel() { 57 EXPECT_FALSE(responded_); 58 responded_ = true; 59 accepted_ = false; 60 } 61 Accept()62void MockTabDownloadState::Accept() { 63 EXPECT_FALSE(responded_); 64 responded_ = true; 65 accepted_ = true; 66 static_cast<DownloadRequestInfoBarDelegate*>(infobar_.get())->set_host(NULL); 67 } 68 69 70 // Tests ---------------------------------------------------------------------- 71 TEST(DownloadRequestInfobarDelegate,AcceptTest)72TEST(DownloadRequestInfobarDelegate, AcceptTest) { 73 MockTabDownloadState state; 74 state.infobar()->Accept(); 75 EXPECT_TRUE(state.accepted()); 76 } 77 TEST(DownloadRequestInfobarDelegate,CancelTest)78TEST(DownloadRequestInfobarDelegate, CancelTest) { 79 MockTabDownloadState state; 80 state.infobar()->Cancel(); 81 EXPECT_FALSE(state.accepted()); 82 } 83 TEST(DownloadRequestInfobarDelegate,CloseTest)84TEST(DownloadRequestInfobarDelegate, CloseTest) { 85 MockTabDownloadState state; 86 state.close_infobar(); 87 EXPECT_FALSE(state.accepted()); 88 } 89