Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(12)

Side by Side Diff: chrome/browser/download/download_ui_controller_unittest.cc

Issue 230103002: [Downloads] Ask DownloadHistory if a download was from history. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix build Created 6 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « chrome/browser/download/download_ui_controller.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/bind.h"
6 #include "base/callback.h"
5 #include "base/files/file_path.h" 7 #include "base/files/file_path.h"
6 #include "base/memory/ref_counted.h" 8 #include "base/memory/ref_counted.h"
7 #include "base/memory/scoped_ptr.h" 9 #include "base/memory/scoped_ptr.h"
8 #include "base/memory/weak_ptr.h" 10 #include "base/memory/weak_ptr.h"
11 #include "base/observer_list.h"
12 #include "chrome/browser/download/download_history.h"
13 #include "chrome/browser/download/download_service.h"
14 #include "chrome/browser/download/download_service_factory.h"
9 #include "chrome/browser/download/download_ui_controller.h" 15 #include "chrome/browser/download/download_ui_controller.h"
16 #include "chrome/browser/history/download_row.h"
17 #include "chrome/browser/profiles/profile.h"
18 #include "chrome/test/base/chrome_render_view_host_test_harness.h"
10 #include "content/public/test/mock_download_item.h" 19 #include "content/public/test/mock_download_item.h"
11 #include "content/public/test/mock_download_manager.h" 20 #include "content/public/test/mock_download_manager.h"
12 #include "testing/gmock/include/gmock/gmock.h" 21 #include "testing/gmock/include/gmock/gmock.h"
13 #include "testing/gtest/include/gtest/gtest.h" 22 #include "testing/gtest/include/gtest/gtest.h"
14 23
15 using content::MockDownloadItem; 24 using content::MockDownloadItem;
16 using content::MockDownloadManager; 25 using content::MockDownloadManager;
17 using testing::AnyNumber; 26 using testing::AnyNumber;
18 using testing::Assign; 27 using testing::Assign;
19 using testing::Return; 28 using testing::Return;
20 using testing::ReturnRefOfCopy; 29 using testing::ReturnRefOfCopy;
21 using testing::SaveArg; 30 using testing::SaveArg;
22 using testing::_; 31 using testing::_;
23 32
24 namespace { 33 namespace {
25 34
26 // A DownloadUIController::Delegate that stores the DownloadItem* for the last 35 // A DownloadUIController::Delegate that stores the DownloadItem* for the last
27 // download that was sent to the UI. 36 // download that was sent to the UI.
28 class TestDelegate : public DownloadUIController::Delegate { 37 class TestDelegate : public DownloadUIController::Delegate {
29 public: 38 public:
30 explicit TestDelegate(base::WeakPtr<content::DownloadItem*> receiver); 39 explicit TestDelegate(base::WeakPtr<content::DownloadItem*> receiver);
31 virtual ~TestDelegate() {} 40 virtual ~TestDelegate() {}
32 41
33 private: 42 private:
34 virtual void NotifyDownloadStarting(content::DownloadItem* item) OVERRIDE; 43 virtual void OnNewDownloadReady(content::DownloadItem* item) OVERRIDE;
35 44
36 base::WeakPtr<content::DownloadItem*> receiver_; 45 base::WeakPtr<content::DownloadItem*> receiver_;
37 }; 46 };
38 47
39 TestDelegate::TestDelegate(base::WeakPtr<content::DownloadItem*> receiver) 48 TestDelegate::TestDelegate(base::WeakPtr<content::DownloadItem*> receiver)
40 : receiver_(receiver) { 49 : receiver_(receiver) {
41 } 50 }
42 51
43 void TestDelegate::NotifyDownloadStarting(content::DownloadItem* item) { 52 void TestDelegate::OnNewDownloadReady(content::DownloadItem* item) {
44 if (receiver_.get()) 53 if (receiver_.get())
45 *receiver_ = item; 54 *receiver_ = item;
46 } 55 }
47 56
48 class DownloadUIControllerTest : public testing::Test { 57 // A DownloadService that returns a custom DownloadHistory.
58 class TestDownloadService : public DownloadService {
59 public:
60 explicit TestDownloadService(Profile* profile);
61 virtual ~TestDownloadService();
62
63 void set_download_history(scoped_ptr<DownloadHistory> download_history) {
64 download_history_.swap(download_history);
65 }
66 virtual DownloadHistory* GetDownloadHistory() OVERRIDE;
67
68 private:
69 scoped_ptr<DownloadHistory> download_history_;
70 };
71
72 TestDownloadService::TestDownloadService(Profile* profile)
73 : DownloadService(profile) {
74 }
75
76 TestDownloadService::~TestDownloadService() {
77 }
78
79 DownloadHistory* TestDownloadService::GetDownloadHistory() {
80 return download_history_.get();
81 }
82
83 // The test fixture:
84 class DownloadUIControllerTest : public ChromeRenderViewHostTestHarness {
49 public: 85 public:
50 DownloadUIControllerTest(); 86 DownloadUIControllerTest();
51 87
52 protected: 88 protected:
53 // testing::Test 89 // testing::Test
54 virtual void SetUp() OVERRIDE; 90 virtual void SetUp() OVERRIDE;
55 91
56 // Returns a TestDelegate. Invoking NotifyDownloadStarting on the returned 92 // Returns a TestDelegate. Invoking OnNewDownloadReady on the returned
57 // delegate results in the DownloadItem* being stored in |received_item_|. 93 // delegate results in the DownloadItem* being stored in |notified_item_|.
58 scoped_ptr<DownloadUIController::Delegate> GetTestDelegate(); 94 scoped_ptr<DownloadUIController::Delegate> GetTestDelegate();
59 95
60 MockDownloadManager* manager() { return manager_.get(); } 96 MockDownloadManager* manager() { return manager_.get(); }
97
98 // Returns the DownloadManager::Observer registered by a test case. This is
99 // the DownloadUIController's observer for all current test cases.
61 content::DownloadManager::Observer* manager_observer() { 100 content::DownloadManager::Observer* manager_observer() {
62 return manager_observer_; 101 return manager_observer_;
63 } 102 }
64 content::DownloadItem* received_item() { return received_item_; } 103
104 // The most recent DownloadItem that was passed into OnNewDownloadReady().
105 content::DownloadItem* notified_item() { return notified_item_; }
106
107 // DownloadHistory performs a query of existing downloads when it is first
108 // instantiated. This method returns the completion callback for that query.
109 // It can be used to inject history downloads.
110 const HistoryService::DownloadQueryCallback& history_query_callback() const {
111 return history_adapter_->download_query_callback_;
112 }
113
114 // DownloadManager::Observer registered by DownloadHistory.
115 content::DownloadManager::Observer* download_history_manager_observer() {
116 return download_history_manager_observer_;
117 }
118
119 scoped_ptr<MockDownloadItem> CreateMockInProgressDownload();
65 120
66 private: 121 private:
122 // A private history adapter that stores the DownloadQueryCallback when
123 // QueryDownloads is called.
124 class HistoryAdapter : public DownloadHistory::HistoryAdapter {
125 public:
126 HistoryAdapter() : DownloadHistory::HistoryAdapter(NULL) {}
127 HistoryService::DownloadQueryCallback download_query_callback_;
128
129 private:
130 virtual void QueryDownloads(
131 const HistoryService::DownloadQueryCallback& callback) OVERRIDE {
132 download_query_callback_ = callback;
133 }
134 };
135
136 // Constructs and returns a TestDownloadService.
137 static KeyedService* TestingDownloadServiceFactory(
138 content::BrowserContext* brwoser_context);
139
67 scoped_ptr<MockDownloadManager> manager_; 140 scoped_ptr<MockDownloadManager> manager_;
141 content::DownloadManager::Observer* download_history_manager_observer_;
68 content::DownloadManager::Observer* manager_observer_; 142 content::DownloadManager::Observer* manager_observer_;
69 content::DownloadItem* received_item_; 143 content::DownloadItem* notified_item_;
70 144 base::WeakPtrFactory<content::DownloadItem*> notified_item_receiver_factory_;
71 base::WeakPtrFactory<content::DownloadItem*> receiver_factory_; 145
146 HistoryAdapter* history_adapter_;
72 }; 147 };
73 148
149 // static
150 KeyedService* DownloadUIControllerTest::TestingDownloadServiceFactory(
151 content::BrowserContext* browser_context) {
152 return new TestDownloadService(Profile::FromBrowserContext(browser_context));
153 }
154
74 DownloadUIControllerTest::DownloadUIControllerTest() 155 DownloadUIControllerTest::DownloadUIControllerTest()
75 : manager_observer_(NULL), 156 : download_history_manager_observer_(NULL),
76 received_item_(NULL), 157 manager_observer_(NULL),
77 receiver_factory_(&received_item_) { 158 notified_item_(NULL),
159 notified_item_receiver_factory_(&notified_item_) {
78 } 160 }
79 161
80 void DownloadUIControllerTest::SetUp() { 162 void DownloadUIControllerTest::SetUp() {
163 ChromeRenderViewHostTestHarness::SetUp();
164
81 manager_.reset(new testing::StrictMock<MockDownloadManager>()); 165 manager_.reset(new testing::StrictMock<MockDownloadManager>());
82 EXPECT_CALL(*manager_, AddObserver(_)) 166 EXPECT_CALL(*manager_, AddObserver(_))
167 .WillOnce(SaveArg<0>(&download_history_manager_observer_));
168 EXPECT_CALL(*manager_,
169 RemoveObserver(testing::Eq(
170 testing::ByRef(download_history_manager_observer_))))
171 .WillOnce(testing::Assign(
172 &download_history_manager_observer_,
173 static_cast<content::DownloadManager::Observer*>(NULL)));
174 EXPECT_CALL(*manager_, GetAllDownloads(_)).Times(AnyNumber());
175
176 scoped_ptr<HistoryAdapter> history_adapter(new HistoryAdapter);
177 history_adapter_ = history_adapter.get();
178 scoped_ptr<DownloadHistory> download_history(new DownloadHistory(
179 manager_.get(),
180 history_adapter.PassAs<DownloadHistory::HistoryAdapter>()));
181 ASSERT_TRUE(download_history_manager_observer_);
182
183 EXPECT_CALL(*manager_, AddObserver(_))
83 .WillOnce(SaveArg<0>(&manager_observer_)); 184 .WillOnce(SaveArg<0>(&manager_observer_));
84 EXPECT_CALL(*manager_, RemoveObserver(_)) 185 EXPECT_CALL(*manager_,
85 .WillOnce(Assign(&manager_observer_, 186 RemoveObserver(testing::Eq(testing::ByRef(manager_observer_))))
86 static_cast<content::DownloadManager::Observer*>(NULL))); 187 .WillOnce(testing::Assign(
87 EXPECT_CALL(*manager_, GetAllDownloads(_)); 188 &manager_observer_,
189 static_cast<content::DownloadManager::Observer*>(NULL)));
190 TestDownloadService* download_service = static_cast<TestDownloadService*>(
191 DownloadServiceFactory::GetInstance()->SetTestingFactoryAndUse(
192 browser_context(), &TestingDownloadServiceFactory));
193 ASSERT_TRUE(download_service);
194 download_service->set_download_history(download_history.Pass());
195 }
196
197 scoped_ptr<MockDownloadItem>
198 DownloadUIControllerTest::CreateMockInProgressDownload() {
199 scoped_ptr<MockDownloadItem> item(
200 new testing::StrictMock<MockDownloadItem>());
201 EXPECT_CALL(*item, GetBrowserContext())
202 .WillRepeatedly(Return(browser_context()));
203 EXPECT_CALL(*item, GetId()).WillRepeatedly(Return(1));
204 EXPECT_CALL(*item, GetTargetFilePath()).WillRepeatedly(
205 ReturnRefOfCopy(base::FilePath(FILE_PATH_LITERAL("foo"))));
206 EXPECT_CALL(*item, GetFullPath()).WillRepeatedly(
207 ReturnRefOfCopy(base::FilePath(FILE_PATH_LITERAL("foo"))));
208 EXPECT_CALL(*item, GetState())
209 .WillRepeatedly(Return(content::DownloadItem::IN_PROGRESS));
210 EXPECT_CALL(*item, GetUrlChain())
211 .WillRepeatedly(testing::ReturnRefOfCopy(std::vector<GURL>()));
212 EXPECT_CALL(*item, GetReferrerUrl())
213 .WillRepeatedly(testing::ReturnRefOfCopy(GURL()));
214 EXPECT_CALL(*item, GetStartTime()).WillRepeatedly(Return(base::Time()));
215 EXPECT_CALL(*item, GetEndTime()).WillRepeatedly(Return(base::Time()));
216 EXPECT_CALL(*item, GetETag()).WillRepeatedly(ReturnRefOfCopy(std::string()));
217 EXPECT_CALL(*item, GetLastModifiedTime())
218 .WillRepeatedly(ReturnRefOfCopy(std::string()));
219 EXPECT_CALL(*item, GetDangerType())
220 .WillRepeatedly(Return(content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS));
221 EXPECT_CALL(*item, GetLastReason())
222 .WillRepeatedly(Return(content::DOWNLOAD_INTERRUPT_REASON_NONE));
223 EXPECT_CALL(*item, GetReceivedBytes()).WillRepeatedly(Return(0));
224 EXPECT_CALL(*item, GetTotalBytes()).WillRepeatedly(Return(0));
225 EXPECT_CALL(*item, GetTargetDisposition()).WillRepeatedly(
226 Return(content::DownloadItem::TARGET_DISPOSITION_OVERWRITE));
227 EXPECT_CALL(*item, GetOpened()).WillRepeatedly(Return(false));
228 EXPECT_CALL(*item, GetMimeType()).WillRepeatedly(Return(std::string()));
229 EXPECT_CALL(*item, GetURL()).WillRepeatedly(testing::ReturnRefOfCopy(GURL()));
230 EXPECT_CALL(*item, IsTemporary()).WillRepeatedly(Return(false));
231 return item.Pass();
88 } 232 }
89 233
90 scoped_ptr<DownloadUIController::Delegate> 234 scoped_ptr<DownloadUIController::Delegate>
91 DownloadUIControllerTest::GetTestDelegate() { 235 DownloadUIControllerTest::GetTestDelegate() {
92 scoped_ptr<DownloadUIController::Delegate> delegate( 236 scoped_ptr<DownloadUIController::Delegate> delegate(
93 new TestDelegate(receiver_factory_.GetWeakPtr())); 237 new TestDelegate(notified_item_receiver_factory_.GetWeakPtr()));
94 return delegate.Pass(); 238 return delegate.Pass();
95 } 239 }
96 240
97 // Normal downloads that are constructed in the IN_PROGRESS state should be 241 // New downloads should be presented to the UI when GetTargetFilePath() returns
98 // presented to the UI when GetTargetFilePath() returns a non-empty path. 242 // a non-empty path. I.e. once the download target has been determined.
99 // I.e. once the download target has been determined.
100 TEST_F(DownloadUIControllerTest, DownloadUIController_NotifyBasic) { 243 TEST_F(DownloadUIControllerTest, DownloadUIController_NotifyBasic) {
101 scoped_ptr<MockDownloadItem> item(new MockDownloadItem); 244 scoped_ptr<MockDownloadItem> item(CreateMockInProgressDownload());
102 DownloadUIController controller(manager(), GetTestDelegate()); 245 DownloadUIController controller(manager(), GetTestDelegate());
103 EXPECT_CALL(*item, GetTargetFilePath()) 246 EXPECT_CALL(*item, GetTargetFilePath())
104 .WillOnce(ReturnRefOfCopy(base::FilePath())); 247 .WillOnce(ReturnRefOfCopy(base::FilePath()));
105 EXPECT_CALL(*item, GetState())
106 .WillRepeatedly(Return(content::DownloadItem::IN_PROGRESS));
107 248
108 ASSERT_TRUE(manager_observer()); 249 ASSERT_TRUE(manager_observer());
109 manager_observer()->OnDownloadCreated(manager(), item.get()); 250 manager_observer()->OnDownloadCreated(manager(), item.get());
110 251
111 // The destination for the download hasn't been determined yet. It should not 252 // The destination for the download hasn't been determined yet. It should not
112 // be displayed. 253 // be displayed.
113 EXPECT_FALSE(received_item()); 254 EXPECT_FALSE(notified_item());
114 255
115 // Once the destination has been determined, then it should be displayed. 256 // Once the destination has been determined, then it should be displayed.
116 EXPECT_CALL(*item, GetTargetFilePath()) 257 EXPECT_CALL(*item, GetTargetFilePath())
117 .WillOnce(ReturnRefOfCopy(base::FilePath(FILE_PATH_LITERAL("foo")))); 258 .WillOnce(ReturnRefOfCopy(base::FilePath(FILE_PATH_LITERAL("foo"))));
118 item->NotifyObserversDownloadUpdated(); 259 item->NotifyObserversDownloadUpdated();
119 260
120 EXPECT_EQ(static_cast<content::DownloadItem*>(item.get()), received_item()); 261 EXPECT_EQ(static_cast<content::DownloadItem*>(item.get()), notified_item());
262 }
263
264 // A download that's created in an interrupted state should also be displayed.
265 TEST_F(DownloadUIControllerTest, DownloadUIController_NotifyBasic_Interrupted) {
266 scoped_ptr<MockDownloadItem> item = CreateMockInProgressDownload();
267 DownloadUIController controller(manager(), GetTestDelegate());
268 EXPECT_CALL(*item, GetState())
269 .WillRepeatedly(Return(content::DownloadItem::INTERRUPTED));
270
271 ASSERT_TRUE(manager_observer());
272 manager_observer()->OnDownloadCreated(manager(), item.get());
273 EXPECT_EQ(static_cast<content::DownloadItem*>(item.get()), notified_item());
121 } 274 }
122 275
123 // Downloads that have a target path on creation and are in the IN_PROGRESS 276 // Downloads that have a target path on creation and are in the IN_PROGRESS
124 // state should be displayed in the UI immediately without requiring an 277 // state should be displayed in the UI immediately without requiring an
125 // additional OnDownloadUpdated() notification. 278 // additional OnDownloadUpdated() notification.
126 TEST_F(DownloadUIControllerTest, DownloadUIController_NotifyReadyOnCreate) { 279 TEST_F(DownloadUIControllerTest, DownloadUIController_NotifyReadyOnCreate) {
127 scoped_ptr<MockDownloadItem> item(new MockDownloadItem); 280 scoped_ptr<MockDownloadItem> item(CreateMockInProgressDownload());
128 DownloadUIController controller(manager(), GetTestDelegate()); 281 DownloadUIController controller(manager(), GetTestDelegate());
129 EXPECT_CALL(*item, GetTargetFilePath()) 282
130 .WillOnce(ReturnRefOfCopy(base::FilePath(FILE_PATH_LITERAL("foo")))); 283 ASSERT_TRUE(manager_observer());
131 EXPECT_CALL(*item, GetState()) 284 manager_observer()->OnDownloadCreated(manager(), item.get());
132 .WillRepeatedly(Return(content::DownloadItem::IN_PROGRESS)); 285 EXPECT_EQ(static_cast<content::DownloadItem*>(item.get()), notified_item());
133 286 }
134 ASSERT_TRUE(manager_observer()); 287
135 manager_observer()->OnDownloadCreated(manager(), item.get()); 288 // The UI shouldn't be notified of downloads that were restored from history.
136 EXPECT_EQ(static_cast<content::DownloadItem*>(item.get()), received_item()); 289 TEST_F(DownloadUIControllerTest, DownloadUIController_HistoryDownload) {
137 } 290 DownloadUIController controller(manager(), GetTestDelegate());
138 291 // DownloadHistory should already have been created. It performs a query of
139 // History downloads (downloads that are not in IN_PROGRESS on create) should 292 // existing downloads upon creation. We'll use the callback to inject a
140 // not be displayed on the shelf. 293 // history download.
141 TEST_F(DownloadUIControllerTest, DownloadUIController_NoNotifyHistory) { 294 ASSERT_FALSE(history_query_callback().is_null());
142 scoped_ptr<MockDownloadItem> item(new MockDownloadItem); 295
143 DownloadUIController controller(manager(), GetTestDelegate()); 296 // download_history_manager_observer is the DownloadManager::Observer
144 EXPECT_CALL(*item, GetState()) 297 // registered by the DownloadHistory. DownloadHistory relies on the
145 .WillRepeatedly(Return(content::DownloadItem::COMPLETE)); 298 // OnDownloadCreated notification to mark a download as having been restored
146 299 // from history.
147 ASSERT_TRUE(manager_observer()); 300 ASSERT_TRUE(download_history_manager_observer());
148 manager_observer()->OnDownloadCreated(manager(), item.get()); 301
149 EXPECT_FALSE(received_item()); 302 scoped_ptr<std::vector<history::DownloadRow> > history_downloads;
150 303 history_downloads.reset(new std::vector<history::DownloadRow>());
151 item->NotifyObserversDownloadUpdated(); 304 history_downloads->push_back(history::DownloadRow());
152 EXPECT_FALSE(received_item()); 305 history_downloads->front().id = 1;
306
307 std::vector<GURL> url_chain;
308 GURL url;
309 scoped_ptr<MockDownloadItem> item = CreateMockInProgressDownload();
310
311 EXPECT_CALL(*manager(), CheckForHistoryFilesRemoval());
312
313 {
314 testing::InSequence s;
315 testing::MockFunction<void()> mock_function;
316 // DownloadHistory will immediately try to create a download using the info
317 // we push through the query callback. When DownloadManager::CreateDownload
318 // is called, we need to first invoke the OnDownloadCreated callback for
319 // DownloadHistory before returning the DownloadItem since that's the
320 // sequence of events expected by DownloadHistory.
321 base::Closure history_on_created_callback =
322 base::Bind(&content::DownloadManager::Observer::OnDownloadCreated,
323 base::Unretained(download_history_manager_observer()),
324 manager(),
325 item.get());
326 EXPECT_CALL(*manager(), MockCreateDownloadItem(_)).WillOnce(
327 testing::DoAll(testing::InvokeWithoutArgs(&history_on_created_callback,
328 &base::Closure::Run),
329 Return(item.get())));
330 EXPECT_CALL(mock_function, Call());
331
332 history_query_callback().Run(history_downloads.Pass());
333 mock_function.Call();
334 }
335
336 // Now pass along the notification to the OnDownloadCreated observer from
337 // DownloadUIController. It should ignore the download since it's marked as
338 // being restored from history.
339 ASSERT_TRUE(manager_observer());
340 manager_observer()->OnDownloadCreated(manager(), item.get());
341
342 // Finally, the expectation we've been waiting for:
343 EXPECT_FALSE(notified_item());
153 } 344 }
154 345
155 } // namespace 346 } // namespace
OLDNEW
« no previous file with comments | « chrome/browser/download/download_ui_controller.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698