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