Upstream version 11.39.256.0
[platform/framework/web/crosswalk.git] / src / xwalk / runtime / browser / xwalk_download_browsertest.cc
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Copyright (c) 2013 Intel Corporation. All rights reserved.
3 // Use of this source code is governed by a BSD-style license that can be
4 // found in the LICENSE file.
5
6 #include "base/file_util.h"
7 #include "base/files/file_path.h"
8 #include "base/strings/string_util.h"
9 #include "base/strings/utf_string_conversions.h"
10 #include "xwalk/runtime/browser/runtime.h"
11 #include "xwalk/runtime/browser/runtime_download_manager_delegate.h"
12 #include "xwalk/runtime/browser/ui/color_chooser.h"
13 #include "xwalk/test/base/in_process_browser_test.h"
14 #include "xwalk/test/base/xwalk_test_utils.h"
15 #include "content/browser/download/download_manager_impl.h"
16 #include "content/public/browser/browser_context.h"
17 #include "content/public/browser/render_view_host.h"
18 #include "content/public/browser/web_contents.h"
19 #include "content/public/test/browser_test_utils.h"
20 #include "content/public/test/download_test_observer.h"
21 #include "content/public/test/test_utils.h"
22
23 using xwalk::Runtime;
24 using xwalk::RuntimeDownloadManagerDelegate;
25 using content::DownloadItem;
26 using content::DownloadManager;
27 using content::DownloadManagerImpl;
28 using content::DownloadTestObserver;
29 using content::DownloadTestObserverTerminal;
30 using content::BrowserContext;
31
32 namespace {
33
34 static DownloadManagerImpl* DownloadManagerForXWalk(Runtime* runtime) {
35   return static_cast<DownloadManagerImpl*>(
36       BrowserContext::GetDownloadManager(
37           runtime->web_contents()->GetBrowserContext()));
38 }
39
40 class XWalkDownloadBrowserTest : public InProcessBrowserTest {
41  public:
42   XWalkDownloadBrowserTest()
43     : InProcessBrowserTest(),
44       runtime_(nullptr) {}
45
46   virtual void SetUpOnMainThread() OVERRIDE {
47     ASSERT_TRUE(downloads_directory_.CreateUniqueTempDir());
48     runtime_ = CreateRuntime(GURL());
49
50     DownloadManagerImpl* manager = DownloadManagerForXWalk(runtime_);
51     RuntimeDownloadManagerDelegate* delegate =
52         static_cast<RuntimeDownloadManagerDelegate*>(manager->GetDelegate());
53     delegate->SetDownloadBehaviorForTesting(downloads_directory_.path());
54   }
55
56   // Create a DownloadTestObserverTerminal that will wait for the
57   // specified number of downloads to finish.
58   DownloadTestObserver* CreateWaiter(Runtime* runtime, int num_downloads) {
59     DownloadManager* download_manager = DownloadManagerForXWalk(runtime);
60     return new DownloadTestObserverTerminal(download_manager, num_downloads,
61         DownloadTestObserver::ON_DANGEROUS_DOWNLOAD_FAIL);
62   }
63
64  protected:
65   Runtime* runtime_;
66
67  private:
68   // Location of the downloads directory for these tests
69   base::ScopedTempDir downloads_directory_;
70 };
71
72 IN_PROC_BROWSER_TEST_F(XWalkDownloadBrowserTest, FileDownload) {
73   GURL url = xwalk_test_utils::GetTestURL(
74       base::FilePath().AppendASCII("download"),
75       base::FilePath().AppendASCII("test.lib"));
76   scoped_ptr<DownloadTestObserver> observer(CreateWaiter(runtime_, 1));
77   xwalk_test_utils::NavigateToURL(runtime_, url);
78   observer->WaitForFinished();
79   EXPECT_EQ(1u, observer->NumDownloadsSeenInState(DownloadItem::COMPLETE));
80   std::vector<DownloadItem*> downloads;
81   DownloadManagerForXWalk(runtime_)->GetAllDownloads(&downloads);
82   ASSERT_EQ(1u, downloads.size());
83   ASSERT_EQ(DownloadItem::COMPLETE, downloads[0]->GetState());
84   base::FilePath file(downloads[0]->GetFullPath());
85   ASSERT_TRUE(base::ContentsEqual(
86       file, xwalk_test_utils::GetTestFilePath(
87           base::FilePath().AppendASCII("download"),
88           base::FilePath().AppendASCII("test.lib"))));
89 }
90
91 }  // namespace