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
3 // found in the LICENSE file.
6 #include "base/files/file_path.h"
7 #include "chrome/browser/download/download_danger_prompt.h"
8 #include "chrome/browser/profiles/profile.h"
9 #include "chrome/browser/ui/browser.h"
10 #include "chrome/browser/ui/browser_commands.h"
11 #include "chrome/browser/ui/browser_tabstrip.h"
12 #include "chrome/browser/ui/tabs/tab_strip_model.h"
13 #include "chrome/test/base/in_process_browser_test.h"
14 #include "chrome/test/base/ui_test_utils.h"
15 #include "content/public/test/mock_download_item.h"
16 #include "testing/gmock/include/gmock/gmock.h"
17 #include "testing/gtest/include/gtest/gtest.h"
21 using ::testing::ByRef
;
23 using ::testing::Return
;
24 using ::testing::ReturnRef
;
25 using ::testing::SaveArg
;
27 class DownloadDangerPromptTest
: public InProcessBrowserTest
{
29 DownloadDangerPromptTest()
31 expected_action_(DownloadDangerPrompt::CANCEL
),
32 did_receive_callback_(false) {
35 ~DownloadDangerPromptTest() override
{}
37 // 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
41 ui_test_utils::NavigateToURLWithDisposition(
42 browser(), GURL("about:blank"),
44 ui_test_utils::BROWSER_TEST_WAIT_FOR_TAB
|
45 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION
);
48 void SetUpExpectations(DownloadDangerPrompt::Action expected_action
) {
49 did_receive_callback_
= false;
50 expected_action_
= expected_action
;
51 SetUpDownloadItemExpectations();
55 void VerifyExpectations() {
56 content::RunAllPendingInMessageLoop();
57 // At the end of each test, we expect no more activity from the prompt. The
58 // prompt shouldn't exist anymore either.
59 EXPECT_TRUE(did_receive_callback_
);
60 EXPECT_FALSE(prompt_
);
61 testing::Mock::VerifyAndClearExpectations(&download_
);
64 void SimulatePromptAction(DownloadDangerPrompt::Action action
) {
65 prompt_
->InvokeActionForTesting(action
);
68 content::MockDownloadItem
& download() { return download_
; }
70 DownloadDangerPrompt
* prompt() { return prompt_
; }
73 void SetUpDownloadItemExpectations() {
74 EXPECT_CALL(download_
, GetFileNameToReportUser()).WillRepeatedly(Return(
75 base::FilePath(FILE_PATH_LITERAL("evil.exe"))));
76 EXPECT_CALL(download_
, GetDangerType())
77 .WillRepeatedly(Return(content::DOWNLOAD_DANGER_TYPE_DANGEROUS_URL
));
81 prompt_
= DownloadDangerPrompt::Create(
83 browser()->tab_strip_model()->GetActiveWebContents(),
85 base::Bind(&DownloadDangerPromptTest::PromptCallback
, this));
86 content::RunAllPendingInMessageLoop();
89 void PromptCallback(DownloadDangerPrompt::Action action
) {
90 EXPECT_FALSE(did_receive_callback_
);
91 EXPECT_EQ(expected_action_
, action
);
92 did_receive_callback_
= true;
96 content::MockDownloadItem download_
;
97 DownloadDangerPrompt
* prompt_
;
98 DownloadDangerPrompt::Action expected_action_
;
99 bool did_receive_callback_
;
101 DISALLOW_COPY_AND_ASSIGN(DownloadDangerPromptTest
);
104 // Disabled for flaky timeouts on Windows. crbug.com/446696
106 #define MAYBE_TestAll DISABLED_TestAll
108 #define MAYBE_TestAll TestAll
110 IN_PROC_BROWSER_TEST_F(DownloadDangerPromptTest
, MAYBE_TestAll
) {
111 // ExperienceSampling: Set default actions for DownloadItem methods we need.
112 ON_CALL(download(), GetURL()).WillByDefault(ReturnRef(GURL::EmptyGURL()));
113 ON_CALL(download(), GetReferrerUrl())
114 .WillByDefault(ReturnRef(GURL::EmptyGURL()));
115 ON_CALL(download(), GetBrowserContext())
116 .WillByDefault(Return(browser()->profile()));
120 // Clicking the Accept button should invoke the ACCEPT action.
121 SetUpExpectations(DownloadDangerPrompt::ACCEPT
);
122 SimulatePromptAction(DownloadDangerPrompt::ACCEPT
);
123 VerifyExpectations();
125 // Clicking the Cancel button should invoke the CANCEL action.
126 SetUpExpectations(DownloadDangerPrompt::CANCEL
);
127 SimulatePromptAction(DownloadDangerPrompt::CANCEL
);
128 VerifyExpectations();
130 // If the download is no longer dangerous (because it was accepted), the
131 // dialog should DISMISS itself.
132 SetUpExpectations(DownloadDangerPrompt::DISMISS
);
133 EXPECT_CALL(download(), IsDangerous()).WillOnce(Return(false));
134 download().NotifyObserversDownloadUpdated();
135 VerifyExpectations();
137 // If the download is in a terminal state then the dialog should DISMISS
139 SetUpExpectations(DownloadDangerPrompt::DISMISS
);
140 EXPECT_CALL(download(), IsDangerous()).WillOnce(Return(true));
141 EXPECT_CALL(download(), IsDone()).WillOnce(Return(true));
142 download().NotifyObserversDownloadUpdated();
143 VerifyExpectations();
145 // If the download is dangerous and is not in a terminal state, don't dismiss
147 SetUpExpectations(DownloadDangerPrompt::ACCEPT
);
148 EXPECT_CALL(download(), IsDangerous()).WillOnce(Return(true));
149 EXPECT_CALL(download(), IsDone()).WillOnce(Return(false));
150 download().NotifyObserversDownloadUpdated();
151 SimulatePromptAction(DownloadDangerPrompt::ACCEPT
);
152 VerifyExpectations();
154 // If the containing tab is closed, the dialog should DISMISS itself.
156 SetUpExpectations(DownloadDangerPrompt::DISMISS
);
157 chrome::CloseTab(browser());
158 VerifyExpectations();