Chromium Code Reviews| 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 "base/bind.h" | 5 #include "base/bind.h" |
| 6 #include "base/command_line.h" | |
| 6 #include "base/files/file_path.h" | 7 #include "base/files/file_path.h" |
| 7 #include "base/macros.h" | 8 #include "base/macros.h" |
| 8 #include "build/build_config.h" | 9 #include "build/build_config.h" |
| 9 #include "chrome/browser/download/download_danger_prompt.h" | 10 #include "chrome/browser/download/download_danger_prompt.h" |
| 10 #include "chrome/browser/profiles/profile.h" | 11 #include "chrome/browser/profiles/profile.h" |
| 11 #include "chrome/browser/safe_browsing/download_protection_service.h" | 12 #include "chrome/browser/safe_browsing/download_protection_service.h" |
| 12 #include "chrome/browser/safe_browsing/test_safe_browsing_service.h" | 13 #include "chrome/browser/safe_browsing/test_safe_browsing_service.h" |
| 13 #include "chrome/browser/ui/browser.h" | 14 #include "chrome/browser/ui/browser.h" |
| 14 #include "chrome/browser/ui/browser_commands.h" | 15 #include "chrome/browser/ui/browser_commands.h" |
| 15 #include "chrome/browser/ui/browser_tabstrip.h" | 16 #include "chrome/browser/ui/browser_tabstrip.h" |
| 16 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 17 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 17 #include "chrome/common/safe_browsing/csd.pb.h" | 18 #include "chrome/common/safe_browsing/csd.pb.h" |
| 18 #include "chrome/test/base/in_process_browser_test.h" | 19 #include "chrome/test/base/in_process_browser_test.h" |
| 19 #include "chrome/test/base/ui_test_utils.h" | 20 #include "chrome/test/base/ui_test_utils.h" |
| 20 #include "components/safe_browsing_db/database_manager.h" | 21 #include "components/safe_browsing_db/database_manager.h" |
| 21 #include "content/public/test/mock_download_item.h" | 22 #include "content/public/test/mock_download_item.h" |
| 22 #include "testing/gmock/include/gmock/gmock.h" | 23 #include "testing/gmock/include/gmock/gmock.h" |
| 23 #include "testing/gtest/include/gtest/gtest.h" | 24 #include "testing/gtest/include/gtest/gtest.h" |
| 25 #include "ui/base/ui_base_switches.h" | |
| 24 #include "url/gurl.h" | 26 #include "url/gurl.h" |
| 25 | 27 |
| 26 using ::testing::_; | 28 using ::testing::_; |
| 27 using ::testing::ByRef; | 29 using ::testing::ByRef; |
| 28 using ::testing::Eq; | 30 using ::testing::Eq; |
| 29 using ::testing::Return; | 31 using ::testing::Return; |
| 30 using ::testing::ReturnRef; | 32 using ::testing::ReturnRef; |
| 31 using ::testing::SaveArg; | 33 using ::testing::SaveArg; |
| 32 using safe_browsing::ClientDownloadResponse; | |
| 33 using safe_browsing::ClientSafeBrowsingReportRequest; | |
| 34 using safe_browsing::SafeBrowsingService; | |
| 35 | 34 |
| 36 namespace safe_browsing { | 35 namespace safe_browsing { |
| 37 | 36 |
| 37 namespace { | |
| 38 | |
| 38 const char kTestDownloadUrl[] = "http://evildownload.com"; | 39 const char kTestDownloadUrl[] = "http://evildownload.com"; |
| 39 const char kDownloadResponseToken[] = "default_token"; | 40 const char kDownloadResponseToken[] = "default_token"; |
| 40 | 41 |
| 41 class DownloadDangerPromptTest : public InProcessBrowserTest { | 42 // An enum to parameterize the tests so that the tests can be run with and |
| 43 // without the secondary-ui-md flag. | |
| 44 enum class SecondaryUiMd { | |
| 45 ENABLED, | |
| 46 DISABLED, | |
| 47 }; | |
| 48 | |
| 49 // Functor to generate the test name suffix depending on the value of the | |
|
tapted
2017/01/25 00:32:17
Can this be a regular function? See e.g. https://c
karandeepb
2017/01/25 00:51:51
Done.
| |
| 50 // SecondaryUiMd param. | |
| 51 struct PrintSecondaryUiMdStatus { | |
| 52 template <class ParamType> | |
| 53 std::string operator()(const testing::TestParamInfo<ParamType>& info) const { | |
| 54 switch (info.param) { | |
| 55 case SecondaryUiMd::ENABLED: | |
| 56 return "SecondaryUiMdEnabled"; | |
| 57 case SecondaryUiMd::DISABLED: | |
| 58 return "SecondaryUiMdDisabled"; | |
| 59 } | |
| 60 NOTREACHED(); | |
| 61 return std::string(); | |
| 62 } | |
| 63 }; | |
| 64 | |
| 65 } // namespace | |
| 66 | |
| 67 class DownloadDangerPromptTest | |
| 68 : public InProcessBrowserTest, | |
| 69 public ::testing::WithParamInterface<SecondaryUiMd> { | |
| 42 public: | 70 public: |
| 43 DownloadDangerPromptTest() | 71 DownloadDangerPromptTest() |
| 44 : prompt_(nullptr), | 72 : prompt_(nullptr), |
| 45 expected_action_(DownloadDangerPrompt::CANCEL), | 73 expected_action_(DownloadDangerPrompt::CANCEL), |
| 46 did_receive_callback_(false), | 74 did_receive_callback_(false), |
| 47 test_safe_browsing_factory_( | 75 test_safe_browsing_factory_(new TestSafeBrowsingServiceFactory()) {} |
|
tapted
2017/01/25 00:32:17
nit: (since you're changing it), I think the prefe
karandeepb
2017/01/25 00:51:51
Done.
| |
| 48 new safe_browsing::TestSafeBrowsingServiceFactory()) {} | |
| 49 | 76 |
| 50 ~DownloadDangerPromptTest() override {} | 77 ~DownloadDangerPromptTest() override {} |
| 51 | 78 |
| 52 void SetUp() override { | 79 void SetUp() override { |
| 53 SafeBrowsingService::RegisterFactory(test_safe_browsing_factory_.get()); | 80 SafeBrowsingService::RegisterFactory(test_safe_browsing_factory_.get()); |
| 54 InProcessBrowserTest::SetUp(); | 81 InProcessBrowserTest::SetUp(); |
| 55 } | 82 } |
| 56 | 83 |
| 57 void TearDown() override { | 84 void TearDown() override { |
| 58 SafeBrowsingService::RegisterFactory(nullptr); | 85 SafeBrowsingService::RegisterFactory(nullptr); |
| 59 InProcessBrowserTest::TearDown(); | 86 InProcessBrowserTest::TearDown(); |
| 60 } | 87 } |
| 61 | 88 |
| 89 void SetUpCommandLine(base::CommandLine* command_line) override { | |
| 90 // TODO(crbug.com/630357): Remove when secondary-ui-md is enabled by default | |
|
tapted
2017/01/25 00:32:17
I don't think we need the TODO. When that happens,
karandeepb
2017/01/25 00:51:51
Done.
| |
| 91 // on all platforms. | |
| 92 if (GetParam() == SecondaryUiMd::ENABLED) | |
| 93 command_line->AppendSwitch(switches::kExtendMdToSecondaryUi); | |
|
karandeepb
2017/01/24 09:17:21
Tried using MaterialDesignControllerTestApi but it
tapted
2017/01/25 00:32:17
This is good - that's how I've been doing it
| |
| 94 } | |
| 95 | |
| 62 // Opens a new tab and waits for navigations to finish. If there are pending | 96 // Opens a new tab and waits for navigations to finish. If there are pending |
| 63 // navigations, the constrained prompt might be dismissed when the navigation | 97 // navigations, the constrained prompt might be dismissed when the navigation |
| 64 // completes. | 98 // completes. |
| 65 void OpenNewTab() { | 99 void OpenNewTab() { |
| 66 ui_test_utils::NavigateToURLWithDisposition( | 100 ui_test_utils::NavigateToURLWithDisposition( |
| 67 browser(), GURL("about:blank"), | 101 browser(), GURL("about:blank"), |
| 68 WindowOpenDisposition::NEW_FOREGROUND_TAB, | 102 WindowOpenDisposition::NEW_FOREGROUND_TAB, |
| 69 ui_test_utils::BROWSER_TEST_WAIT_FOR_TAB | | 103 ui_test_utils::BROWSER_TEST_WAIT_FOR_TAB | |
| 70 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION); | 104 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION); |
| 71 } | 105 } |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 116 | 150 |
| 117 DownloadDangerPrompt* prompt() { return prompt_; } | 151 DownloadDangerPrompt* prompt() { return prompt_; } |
| 118 | 152 |
| 119 private: | 153 private: |
| 120 void SetUpDownloadItemExpectations( | 154 void SetUpDownloadItemExpectations( |
| 121 const content::DownloadDangerType& danger_type, | 155 const content::DownloadDangerType& danger_type, |
| 122 const std::string& token) { | 156 const std::string& token) { |
| 123 EXPECT_CALL(download_, GetFileNameToReportUser()).WillRepeatedly(Return( | 157 EXPECT_CALL(download_, GetFileNameToReportUser()).WillRepeatedly(Return( |
| 124 base::FilePath(FILE_PATH_LITERAL("evil.exe")))); | 158 base::FilePath(FILE_PATH_LITERAL("evil.exe")))); |
| 125 EXPECT_CALL(download_, GetDangerType()).WillRepeatedly(Return(danger_type)); | 159 EXPECT_CALL(download_, GetDangerType()).WillRepeatedly(Return(danger_type)); |
| 126 safe_browsing::DownloadProtectionService::DownloadPingToken* token_obj | 160 DownloadProtectionService::DownloadPingToken* token_obj = |
| 127 = new safe_browsing::DownloadProtectionService::DownloadPingToken( | 161 new DownloadProtectionService::DownloadPingToken(token); |
| 128 token); | 162 download_.SetUserData(DownloadProtectionService::kDownloadPingTokenKey, |
| 129 download_.SetUserData( | 163 token_obj); |
| 130 safe_browsing::DownloadProtectionService::kDownloadPingTokenKey, | |
| 131 token_obj); | |
| 132 } | 164 } |
| 133 | 165 |
| 134 void SetUpSafeBrowsingReportExpectations( | 166 void SetUpSafeBrowsingReportExpectations( |
| 135 bool did_proceed, | 167 bool did_proceed, |
| 136 const ClientDownloadResponse::Verdict& download_verdict, | 168 const ClientDownloadResponse::Verdict& download_verdict, |
| 137 const std::string& token, | 169 const std::string& token, |
| 138 bool from_download_api) { | 170 bool from_download_api) { |
| 139 ClientSafeBrowsingReportRequest expected_report; | 171 ClientSafeBrowsingReportRequest expected_report; |
| 140 expected_report.set_url(GURL(kTestDownloadUrl).spec()); | 172 expected_report.set_url(GURL(kTestDownloadUrl).spec()); |
| 141 if (from_download_api) | 173 if (from_download_api) |
| (...skipping 23 matching lines...) Expand all Loading... | |
| 165 EXPECT_FALSE(did_receive_callback_); | 197 EXPECT_FALSE(did_receive_callback_); |
| 166 EXPECT_EQ(expected_action_, action); | 198 EXPECT_EQ(expected_action_, action); |
| 167 did_receive_callback_ = true; | 199 did_receive_callback_ = true; |
| 168 prompt_ = nullptr; | 200 prompt_ = nullptr; |
| 169 } | 201 } |
| 170 | 202 |
| 171 content::MockDownloadItem download_; | 203 content::MockDownloadItem download_; |
| 172 DownloadDangerPrompt* prompt_; | 204 DownloadDangerPrompt* prompt_; |
| 173 DownloadDangerPrompt::Action expected_action_; | 205 DownloadDangerPrompt::Action expected_action_; |
| 174 bool did_receive_callback_; | 206 bool did_receive_callback_; |
| 175 std::unique_ptr<safe_browsing::TestSafeBrowsingServiceFactory> | 207 std::unique_ptr<TestSafeBrowsingServiceFactory> test_safe_browsing_factory_; |
| 176 test_safe_browsing_factory_; | |
| 177 std::string expected_serialized_report_; | 208 std::string expected_serialized_report_; |
| 178 | 209 |
| 179 DISALLOW_COPY_AND_ASSIGN(DownloadDangerPromptTest); | 210 DISALLOW_COPY_AND_ASSIGN(DownloadDangerPromptTest); |
| 180 }; | 211 }; |
| 181 | 212 |
| 182 // Disabled for flaky timeouts on Windows. crbug.com/446696 | 213 // Disabled for flaky timeouts on Windows. crbug.com/446696 |
| 183 #if defined(OS_WIN) | 214 #if defined(OS_WIN) |
| 184 #define MAYBE_TestAll DISABLED_TestAll | 215 #define MAYBE_TestAll DISABLED_TestAll |
| 185 #else | 216 #else |
| 186 #define MAYBE_TestAll TestAll | 217 #define MAYBE_TestAll TestAll |
| 187 #endif | 218 #endif |
| 188 IN_PROC_BROWSER_TEST_F(DownloadDangerPromptTest, MAYBE_TestAll) { | 219 IN_PROC_BROWSER_TEST_P(DownloadDangerPromptTest, MAYBE_TestAll) { |
| 189 // ExperienceSampling: Set default actions for DownloadItem methods we need. | 220 // ExperienceSampling: Set default actions for DownloadItem methods we need. |
| 190 GURL download_url(kTestDownloadUrl); | 221 GURL download_url(kTestDownloadUrl); |
| 191 ON_CALL(download(), GetURL()).WillByDefault(ReturnRef(download_url)); | 222 ON_CALL(download(), GetURL()).WillByDefault(ReturnRef(download_url)); |
| 192 ON_CALL(download(), GetReferrerUrl()) | 223 ON_CALL(download(), GetReferrerUrl()) |
| 193 .WillByDefault(ReturnRef(GURL::EmptyGURL())); | 224 .WillByDefault(ReturnRef(GURL::EmptyGURL())); |
| 194 ON_CALL(download(), GetBrowserContext()) | 225 ON_CALL(download(), GetBrowserContext()) |
| 195 .WillByDefault(Return(browser()->profile())); | 226 .WillByDefault(Return(browser()->profile())); |
| 196 base::FilePath empty_file_path; | 227 base::FilePath empty_file_path; |
| 197 ON_CALL(download(), GetTargetFilePath()) | 228 ON_CALL(download(), GetTargetFilePath()) |
| 198 .WillByDefault(ReturnRef(empty_file_path)); | 229 .WillByDefault(ReturnRef(empty_file_path)); |
| (...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 295 SetUpExpectations(DownloadDangerPrompt::CANCEL, | 326 SetUpExpectations(DownloadDangerPrompt::CANCEL, |
| 296 content::DOWNLOAD_DANGER_TYPE_UNCOMMON_CONTENT, | 327 content::DOWNLOAD_DANGER_TYPE_UNCOMMON_CONTENT, |
| 297 ClientDownloadResponse::UNCOMMON, | 328 ClientDownloadResponse::UNCOMMON, |
| 298 std::string(), | 329 std::string(), |
| 299 true); | 330 true); |
| 300 EXPECT_CALL(download(), IsDangerous()).WillRepeatedly(Return(true)); | 331 EXPECT_CALL(download(), IsDangerous()).WillRepeatedly(Return(true)); |
| 301 SimulatePromptAction(DownloadDangerPrompt::CANCEL); | 332 SimulatePromptAction(DownloadDangerPrompt::CANCEL); |
| 302 VerifyExpectations(true); | 333 VerifyExpectations(true); |
| 303 } | 334 } |
| 304 | 335 |
| 336 // Prefix for test instantiations intentionally left blank since the test | |
| 337 // fixture class has a single parameterization. | |
| 338 INSTANTIATE_TEST_CASE_P(, | |
| 339 DownloadDangerPromptTest, | |
| 340 ::testing::Values(SecondaryUiMd::ENABLED, | |
| 341 SecondaryUiMd::DISABLED), | |
| 342 PrintSecondaryUiMdStatus()); | |
| 343 | |
| 305 } // namespace safe_browsing | 344 } // namespace safe_browsing |
| OLD | NEW |