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