Adding instrumentation to locate the source of jankiness
[chromium-blink-merge.git] / chrome / browser / download / download_ui_controller_unittest.cc
blobd540ca8da6b26ffebee121054b9a596a4630ffc9
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
3 // found in the LICENSE file.
5 #include "base/bind.h"
6 #include "base/callback.h"
7 #include "base/files/file_path.h"
8 #include "base/memory/ref_counted.h"
9 #include "base/memory/scoped_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"
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"
19 #include "content/public/test/mock_download_item.h"
20 #include "content/public/test/mock_download_manager.h"
21 #include "testing/gmock/include/gmock/gmock.h"
22 #include "testing/gtest/include/gtest/gtest.h"
24 using content::MockDownloadItem;
25 using content::MockDownloadManager;
26 using testing::AnyNumber;
27 using testing::Assign;
28 using testing::Return;
29 using testing::ReturnRefOfCopy;
30 using testing::SaveArg;
31 using testing::_;
33 namespace {
35 // A DownloadUIController::Delegate that stores the DownloadItem* for the last
36 // download that was sent to the UI.
37 class TestDelegate : public DownloadUIController::Delegate {
38 public:
39 explicit TestDelegate(base::WeakPtr<content::DownloadItem*> receiver);
40 virtual ~TestDelegate() {}
42 private:
43 virtual void OnNewDownloadReady(content::DownloadItem* item) override;
45 base::WeakPtr<content::DownloadItem*> receiver_;
48 TestDelegate::TestDelegate(base::WeakPtr<content::DownloadItem*> receiver)
49 : receiver_(receiver) {
52 void TestDelegate::OnNewDownloadReady(content::DownloadItem* item) {
53 if (receiver_.get())
54 *receiver_ = item;
57 // A DownloadService that returns a custom DownloadHistory.
58 class TestDownloadService : public DownloadService {
59 public:
60 explicit TestDownloadService(Profile* profile);
61 virtual ~TestDownloadService();
63 void set_download_history(scoped_ptr<DownloadHistory> download_history) {
64 download_history_.swap(download_history);
66 virtual DownloadHistory* GetDownloadHistory() override;
68 private:
69 scoped_ptr<DownloadHistory> download_history_;
72 TestDownloadService::TestDownloadService(Profile* profile)
73 : DownloadService(profile) {
76 TestDownloadService::~TestDownloadService() {
79 DownloadHistory* TestDownloadService::GetDownloadHistory() {
80 return download_history_.get();
83 // The test fixture:
84 class DownloadUIControllerTest : public ChromeRenderViewHostTestHarness {
85 public:
86 DownloadUIControllerTest();
88 protected:
89 // testing::Test
90 virtual void SetUp() override;
92 // Returns a TestDelegate. Invoking OnNewDownloadReady on the returned
93 // delegate results in the DownloadItem* being stored in |notified_item_|.
94 scoped_ptr<DownloadUIController::Delegate> GetTestDelegate();
96 MockDownloadManager* manager() { return manager_.get(); }
98 // Returns the DownloadManager::Observer registered by a test case. This is
99 // the DownloadUIController's observer for all current test cases.
100 content::DownloadManager::Observer* manager_observer() {
101 return manager_observer_;
104 // The most recent DownloadItem that was passed into OnNewDownloadReady().
105 content::DownloadItem* notified_item() { return notified_item_; }
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_;
114 // DownloadManager::Observer registered by DownloadHistory.
115 content::DownloadManager::Observer* download_history_manager_observer() {
116 return download_history_manager_observer_;
119 scoped_ptr<MockDownloadItem> CreateMockInProgressDownload();
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_;
129 private:
130 virtual void QueryDownloads(
131 const HistoryService::DownloadQueryCallback& callback) override {
132 download_query_callback_ = callback;
136 // Constructs and returns a TestDownloadService.
137 static KeyedService* TestingDownloadServiceFactory(
138 content::BrowserContext* browser_context);
140 scoped_ptr<MockDownloadManager> manager_;
141 content::DownloadManager::Observer* download_history_manager_observer_;
142 content::DownloadManager::Observer* manager_observer_;
143 content::DownloadItem* notified_item_;
144 base::WeakPtrFactory<content::DownloadItem*> notified_item_receiver_factory_;
146 HistoryAdapter* history_adapter_;
149 // static
150 KeyedService* DownloadUIControllerTest::TestingDownloadServiceFactory(
151 content::BrowserContext* browser_context) {
152 return new TestDownloadService(Profile::FromBrowserContext(browser_context));
155 DownloadUIControllerTest::DownloadUIControllerTest()
156 : download_history_manager_observer_(NULL),
157 manager_observer_(NULL),
158 notified_item_(NULL),
159 notified_item_receiver_factory_(&notified_item_) {
162 void DownloadUIControllerTest::SetUp() {
163 ChromeRenderViewHostTestHarness::SetUp();
165 manager_.reset(new testing::StrictMock<MockDownloadManager>());
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());
176 scoped_ptr<HistoryAdapter> history_adapter(new HistoryAdapter);
177 history_adapter_ = history_adapter.get();
178 scoped_ptr<DownloadHistory> download_history(
179 new DownloadHistory(manager_.get(), history_adapter.Pass()));
180 ASSERT_TRUE(download_history_manager_observer_);
182 EXPECT_CALL(*manager_, AddObserver(_))
183 .WillOnce(SaveArg<0>(&manager_observer_));
184 EXPECT_CALL(*manager_,
185 RemoveObserver(testing::Eq(testing::ByRef(manager_observer_))))
186 .WillOnce(testing::Assign(
187 &manager_observer_,
188 static_cast<content::DownloadManager::Observer*>(NULL)));
189 TestDownloadService* download_service = static_cast<TestDownloadService*>(
190 DownloadServiceFactory::GetInstance()->SetTestingFactoryAndUse(
191 browser_context(), &TestingDownloadServiceFactory));
192 ASSERT_TRUE(download_service);
193 download_service->set_download_history(download_history.Pass());
196 scoped_ptr<MockDownloadItem>
197 DownloadUIControllerTest::CreateMockInProgressDownload() {
198 scoped_ptr<MockDownloadItem> item(
199 new testing::StrictMock<MockDownloadItem>());
200 EXPECT_CALL(*item, GetBrowserContext())
201 .WillRepeatedly(Return(browser_context()));
202 EXPECT_CALL(*item, GetId()).WillRepeatedly(Return(1));
203 EXPECT_CALL(*item, GetTargetFilePath()).WillRepeatedly(
204 ReturnRefOfCopy(base::FilePath(FILE_PATH_LITERAL("foo"))));
205 EXPECT_CALL(*item, GetFullPath()).WillRepeatedly(
206 ReturnRefOfCopy(base::FilePath(FILE_PATH_LITERAL("foo"))));
207 EXPECT_CALL(*item, GetState())
208 .WillRepeatedly(Return(content::DownloadItem::IN_PROGRESS));
209 EXPECT_CALL(*item, GetUrlChain())
210 .WillRepeatedly(testing::ReturnRefOfCopy(std::vector<GURL>()));
211 EXPECT_CALL(*item, GetReferrerUrl())
212 .WillRepeatedly(testing::ReturnRefOfCopy(GURL()));
213 EXPECT_CALL(*item, GetStartTime()).WillRepeatedly(Return(base::Time()));
214 EXPECT_CALL(*item, GetEndTime()).WillRepeatedly(Return(base::Time()));
215 EXPECT_CALL(*item, GetETag()).WillRepeatedly(ReturnRefOfCopy(std::string()));
216 EXPECT_CALL(*item, GetLastModifiedTime())
217 .WillRepeatedly(ReturnRefOfCopy(std::string()));
218 EXPECT_CALL(*item, GetDangerType())
219 .WillRepeatedly(Return(content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS));
220 EXPECT_CALL(*item, GetLastReason())
221 .WillRepeatedly(Return(content::DOWNLOAD_INTERRUPT_REASON_NONE));
222 EXPECT_CALL(*item, GetReceivedBytes()).WillRepeatedly(Return(0));
223 EXPECT_CALL(*item, GetTotalBytes()).WillRepeatedly(Return(0));
224 EXPECT_CALL(*item, GetTargetDisposition()).WillRepeatedly(
225 Return(content::DownloadItem::TARGET_DISPOSITION_OVERWRITE));
226 EXPECT_CALL(*item, GetOpened()).WillRepeatedly(Return(false));
227 EXPECT_CALL(*item, GetMimeType()).WillRepeatedly(Return(std::string()));
228 EXPECT_CALL(*item, GetURL()).WillRepeatedly(testing::ReturnRefOfCopy(GURL()));
229 EXPECT_CALL(*item, IsTemporary()).WillRepeatedly(Return(false));
230 return item.Pass();
233 scoped_ptr<DownloadUIController::Delegate>
234 DownloadUIControllerTest::GetTestDelegate() {
235 scoped_ptr<DownloadUIController::Delegate> delegate(
236 new TestDelegate(notified_item_receiver_factory_.GetWeakPtr()));
237 return delegate.Pass();
240 // New downloads should be presented to the UI when GetTargetFilePath() returns
241 // a non-empty path. I.e. once the download target has been determined.
242 TEST_F(DownloadUIControllerTest, DownloadUIController_NotifyBasic) {
243 scoped_ptr<MockDownloadItem> item(CreateMockInProgressDownload());
244 DownloadUIController controller(manager(), GetTestDelegate());
245 EXPECT_CALL(*item, GetTargetFilePath())
246 .WillOnce(ReturnRefOfCopy(base::FilePath()));
248 ASSERT_TRUE(manager_observer());
249 manager_observer()->OnDownloadCreated(manager(), item.get());
251 // The destination for the download hasn't been determined yet. It should not
252 // be displayed.
253 EXPECT_FALSE(notified_item());
255 // Once the destination has been determined, then it should be displayed.
256 EXPECT_CALL(*item, GetTargetFilePath())
257 .WillOnce(ReturnRefOfCopy(base::FilePath(FILE_PATH_LITERAL("foo"))));
258 item->NotifyObserversDownloadUpdated();
260 EXPECT_EQ(static_cast<content::DownloadItem*>(item.get()), notified_item());
263 // A download that's created in an interrupted state should also be displayed.
264 TEST_F(DownloadUIControllerTest, DownloadUIController_NotifyBasic_Interrupted) {
265 scoped_ptr<MockDownloadItem> item = CreateMockInProgressDownload();
266 DownloadUIController controller(manager(), GetTestDelegate());
267 EXPECT_CALL(*item, GetState())
268 .WillRepeatedly(Return(content::DownloadItem::INTERRUPTED));
270 ASSERT_TRUE(manager_observer());
271 manager_observer()->OnDownloadCreated(manager(), item.get());
272 EXPECT_EQ(static_cast<content::DownloadItem*>(item.get()), notified_item());
275 // Downloads that have a target path on creation and are in the IN_PROGRESS
276 // state should be displayed in the UI immediately without requiring an
277 // additional OnDownloadUpdated() notification.
278 TEST_F(DownloadUIControllerTest, DownloadUIController_NotifyReadyOnCreate) {
279 scoped_ptr<MockDownloadItem> item(CreateMockInProgressDownload());
280 DownloadUIController controller(manager(), GetTestDelegate());
282 ASSERT_TRUE(manager_observer());
283 manager_observer()->OnDownloadCreated(manager(), item.get());
284 EXPECT_EQ(static_cast<content::DownloadItem*>(item.get()), notified_item());
287 // The UI shouldn't be notified of downloads that were restored from history.
288 TEST_F(DownloadUIControllerTest, DownloadUIController_HistoryDownload) {
289 DownloadUIController controller(manager(), GetTestDelegate());
290 // DownloadHistory should already have been created. It performs a query of
291 // existing downloads upon creation. We'll use the callback to inject a
292 // history download.
293 ASSERT_FALSE(history_query_callback().is_null());
295 // download_history_manager_observer is the DownloadManager::Observer
296 // registered by the DownloadHistory. DownloadHistory relies on the
297 // OnDownloadCreated notification to mark a download as having been restored
298 // from history.
299 ASSERT_TRUE(download_history_manager_observer());
301 scoped_ptr<std::vector<history::DownloadRow> > history_downloads;
302 history_downloads.reset(new std::vector<history::DownloadRow>());
303 history_downloads->push_back(history::DownloadRow());
304 history_downloads->front().id = 1;
306 std::vector<GURL> url_chain;
307 GURL url;
308 scoped_ptr<MockDownloadItem> item = CreateMockInProgressDownload();
310 EXPECT_CALL(*item, GetOriginalMimeType());
311 EXPECT_CALL(*manager(), CheckForHistoryFilesRemoval());
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());
332 history_query_callback().Run(history_downloads.Pass());
333 mock_function.Call();
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());
342 // Finally, the expectation we've been waiting for:
343 EXPECT_FALSE(notified_item());
346 } // namespace