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 |
11 namespace { | 19 namespace { |
12 | 20 |
13 PluginInstaller::SecurityStatus GetSecurityStatus(PluginInstaller* installer, | 21 class PluginInstallerTest : public ChromeRenderViewHostTestHarness { |
14 const char* version) { | 22 public: |
15 content:: WebPluginInfo plugin( | 23 PluginInstallerTest(); |
16 base::ASCIIToUTF16("Foo plug-in"), | 24 virtual void SetUp() OVERRIDE; |
17 base::FilePath(FILE_PATH_LITERAL("/tmp/plugin.so")), | 25 virtual void TearDown() OVERRIDE; |
18 base::ASCIIToUTF16(version), | 26 |
19 base::ASCIIToUTF16("Foo plug-in.")); | 27 PluginInstaller* installer() { return installer_.get(); } |
20 return installer->GetSecurityStatus(plugin); | 28 content::DownloadItem::Observer* last_download_item_observer() { |
21 } | 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"; |
22 | 104 |
23 } // namespace | 105 } // namespace |
24 | 106 |
25 TEST(PluginInstallerTest, SecurityStatus) { | 107 // Test that DownloadStarted()/DownloadFinished() notifications are sent to |
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 |