summaryrefslogtreecommitdiff
path: root/chromium/weblayer/browser/download_browsertest.cc
blob: 15918ee90308a2c7d117a51158ca9571c603e7c3 (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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
// Copyright 2020 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 "weblayer/test/weblayer_browser_test.h"

#include "base/files/file_util.h"
#include "base/files/scoped_temp_dir.h"
#include "base/path_service.h"
#include "base/run_loop.h"
#include "base/task/post_task.h"
#include "base/test/bind_test_util.h"
#include "base/threading/thread_restrictions.h"
#include "content/public/browser/download_manager.h"
#include "content/public/browser/web_contents.h"
#include "content/public/test/slow_download_http_response.h"
#include "net/test/embedded_test_server/embedded_test_server.h"
#include "weblayer/browser/browser_context_impl.h"
#include "weblayer/browser/download_manager_delegate_impl.h"
#include "weblayer/browser/profile_impl.h"
#include "weblayer/browser/tab_impl.h"
#include "weblayer/public/download.h"
#include "weblayer/public/download_delegate.h"
#include "weblayer/public/navigation_controller.h"
#include "weblayer/shell/browser/shell.h"
#include "weblayer/test/test_navigation_observer.h"

namespace weblayer {

namespace {

class DownloadBrowserTest : public WebLayerBrowserTest,
                            public DownloadDelegate {
 public:
  DownloadBrowserTest() = default;
  ~DownloadBrowserTest() override = default;

  void SetUpOnMainThread() override {
    embedded_test_server()->RegisterRequestHandler(base::BindRepeating(
        &content::SlowDownloadHttpResponse::HandleSlowDownloadRequest));
    ASSERT_TRUE(embedded_test_server()->Start());

    allow_run_loop_ = std::make_unique<base::RunLoop>();
    started_run_loop_ = std::make_unique<base::RunLoop>();
    intercept_run_loop_ = std::make_unique<base::RunLoop>();
    completed_run_loop_ = std::make_unique<base::RunLoop>();
    failed_run_loop_ = std::make_unique<base::RunLoop>();

    Tab* tab = shell()->tab();
    TabImpl* tab_impl = static_cast<TabImpl*>(tab);

    tab_impl->profile()->SetDownloadDelegate(this);

    auto* browser_context = tab_impl->web_contents()->GetBrowserContext();
    auto* download_manager_delegate =
        content::BrowserContext::GetDownloadManager(browser_context)
            ->GetDelegate();
    static_cast<DownloadManagerDelegateImpl*>(download_manager_delegate)
        ->set_download_dropped_closure_for_testing(base::BindRepeating(
            &DownloadBrowserTest::DownloadDropped, base::Unretained(this)));
  }

  void WaitForAllow() { allow_run_loop_->Run(); }
  void WaitForIntercept() { intercept_run_loop_->Run(); }
  void WaitForStarted() { started_run_loop_->Run(); }
  void WaitForCompleted() { completed_run_loop_->Run(); }
  void WaitForFailed() { failed_run_loop_->Run(); }

  void set_intercept() { intercept_ = true; }
  void set_disallow() { allow_ = false; }
  void set_started_callback(
      base::OnceCallback<void(Download* download)> callback) {
    started_callback_ = std::move(callback);
  }
  void set_failed_callback(
      base::OnceCallback<void(Download* download)> callback) {
    failed_callback_ = std::move(callback);
  }
  bool started() { return started_; }
  base::FilePath download_location() { return download_location_; }
  int64_t total_bytes() { return total_bytes_; }
  DownloadError download_state() { return download_state_; }
  std::string mime_type() { return mime_type_; }
  int completed_count() { return completed_count_; }
  int failed_count() { return failed_count_; }
  int download_dropped_count() { return download_dropped_count_; }

 private:
  // DownloadDelegate implementation:
  void AllowDownload(Tab* tab,
                     const GURL& url,
                     const std::string& request_method,
                     base::Optional<url::Origin> request_initiator,
                     AllowDownloadCallback callback) override {
    std::move(callback).Run(allow_);
    allow_run_loop_->Quit();
  }

  bool InterceptDownload(const GURL& url,
                         const std::string& user_agent,
                         const std::string& content_disposition,
                         const std::string& mime_type,
                         int64_t content_length) override {
    intercept_run_loop_->Quit();
    return intercept_;
  }

  void DownloadStarted(Download* download) override {
    started_ = true;
    started_run_loop_->Quit();

    CHECK_EQ(download->GetState(), DownloadState::kInProgress);

    if (started_callback_)
      std::move(started_callback_).Run(download);
  }

  void DownloadCompleted(Download* download) override {
    completed_count_++;
    download_location_ = download->GetLocation();
    total_bytes_ = download->GetTotalBytes();
    download_state_ = download->GetError();
    mime_type_ = download->GetMimeType();
    CHECK_EQ(download->GetReceivedBytes(), total_bytes_);
    CHECK_EQ(download->GetState(), DownloadState::kComplete);
    completed_run_loop_->Quit();
  }

  void DownloadFailed(Download* download) override {
    failed_count_++;
    download_state_ = download->GetError();
    failed_run_loop_->Quit();

    if (failed_callback_)
      std::move(failed_callback_).Run(download);
  }

  void DownloadDropped() { download_dropped_count_++; }

  bool intercept_ = false;
  bool allow_ = true;
  bool started_ = false;
  base::OnceCallback<void(Download* download)> started_callback_;
  base::OnceCallback<void(Download* download)> failed_callback_;
  base::FilePath download_location_;
  int64_t total_bytes_ = 0;
  DownloadError download_state_ = DownloadError::kNoError;
  std::string mime_type_;
  int completed_count_ = 0;
  int failed_count_ = 0;
  int download_dropped_count_ = 0;
  std::unique_ptr<base::RunLoop> allow_run_loop_;
  std::unique_ptr<base::RunLoop> intercept_run_loop_;
  std::unique_ptr<base::RunLoop> started_run_loop_;
  std::unique_ptr<base::RunLoop> completed_run_loop_;
  std::unique_ptr<base::RunLoop> failed_run_loop_;
};

}  // namespace

// Ensures that if the delegate disallows the downloads then WebLayer
// doesn't download it.
IN_PROC_BROWSER_TEST_F(DownloadBrowserTest, DisallowNoDownload) {
  set_disallow();

  GURL url(embedded_test_server()->GetURL("/content-disposition.html"));

  // Downloads always count as failed navigations.
  TestNavigationObserver observer(
      url, TestNavigationObserver::NavigationEvent::kFailure, shell());
  shell()->tab()->GetNavigationController()->Navigate(url);
  observer.Wait();

  WaitForAllow();

  EXPECT_FALSE(started());
  EXPECT_EQ(completed_count(), 0);
  EXPECT_EQ(failed_count(), 0);
  EXPECT_EQ(download_dropped_count(), 1);
}

// Ensures that if the delegate chooses to intercept downloads then WebLayer
// doesn't download it.
IN_PROC_BROWSER_TEST_F(DownloadBrowserTest, InterceptNoDownload) {
  set_intercept();

  GURL url(embedded_test_server()->GetURL("/content-disposition.html"));

  shell()->tab()->GetNavigationController()->Navigate(url);

  WaitForIntercept();

  EXPECT_FALSE(started());
  EXPECT_EQ(completed_count(), 0);
  EXPECT_EQ(failed_count(), 0);
  EXPECT_EQ(download_dropped_count(), 1);
}

IN_PROC_BROWSER_TEST_F(DownloadBrowserTest, Basic) {
  GURL url(embedded_test_server()->GetURL("/content-disposition.html"));

  shell()->tab()->GetNavigationController()->Navigate(url);

  WaitForCompleted();

  EXPECT_TRUE(started());
  EXPECT_EQ(completed_count(), 1);
  EXPECT_EQ(failed_count(), 0);
  EXPECT_EQ(download_dropped_count(), 0);
  EXPECT_EQ(download_state(), DownloadError::kNoError);
  EXPECT_EQ(mime_type(), "text/html");

  // Check that the size on disk matches what's expected.
  {
    base::ScopedAllowBlockingForTesting allow_blocking;
    int64_t downloaded_file_size, original_file_size;
    EXPECT_TRUE(base::GetFileSize(download_location(), &downloaded_file_size));
    base::FilePath test_data_dir;
    CHECK(base::PathService::Get(base::DIR_SOURCE_ROOT, &test_data_dir));
    EXPECT_TRUE(base::GetFileSize(
        test_data_dir.Append(base::FilePath(
            FILE_PATH_LITERAL("weblayer/test/data/content-disposition.html"))),
        &original_file_size));
    EXPECT_EQ(downloaded_file_size, total_bytes());
  }

  // Ensure browser tests don't write to the default machine download directory
  // to avoid filing it up.
  EXPECT_NE(BrowserContextImpl::GetDefaultDownloadDirectory(),
            download_location().DirName());
}

IN_PROC_BROWSER_TEST_F(DownloadBrowserTest, OverrideDownloadDirectory) {
  base::ScopedAllowBlockingForTesting allow_blocking;
  base::ScopedTempDir download_dir;
  ASSERT_TRUE(download_dir.CreateUniqueTempDir());

  TabImpl* tab_impl = static_cast<TabImpl*>(shell()->tab());
  auto* browser_context = tab_impl->web_contents()->GetBrowserContext();
  auto* browser_context_impl =
      static_cast<BrowserContextImpl*>(browser_context);
  browser_context_impl->profile_impl()->SetDownloadDirectory(
      download_dir.GetPath());

  GURL url(embedded_test_server()->GetURL("/content-disposition.html"));

  shell()->tab()->GetNavigationController()->Navigate(url);

  WaitForCompleted();

  EXPECT_EQ(completed_count(), 1);
  EXPECT_EQ(failed_count(), 0);
  EXPECT_EQ(download_dir.GetPath(), download_location().DirName());
}

IN_PROC_BROWSER_TEST_F(DownloadBrowserTest, Cancel) {
  set_started_callback(base::BindLambdaForTesting([&](Download* download) {
    download->Cancel();

    // Also allow the download to complete.
    GURL url = embedded_test_server()->GetURL(
        content::SlowDownloadHttpResponse::kFinishSlowResponseUrl);
    shell()->tab()->GetNavigationController()->Navigate(url);
  }));

  set_failed_callback(base::BindLambdaForTesting([](Download* download) {
    CHECK_EQ(download->GetState(), DownloadState::kCancelled);
  }));

  // Create a request that doesn't complete right away to avoid flakiness.
  GURL url(embedded_test_server()->GetURL(
      content::SlowDownloadHttpResponse::kKnownSizeUrl));

  shell()->tab()->GetNavigationController()->Navigate(url);

  WaitForFailed();
  EXPECT_EQ(completed_count(), 0);
  EXPECT_EQ(failed_count(), 1);
  EXPECT_EQ(download_dropped_count(), 0);
  EXPECT_EQ(download_state(), DownloadError::kCancelled);
}

IN_PROC_BROWSER_TEST_F(DownloadBrowserTest, PauseResume) {
  set_started_callback(base::BindLambdaForTesting([&](Download* download) {
    download->Pause();
    GURL url = embedded_test_server()->GetURL(
        content::SlowDownloadHttpResponse::kFinishSlowResponseUrl);
    base::SequencedTaskRunnerHandle::Get()->PostTask(
        FROM_HERE, base::BindOnce(
                       [](Download* download, Shell* shell, const GURL& url) {
                         CHECK_EQ(download->GetState(), DownloadState::kPaused);
                         download->Resume();

                         // Also allow the download to complete.
                         shell->tab()->GetNavigationController()->Navigate(url);
                       },
                       download, shell(), url));
  }));

  // Create a request that doesn't complete right away to avoid flakiness.
  GURL url(embedded_test_server()->GetURL(
      content::SlowDownloadHttpResponse::kKnownSizeUrl));
  shell()->tab()->GetNavigationController()->Navigate(url);

  WaitForCompleted();
  EXPECT_EQ(completed_count(), 1);
  EXPECT_EQ(failed_count(), 0);
  EXPECT_EQ(download_dropped_count(), 0);
}

IN_PROC_BROWSER_TEST_F(DownloadBrowserTest, NetworkError) {
  set_failed_callback(base::BindLambdaForTesting([](Download* download) {
    CHECK_EQ(download->GetState(), DownloadState::kFailed);
  }));

  // Create a request that doesn't complete right away.
  GURL url(embedded_test_server()->GetURL(
      content::SlowDownloadHttpResponse::kKnownSizeUrl));

  shell()->tab()->GetNavigationController()->Navigate(url);

  WaitForStarted();
  EXPECT_TRUE(embedded_test_server()->ShutdownAndWaitUntilComplete());

  WaitForFailed();
  EXPECT_EQ(completed_count(), 0);
  EXPECT_EQ(failed_count(), 1);
  EXPECT_EQ(download_dropped_count(), 0);
  EXPECT_EQ(download_state(), DownloadError::kConnectivityError);
}

IN_PROC_BROWSER_TEST_F(DownloadBrowserTest, PendingOnExist) {
  // Create a request that doesn't complete right away.
  GURL url(embedded_test_server()->GetURL(
      content::SlowDownloadHttpResponse::kKnownSizeUrl));

  shell()->tab()->GetNavigationController()->Navigate(url);

  WaitForStarted();

  // If this test crashes later then there'd be a regression.
}

}  // namespace weblayer