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 "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"
10
11// MockTabDownloadState -------------------------------------------------------
12
13class MockTabDownloadState : public DownloadRequestLimiter::TabDownloadState {
14 public:
15  MockTabDownloadState();
16  virtual ~MockTabDownloadState();
17
18  // DownloadRequestLimiter::TabDownloadState:
19  virtual void Cancel() OVERRIDE;
20  virtual void Accept() OVERRIDE;
21  virtual void CancelOnce() OVERRIDE;
22
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_; }
27
28 private:
29  // The actual infobar delegate we're listening to.
30  scoped_ptr<DownloadRequestInfoBarDelegate> infobar_delegate_;
31
32  // True if we have gotten some sort of response.
33  bool responded_;
34
35  // True if we have gotten a Accept response. Meaningless if |responded_| is
36  // not true.
37  bool accepted_;
38
39  // To produce weak pointers for infobar_ construction.
40  base::WeakPtrFactory<MockTabDownloadState> weak_ptr_factory_;
41
42  DISALLOW_COPY_AND_ASSIGN(MockTabDownloadState);
43};
44
45MockTabDownloadState::MockTabDownloadState()
46    : responded_(false),
47      accepted_(false),
48      weak_ptr_factory_(this) {
49  infobar_delegate_ =
50      DownloadRequestInfoBarDelegate::Create(weak_ptr_factory_.GetWeakPtr());
51}
52
53MockTabDownloadState::~MockTabDownloadState() {
54  EXPECT_TRUE(responded_);
55}
56
57void MockTabDownloadState::Cancel() {
58  EXPECT_FALSE(responded_);
59  responded_ = true;
60  accepted_ = false;
61}
62
63void MockTabDownloadState::Accept() {
64  EXPECT_FALSE(responded_);
65  responded_ = true;
66  accepted_ = true;
67  weak_ptr_factory_.InvalidateWeakPtrs();
68}
69
70void MockTabDownloadState::CancelOnce() {
71  Cancel();
72}
73
74
75// Tests ----------------------------------------------------------------------
76
77TEST(DownloadRequestInfoBarDelegate, AcceptTest) {
78  MockTabDownloadState state;
79  if (state.infobar_delegate()->Accept())
80    state.delete_infobar_delegate();
81  EXPECT_TRUE(state.accepted());
82}
83
84TEST(DownloadRequestInfoBarDelegate, CancelTest) {
85  MockTabDownloadState state;
86  if (state.infobar_delegate()->Cancel())
87    state.delete_infobar_delegate();
88  EXPECT_FALSE(state.accepted());
89}
90
91TEST(DownloadRequestInfoBarDelegate, CloseTest) {
92  MockTabDownloadState state;
93  state.delete_infobar_delegate();
94  EXPECT_FALSE(state.accepted());
95}
96