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.
5 #include "base/memory/scoped_ptr.h"
6 #include "base/memory/weak_ptr.h"
7 #include "chrome/browser/download/download_request_infobar_delegate.h"
8 #include "chrome/browser/download/download_request_limiter.h"
9 #include "testing/gtest/include/gtest/gtest.h"
11 // MockTabDownloadState -------------------------------------------------------
13 class MockTabDownloadState
: public DownloadRequestLimiter::TabDownloadState
{
15 MockTabDownloadState();
16 virtual ~MockTabDownloadState();
18 // DownloadRequestLimiter::TabDownloadState:
19 virtual void Cancel() OVERRIDE
;
20 virtual void Accept() OVERRIDE
;
21 virtual void CancelOnce() OVERRIDE
;
23 ConfirmInfoBarDelegate
* infobar_delegate() { return infobar_delegate_
.get(); }
24 void delete_infobar_delegate() { infobar_delegate_
.reset(); }
25 bool responded() const { return responded_
; }
26 bool accepted() const { return accepted_
; }
29 // The actual infobar delegate we're listening to.
30 scoped_ptr
<DownloadRequestInfoBarDelegate
> infobar_delegate_
;
32 // True if we have gotten some sort of response.
35 // True if we have gotten a Accept response. Meaningless if |responded_| is
39 // To produce weak pointers for infobar_ construction.
40 base::WeakPtrFactory
<MockTabDownloadState
> weak_ptr_factory_
;
42 DISALLOW_COPY_AND_ASSIGN(MockTabDownloadState
);
45 MockTabDownloadState::MockTabDownloadState()
48 weak_ptr_factory_(this) {
50 DownloadRequestInfoBarDelegate::Create(weak_ptr_factory_
.GetWeakPtr());
53 MockTabDownloadState::~MockTabDownloadState() {
54 EXPECT_TRUE(responded_
);
57 void MockTabDownloadState::Cancel() {
58 EXPECT_FALSE(responded_
);
63 void MockTabDownloadState::Accept() {
64 EXPECT_FALSE(responded_
);
67 weak_ptr_factory_
.InvalidateWeakPtrs();
70 void MockTabDownloadState::CancelOnce() {
75 // Tests ----------------------------------------------------------------------
77 TEST(DownloadRequestInfoBarDelegate
, AcceptTest
) {
78 MockTabDownloadState state
;
79 if (state
.infobar_delegate()->Accept())
80 state
.delete_infobar_delegate();
81 EXPECT_TRUE(state
.accepted());
84 TEST(DownloadRequestInfoBarDelegate
, CancelTest
) {
85 MockTabDownloadState state
;
86 if (state
.infobar_delegate()->Cancel())
87 state
.delete_infobar_delegate();
88 EXPECT_FALSE(state
.accepted());
91 TEST(DownloadRequestInfoBarDelegate
, CloseTest
) {
92 MockTabDownloadState state
;
93 state
.delete_infobar_delegate();
94 EXPECT_FALSE(state
.accepted());