summaryrefslogtreecommitdiffstats
path: root/chrome/browser/plugin_download_helper_unittest.cc
blob: 5615c5c65e2de4d0f8dbd22861d8ec7773bb8b93 (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
// Copyright (c) 2012 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 "chrome/browser/plugin_download_helper.h"

#include "base/bind.h"
#include "base/file_path.h"
#include "base/message_loop.h"
#include "base/test/test_timeouts.h"
#include "chrome/common/chrome_paths.h"
#include "content/test/test_browser_thread.h"
#include "net/base/net_util.h"
#include "net/url_request/url_request_context.h"
#include "net/url_request/url_request_context_getter.h"
#include "net/url_request/url_request_test_util.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace {

class TestURLRequestContextGetter : public net::URLRequestContextGetter {
 public:
  TestURLRequestContextGetter()
      : io_message_loop_proxy_(base::MessageLoopProxy::current()) {
  }

  virtual net::URLRequestContext* GetURLRequestContext() {
    if (!context_)
      context_ = new TestURLRequestContext();
    return context_;
  }
  virtual scoped_refptr<base::MessageLoopProxy> GetIOMessageLoopProxy() const {
    return io_message_loop_proxy_;
  }

 protected:
  scoped_refptr<base::MessageLoopProxy> io_message_loop_proxy_;

 private:
  virtual ~TestURLRequestContextGetter() {}

  scoped_refptr<net::URLRequestContext> context_;
};

}  // namespace

// This class provides functionality to test the plugin installer download
// file functionality.
class PluginInstallerDownloadTest : public testing::Test {
 public:
  PluginInstallerDownloadTest()
      : message_loop_(MessageLoop::TYPE_IO),
        file_thread_(content::BrowserThread::FILE, &message_loop_),
        download_helper_(NULL),
        success_(false) {}
  ~PluginInstallerDownloadTest() {}

  void Start() {
    FilePath path;
    PathService::Get(chrome::DIR_TEST_DATA, &path);
    initial_download_path_ = net::FilePathToFileURL(
        path.AppendASCII("download-test1.lib"));
    download_helper_ = new PluginDownloadUrlHelper();
    TestURLRequestContextGetter* context_getter =
        new TestURLRequestContextGetter;
    download_helper_->InitiateDownload(
        initial_download_path_,
        context_getter,
        base::Bind(&PluginInstallerDownloadTest::OnDownloadCompleted,
                   base::Unretained(this)),
        base::Bind(&PluginInstallerDownloadTest::OnDownloadError,
                   base::Unretained(this)));

    message_loop_.PostDelayedTask(
        FROM_HERE, MessageLoop::QuitClosure(),
        TestTimeouts::action_max_timeout_ms());
  }

  void OnDownloadCompleted(const FilePath& download_path) {
    success_ = true;
    final_download_path_ = download_path;
    message_loop_.Quit();
    download_helper_ = NULL;
  }

  void OnDownloadError(const std::string& error) {
    ADD_FAILURE() << error;
    message_loop_.Quit();
    download_helper_ = NULL;
  }

  FilePath final_download_path() const {
    return final_download_path_;
  }

  FilePath initial_download_path() const {
    return final_download_path_;
  }

  bool success() const {
    return success_;
  }

 private:
  MessageLoop message_loop_;
  content::TestBrowserThread file_thread_;
  FilePath final_download_path_;
  PluginDownloadUrlHelper* download_helper_;
  bool success_;
  GURL initial_download_path_;
};

// This test validates that the plugin downloader downloads the specified file
// to a temporary path with the same file name.
TEST_F(PluginInstallerDownloadTest, PluginInstallerDownloadPathTest) {
  Start();
  MessageLoop::current()->Run();

  ASSERT_TRUE(success());
  EXPECT_TRUE(initial_download_path().BaseName().value() ==
              final_download_path().BaseName().value());
}