OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/plugins/plugin_installer.h" | 5 #include "chrome/browser/plugins/plugin_installer.h" |
6 | 6 |
7 #include "base/strings/utf_string_conversions.h" | 7 #include "base/memory/scoped_ptr.h" |
8 #include "content/public/common/webplugininfo.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" | |
9 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
10 | 16 |
17 using ::testing::_; | |
18 | |
19 class PluginInstallerTest : public ChromeRenderViewHostTestHarness { | |
20 public: | |
21 PluginInstallerTest(); | |
22 virtual void SetUp() OVERRIDE; | |
23 virtual void TearDown() OVERRIDE; | |
24 | |
25 PluginInstaller* installer() { return installer_.get(); } | |
26 content::DownloadItem::Observer* last_download_item_observer() { | |
27 return last_download_item_observer_; | |
28 } | |
29 | |
30 scoped_ptr<content::MockDownloadItem> CreateMockDownloadItem(); | |
31 | |
32 private: | |
33 scoped_ptr<PluginInstaller> installer_; | |
34 content::DownloadItem::Observer* last_download_item_observer_; | |
35 }; | |
36 | |
37 PluginInstallerTest::PluginInstallerTest() | |
38 : last_download_item_observer_(NULL) {} | |
39 | |
40 void PluginInstallerTest::SetUp() { | |
41 content::RenderViewHostTestHarness::SetUp(); | |
42 installer_.reset(new PluginInstaller()); | |
43 } | |
44 | |
45 void PluginInstallerTest::TearDown() { | |
46 installer_.reset(); | |
47 content::RenderViewHostTestHarness::TearDown(); | |
48 } | |
49 | |
50 scoped_ptr<content::MockDownloadItem> | |
51 PluginInstallerTest::CreateMockDownloadItem() { | |
52 scoped_ptr<content::MockDownloadItem> mock_download_item( | |
53 new testing::StrictMock<content::MockDownloadItem>()); | |
54 ON_CALL(*mock_download_item, AddObserver(_)) | |
55 .WillByDefault(testing::SaveArg<0>(&last_download_item_observer_)); | |
56 ON_CALL(*mock_download_item, RemoveObserver(_)).WillByDefault( | |
57 testing::Assign(&last_download_item_observer_, | |
58 static_cast<content::DownloadItem::Observer*>(NULL))); | |
59 ON_CALL(*mock_download_item, GetState()) | |
60 .WillByDefault(testing::Return(content::DownloadItem::IN_PROGRESS)); | |
61 return mock_download_item.Pass(); | |
62 } | |
63 | |
64 class TestPluginInstallerObserver : public PluginInstallerObserver { | |
65 public: | |
66 explicit TestPluginInstallerObserver(PluginInstaller* installer) | |
67 : PluginInstallerObserver(installer), | |
68 download_started_(false), | |
69 download_finished_(false), | |
70 download_cancelled_(false) {} | |
71 | |
72 bool download_started() const { return download_started_; } | |
73 bool download_finished() const { return download_finished_; } | |
74 bool download_cancelled() const { return download_cancelled_; } | |
75 const std::string& download_error() const { return download_error_; } | |
76 | |
77 private: | |
78 virtual void DownloadStarted() OVERRIDE { download_started_ = true; } | |
79 virtual void DownloadFinished() OVERRIDE { download_finished_ = true; } | |
80 virtual void DownloadError(const std::string& message) OVERRIDE { | |
81 download_error_ = message; | |
82 } | |
83 virtual void DownloadCancelled() OVERRIDE { download_cancelled_ = true; } | |
84 | |
85 bool download_started_; | |
86 bool download_finished_; | |
87 std::string download_error_; | |
88 bool download_cancelled_; | |
89 }; | |
90 | |
11 namespace { | 91 namespace { |
Bernhard Bauer
2014/04/10 10:26:40
Nit: move this to the top of the file?
asanka
2014/04/18 18:07:49
Done.
| |
12 | 92 |
13 PluginInstaller::SecurityStatus GetSecurityStatus(PluginInstaller* installer, | 93 // Action for invoking the OnStartedCallback of DownloadURLParameters object |
14 const char* version) { | 94 // which is assumed to be pointed to by arg0. |
15 content:: WebPluginInfo plugin( | 95 ACTION_P2(InvokeOnStartedCallback, download_item, interrupt_reason) { |
16 base::ASCIIToUTF16("Foo plug-in"), | 96 arg0->callback().Run(download_item, interrupt_reason); |
17 base::FilePath(FILE_PATH_LITERAL("/tmp/plugin.so")), | 97 } |
18 base::ASCIIToUTF16(version), | 98 |
19 base::ASCIIToUTF16("Foo plug-in.")); | 99 ACTION_P(InvokeClosure, closure) { |
20 return installer->GetSecurityStatus(plugin); | 100 closure.Run(); |
21 } | 101 } |
102 | |
103 const char kTestUrl[] = "http://example.com/some-url"; | |
22 | 104 |
23 } // namespace | 105 } // namespace |
24 | 106 |
25 TEST(PluginInstallerTest, SecurityStatus) { | 107 // Test that DownloadStarted()/DownloadFinished() notifications are sent to |
Bernhard Bauer
2014/04/10 10:26:40
I'd be interested in getting this working again. D
asanka
2014/04/18 18:07:49
The test didn't compile. Also GetSecurityStatus()
Bernhard Bauer
2014/04/23 08:33:25
Oh, so the test in here was most likely left over
| |
26 const PluginInstaller::SecurityStatus kUpToDate = | 108 // observers when a download initiated by PluginInstaller completes |
27 PluginInstaller::SECURITY_STATUS_UP_TO_DATE; | 109 // successfully. |
28 const PluginInstaller::SecurityStatus kOutOfDate = | 110 TEST_F(PluginInstallerTest, StartInstalling_SuccessfulDownload) { |
29 PluginInstaller::SECURITY_STATUS_OUT_OF_DATE; | 111 content::MockDownloadManager mock_download_manager; |
30 const PluginInstaller::SecurityStatus kRequiresAuthorization = | 112 base::RunLoop run_loop; |
31 PluginInstaller::SECURITY_STATUS_REQUIRES_AUTHORIZATION; | 113 scoped_ptr<content::MockDownloadItem> download_item(CreateMockDownloadItem()); |
32 | 114 |
33 PluginInstaller installer("claybrick-writer", | 115 EXPECT_CALL(mock_download_manager, |
34 base::ASCIIToUTF16("ClayBrick Writer"), | 116 DownloadUrlMock(testing::Property( |
35 true, GURL(), GURL(), | 117 &content::DownloadUrlParameters::url, GURL(kTestUrl)))) |
36 base::ASCIIToUTF16("ClayBrick")); | 118 .WillOnce(testing::DoAll( |
37 | 119 InvokeOnStartedCallback(download_item.get(), |
38 #if defined(OS_LINUX) | 120 content::DOWNLOAD_INTERRUPT_REASON_NONE), |
39 EXPECT_EQ(kRequiresAuthorization, GetSecurityStatus(&installer, "1.2.3")); | 121 InvokeClosure(run_loop.QuitClosure()))); |
40 #else | 122 EXPECT_CALL(*download_item, AddObserver(_)); |
41 EXPECT_EQ(kUpToDate, GetSecurityStatus(&installer, "1.2.3")); | 123 EXPECT_CALL(*download_item, SetOpenWhenComplete(_)); |
42 #endif | 124 |
43 | 125 TestPluginInstallerObserver installer_observer(installer()); |
44 installer.AddVersion(Version("9.4.1"), kRequiresAuthorization); | 126 installer()->StartInstallingWithDownloadManager( |
45 installer.AddVersion(Version("10"), kOutOfDate); | 127 GURL(kTestUrl), web_contents(), &mock_download_manager); |
46 installer.AddVersion(Version("10.2.1"), kUpToDate); | 128 run_loop.Run(); |
47 | 129 |
48 // Invalid version. | 130 ASSERT_TRUE(last_download_item_observer()); |
49 EXPECT_EQ(kOutOfDate, GetSecurityStatus(&installer, "foo")); | 131 EXPECT_TRUE(installer_observer.download_started()); |
50 | 132 EXPECT_FALSE(installer_observer.download_finished()); |
51 EXPECT_EQ(kOutOfDate, GetSecurityStatus(&installer, "0")); | 133 |
52 EXPECT_EQ(kOutOfDate, GetSecurityStatus(&installer, "1.2.3")); | 134 EXPECT_CALL(*download_item, GetState()) |
53 EXPECT_EQ(kRequiresAuthorization, GetSecurityStatus(&installer, "9.4.1")); | 135 .WillOnce(testing::Return(content::DownloadItem::COMPLETE)); |
54 EXPECT_EQ(kRequiresAuthorization, GetSecurityStatus(&installer, "9.4.2")); | 136 EXPECT_CALL(*download_item, RemoveObserver(_)); |
55 EXPECT_EQ(kOutOfDate, GetSecurityStatus(&installer, "10.2.0")); | 137 last_download_item_observer()->OnDownloadUpdated(download_item.get()); |
56 EXPECT_EQ(kUpToDate, GetSecurityStatus(&installer, "10.2.1")); | 138 EXPECT_TRUE(installer_observer.download_finished()); |
57 EXPECT_EQ(kUpToDate, GetSecurityStatus(&installer, "11")); | 139 } |
58 } | 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 } | |
OLD | NEW |