Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / download / download_request_infobar_delegate_unittest.cc
blobe0b657b631afc7b5add8c746207b0f73e029cc74
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 {
14 public:
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_; }
28 private:
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.
33 bool responded_;
35 // True if we have gotten a Accept response. Meaningless if |responded_| is
36 // not true.
37 bool accepted_;
39 // To produce weak pointers for infobar_ construction.
40 base::WeakPtrFactory<MockTabDownloadState> weak_ptr_factory_;
42 DISALLOW_COPY_AND_ASSIGN(MockTabDownloadState);
45 MockTabDownloadState::MockTabDownloadState()
46 : responded_(false),
47 accepted_(false),
48 weak_ptr_factory_(this) {
49 infobar_delegate_ =
50 DownloadRequestInfoBarDelegate::Create(weak_ptr_factory_.GetWeakPtr());
53 MockTabDownloadState::~MockTabDownloadState() {
54 EXPECT_TRUE(responded_);
57 void MockTabDownloadState::Cancel() {
58 EXPECT_FALSE(responded_);
59 responded_ = true;
60 accepted_ = false;
63 void MockTabDownloadState::Accept() {
64 EXPECT_FALSE(responded_);
65 responded_ = true;
66 accepted_ = true;
67 weak_ptr_factory_.InvalidateWeakPtrs();
70 void MockTabDownloadState::CancelOnce() {
71 Cancel();
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());