summaryrefslogtreecommitdiffstats
path: root/chrome/browser/download/download_request_infobar_delegate_unittest.cc
blob: 8b73a3fbdb128545d4497a1acf72853244112744 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/memory/scoped_ptr.h"
#include "base/memory/weak_ptr.h"
#include "chrome/browser/download/download_request_infobar_delegate.h"
#include "chrome/browser/download/download_request_limiter.h"
#include "testing/gtest/include/gtest/gtest.h"

// MockTabDownloadState -------------------------------------------------------

class MockTabDownloadState : public DownloadRequestLimiter::TabDownloadState {
 public:
  MockTabDownloadState();
  virtual ~MockTabDownloadState();

  // DownloadRequestLimiter::TabDownloadState
  virtual void Cancel() OVERRIDE;
  virtual void Accept() OVERRIDE;

  ConfirmInfoBarDelegate* infobar() { return infobar_.get(); }
  void delete_infobar_delegate() { infobar_.reset(); }
  bool responded() const { return responded_; }
  bool accepted() const { return accepted_; }

 private:
  // To produce weak pointers for infobar_ construction.
  base::WeakPtrFactory<DownloadRequestLimiter::TabDownloadState> factory_;

  // The actual infobar delegate we're listening to.
  scoped_ptr<DownloadRequestInfoBarDelegate> infobar_;

  // True if we have gotten some sort of response.
  bool responded_;

  // True if we have gotten a Accept response. Meaningless if |responded_| is
  // not true.
  bool accepted_;

};

MockTabDownloadState::MockTabDownloadState()
    : factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)),
      infobar_(DownloadRequestInfoBarDelegate::Create(factory_.GetWeakPtr())),
      responded_(false),
      accepted_(false) {}

MockTabDownloadState::~MockTabDownloadState() {
  EXPECT_TRUE(responded_);
}

void MockTabDownloadState::Cancel() {
  EXPECT_FALSE(responded_);
  responded_ = true;
  accepted_ = false;
}

void MockTabDownloadState::Accept() {
  EXPECT_FALSE(responded_);
  responded_ = true;
  accepted_ = true;
  factory_.InvalidateWeakPtrs();
}


// Tests ----------------------------------------------------------------------

TEST(DownloadRequestInfobarDelegate, AcceptTest) {
  MockTabDownloadState state;
  if (state.infobar()->Accept())
    state.delete_infobar_delegate();
  EXPECT_TRUE(state.accepted());
}

TEST(DownloadRequestInfobarDelegate, CancelTest) {
  MockTabDownloadState state;
  if (state.infobar()->Cancel())
    state.delete_infobar_delegate();
  EXPECT_FALSE(state.accepted());
}

TEST(DownloadRequestInfobarDelegate, CloseTest) {
  MockTabDownloadState state;
  state.delete_infobar_delegate();
  EXPECT_FALSE(state.accepted());
}