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/memory/scoped_ptr.h" | 7 #include "base/memory/scoped_ptr.h" |
8 #include "base/run_loop.h" | 8 #include "base/run_loop.h" |
9 #include "chrome/browser/plugins/plugin_installer_observer.h" | 9 #include "chrome/browser/plugins/plugin_installer_observer.h" |
10 #include "chrome/test/base/chrome_render_view_host_test_harness.h" | 10 #include "chrome/test/base/chrome_render_view_host_test_harness.h" |
11 #include "content/public/browser/download_url_parameters.h" | 11 #include "content/public/browser/download_url_parameters.h" |
12 #include "content/public/test/mock_download_item.h" | 12 #include "content/public/test/mock_download_item.h" |
13 #include "content/public/test/mock_download_manager.h" | 13 #include "content/public/test/mock_download_manager.h" |
14 #include "testing/gmock/include/gmock/gmock.h" | 14 #include "testing/gmock/include/gmock/gmock.h" |
15 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
16 | 16 |
17 using ::testing::_; | 17 using ::testing::_; |
18 | 18 |
19 namespace { | 19 namespace { |
20 | 20 |
21 class PluginInstallerTest : public ChromeRenderViewHostTestHarness { | 21 class PluginInstallerTest : public ChromeRenderViewHostTestHarness { |
22 public: | 22 public: |
23 PluginInstallerTest(); | 23 PluginInstallerTest(); |
24 virtual void SetUp() OVERRIDE; | 24 virtual void SetUp() OVERRIDE; |
25 virtual void TearDown() OVERRIDE; | 25 virtual void TearDown() OVERRIDE; |
26 | 26 |
27 PluginInstaller* installer() { return installer_.get(); } | 27 PluginInstaller* installer() { return installer_.get(); } |
28 content::DownloadItem::Observer* last_download_item_observer() { | |
29 return last_download_item_observer_; | |
30 } | |
31 | 28 |
32 scoped_ptr<content::MockDownloadItem> CreateMockDownloadItem(); | 29 scoped_ptr<content::MockDownloadItem> CreateMockDownloadItem(); |
33 | 30 |
34 private: | 31 private: |
35 scoped_ptr<PluginInstaller> installer_; | 32 scoped_ptr<PluginInstaller> installer_; |
36 content::DownloadItem::Observer* last_download_item_observer_; | |
37 }; | 33 }; |
38 | 34 |
39 PluginInstallerTest::PluginInstallerTest() | 35 PluginInstallerTest::PluginInstallerTest() { |
40 : last_download_item_observer_(NULL) {} | 36 } |
41 | 37 |
42 void PluginInstallerTest::SetUp() { | 38 void PluginInstallerTest::SetUp() { |
43 content::RenderViewHostTestHarness::SetUp(); | 39 content::RenderViewHostTestHarness::SetUp(); |
44 installer_.reset(new PluginInstaller()); | 40 installer_.reset(new PluginInstaller()); |
45 } | 41 } |
46 | 42 |
47 void PluginInstallerTest::TearDown() { | 43 void PluginInstallerTest::TearDown() { |
48 installer_.reset(); | 44 installer_.reset(); |
49 content::RenderViewHostTestHarness::TearDown(); | 45 content::RenderViewHostTestHarness::TearDown(); |
50 } | 46 } |
51 | 47 |
52 scoped_ptr<content::MockDownloadItem> | 48 scoped_ptr<content::MockDownloadItem> |
53 PluginInstallerTest::CreateMockDownloadItem() { | 49 PluginInstallerTest::CreateMockDownloadItem() { |
54 scoped_ptr<content::MockDownloadItem> mock_download_item( | 50 scoped_ptr<content::MockDownloadItem> mock_download_item( |
55 new testing::StrictMock<content::MockDownloadItem>()); | 51 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()) | 52 ON_CALL(*mock_download_item, GetState()) |
62 .WillByDefault(testing::Return(content::DownloadItem::IN_PROGRESS)); | 53 .WillByDefault(testing::Return(content::DownloadItem::IN_PROGRESS)); |
63 return mock_download_item.Pass(); | 54 return mock_download_item.Pass(); |
64 } | 55 } |
65 | 56 |
66 class TestPluginInstallerObserver : public PluginInstallerObserver { | 57 class TestPluginInstallerObserver : public PluginInstallerObserver { |
67 public: | 58 public: |
68 explicit TestPluginInstallerObserver(PluginInstaller* installer) | 59 explicit TestPluginInstallerObserver(PluginInstaller* installer) |
69 : PluginInstallerObserver(installer), | 60 : PluginInstallerObserver(installer), |
70 download_started_(false), | 61 download_started_(false), |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
112 base::RunLoop run_loop; | 103 base::RunLoop run_loop; |
113 scoped_ptr<content::MockDownloadItem> download_item(CreateMockDownloadItem()); | 104 scoped_ptr<content::MockDownloadItem> download_item(CreateMockDownloadItem()); |
114 | 105 |
115 EXPECT_CALL(mock_download_manager, | 106 EXPECT_CALL(mock_download_manager, |
116 DownloadUrlMock(testing::Property( | 107 DownloadUrlMock(testing::Property( |
117 &content::DownloadUrlParameters::url, GURL(kTestUrl)))) | 108 &content::DownloadUrlParameters::url, GURL(kTestUrl)))) |
118 .WillOnce(testing::DoAll( | 109 .WillOnce(testing::DoAll( |
119 InvokeOnStartedCallback(download_item.get(), | 110 InvokeOnStartedCallback(download_item.get(), |
120 content::DOWNLOAD_INTERRUPT_REASON_NONE), | 111 content::DOWNLOAD_INTERRUPT_REASON_NONE), |
121 InvokeClosure(run_loop.QuitClosure()))); | 112 InvokeClosure(run_loop.QuitClosure()))); |
122 EXPECT_CALL(*download_item, AddObserver(_)); | |
123 EXPECT_CALL(*download_item, SetOpenWhenComplete(_)); | 113 EXPECT_CALL(*download_item, SetOpenWhenComplete(_)); |
124 | 114 |
125 TestPluginInstallerObserver installer_observer(installer()); | 115 TestPluginInstallerObserver installer_observer(installer()); |
126 installer()->StartInstallingWithDownloadManager( | 116 installer()->StartInstallingWithDownloadManager( |
127 GURL(kTestUrl), web_contents(), &mock_download_manager); | 117 GURL(kTestUrl), web_contents(), &mock_download_manager); |
128 run_loop.Run(); | 118 run_loop.Run(); |
129 | 119 |
130 ASSERT_TRUE(last_download_item_observer()); | |
131 EXPECT_TRUE(installer_observer.download_started()); | 120 EXPECT_TRUE(installer_observer.download_started()); |
132 EXPECT_FALSE(installer_observer.download_finished()); | 121 EXPECT_FALSE(installer_observer.download_finished()); |
133 | 122 |
134 EXPECT_CALL(*download_item, GetState()) | 123 EXPECT_CALL(*download_item, GetState()) |
135 .WillOnce(testing::Return(content::DownloadItem::COMPLETE)); | 124 .WillOnce(testing::Return(content::DownloadItem::COMPLETE)); |
136 EXPECT_CALL(*download_item, RemoveObserver(_)); | 125 download_item->NotifyObserversDownloadUpdated(); |
137 last_download_item_observer()->OnDownloadUpdated(download_item.get()); | |
138 EXPECT_TRUE(installer_observer.download_finished()); | 126 EXPECT_TRUE(installer_observer.download_finished()); |
139 } | 127 } |
140 | 128 |
141 // Test that DownloadStarted()/DownloadError() notifications are sent to | 129 // Test that DownloadStarted()/DownloadError() notifications are sent to |
142 // observers when a download initiated by PluginInstaller fails to start. | 130 // observers when a download initiated by PluginInstaller fails to start. |
143 TEST_F(PluginInstallerTest, StartInstalling_FailedStart) { | 131 TEST_F(PluginInstallerTest, StartInstalling_FailedStart) { |
144 content::MockDownloadManager mock_download_manager; | 132 content::MockDownloadManager mock_download_manager; |
145 base::RunLoop run_loop; | 133 base::RunLoop run_loop; |
146 scoped_ptr<content::MockDownloadItem> download_item(CreateMockDownloadItem()); | 134 scoped_ptr<content::MockDownloadItem> download_item(CreateMockDownloadItem()); |
147 | 135 |
148 EXPECT_CALL(mock_download_manager, | 136 EXPECT_CALL(mock_download_manager, |
149 DownloadUrlMock(testing::Property( | 137 DownloadUrlMock(testing::Property( |
150 &content::DownloadUrlParameters::url, GURL(kTestUrl)))) | 138 &content::DownloadUrlParameters::url, GURL(kTestUrl)))) |
151 .WillOnce( | 139 .WillOnce( |
152 testing::DoAll(InvokeOnStartedCallback( | 140 testing::DoAll(InvokeOnStartedCallback( |
153 download_item.get(), | 141 download_item.get(), |
154 content::DOWNLOAD_INTERRUPT_REASON_NETWORK_FAILED), | 142 content::DOWNLOAD_INTERRUPT_REASON_NETWORK_FAILED), |
155 InvokeClosure(run_loop.QuitClosure()))); | 143 InvokeClosure(run_loop.QuitClosure()))); |
156 | 144 |
157 TestPluginInstallerObserver installer_observer(installer()); | 145 TestPluginInstallerObserver installer_observer(installer()); |
158 installer()->StartInstallingWithDownloadManager( | 146 installer()->StartInstallingWithDownloadManager( |
159 GURL(kTestUrl), web_contents(), &mock_download_manager); | 147 GURL(kTestUrl), web_contents(), &mock_download_manager); |
160 run_loop.Run(); | 148 run_loop.Run(); |
161 | 149 |
162 EXPECT_FALSE(last_download_item_observer()); | |
163 EXPECT_TRUE(installer_observer.download_started()); | 150 EXPECT_TRUE(installer_observer.download_started()); |
164 EXPECT_FALSE(installer_observer.download_finished()); | 151 EXPECT_FALSE(installer_observer.download_finished()); |
165 EXPECT_EQ("Error 20: NETWORK_FAILED", installer_observer.download_error()); | 152 EXPECT_EQ("Error 20: NETWORK_FAILED", installer_observer.download_error()); |
166 } | 153 } |
167 | 154 |
168 // Test that DownloadStarted()/DownloadError() notifications are sent to | 155 // Test that DownloadStarted()/DownloadError() notifications are sent to |
169 // observers when a download initiated by PluginInstaller starts successfully | 156 // observers when a download initiated by PluginInstaller starts successfully |
170 // but is interrupted later. | 157 // but is interrupted later. |
171 TEST_F(PluginInstallerTest, StartInstalling_Interrupted) { | 158 TEST_F(PluginInstallerTest, StartInstalling_Interrupted) { |
172 content::MockDownloadManager mock_download_manager; | 159 content::MockDownloadManager mock_download_manager; |
173 base::RunLoop run_loop; | 160 base::RunLoop run_loop; |
174 scoped_ptr<content::MockDownloadItem> download_item(CreateMockDownloadItem()); | 161 scoped_ptr<content::MockDownloadItem> download_item(CreateMockDownloadItem()); |
175 | 162 |
176 EXPECT_CALL(mock_download_manager, | 163 EXPECT_CALL(mock_download_manager, |
177 DownloadUrlMock(testing::Property( | 164 DownloadUrlMock(testing::Property( |
178 &content::DownloadUrlParameters::url, GURL(kTestUrl)))) | 165 &content::DownloadUrlParameters::url, GURL(kTestUrl)))) |
179 .WillOnce(testing::DoAll( | 166 .WillOnce(testing::DoAll( |
180 InvokeOnStartedCallback(download_item.get(), | 167 InvokeOnStartedCallback(download_item.get(), |
181 content::DOWNLOAD_INTERRUPT_REASON_NONE), | 168 content::DOWNLOAD_INTERRUPT_REASON_NONE), |
182 InvokeClosure(run_loop.QuitClosure()))); | 169 InvokeClosure(run_loop.QuitClosure()))); |
183 EXPECT_CALL(*download_item, AddObserver(_)); | |
184 EXPECT_CALL(*download_item, SetOpenWhenComplete(_)); | 170 EXPECT_CALL(*download_item, SetOpenWhenComplete(_)); |
185 | 171 |
186 TestPluginInstallerObserver installer_observer(installer()); | 172 TestPluginInstallerObserver installer_observer(installer()); |
187 installer()->StartInstallingWithDownloadManager( | 173 installer()->StartInstallingWithDownloadManager( |
188 GURL(kTestUrl), web_contents(), &mock_download_manager); | 174 GURL(kTestUrl), web_contents(), &mock_download_manager); |
189 run_loop.Run(); | 175 run_loop.Run(); |
190 | 176 |
191 ASSERT_TRUE(last_download_item_observer()); | |
192 EXPECT_TRUE(installer_observer.download_started()); | 177 EXPECT_TRUE(installer_observer.download_started()); |
193 EXPECT_FALSE(installer_observer.download_finished()); | 178 EXPECT_FALSE(installer_observer.download_finished()); |
194 | 179 |
195 EXPECT_CALL(*download_item, GetState()) | 180 EXPECT_CALL(*download_item, GetState()) |
196 .WillOnce(testing::Return(content::DownloadItem::INTERRUPTED)); | 181 .WillOnce(testing::Return(content::DownloadItem::INTERRUPTED)); |
197 EXPECT_CALL(*download_item, RemoveObserver(_)); | |
198 EXPECT_CALL(*download_item, GetLastReason()).WillOnce( | 182 EXPECT_CALL(*download_item, GetLastReason()).WillOnce( |
199 testing::Return(content::DOWNLOAD_INTERRUPT_REASON_NETWORK_FAILED)); | 183 testing::Return(content::DOWNLOAD_INTERRUPT_REASON_NETWORK_FAILED)); |
200 last_download_item_observer()->OnDownloadUpdated(download_item.get()); | 184 download_item->NotifyObserversDownloadUpdated(); |
201 | 185 |
202 EXPECT_FALSE(last_download_item_observer()); | |
203 EXPECT_TRUE(installer_observer.download_started()); | 186 EXPECT_TRUE(installer_observer.download_started()); |
204 EXPECT_FALSE(installer_observer.download_finished()); | 187 EXPECT_FALSE(installer_observer.download_finished()); |
205 EXPECT_EQ("NETWORK_FAILED", installer_observer.download_error()); | 188 EXPECT_EQ("NETWORK_FAILED", installer_observer.download_error()); |
206 } | 189 } |
OLD | NEW |