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 20 ConfirmInfoBarDelegate* infobar() { 21 return infobar_->AsConfirmInfoBarDelegate(); 22 } 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 } 31 bool responded() const { return responded_; } 32 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 46 MockTabDownloadState::MockTabDownloadState() 47 : responded_(false), accepted_(false) { 48 infobar_.reset(new DownloadRequestInfoBarDelegate(NULL, this)); 49 } 50 51 MockTabDownloadState::~MockTabDownloadState() { 52 close_infobar(); 53 EXPECT_TRUE(responded_); 54 } 55 56 void MockTabDownloadState::Cancel() { 57 EXPECT_FALSE(responded_); 58 responded_ = true; 59 accepted_ = false; 60 } 61 62 void 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 72 TEST(DownloadRequestInfobarDelegate, AcceptTest) { 73 MockTabDownloadState state; 74 state.infobar()->Accept(); 75 EXPECT_TRUE(state.accepted()); 76 } 77 78 TEST(DownloadRequestInfobarDelegate, CancelTest) { 79 MockTabDownloadState state; 80 state.infobar()->Cancel(); 81 EXPECT_FALSE(state.accepted()); 82 } 83 84 TEST(DownloadRequestInfobarDelegate, CloseTest) { 85 MockTabDownloadState state; 86 state.close_infobar(); 87 EXPECT_FALSE(state.accepted()); 88 } 89