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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
|
// 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/plugins/plugin_installer.h"
#include "base/memory/scoped_ptr.h"
#include "base/run_loop.h"
#include "chrome/browser/plugins/plugin_installer_observer.h"
#include "chrome/test/base/chrome_render_view_host_test_harness.h"
#include "content/public/browser/download_url_parameters.h"
#include "content/public/test/mock_download_item.h"
#include "content/public/test/mock_download_manager.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
using ::testing::_;
namespace {
class PluginInstallerTest : public ChromeRenderViewHostTestHarness {
public:
PluginInstallerTest();
virtual void SetUp() override;
virtual void TearDown() override;
PluginInstaller* installer() { return installer_.get(); }
scoped_ptr<content::MockDownloadItem> CreateMockDownloadItem();
private:
scoped_ptr<PluginInstaller> installer_;
};
PluginInstallerTest::PluginInstallerTest() {
}
void PluginInstallerTest::SetUp() {
content::RenderViewHostTestHarness::SetUp();
installer_.reset(new PluginInstaller());
}
void PluginInstallerTest::TearDown() {
installer_.reset();
content::RenderViewHostTestHarness::TearDown();
}
scoped_ptr<content::MockDownloadItem>
PluginInstallerTest::CreateMockDownloadItem() {
scoped_ptr<content::MockDownloadItem> mock_download_item(
new testing::StrictMock<content::MockDownloadItem>());
ON_CALL(*mock_download_item, GetState())
.WillByDefault(testing::Return(content::DownloadItem::IN_PROGRESS));
return mock_download_item.Pass();
}
class TestPluginInstallerObserver : public PluginInstallerObserver {
public:
explicit TestPluginInstallerObserver(PluginInstaller* installer)
: PluginInstallerObserver(installer),
download_started_(false),
download_finished_(false),
download_cancelled_(false) {}
bool download_started() const { return download_started_; }
bool download_finished() const { return download_finished_; }
bool download_cancelled() const { return download_cancelled_; }
const std::string& download_error() const { return download_error_; }
private:
void DownloadStarted() override { download_started_ = true; }
void DownloadFinished() override { download_finished_ = true; }
void DownloadError(const std::string& message) override {
download_error_ = message;
}
void DownloadCancelled() override { download_cancelled_ = true; }
bool download_started_;
bool download_finished_;
std::string download_error_;
bool download_cancelled_;
};
// Action for invoking the OnStartedCallback of DownloadURLParameters object
// which is assumed to be pointed to by arg0.
ACTION_P2(InvokeOnStartedCallback, download_item, interrupt_reason) {
arg0->callback().Run(download_item, interrupt_reason);
}
ACTION_P(InvokeClosure, closure) {
closure.Run();
}
const char kTestUrl[] = "http://example.com/some-url";
} // namespace
// Test that DownloadStarted()/DownloadFinished() notifications are sent to
// observers when a download initiated by PluginInstaller completes
// successfully.
TEST_F(PluginInstallerTest, StartInstalling_SuccessfulDownload) {
content::MockDownloadManager mock_download_manager;
base::RunLoop run_loop;
scoped_ptr<content::MockDownloadItem> download_item(CreateMockDownloadItem());
EXPECT_CALL(mock_download_manager,
DownloadUrlMock(testing::Property(
&content::DownloadUrlParameters::url, GURL(kTestUrl))))
.WillOnce(testing::DoAll(
InvokeOnStartedCallback(download_item.get(),
content::DOWNLOAD_INTERRUPT_REASON_NONE),
InvokeClosure(run_loop.QuitClosure())));
EXPECT_CALL(*download_item, SetOpenWhenComplete(_));
TestPluginInstallerObserver installer_observer(installer());
installer()->StartInstallingWithDownloadManager(
GURL(kTestUrl), web_contents(), &mock_download_manager);
run_loop.Run();
EXPECT_TRUE(installer_observer.download_started());
EXPECT_FALSE(installer_observer.download_finished());
EXPECT_CALL(*download_item, GetState())
.WillOnce(testing::Return(content::DownloadItem::COMPLETE));
download_item->NotifyObserversDownloadUpdated();
EXPECT_TRUE(installer_observer.download_finished());
}
// Test that DownloadStarted()/DownloadError() notifications are sent to
// observers when a download initiated by PluginInstaller fails to start.
TEST_F(PluginInstallerTest, StartInstalling_FailedStart) {
content::MockDownloadManager mock_download_manager;
base::RunLoop run_loop;
scoped_ptr<content::MockDownloadItem> download_item(CreateMockDownloadItem());
EXPECT_CALL(mock_download_manager,
DownloadUrlMock(testing::Property(
&content::DownloadUrlParameters::url, GURL(kTestUrl))))
.WillOnce(
testing::DoAll(InvokeOnStartedCallback(
download_item.get(),
content::DOWNLOAD_INTERRUPT_REASON_NETWORK_FAILED),
InvokeClosure(run_loop.QuitClosure())));
TestPluginInstallerObserver installer_observer(installer());
installer()->StartInstallingWithDownloadManager(
GURL(kTestUrl), web_contents(), &mock_download_manager);
run_loop.Run();
EXPECT_TRUE(installer_observer.download_started());
EXPECT_FALSE(installer_observer.download_finished());
EXPECT_EQ("Error 20: NETWORK_FAILED", installer_observer.download_error());
}
// Test that DownloadStarted()/DownloadError() notifications are sent to
// observers when a download initiated by PluginInstaller starts successfully
// but is interrupted later.
TEST_F(PluginInstallerTest, StartInstalling_Interrupted) {
content::MockDownloadManager mock_download_manager;
base::RunLoop run_loop;
scoped_ptr<content::MockDownloadItem> download_item(CreateMockDownloadItem());
EXPECT_CALL(mock_download_manager,
DownloadUrlMock(testing::Property(
&content::DownloadUrlParameters::url, GURL(kTestUrl))))
.WillOnce(testing::DoAll(
InvokeOnStartedCallback(download_item.get(),
content::DOWNLOAD_INTERRUPT_REASON_NONE),
InvokeClosure(run_loop.QuitClosure())));
EXPECT_CALL(*download_item, SetOpenWhenComplete(_));
TestPluginInstallerObserver installer_observer(installer());
installer()->StartInstallingWithDownloadManager(
GURL(kTestUrl), web_contents(), &mock_download_manager);
run_loop.Run();
EXPECT_TRUE(installer_observer.download_started());
EXPECT_FALSE(installer_observer.download_finished());
EXPECT_CALL(*download_item, GetState())
.WillOnce(testing::Return(content::DownloadItem::INTERRUPTED));
EXPECT_CALL(*download_item, GetLastReason()).WillOnce(
testing::Return(content::DOWNLOAD_INTERRUPT_REASON_NETWORK_FAILED));
download_item->NotifyObserversDownloadUpdated();
EXPECT_TRUE(installer_observer.download_started());
EXPECT_FALSE(installer_observer.download_finished());
EXPECT_EQ("NETWORK_FAILED", installer_observer.download_error());
}
|