Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / plugins / plugin_installer_unittest.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 "chrome/browser/plugins/plugin_installer.h"
6
7 #include "base/memory/scoped_ptr.h"
8 #include "base/run_loop.h"
9 #include "chrome/browser/plugins/plugin_installer_observer.h"
10 #include "chrome/test/base/chrome_render_view_host_test_harness.h"
11 #include "content/public/browser/download_url_parameters.h"
12 #include "content/public/test/mock_download_item.h"
13 #include "content/public/test/mock_download_manager.h"
14 #include "testing/gmock/include/gmock/gmock.h"
15 #include "testing/gtest/include/gtest/gtest.h"
16
17 using ::testing::_;
18
19 namespace {
20
21 class PluginInstallerTest : public ChromeRenderViewHostTestHarness {
22  public:
23   PluginInstallerTest();
24   virtual void SetUp() OVERRIDE;
25   virtual void TearDown() OVERRIDE;
26
27   PluginInstaller* installer() { return installer_.get(); }
28   content::DownloadItem::Observer* last_download_item_observer() {
29     return last_download_item_observer_;
30   }
31
32   scoped_ptr<content::MockDownloadItem> CreateMockDownloadItem();
33
34  private:
35   scoped_ptr<PluginInstaller> installer_;
36   content::DownloadItem::Observer* last_download_item_observer_;
37 };
38
39 PluginInstallerTest::PluginInstallerTest()
40     : last_download_item_observer_(NULL) {}
41
42 void PluginInstallerTest::SetUp() {
43   content::RenderViewHostTestHarness::SetUp();
44   installer_.reset(new PluginInstaller());
45 }
46
47 void PluginInstallerTest::TearDown() {
48   installer_.reset();
49   content::RenderViewHostTestHarness::TearDown();
50 }
51
52 scoped_ptr<content::MockDownloadItem>
53 PluginInstallerTest::CreateMockDownloadItem() {
54   scoped_ptr<content::MockDownloadItem> mock_download_item(
55       new testing::StrictMock<content::MockDownloadItem>());
56   ON_CALL(*mock_download_item, AddObserver(_))
57       .WillByDefault(testing::SaveArg<0>(&last_download_item_observer_));
58   ON_CALL(*mock_download_item, RemoveObserver(_)).WillByDefault(
59       testing::Assign(&last_download_item_observer_,
60                       static_cast<content::DownloadItem::Observer*>(NULL)));
61   ON_CALL(*mock_download_item, GetState())
62       .WillByDefault(testing::Return(content::DownloadItem::IN_PROGRESS));
63   return mock_download_item.Pass();
64 }
65
66 class TestPluginInstallerObserver : public PluginInstallerObserver {
67  public:
68   explicit TestPluginInstallerObserver(PluginInstaller* installer)
69       : PluginInstallerObserver(installer),
70         download_started_(false),
71         download_finished_(false),
72         download_cancelled_(false) {}
73
74   bool download_started() const { return download_started_; }
75   bool download_finished() const { return download_finished_; }
76   bool download_cancelled() const { return download_cancelled_; }
77   const std::string& download_error() const { return download_error_; }
78
79  private:
80   virtual void DownloadStarted() OVERRIDE { download_started_ = true; }
81   virtual void DownloadFinished() OVERRIDE { download_finished_ = true; }
82   virtual void DownloadError(const std::string& message) OVERRIDE {
83     download_error_ = message;
84   }
85   virtual void DownloadCancelled() OVERRIDE { download_cancelled_ = true; }
86
87   bool download_started_;
88   bool download_finished_;
89   std::string download_error_;
90   bool download_cancelled_;
91 };
92
93 // Action for invoking the OnStartedCallback of DownloadURLParameters object
94 // which is assumed to be pointed to by arg0.
95 ACTION_P2(InvokeOnStartedCallback, download_item, interrupt_reason) {
96   arg0->callback().Run(download_item, interrupt_reason);
97 }
98
99 ACTION_P(InvokeClosure, closure) {
100   closure.Run();
101 }
102
103 const char kTestUrl[] = "http://example.com/some-url";
104
105 }  // namespace
106
107 // Test that DownloadStarted()/DownloadFinished() notifications are sent to
108 // observers when a download initiated by PluginInstaller completes
109 // successfully.
110 TEST_F(PluginInstallerTest, StartInstalling_SuccessfulDownload) {
111   content::MockDownloadManager mock_download_manager;
112   base::RunLoop run_loop;
113   scoped_ptr<content::MockDownloadItem> download_item(CreateMockDownloadItem());
114
115   EXPECT_CALL(mock_download_manager,
116               DownloadUrlMock(testing::Property(
117                   &content::DownloadUrlParameters::url, GURL(kTestUrl))))
118       .WillOnce(testing::DoAll(
119           InvokeOnStartedCallback(download_item.get(),
120                                   content::DOWNLOAD_INTERRUPT_REASON_NONE),
121           InvokeClosure(run_loop.QuitClosure())));
122   EXPECT_CALL(*download_item, AddObserver(_));
123   EXPECT_CALL(*download_item, SetOpenWhenComplete(_));
124
125   TestPluginInstallerObserver installer_observer(installer());
126   installer()->StartInstallingWithDownloadManager(
127       GURL(kTestUrl), web_contents(), &mock_download_manager);
128   run_loop.Run();
129
130   ASSERT_TRUE(last_download_item_observer());
131   EXPECT_TRUE(installer_observer.download_started());
132   EXPECT_FALSE(installer_observer.download_finished());
133
134   EXPECT_CALL(*download_item, GetState())
135       .WillOnce(testing::Return(content::DownloadItem::COMPLETE));
136   EXPECT_CALL(*download_item, RemoveObserver(_));
137   last_download_item_observer()->OnDownloadUpdated(download_item.get());
138   EXPECT_TRUE(installer_observer.download_finished());
139 }
140
141 // Test that DownloadStarted()/DownloadError() notifications are sent to
142 // observers when a download initiated by PluginInstaller fails to start.
143 TEST_F(PluginInstallerTest, StartInstalling_FailedStart) {
144   content::MockDownloadManager mock_download_manager;
145   base::RunLoop run_loop;
146   scoped_ptr<content::MockDownloadItem> download_item(CreateMockDownloadItem());
147
148   EXPECT_CALL(mock_download_manager,
149               DownloadUrlMock(testing::Property(
150                   &content::DownloadUrlParameters::url, GURL(kTestUrl))))
151       .WillOnce(
152           testing::DoAll(InvokeOnStartedCallback(
153                              download_item.get(),
154                              content::DOWNLOAD_INTERRUPT_REASON_NETWORK_FAILED),
155                          InvokeClosure(run_loop.QuitClosure())));
156
157   TestPluginInstallerObserver installer_observer(installer());
158   installer()->StartInstallingWithDownloadManager(
159       GURL(kTestUrl), web_contents(), &mock_download_manager);
160   run_loop.Run();
161
162   EXPECT_FALSE(last_download_item_observer());
163   EXPECT_TRUE(installer_observer.download_started());
164   EXPECT_FALSE(installer_observer.download_finished());
165   EXPECT_EQ("Error 20: NETWORK_FAILED", installer_observer.download_error());
166 }
167
168 // Test that DownloadStarted()/DownloadError() notifications are sent to
169 // observers when a download initiated by PluginInstaller starts successfully
170 // but is interrupted later.
171 TEST_F(PluginInstallerTest, StartInstalling_Interrupted) {
172   content::MockDownloadManager mock_download_manager;
173   base::RunLoop run_loop;
174   scoped_ptr<content::MockDownloadItem> download_item(CreateMockDownloadItem());
175
176   EXPECT_CALL(mock_download_manager,
177               DownloadUrlMock(testing::Property(
178                   &content::DownloadUrlParameters::url, GURL(kTestUrl))))
179       .WillOnce(testing::DoAll(
180           InvokeOnStartedCallback(download_item.get(),
181                                   content::DOWNLOAD_INTERRUPT_REASON_NONE),
182           InvokeClosure(run_loop.QuitClosure())));
183   EXPECT_CALL(*download_item, AddObserver(_));
184   EXPECT_CALL(*download_item, SetOpenWhenComplete(_));
185
186   TestPluginInstallerObserver installer_observer(installer());
187   installer()->StartInstallingWithDownloadManager(
188       GURL(kTestUrl), web_contents(), &mock_download_manager);
189   run_loop.Run();
190
191   ASSERT_TRUE(last_download_item_observer());
192   EXPECT_TRUE(installer_observer.download_started());
193   EXPECT_FALSE(installer_observer.download_finished());
194
195   EXPECT_CALL(*download_item, GetState())
196       .WillOnce(testing::Return(content::DownloadItem::INTERRUPTED));
197   EXPECT_CALL(*download_item, RemoveObserver(_));
198   EXPECT_CALL(*download_item, GetLastReason()).WillOnce(
199       testing::Return(content::DOWNLOAD_INTERRUPT_REASON_NETWORK_FAILED));
200   last_download_item_observer()->OnDownloadUpdated(download_item.get());
201
202   EXPECT_FALSE(last_download_item_observer());
203   EXPECT_TRUE(installer_observer.download_started());
204   EXPECT_FALSE(installer_observer.download_finished());
205   EXPECT_EQ("NETWORK_FAILED", installer_observer.download_error());
206 }