OLD | NEW |
---|---|
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "chrome/browser/android/download/download_manager_service.h" | 5 #include "chrome/browser/android/download/download_manager_service.h" |
6 | 6 |
7 #include "base/android/jni_android.h" | 7 #include "base/android/jni_android.h" |
8 #include "base/android/jni_string.h" | 8 #include "base/android/jni_string.h" |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/macros.h" | 10 #include "base/macros.h" |
11 #include "base/message_loop/message_loop.h" | 11 #include "base/message_loop/message_loop.h" |
12 #include "content/public/browser/download_item.h" | 12 #include "content/public/browser/download_item.h" |
13 #include "content/public/browser/download_manager.h" | 13 #include "content/public/browser/download_manager.h" |
14 #include "content/public/browser/download_url_parameters.h" | 14 #include "content/public/browser/download_url_parameters.h" |
15 #include "content/public/test/mock_download_item.h" | |
16 #include "content/public/test/mock_download_manager.h" | |
15 #include "testing/gmock/include/gmock/gmock.h" | 17 #include "testing/gmock/include/gmock/gmock.h" |
16 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
17 #include "url/origin.h" | 19 #include "url/origin.h" |
18 | 20 |
19 using ::testing::_; | 21 using ::testing::_; |
20 | 22 |
21 namespace content { | 23 namespace content { |
22 class BrowserContext; | 24 class BrowserContext; |
23 class ByteStreamReader; | 25 class ByteStreamReader; |
24 class DownloadManagerDelegate; | 26 class DownloadManagerDelegate; |
25 struct DownloadCreateInfo; | 27 struct DownloadCreateInfo; |
26 } | 28 } |
27 | 29 |
28 // Mock implementation of content::DownloadItem. | |
29 class MockDownloadItem : public content::DownloadItem { | |
30 public: | |
31 explicit MockDownloadItem(bool can_resume) : can_resume_(can_resume) {} | |
32 ~MockDownloadItem() override {} | |
33 bool CanResume() const override { return can_resume_; } | |
34 | |
35 MOCK_METHOD1(AddObserver, void(content::DownloadItem::Observer*)); | |
36 MOCK_METHOD1(RemoveObserver, void(content::DownloadItem::Observer*)); | |
37 MOCK_METHOD0(UpdateObservers, void()); | |
38 MOCK_METHOD0(ValidateDangerousDownload, void()); | |
39 MOCK_METHOD1(StealDangerousDownload, | |
40 void(const content::DownloadItem::AcquireFileCallback&)); | |
41 MOCK_METHOD0(Pause, void()); | |
42 MOCK_METHOD0(Resume, void()); | |
43 MOCK_METHOD1(Cancel, void(bool)); | |
44 MOCK_METHOD0(Remove, void()); | |
45 MOCK_METHOD0(OpenDownload, void()); | |
46 MOCK_METHOD0(ShowDownloadInShell, void()); | |
47 MOCK_CONST_METHOD0(GetId, uint32_t()); | |
48 MOCK_CONST_METHOD0(GetState, content::DownloadItem::DownloadState()); | |
49 MOCK_CONST_METHOD0(GetLastReason, content::DownloadInterruptReason()); | |
50 MOCK_CONST_METHOD0(IsPaused, bool()); | |
51 MOCK_CONST_METHOD0(IsTemporary, bool()); | |
52 MOCK_CONST_METHOD0(IsDone, bool()); | |
53 MOCK_CONST_METHOD0(GetURL, const GURL&()); | |
54 MOCK_CONST_METHOD0(GetUrlChain, std::vector<GURL>&()); | |
55 MOCK_CONST_METHOD0(GetOriginalUrl, const GURL&()); | |
56 MOCK_CONST_METHOD0(GetReferrerUrl, const GURL&()); | |
57 MOCK_CONST_METHOD0(GetTabUrl, const GURL&()); | |
58 MOCK_CONST_METHOD0(GetTabReferrerUrl, const GURL&()); | |
59 MOCK_CONST_METHOD0(GetSuggestedFilename, std::string()); | |
60 MOCK_CONST_METHOD0(GetContentDisposition, std::string()); | |
61 MOCK_CONST_METHOD0(GetMimeType, std::string()); | |
62 MOCK_CONST_METHOD0(GetOriginalMimeType, std::string()); | |
63 MOCK_CONST_METHOD0(GetRemoteAddress, std::string()); | |
64 MOCK_CONST_METHOD0(HasUserGesture, bool()); | |
65 MOCK_CONST_METHOD0(GetTransitionType, ui::PageTransition()); | |
66 MOCK_CONST_METHOD0(GetLastModifiedTime, const std::string&()); | |
67 MOCK_CONST_METHOD0(GetETag, const std::string&()); | |
68 MOCK_CONST_METHOD0(IsSavePackageDownload, bool()); | |
69 MOCK_CONST_METHOD0(GetFullPath, const base::FilePath&()); | |
70 MOCK_CONST_METHOD0(GetTargetFilePath, const base::FilePath&()); | |
71 MOCK_CONST_METHOD0(GetForcedFilePath, const base::FilePath&()); | |
72 MOCK_CONST_METHOD0(GetFileNameToReportUser, base::FilePath()); | |
73 MOCK_CONST_METHOD0(GetTargetDisposition, | |
74 content::DownloadItem::TargetDisposition()); | |
75 MOCK_CONST_METHOD0(GetHash, const std::string&()); | |
76 MOCK_CONST_METHOD0(GetHashState, const std::string&()); | |
77 MOCK_CONST_METHOD0(GetFileExternallyRemoved, bool()); | |
78 MOCK_METHOD1(DeleteFile, void(const base::Callback<void(bool)>&)); | |
79 MOCK_CONST_METHOD0(IsDangerous, bool()); | |
80 MOCK_CONST_METHOD0(GetDangerType, content::DownloadDangerType()); | |
81 MOCK_CONST_METHOD1(TimeRemaining, bool(base::TimeDelta*)); | |
82 MOCK_CONST_METHOD0(CurrentSpeed, int64_t()); | |
83 MOCK_CONST_METHOD0(PercentComplete, int()); | |
84 MOCK_CONST_METHOD0(AllDataSaved, bool()); | |
85 MOCK_CONST_METHOD0(GetTotalBytes, int64_t()); | |
86 MOCK_CONST_METHOD0(GetReceivedBytes, int64_t()); | |
87 MOCK_CONST_METHOD0(GetStartTime, base::Time()); | |
88 MOCK_CONST_METHOD0(GetEndTime, base::Time()); | |
89 MOCK_METHOD0(CanShowInFolder, bool()); | |
90 MOCK_METHOD0(CanOpenDownload, bool()); | |
91 MOCK_METHOD0(ShouldOpenFileBasedOnExtension, bool()); | |
92 MOCK_CONST_METHOD0(GetOpenWhenComplete, bool()); | |
93 MOCK_METHOD0(GetAutoOpened, bool()); | |
94 MOCK_CONST_METHOD0(GetOpened, bool()); | |
95 MOCK_CONST_METHOD0(GetBrowserContext, content::BrowserContext*()); | |
96 MOCK_CONST_METHOD0(GetWebContents, content::WebContents*()); | |
97 MOCK_METHOD1(OnContentCheckCompleted, void(content::DownloadDangerType)); | |
98 MOCK_METHOD1(SetOpenWhenComplete, void(bool)); | |
99 MOCK_METHOD1(SetIsTemporary, void(bool)); | |
100 MOCK_METHOD1(SetOpened, void(bool)); | |
101 MOCK_METHOD1(SetDisplayName, void(const base::FilePath&)); | |
102 MOCK_CONST_METHOD1(DebugString, std::string(bool)); | |
103 | |
104 private: | |
105 bool can_resume_; | |
106 }; | |
107 | |
108 // Mock implementation of content::DownloadManager. | |
109 class MockDownloadManager : public content::DownloadManager { | |
110 public: | |
111 MockDownloadManager() {} | |
112 ~MockDownloadManager() override {} | |
113 | |
114 MOCK_METHOD1(SetDelegate, void(content::DownloadManagerDelegate*)); | |
115 MOCK_CONST_METHOD0(GetDelegate, content::DownloadManagerDelegate*()); | |
116 MOCK_METHOD0(Shutdown, void()); | |
117 MOCK_METHOD1(GetAllDownloads, void(DownloadVector*)); | |
118 MOCK_METHOD3(RemoveDownloadsByURLAndTime, | |
119 int(const base::Callback<bool(const GURL&)>& url_filter, | |
120 base::Time, | |
121 base::Time)); | |
122 MOCK_METHOD2(RemoveDownloadsBetween, int(base::Time, base::Time)); | |
123 MOCK_METHOD1(RemoveDownloads, int(base::Time)); | |
124 MOCK_METHOD0(RemoveAllDownloads, int()); | |
125 void DownloadUrl(scoped_ptr<content::DownloadUrlParameters>) override {} | |
126 MOCK_METHOD1(AddObserver, void(content::DownloadManager::Observer*)); | |
127 MOCK_METHOD1(RemoveObserver, void(content::DownloadManager::Observer*)); | |
128 MOCK_CONST_METHOD0(InProgressCount, int()); | |
129 MOCK_CONST_METHOD0(NonMaliciousInProgressCount, int()); | |
130 MOCK_CONST_METHOD0(GetBrowserContext, content::BrowserContext*()); | |
131 MOCK_METHOD0(CheckForHistoryFilesRemoval, void()); | |
132 void StartDownload( | |
133 scoped_ptr<content::DownloadCreateInfo>, | |
134 scoped_ptr<content::ByteStreamReader>, | |
135 const content::DownloadUrlParameters::OnStartedCallback&) override {} | |
136 content::DownloadItem* CreateDownloadItem( | |
137 uint32_t id, | |
138 const base::FilePath& current_path, | |
139 const base::FilePath& target_path, | |
140 const std::vector<GURL>& url_chain, | |
141 const GURL& referrer_url, | |
142 const std::string& mime_type, | |
143 const std::string& original_mime_type, | |
144 const base::Time& start_time, | |
145 const base::Time& end_time, | |
146 const std::string& etag, | |
147 const std::string& last_modified, | |
148 int64_t received_bytes, | |
149 int64_t total_bytes, | |
150 content::DownloadItem::DownloadState state, | |
151 content::DownloadDangerType danger_type, | |
152 content::DownloadInterruptReason interrupt_reason, | |
153 bool opened) override { | |
154 return nullptr; | |
155 } | |
156 content::DownloadItem* GetDownload(uint32_t id) override { | |
157 return download_item_.get(); | |
158 } | |
159 void SetDownload(MockDownloadItem* item) { download_item_.reset(item); } | |
160 | |
161 private: | |
162 scoped_ptr<MockDownloadItem> download_item_; | |
163 }; | |
164 | |
165 class DownloadManagerServiceTest : public testing::Test { | 30 class DownloadManagerServiceTest : public testing::Test { |
166 public: | 31 public: |
167 DownloadManagerServiceTest() | 32 DownloadManagerServiceTest() |
168 : service_( | 33 : service_( |
169 new DownloadManagerService(base::android::AttachCurrentThread(), | 34 new DownloadManagerService(base::android::AttachCurrentThread(), |
170 nullptr, | 35 nullptr, |
171 &manager_)), | 36 &manager_)), |
172 finished_(false), | 37 finished_(false), |
173 success_(false) {} | 38 success_(false) { |
39 ON_CALL(manager_, GetDownload(_)) | |
sky
2016/03/11 20:49:16
UGH! gmock, UGH!
asanka
2016/03/14 19:18:33
Should've warned about the accidental gmock exposu
| |
40 .WillByDefault( | |
41 ::testing::Invoke(this, &DownloadManagerServiceTest::GetDownload)); | |
42 } | |
174 | 43 |
175 void OnResumptionDone(bool success) { | 44 void OnResumptionDone(bool success) { |
176 finished_ = true; | 45 finished_ = true; |
177 success_ = success; | 46 success_ = success; |
178 } | 47 } |
179 | 48 |
180 void StartDownload(int download_id) { | 49 void StartDownload(int download_id) { |
181 JNIEnv* env = base::android::AttachCurrentThread(); | 50 JNIEnv* env = base::android::AttachCurrentThread(); |
182 service_->set_resume_callback_for_testing(base::Bind( | 51 service_->set_resume_callback_for_testing(base::Bind( |
183 &DownloadManagerServiceTest::OnResumptionDone, base::Unretained(this))); | 52 &DownloadManagerServiceTest::OnResumptionDone, base::Unretained(this))); |
184 service_->ResumeDownload( | 53 service_->ResumeDownload( |
185 env, nullptr, download_id, | 54 env, nullptr, download_id, |
186 base::android::ConvertUTF8ToJavaString(env, "test").obj()); | 55 base::android::ConvertUTF8ToJavaString(env, "test").obj()); |
187 while (!finished_) | 56 while (!finished_) |
188 message_loop_.RunUntilIdle(); | 57 message_loop_.RunUntilIdle(); |
189 } | 58 } |
190 | 59 |
191 void CreateDownloadItem(bool can_resume) { | 60 void CreateDownloadItem(bool can_resume) { |
192 manager_.SetDownload(new MockDownloadItem(can_resume)); | 61 download_.reset(new content::MockDownloadItem()); |
62 ON_CALL(*download_, CanResume()) | |
63 .WillByDefault(::testing::Return(can_resume)); | |
193 } | 64 } |
194 | 65 |
195 protected: | 66 protected: |
67 content::DownloadItem* GetDownload(uint32_t) { return download_.get(); } | |
68 | |
196 base::MessageLoop message_loop_; | 69 base::MessageLoop message_loop_; |
197 MockDownloadManager manager_; | 70 scoped_ptr<content::MockDownloadItem> download_; |
71 content::MockDownloadManager manager_; | |
198 DownloadManagerService* service_; | 72 DownloadManagerService* service_; |
199 bool finished_; | 73 bool finished_; |
200 bool success_; | 74 bool success_; |
201 | 75 |
202 DISALLOW_COPY_AND_ASSIGN(DownloadManagerServiceTest); | 76 DISALLOW_COPY_AND_ASSIGN(DownloadManagerServiceTest); |
203 }; | 77 }; |
204 | 78 |
205 // Test that resumption will fail if no download item is found before times out. | 79 // Test that resumption will fail if no download item is found before times out. |
206 TEST_F(DownloadManagerServiceTest, ResumptionTimeOut) { | 80 TEST_F(DownloadManagerServiceTest, ResumptionTimeOut) { |
207 StartDownload(1); | 81 StartDownload(1); |
208 EXPECT_FALSE(success_); | 82 EXPECT_FALSE(success_); |
209 } | 83 } |
210 | 84 |
211 // Test that resumption succeeds if the download item is found and can be | 85 // Test that resumption succeeds if the download item is found and can be |
212 // resumed. | 86 // resumed. |
213 TEST_F(DownloadManagerServiceTest, ResumptionWithResumableItem) { | 87 TEST_F(DownloadManagerServiceTest, ResumptionWithResumableItem) { |
214 CreateDownloadItem(true); | 88 CreateDownloadItem(true); |
215 StartDownload(1); | 89 StartDownload(1); |
216 EXPECT_TRUE(success_); | 90 EXPECT_TRUE(success_); |
217 } | 91 } |
218 | 92 |
219 // Test that resumption fails if the target download item is not resumable. | 93 // Test that resumption fails if the target download item is not resumable. |
220 TEST_F(DownloadManagerServiceTest, ResumptionWithNonResumableItem) { | 94 TEST_F(DownloadManagerServiceTest, ResumptionWithNonResumableItem) { |
221 CreateDownloadItem(false); | 95 CreateDownloadItem(false); |
222 StartDownload(1); | 96 StartDownload(1); |
223 EXPECT_FALSE(success_); | 97 EXPECT_FALSE(success_); |
224 } | 98 } |
OLD | NEW |