Apply _RELATIVE relocations ahead of others.
[chromium-blink-merge.git] / content / browser / download / drag_download_file_browsertest.cc
blob0b1de7d6fcd510c0eeb49215050015d93c22d244
1 // Copyright (c) 2012 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/files/file_path.h"
6 #include "base/files/scoped_temp_dir.h"
7 #include "base/memory/ref_counted.h"
8 #include "content/browser/download/download_file_factory.h"
9 #include "content/browser/download/download_file_impl.h"
10 #include "content/browser/download/download_item_impl.h"
11 #include "content/browser/download/download_manager_impl.h"
12 #include "content/browser/download/drag_download_file.h"
13 #include "content/browser/download/drag_download_util.h"
14 #include "content/browser/web_contents/web_contents_impl.h"
15 #include "content/public/browser/content_browser_client.h"
16 #include "content/public/browser/power_save_blocker.h"
17 #include "content/public/common/content_client.h"
18 #include "content/public/test/content_browser_test.h"
19 #include "content/public/test/content_browser_test_utils.h"
20 #include "content/public/test/download_test_observer.h"
21 #include "content/public/test/test_utils.h"
22 #include "content/shell/browser/shell.h"
23 #include "content/shell/browser/shell_browser_context.h"
24 #include "content/shell/browser/shell_download_manager_delegate.h"
25 #include "content/test/net/url_request_slow_download_job.h"
26 #include "net/test/url_request/url_request_mock_http_job.h"
27 #include "testing/gmock/include/gmock/gmock.h"
28 #include "testing/gtest/include/gtest/gtest.h"
29 #include "url/gurl.h"
31 using testing::_;
32 using testing::InvokeWithoutArgs;
34 namespace content {
36 class MockDownloadFileObserver : public ui::DownloadFileObserver {
37 public:
38 MockDownloadFileObserver() {}
40 MOCK_METHOD1(OnDownloadCompleted, void(const base::FilePath& file_path));
41 MOCK_METHOD0(OnDownloadAborted, void());
43 private:
44 virtual ~MockDownloadFileObserver() {}
46 DISALLOW_COPY_AND_ASSIGN(MockDownloadFileObserver);
49 class DragDownloadFileTest : public ContentBrowserTest {
50 public:
51 DragDownloadFileTest() {}
52 ~DragDownloadFileTest() override {}
54 void Succeed() {
55 BrowserThread::PostTask(BrowserThread::UI,
56 FROM_HERE,
57 base::MessageLoopForUI::current()->QuitClosure());
60 void FailFast() {
61 CHECK(false);
64 protected:
65 void SetUpOnMainThread() override {
66 ASSERT_TRUE(downloads_directory_.CreateUniqueTempDir());
67 ShellDownloadManagerDelegate* delegate =
68 static_cast<ShellDownloadManagerDelegate*>(
69 shell()->web_contents()->GetBrowserContext()
70 ->GetDownloadManagerDelegate());
71 delegate->SetDownloadBehaviorForTesting(downloads_directory());
74 void SetUpServer() {
75 base::FilePath mock_base(GetTestFilePath("download", ""));
76 BrowserThread::PostTask(
77 BrowserThread::IO,
78 FROM_HERE,
79 base::Bind(
80 &net::URLRequestMockHTTPJob::AddUrlHandler,
81 mock_base,
82 make_scoped_refptr(content::BrowserThread::GetBlockingPool())));
85 const base::FilePath& downloads_directory() const {
86 return downloads_directory_.path();
89 private:
90 base::ScopedTempDir downloads_directory_;
92 DISALLOW_COPY_AND_ASSIGN(DragDownloadFileTest);
95 IN_PROC_BROWSER_TEST_F(DragDownloadFileTest, DragDownloadFileTest_NetError) {
96 base::FilePath name(downloads_directory().AppendASCII(
97 "DragDownloadFileTest_NetError.txt"));
98 GURL url(net::URLRequestMockHTTPJob::GetMockUrl(
99 base::FilePath(FILE_PATH_LITERAL("download-test.lib"))));
100 Referrer referrer;
101 std::string referrer_encoding;
102 scoped_refptr<DragDownloadFile> file(
103 new DragDownloadFile(name, base::File(), url, referrer,
104 referrer_encoding, shell()->web_contents()));
105 scoped_refptr<MockDownloadFileObserver> observer(
106 new MockDownloadFileObserver());
107 EXPECT_CALL(*observer.get(), OnDownloadAborted())
108 .WillOnce(InvokeWithoutArgs(this, &DragDownloadFileTest::Succeed));
109 ON_CALL(*observer.get(), OnDownloadCompleted(_))
110 .WillByDefault(InvokeWithoutArgs(this, &DragDownloadFileTest::FailFast));
111 file->Start(observer.get());
112 RunMessageLoop();
115 IN_PROC_BROWSER_TEST_F(DragDownloadFileTest, DragDownloadFileTest_Complete) {
116 base::FilePath name(downloads_directory().AppendASCII(
117 "DragDownloadFileTest_Complete.txt"));
118 GURL url(net::URLRequestMockHTTPJob::GetMockUrl(
119 base::FilePath(FILE_PATH_LITERAL("download-test.lib"))));
120 Referrer referrer;
121 std::string referrer_encoding;
122 SetUpServer();
123 scoped_refptr<DragDownloadFile> file(new DragDownloadFile(
124 name, base::File(), url, referrer,
125 referrer_encoding, shell()->web_contents()));
126 scoped_refptr<MockDownloadFileObserver> observer(
127 new MockDownloadFileObserver());
128 EXPECT_CALL(*observer.get(), OnDownloadCompleted(_))
129 .WillOnce(InvokeWithoutArgs(this, &DragDownloadFileTest::Succeed));
130 ON_CALL(*observer.get(), OnDownloadAborted())
131 .WillByDefault(InvokeWithoutArgs(this, &DragDownloadFileTest::FailFast));
132 file->Start(observer.get());
133 RunMessageLoop();
136 // TODO(benjhayden): Test Stop().
138 } // namespace content