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/files/file_path.h" | 6 #include "base/files/file_path.h" |
| 7 #include "chrome/browser/download/download_danger_prompt.h" | 7 #include "chrome/browser/download/download_danger_prompt.h" |
| 8 #include "chrome/browser/profiles/profile.h" | 8 #include "chrome/browser/profiles/profile.h" |
| 9 #include "chrome/browser/ui/browser.h" | 9 #include "chrome/browser/ui/browser.h" |
| 10 #include "chrome/browser/ui/browser_commands.h" | 10 #include "chrome/browser/ui/browser_commands.h" |
| 11 #include "chrome/browser/ui/browser_tabstrip.h" | 11 #include "chrome/browser/ui/browser_tabstrip.h" |
| 12 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 12 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 13 #include "chrome/common/safe_browsing/csd.pb.h" | |
| 13 #include "chrome/test/base/in_process_browser_test.h" | 14 #include "chrome/test/base/in_process_browser_test.h" |
| 14 #include "chrome/test/base/ui_test_utils.h" | 15 #include "chrome/test/base/ui_test_utils.h" |
| 15 #include "content/public/test/mock_download_item.h" | 16 #include "content/public/test/mock_download_item.h" |
| 16 #include "testing/gmock/include/gmock/gmock.h" | 17 #include "testing/gmock/include/gmock/gmock.h" |
| 17 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
| 18 #include "url/gurl.h" | 19 #include "url/gurl.h" |
| 19 | 20 |
| 20 using ::testing::_; | 21 using ::testing::_; |
| 21 using ::testing::ByRef; | 22 using ::testing::ByRef; |
| 22 using ::testing::Eq; | 23 using ::testing::Eq; |
| 23 using ::testing::Return; | 24 using ::testing::Return; |
| 24 using ::testing::ReturnRef; | 25 using ::testing::ReturnRef; |
| 25 using ::testing::SaveArg; | 26 using ::testing::SaveArg; |
| 27 using safe_browsing::ClientSafeBrowsingReportRequest; | |
| 26 | 28 |
| 27 class DownloadDangerPromptTest : public InProcessBrowserTest { | 29 class DownloadDangerPromptTest : public InProcessBrowserTest { |
| 28 public: | 30 public: |
| 29 DownloadDangerPromptTest() | 31 DownloadDangerPromptTest() |
| 30 : prompt_(NULL), | 32 : prompt_(NULL), |
| 31 expected_action_(DownloadDangerPrompt::CANCEL), | 33 expected_action_(DownloadDangerPrompt::CANCEL), |
| 32 did_receive_callback_(false) { | 34 did_receive_callback_(false), |
| 33 } | 35 report_sent_(false) {} |
| 34 | 36 |
| 35 ~DownloadDangerPromptTest() override {} | 37 ~DownloadDangerPromptTest() override {} |
| 36 | 38 |
| 37 // Opens a new tab and waits for navigations to finish. If there are pending | 39 // Opens a new tab and waits for navigations to finish. If there are pending |
| 38 // navigations, the constrained prompt might be dismissed when the navigation | 40 // navigations, the constrained prompt might be dismissed when the navigation |
| 39 // completes. | 41 // completes. |
| 40 void OpenNewTab() { | 42 void OpenNewTab() { |
| 41 ui_test_utils::NavigateToURLWithDisposition( | 43 ui_test_utils::NavigateToURLWithDisposition( |
| 42 browser(), GURL("about:blank"), | 44 browser(), GURL("about:blank"), |
| 43 NEW_FOREGROUND_TAB, | 45 NEW_FOREGROUND_TAB, |
| 44 ui_test_utils::BROWSER_TEST_WAIT_FOR_TAB | | 46 ui_test_utils::BROWSER_TEST_WAIT_FOR_TAB | |
| 45 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION); | 47 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION); |
| 46 } | 48 } |
| 47 | 49 |
| 48 void SetUpExpectations(DownloadDangerPrompt::Action expected_action) { | 50 void SetUpExpectations(DownloadDangerPrompt::Action expected_action) { |
| 49 did_receive_callback_ = false; | 51 did_receive_callback_ = false; |
| 50 expected_action_ = expected_action; | 52 expected_action_ = expected_action; |
| 53 report_sent_ = false; | |
| 54 expected_safe_browsing_report_.set_url(download_.GetURL().spec()); | |
| 55 expected_safe_browsing_report_.set_type( | |
| 56 ClientSafeBrowsingReportRequest::MALICIOUS_DOWNLOAD_RECOVERY); | |
| 57 expected_safe_browsing_report_.set_did_proceed( | |
| 58 expected_action == DownloadDangerPrompt::ACCEPT); | |
| 51 SetUpDownloadItemExpectations(); | 59 SetUpDownloadItemExpectations(); |
| 52 CreatePrompt(); | 60 CreatePrompt(); |
| 53 } | 61 } |
| 54 | 62 |
| 55 void VerifyExpectations() { | 63 void VerifyExpectations() { |
| 56 content::RunAllPendingInMessageLoop(); | 64 content::RunAllPendingInMessageLoop(); |
| 57 // At the end of each test, we expect no more activity from the prompt. The | 65 // At the end of each test, we expect no more activity from the prompt. The |
| 58 // prompt shouldn't exist anymore either. | 66 // prompt shouldn't exist anymore either. |
| 59 EXPECT_TRUE(did_receive_callback_); | 67 EXPECT_TRUE(did_receive_callback_); |
| 60 EXPECT_FALSE(prompt_); | 68 EXPECT_FALSE(prompt_); |
| 61 testing::Mock::VerifyAndClearExpectations(&download_); | 69 testing::Mock::VerifyAndClearExpectations(&download_); |
| 70 if (report_sent_) { | |
| 71 std::string expected_report_string; | |
| 72 expected_safe_browsing_report_.SerializeToString(&expected_report_string); | |
| 73 EXPECT_EQ(expected_report_string, safe_browsing_report_); | |
| 74 } | |
| 62 } | 75 } |
| 63 | 76 |
| 64 void SimulatePromptAction(DownloadDangerPrompt::Action action) { | 77 void SimulatePromptAction(DownloadDangerPrompt::Action action) { |
| 65 prompt_->InvokeActionForTesting(action); | 78 safe_browsing_report_ = |
| 79 prompt()->InvokeActionForTesting(action, download().GetURL()); | |
| 80 // If download_ changes to safe, prompt will be dismiss instead. | |
| 81 if (!download().IsDangerous()) { | |
|
asanka
2015/11/18 21:23:23
Nit: no braces
Jialiu Lin
2015/11/23 17:57:53
Done.
| |
| 82 expected_safe_browsing_report_.set_did_proceed(false); | |
| 83 } | |
| 84 report_sent_ = true; | |
| 66 } | 85 } |
| 67 | 86 |
| 68 content::MockDownloadItem& download() { return download_; } | 87 content::MockDownloadItem& download() { return download_; } |
| 69 | 88 |
| 70 DownloadDangerPrompt* prompt() { return prompt_; } | 89 DownloadDangerPrompt* prompt() { return prompt_; } |
| 71 | 90 |
| 72 private: | 91 private: |
| 73 void SetUpDownloadItemExpectations() { | 92 void SetUpDownloadItemExpectations() { |
| 74 EXPECT_CALL(download_, GetFileNameToReportUser()).WillRepeatedly(Return( | 93 EXPECT_CALL(download_, GetFileNameToReportUser()).WillRepeatedly(Return( |
| 75 base::FilePath(FILE_PATH_LITERAL("evil.exe")))); | 94 base::FilePath(FILE_PATH_LITERAL("evil.exe")))); |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 90 EXPECT_FALSE(did_receive_callback_); | 109 EXPECT_FALSE(did_receive_callback_); |
| 91 EXPECT_EQ(expected_action_, action); | 110 EXPECT_EQ(expected_action_, action); |
| 92 did_receive_callback_ = true; | 111 did_receive_callback_ = true; |
| 93 prompt_ = NULL; | 112 prompt_ = NULL; |
| 94 } | 113 } |
| 95 | 114 |
| 96 content::MockDownloadItem download_; | 115 content::MockDownloadItem download_; |
| 97 DownloadDangerPrompt* prompt_; | 116 DownloadDangerPrompt* prompt_; |
| 98 DownloadDangerPrompt::Action expected_action_; | 117 DownloadDangerPrompt::Action expected_action_; |
| 99 bool did_receive_callback_; | 118 bool did_receive_callback_; |
| 119 std::string safe_browsing_report_; | |
| 120 ClientSafeBrowsingReportRequest expected_safe_browsing_report_; | |
| 121 bool report_sent_; | |
| 100 | 122 |
| 101 DISALLOW_COPY_AND_ASSIGN(DownloadDangerPromptTest); | 123 DISALLOW_COPY_AND_ASSIGN(DownloadDangerPromptTest); |
| 102 }; | 124 }; |
| 103 | 125 |
| 104 // Disabled for flaky timeouts on Windows. crbug.com/446696 | 126 // Disabled for flaky timeouts on Windows. crbug.com/446696 |
| 105 #if defined(OS_WIN) | 127 #if defined(OS_WIN) |
| 106 #define MAYBE_TestAll DISABLED_TestAll | 128 #define MAYBE_TestAll DISABLED_TestAll |
| 107 #else | 129 #else |
| 108 #define MAYBE_TestAll TestAll | 130 #define MAYBE_TestAll TestAll |
| 109 #endif | 131 #endif |
| 110 IN_PROC_BROWSER_TEST_F(DownloadDangerPromptTest, MAYBE_TestAll) { | 132 IN_PROC_BROWSER_TEST_F(DownloadDangerPromptTest, MAYBE_TestAll) { |
| 111 // ExperienceSampling: Set default actions for DownloadItem methods we need. | 133 // ExperienceSampling: Set default actions for DownloadItem methods we need. |
| 134 // Intentionally makes download() url empty, such that no actual report will | |
| 135 // be sent to server side. | |
| 112 ON_CALL(download(), GetURL()).WillByDefault(ReturnRef(GURL::EmptyGURL())); | 136 ON_CALL(download(), GetURL()).WillByDefault(ReturnRef(GURL::EmptyGURL())); |
| 113 ON_CALL(download(), GetReferrerUrl()) | 137 ON_CALL(download(), GetReferrerUrl()) |
| 114 .WillByDefault(ReturnRef(GURL::EmptyGURL())); | 138 .WillByDefault(ReturnRef(GURL::EmptyGURL())); |
| 115 ON_CALL(download(), GetBrowserContext()) | 139 ON_CALL(download(), GetBrowserContext()) |
| 116 .WillByDefault(Return(browser()->profile())); | 140 .WillByDefault(Return(browser()->profile())); |
| 141 ON_CALL(download(), IsDangerous()).WillByDefault(Return(true)); | |
| 142 ON_CALL(download(), IsDone()).WillByDefault(Return(true)); | |
| 117 | 143 |
| 118 OpenNewTab(); | 144 OpenNewTab(); |
| 119 | 145 |
| 120 // Clicking the Accept button should invoke the ACCEPT action. | 146 // Clicking the Accept button should invoke the ACCEPT action. |
| 121 SetUpExpectations(DownloadDangerPrompt::ACCEPT); | 147 SetUpExpectations(DownloadDangerPrompt::ACCEPT); |
| 122 SimulatePromptAction(DownloadDangerPrompt::ACCEPT); | 148 SimulatePromptAction(DownloadDangerPrompt::ACCEPT); |
| 123 VerifyExpectations(); | 149 VerifyExpectations(); |
| 124 | 150 |
| 125 // Clicking the Cancel button should invoke the CANCEL action. | 151 // Clicking the Cancel button should invoke the CANCEL action. |
| 126 SetUpExpectations(DownloadDangerPrompt::CANCEL); | 152 SetUpExpectations(DownloadDangerPrompt::CANCEL); |
| 127 SimulatePromptAction(DownloadDangerPrompt::CANCEL); | 153 SimulatePromptAction(DownloadDangerPrompt::CANCEL); |
| 128 VerifyExpectations(); | 154 VerifyExpectations(); |
| 129 | 155 |
| 130 // If the download is no longer dangerous (because it was accepted), the | 156 // If the download is no longer dangerous (because it was accepted), the |
| 131 // dialog should DISMISS itself. | 157 // dialog should DISMISS itself. |
| 132 SetUpExpectations(DownloadDangerPrompt::DISMISS); | 158 SetUpExpectations(DownloadDangerPrompt::DISMISS); |
| 133 EXPECT_CALL(download(), IsDangerous()).WillOnce(Return(false)); | 159 EXPECT_CALL(download(), IsDangerous()).WillOnce(Return(false)); |
| 134 download().NotifyObserversDownloadUpdated(); | 160 download().NotifyObserversDownloadUpdated(); |
| 135 VerifyExpectations(); | 161 VerifyExpectations(); |
| 136 | 162 |
| 137 // If the download is in a terminal state then the dialog should DISMISS | 163 // If the download is in a terminal state then the dialog should DISMISS |
| 138 // itself. | 164 // itself. |
| 139 SetUpExpectations(DownloadDangerPrompt::DISMISS); | 165 SetUpExpectations(DownloadDangerPrompt::DISMISS); |
| 140 EXPECT_CALL(download(), IsDangerous()).WillOnce(Return(true)); | |
| 141 EXPECT_CALL(download(), IsDone()).WillOnce(Return(true)); | 166 EXPECT_CALL(download(), IsDone()).WillOnce(Return(true)); |
| 142 download().NotifyObserversDownloadUpdated(); | 167 download().NotifyObserversDownloadUpdated(); |
| 143 VerifyExpectations(); | 168 VerifyExpectations(); |
| 144 | 169 |
| 145 // If the download is dangerous and is not in a terminal state, don't dismiss | 170 // If the download is dangerous and is not in a terminal state, don't dismiss |
| 146 // the dialog. | 171 // the dialog. |
| 147 SetUpExpectations(DownloadDangerPrompt::ACCEPT); | 172 SetUpExpectations(DownloadDangerPrompt::ACCEPT); |
| 148 EXPECT_CALL(download(), IsDangerous()).WillOnce(Return(true)); | |
| 149 EXPECT_CALL(download(), IsDone()).WillOnce(Return(false)); | 173 EXPECT_CALL(download(), IsDone()).WillOnce(Return(false)); |
| 150 download().NotifyObserversDownloadUpdated(); | 174 download().NotifyObserversDownloadUpdated(); |
| 151 SimulatePromptAction(DownloadDangerPrompt::ACCEPT); | 175 SimulatePromptAction(DownloadDangerPrompt::ACCEPT); |
| 152 VerifyExpectations(); | 176 VerifyExpectations(); |
| 153 | 177 |
| 154 // If the containing tab is closed, the dialog should DISMISS itself. | 178 // If the containing tab is closed, the dialog should DISMISS itself. |
| 155 OpenNewTab(); | 179 OpenNewTab(); |
| 156 SetUpExpectations(DownloadDangerPrompt::DISMISS); | 180 SetUpExpectations(DownloadDangerPrompt::DISMISS); |
| 157 chrome::CloseTab(browser()); | 181 chrome::CloseTab(browser()); |
| 158 VerifyExpectations(); | 182 VerifyExpectations(); |
| 159 } | 183 } |
| OLD | NEW |