summaryrefslogtreecommitdiffstats
path: root/chrome/browser/download/download_ui_controller_unittest.cc
blob: d3fc9cbf71c143e6c5423579a740f8b9e9cad312 (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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
// Copyright 2013 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/files/file_path.h"
#include "base/memory/ref_counted.h"
#include "base/memory/scoped_ptr.h"
#include "base/memory/weak_ptr.h"
#include "chrome/browser/download/download_ui_controller.h"
#include "content/public/test/mock_download_item.h"
#include "content/public/test/mock_download_manager.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

using content::MockDownloadItem;
using content::MockDownloadManager;
using testing::AnyNumber;
using testing::Assign;
using testing::Return;
using testing::ReturnRefOfCopy;
using testing::SaveArg;
using testing::_;

namespace {

// A DownloadUIController::Delegate that stores the DownloadItem* for the last
// download that was sent to the UI.
class TestDelegate : public DownloadUIController::Delegate {
 public:
  explicit TestDelegate(base::WeakPtr<content::DownloadItem*> receiver);
  virtual ~TestDelegate() {}

 private:
  virtual void NotifyDownloadStarting(content::DownloadItem* item) OVERRIDE;

  base::WeakPtr<content::DownloadItem*> receiver_;
};

TestDelegate::TestDelegate(base::WeakPtr<content::DownloadItem*> receiver)
    : receiver_(receiver) {
}

void TestDelegate::NotifyDownloadStarting(content::DownloadItem* item) {
  if (receiver_.get())
    *receiver_ = item;
}

class DownloadUIControllerTest : public testing::Test {
 public:
  DownloadUIControllerTest();

 protected:
  // testing::Test
  virtual void SetUp() OVERRIDE;

  // Returns a MockDownloadItem that has AddObserver and RemoveObserver
  // expectations set up to store the observer in |item_observer_|.
  scoped_ptr<MockDownloadItem> GetMockDownload();

  // Returns a TestDelegate. Invoking NotifyDownloadStarting on the returned
  // delegate results in the DownloadItem* being stored in |received_item_|.
  scoped_ptr<DownloadUIController::Delegate> GetTestDelegate();

  MockDownloadManager* manager() { return manager_.get(); }
  content::DownloadManager::Observer* manager_observer() {
    return manager_observer_;
  }
  content::DownloadItem::Observer* item_observer() { return item_observer_; }
  content::DownloadItem* received_item() { return received_item_; }

 private:
  scoped_ptr<MockDownloadManager> manager_;
  content::DownloadManager::Observer* manager_observer_;
  content::DownloadItem::Observer* item_observer_;
  content::DownloadItem* received_item_;

  base::WeakPtrFactory<content::DownloadItem*> receiver_factory_;
};

DownloadUIControllerTest::DownloadUIControllerTest()
    : manager_observer_(NULL),
      item_observer_(NULL),
      received_item_(NULL),
      receiver_factory_(&received_item_) {
}

void DownloadUIControllerTest::SetUp() {
  manager_.reset(new testing::StrictMock<MockDownloadManager>());
  EXPECT_CALL(*manager_, AddObserver(_))
      .WillOnce(SaveArg<0>(&manager_observer_));
  EXPECT_CALL(*manager_, RemoveObserver(_))
      .WillOnce(Assign(&manager_observer_,
                       static_cast<content::DownloadManager::Observer*>(NULL)));
  EXPECT_CALL(*manager_, GetAllDownloads(_));
}

scoped_ptr<MockDownloadItem> DownloadUIControllerTest::GetMockDownload() {
  scoped_ptr<MockDownloadItem> item(
      new testing::StrictMock<MockDownloadItem>());
  EXPECT_CALL(*item, AddObserver(_))
      .WillOnce(SaveArg<0>(&item_observer_));
  EXPECT_CALL(*item, RemoveObserver(_))
      .WillOnce(Assign(&item_observer_,
                       static_cast<content::DownloadItem::Observer*>(NULL)));
  return item.Pass();
}

scoped_ptr<DownloadUIController::Delegate>
DownloadUIControllerTest::GetTestDelegate() {
  scoped_ptr<DownloadUIController::Delegate> delegate(
      new TestDelegate(receiver_factory_.GetWeakPtr()));
  return delegate.Pass();
}

// Normal downloads that are constructed in the IN_PROGRESS state should be
// presented to the UI when GetTargetFilePath() returns a non-empty path.
// I.e. once the download target has been determined.
TEST_F(DownloadUIControllerTest, DownloadUIController_NotifyBasic) {
  scoped_ptr<MockDownloadItem> item = GetMockDownload();
  DownloadUIController controller(manager(), GetTestDelegate());
  EXPECT_CALL(*item, GetTargetFilePath())
      .WillOnce(ReturnRefOfCopy(base::FilePath()));
  EXPECT_CALL(*item, GetState())
      .WillRepeatedly(Return(content::DownloadItem::IN_PROGRESS));

  ASSERT_TRUE(manager_observer());
  manager_observer()->OnDownloadCreated(manager(), item.get());

  // The destination for the download hasn't been determined yet. It should not
  // be displayed.
  EXPECT_FALSE(received_item());
  ASSERT_TRUE(item_observer());

  // Once the destination has been determined, then it should be displayed.
  EXPECT_CALL(*item, GetTargetFilePath())
      .WillOnce(ReturnRefOfCopy(base::FilePath(FILE_PATH_LITERAL("foo"))));
  item_observer()->OnDownloadUpdated(item.get());

  EXPECT_EQ(static_cast<content::DownloadItem*>(item.get()), received_item());
}

// Downloads that have a target path on creation and are in the IN_PROGRESS
// state should be displayed in the UI immediately without requiring an
// additional OnDownloadUpdated() notification.
TEST_F(DownloadUIControllerTest, DownloadUIController_NotifyReadyOnCreate) {
  scoped_ptr<MockDownloadItem> item = GetMockDownload();
  DownloadUIController controller(manager(), GetTestDelegate());
  EXPECT_CALL(*item, GetTargetFilePath())
      .WillOnce(ReturnRefOfCopy(base::FilePath(FILE_PATH_LITERAL("foo"))));
  EXPECT_CALL(*item, GetState())
      .WillRepeatedly(Return(content::DownloadItem::IN_PROGRESS));

  ASSERT_TRUE(manager_observer());
  manager_observer()->OnDownloadCreated(manager(), item.get());
  EXPECT_EQ(static_cast<content::DownloadItem*>(item.get()), received_item());
}

// History downloads (downloads that are not in IN_PROGRESS on create) should
// not be displayed on the shelf.
TEST_F(DownloadUIControllerTest, DownloadUIController_NoNotifyHistory) {
  scoped_ptr<MockDownloadItem> item = GetMockDownload();
  DownloadUIController controller(manager(), GetTestDelegate());
  EXPECT_CALL(*item, GetState())
      .WillRepeatedly(Return(content::DownloadItem::COMPLETE));

  ASSERT_TRUE(manager_observer());
  manager_observer()->OnDownloadCreated(manager(), item.get());
  EXPECT_FALSE(received_item());

  item_observer()->OnDownloadUpdated(item.get());
  EXPECT_FALSE(received_item());
}

} // namespace