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/ui/browser.h" | 8 #include "chrome/browser/ui/browser.h" |
9 #include "chrome/browser/ui/browser_commands.h" | 9 #include "chrome/browser/ui/browser_commands.h" |
10 #include "chrome/browser/ui/browser_tabstrip.h" | 10 #include "chrome/browser/ui/browser_tabstrip.h" |
11 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 11 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
12 #include "chrome/test/base/in_process_browser_test.h" | 12 #include "chrome/test/base/in_process_browser_test.h" |
13 #include "chrome/test/base/ui_test_utils.h" | 13 #include "chrome/test/base/ui_test_utils.h" |
14 #include "content/public/test/mock_download_item.h" | 14 #include "content/public/test/mock_download_item.h" |
15 #include "testing/gmock/include/gmock/gmock.h" | 15 #include "testing/gmock/include/gmock/gmock.h" |
16 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
17 | 17 |
18 using ::testing::_; | 18 using ::testing::_; |
19 using ::testing::ByRef; | 19 using ::testing::ByRef; |
20 using ::testing::Eq; | 20 using ::testing::Eq; |
21 using ::testing::Return; | 21 using ::testing::Return; |
22 using ::testing::SaveArg; | 22 using ::testing::SaveArg; |
23 | 23 |
24 class DownloadDangerPromptTest : public InProcessBrowserTest { | 24 class DownloadDangerPromptTest : public InProcessBrowserTest { |
25 public: | 25 public: |
26 DownloadDangerPromptTest() | 26 DownloadDangerPromptTest() |
27 : download_observer_(NULL), | 27 : prompt_(NULL), |
28 prompt_(NULL), | |
29 expected_action_(DownloadDangerPrompt::CANCEL), | 28 expected_action_(DownloadDangerPrompt::CANCEL), |
30 did_receive_callback_(false) { | 29 did_receive_callback_(false) { |
31 } | 30 } |
32 | 31 |
33 virtual ~DownloadDangerPromptTest() { | 32 virtual ~DownloadDangerPromptTest() { |
34 } | 33 } |
35 | 34 |
36 // Opens a new tab and waits for navigations to finish. If there are pending | 35 // Opens a new tab and waits for navigations to finish. If there are pending |
37 // navigations, the constrained prompt might be dismissed when the navigation | 36 // navigations, the constrained prompt might be dismissed when the navigation |
38 // completes. | 37 // completes. |
(...skipping 20 matching lines...) Expand all Loading... |
59 EXPECT_FALSE(prompt_); | 58 EXPECT_FALSE(prompt_); |
60 testing::Mock::VerifyAndClearExpectations(&download_); | 59 testing::Mock::VerifyAndClearExpectations(&download_); |
61 } | 60 } |
62 | 61 |
63 void SimulatePromptAction(DownloadDangerPrompt::Action action) { | 62 void SimulatePromptAction(DownloadDangerPrompt::Action action) { |
64 prompt_->InvokeActionForTesting(action); | 63 prompt_->InvokeActionForTesting(action); |
65 } | 64 } |
66 | 65 |
67 content::MockDownloadItem& download() { return download_; } | 66 content::MockDownloadItem& download() { return download_; } |
68 | 67 |
69 content::DownloadItem::Observer* download_observer() { | |
70 return download_observer_; | |
71 } | |
72 | |
73 DownloadDangerPrompt* prompt() { return prompt_; } | 68 DownloadDangerPrompt* prompt() { return prompt_; } |
74 | 69 |
75 private: | 70 private: |
76 void SetUpDownloadItemExpectations() { | 71 void SetUpDownloadItemExpectations() { |
77 EXPECT_CALL(download_, GetFileNameToReportUser()).WillRepeatedly(Return( | 72 EXPECT_CALL(download_, GetFileNameToReportUser()).WillRepeatedly(Return( |
78 base::FilePath(FILE_PATH_LITERAL("evil.exe")))); | 73 base::FilePath(FILE_PATH_LITERAL("evil.exe")))); |
79 EXPECT_CALL(download_, AddObserver(_)) | |
80 .WillOnce(SaveArg<0>(&download_observer_)); | |
81 EXPECT_CALL(download_, RemoveObserver(Eq(ByRef(download_observer_)))); | |
82 EXPECT_CALL(download_, GetDangerType()) | 74 EXPECT_CALL(download_, GetDangerType()) |
83 .WillRepeatedly(Return(content::DOWNLOAD_DANGER_TYPE_DANGEROUS_URL)); | 75 .WillRepeatedly(Return(content::DOWNLOAD_DANGER_TYPE_DANGEROUS_URL)); |
84 } | 76 } |
85 | 77 |
86 void CreatePrompt() { | 78 void CreatePrompt() { |
87 prompt_ = DownloadDangerPrompt::Create( | 79 prompt_ = DownloadDangerPrompt::Create( |
88 &download_, | 80 &download_, |
89 browser()->tab_strip_model()->GetActiveWebContents(), | 81 browser()->tab_strip_model()->GetActiveWebContents(), |
90 false, | 82 false, |
91 base::Bind(&DownloadDangerPromptTest::PromptCallback, this)); | 83 base::Bind(&DownloadDangerPromptTest::PromptCallback, this)); |
92 content::RunAllPendingInMessageLoop(); | 84 content::RunAllPendingInMessageLoop(); |
93 } | 85 } |
94 | 86 |
95 void PromptCallback(DownloadDangerPrompt::Action action) { | 87 void PromptCallback(DownloadDangerPrompt::Action action) { |
96 EXPECT_FALSE(did_receive_callback_); | 88 EXPECT_FALSE(did_receive_callback_); |
97 EXPECT_EQ(expected_action_, action); | 89 EXPECT_EQ(expected_action_, action); |
98 did_receive_callback_ = true; | 90 did_receive_callback_ = true; |
99 prompt_ = NULL; | 91 prompt_ = NULL; |
100 } | 92 } |
101 | 93 |
102 content::MockDownloadItem download_; | 94 content::MockDownloadItem download_; |
103 content::DownloadItem::Observer* download_observer_; | |
104 DownloadDangerPrompt* prompt_; | 95 DownloadDangerPrompt* prompt_; |
105 DownloadDangerPrompt::Action expected_action_; | 96 DownloadDangerPrompt::Action expected_action_; |
106 bool did_receive_callback_; | 97 bool did_receive_callback_; |
107 | 98 |
108 DISALLOW_COPY_AND_ASSIGN(DownloadDangerPromptTest); | 99 DISALLOW_COPY_AND_ASSIGN(DownloadDangerPromptTest); |
109 }; | 100 }; |
110 | 101 |
111 IN_PROC_BROWSER_TEST_F(DownloadDangerPromptTest, TestAll) { | 102 IN_PROC_BROWSER_TEST_F(DownloadDangerPromptTest, TestAll) { |
112 OpenNewTab(); | 103 OpenNewTab(); |
113 | 104 |
114 // Clicking the Accept button should invoke the ACCEPT action. | 105 // Clicking the Accept button should invoke the ACCEPT action. |
115 SetUpExpectations(DownloadDangerPrompt::ACCEPT); | 106 SetUpExpectations(DownloadDangerPrompt::ACCEPT); |
116 SimulatePromptAction(DownloadDangerPrompt::ACCEPT); | 107 SimulatePromptAction(DownloadDangerPrompt::ACCEPT); |
117 VerifyExpectations(); | 108 VerifyExpectations(); |
118 | 109 |
119 // Clicking the Cancel button should invoke the CANCEL action. | 110 // Clicking the Cancel button should invoke the CANCEL action. |
120 SetUpExpectations(DownloadDangerPrompt::CANCEL); | 111 SetUpExpectations(DownloadDangerPrompt::CANCEL); |
121 SimulatePromptAction(DownloadDangerPrompt::CANCEL); | 112 SimulatePromptAction(DownloadDangerPrompt::CANCEL); |
122 VerifyExpectations(); | 113 VerifyExpectations(); |
123 | 114 |
124 // If the download is no longer dangerous (because it was accepted), the | 115 // If the download is no longer dangerous (because it was accepted), the |
125 // dialog should DISMISS itself. | 116 // dialog should DISMISS itself. |
126 SetUpExpectations(DownloadDangerPrompt::DISMISS); | 117 SetUpExpectations(DownloadDangerPrompt::DISMISS); |
127 EXPECT_CALL(download(), IsDangerous()).WillOnce(Return(false)); | 118 EXPECT_CALL(download(), IsDangerous()).WillOnce(Return(false)); |
128 download_observer()->OnDownloadUpdated(&download()); | 119 download().UpdateObservers(); |
129 VerifyExpectations(); | 120 VerifyExpectations(); |
130 | 121 |
131 // If the download is in a terminal state then the dialog should DISMISS | 122 // If the download is in a terminal state then the dialog should DISMISS |
132 // itself. | 123 // itself. |
133 SetUpExpectations(DownloadDangerPrompt::DISMISS); | 124 SetUpExpectations(DownloadDangerPrompt::DISMISS); |
134 EXPECT_CALL(download(), IsDangerous()).WillOnce(Return(true)); | 125 EXPECT_CALL(download(), IsDangerous()).WillOnce(Return(true)); |
135 EXPECT_CALL(download(), IsDone()).WillOnce(Return(true)); | 126 EXPECT_CALL(download(), IsDone()).WillOnce(Return(true)); |
136 download_observer()->OnDownloadUpdated(&download()); | 127 download().UpdateObservers(); |
137 VerifyExpectations(); | 128 VerifyExpectations(); |
138 | 129 |
139 // If the download is dangerous and is not in a terminal state, don't dismiss | 130 // If the download is dangerous and is not in a terminal state, don't dismiss |
140 // the dialog. | 131 // the dialog. |
141 SetUpExpectations(DownloadDangerPrompt::ACCEPT); | 132 SetUpExpectations(DownloadDangerPrompt::ACCEPT); |
142 EXPECT_CALL(download(), IsDangerous()).WillOnce(Return(true)); | 133 EXPECT_CALL(download(), IsDangerous()).WillOnce(Return(true)); |
143 EXPECT_CALL(download(), IsDone()).WillOnce(Return(false)); | 134 EXPECT_CALL(download(), IsDone()).WillOnce(Return(false)); |
144 download_observer()->OnDownloadUpdated(&download()); | 135 download().UpdateObservers(); |
145 SimulatePromptAction(DownloadDangerPrompt::ACCEPT); | 136 SimulatePromptAction(DownloadDangerPrompt::ACCEPT); |
146 VerifyExpectations(); | 137 VerifyExpectations(); |
147 | 138 |
148 // If the containing tab is closed, the dialog should DISMISS itself. | 139 // If the containing tab is closed, the dialog should DISMISS itself. |
149 OpenNewTab(); | 140 OpenNewTab(); |
150 SetUpExpectations(DownloadDangerPrompt::DISMISS); | 141 SetUpExpectations(DownloadDangerPrompt::DISMISS); |
151 chrome::CloseTab(browser()); | 142 chrome::CloseTab(browser()); |
152 VerifyExpectations(); | 143 VerifyExpectations(); |
153 } | 144 } |
OLD | NEW |