1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
|
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/download/download_item_warning_data.h"
#include <vector>
#include "base/test/task_environment.h"
#include "components/download/public/common/mock_download_item.h"
#include "testing/gtest/include/gtest/gtest.h"
using WarningSurface = DownloadItemWarningData::WarningSurface;
using WarningAction = DownloadItemWarningData::WarningAction;
using WarningActionEvent = DownloadItemWarningData::WarningActionEvent;
using DeepScanTrigger = DownloadItemWarningData::DeepScanTrigger;
using ::testing::Return;
class DownloadItemWarningDataTest : public testing::Test {
public:
DownloadItemWarningDataTest() {
ON_CALL(download_, IsDangerous()).WillByDefault(Return(true));
}
protected:
void FastForwardAndAddEvent(base::TimeDelta time_delta,
WarningSurface surface,
WarningAction action) {
task_environment_.FastForwardBy(time_delta);
DownloadItemWarningData::AddWarningActionEvent(&download_, surface, action);
}
std::vector<WarningActionEvent> GetEvents() {
return DownloadItemWarningData::GetWarningActionEvents(&download_);
}
bool VerifyEventValue(const WarningActionEvent& actual_event,
WarningSurface expected_surface,
WarningAction expected_action,
int64_t expected_latency,
bool expected_is_terminal_action) {
bool success = true;
if (actual_event.surface != expected_surface) {
success = false;
ADD_FAILURE() << "Warning action event should have surface "
<< static_cast<int>(expected_surface) << ", but found "
<< static_cast<int>(actual_event.surface);
}
if (actual_event.action != expected_action) {
success = false;
ADD_FAILURE() << "Warning action event should have action "
<< static_cast<int>(expected_action) << ", but found "
<< static_cast<int>(actual_event.action);
}
if (actual_event.action_latency_msec != expected_latency) {
success = false;
ADD_FAILURE() << "Warning action event should have latency "
<< expected_latency << ", but found "
<< actual_event.action_latency_msec;
}
if (actual_event.is_terminal_action != expected_is_terminal_action) {
success = false;
ADD_FAILURE() << "Warning action event should have is_terminal_action "
<< expected_is_terminal_action << ", but found "
<< actual_event.is_terminal_action;
}
return success;
}
base::test::TaskEnvironment task_environment_{
base::test::TaskEnvironment::TimeSource::MOCK_TIME};
download::MockDownloadItem download_;
};
TEST_F(DownloadItemWarningDataTest, GetEvents) {
FastForwardAndAddEvent(base::Seconds(0), WarningSurface::BUBBLE_MAINPAGE,
WarningAction::SHOWN);
FastForwardAndAddEvent(base::Seconds(5), WarningSurface::BUBBLE_SUBPAGE,
WarningAction::CLOSE);
FastForwardAndAddEvent(base::Seconds(10), WarningSurface::DOWNLOAD_PROMPT,
WarningAction::CANCEL);
FastForwardAndAddEvent(base::Seconds(15), WarningSurface::DOWNLOADS_PAGE,
WarningAction::DISCARD);
std::vector<WarningActionEvent> events = GetEvents();
ASSERT_EQ(3u, events.size());
EXPECT_TRUE(VerifyEventValue(events[0], WarningSurface::BUBBLE_SUBPAGE,
WarningAction::CLOSE, /*expected_latency=*/5000,
/*expected_is_terminal_action=*/false));
EXPECT_TRUE(VerifyEventValue(
events[1], WarningSurface::DOWNLOAD_PROMPT, WarningAction::CANCEL,
/*expected_latency=*/15000, /*expected_is_terminal_action=*/false));
EXPECT_TRUE(VerifyEventValue(
events[2], WarningSurface::DOWNLOADS_PAGE, WarningAction::DISCARD,
/*expected_latency=*/30000, /*expected_is_terminal_action=*/true));
}
TEST_F(DownloadItemWarningDataTest, GetEvents_Notification) {
FastForwardAndAddEvent(base::Seconds(0),
WarningSurface::DOWNLOAD_NOTIFICATION,
WarningAction::SHOWN);
FastForwardAndAddEvent(base::Seconds(5),
WarningSurface::DOWNLOAD_NOTIFICATION,
WarningAction::OPEN_SUBPAGE);
std::vector<WarningActionEvent> events = GetEvents();
ASSERT_EQ(1u, events.size());
EXPECT_TRUE(VerifyEventValue(events[0], WarningSurface::DOWNLOAD_NOTIFICATION,
WarningAction::OPEN_SUBPAGE,
/*expected_latency=*/5000,
/*expected_is_terminal_action=*/false));
}
TEST_F(DownloadItemWarningDataTest, GetEvents_WarningShownNotLogged) {
FastForwardAndAddEvent(base::Seconds(5), WarningSurface::BUBBLE_SUBPAGE,
WarningAction::PROCEED);
std::vector<WarningActionEvent> events = GetEvents();
// The events are not logged because the SHOWN event is not logged, so there
// is no anchor event to calculate the latency.
EXPECT_EQ(0u, events.size());
}
TEST_F(DownloadItemWarningDataTest, GetEvents_NotDangerous) {
ON_CALL(download_, IsDangerous()).WillByDefault(Return(false));
FastForwardAndAddEvent(base::Seconds(0), WarningSurface::BUBBLE_MAINPAGE,
WarningAction::SHOWN);
FastForwardAndAddEvent(base::Seconds(5), WarningSurface::BUBBLE_SUBPAGE,
WarningAction::PROCEED);
std::vector<WarningActionEvent> events = GetEvents();
// The events are not logged because download is not dangerous.
EXPECT_EQ(0u, events.size());
}
TEST_F(DownloadItemWarningDataTest, GetEvents_MultipleWarningShownLogged) {
FastForwardAndAddEvent(base::Seconds(0), WarningSurface::BUBBLE_MAINPAGE,
WarningAction::SHOWN);
FastForwardAndAddEvent(base::Seconds(5), WarningSurface::BUBBLE_MAINPAGE,
WarningAction::SHOWN);
FastForwardAndAddEvent(base::Seconds(5),
WarningSurface::DOWNLOAD_NOTIFICATION,
WarningAction::SHOWN);
FastForwardAndAddEvent(base::Seconds(5), WarningSurface::BUBBLE_SUBPAGE,
WarningAction::DISCARD);
std::vector<WarningActionEvent> events = GetEvents();
ASSERT_EQ(1u, events.size());
// The expected latency should be 15000 instead of 5000 because the latency
// should be anchored to the first shown event.
EXPECT_TRUE(VerifyEventValue(events[0], WarningSurface::BUBBLE_SUBPAGE,
WarningAction::DISCARD,
/*expected_latency=*/15000,
/*expected_is_terminal_action=*/true));
}
TEST_F(DownloadItemWarningDataTest, GetEvents_ExceedEventMaxLength) {
FastForwardAndAddEvent(base::Seconds(0), WarningSurface::BUBBLE_MAINPAGE,
WarningAction::SHOWN);
for (int i = 0; i <= 30; i++) {
FastForwardAndAddEvent(base::Seconds(5), WarningSurface::BUBBLE_SUBPAGE,
WarningAction::CLOSE);
}
std::vector<WarningActionEvent> events = GetEvents();
ASSERT_EQ(20u, events.size());
// The newer events should be ignored, so the expected latency of the last
// event should be the 20th event.
EXPECT_TRUE(VerifyEventValue(events[19], WarningSurface::BUBBLE_SUBPAGE,
WarningAction::CLOSE,
/*expected_latency=*/20 * 5000,
/*expected_is_terminal_action=*/false));
}
TEST_F(DownloadItemWarningDataTest, IsEncryptedArchive) {
EXPECT_FALSE(DownloadItemWarningData::IsTopLevelEncryptedArchive(&download_));
DownloadItemWarningData::SetIsTopLevelEncryptedArchive(&download_, true);
EXPECT_TRUE(DownloadItemWarningData::IsTopLevelEncryptedArchive(&download_));
}
TEST_F(DownloadItemWarningDataTest, HasIncorrectPassword) {
EXPECT_FALSE(DownloadItemWarningData::HasIncorrectPassword(&download_));
DownloadItemWarningData::SetHasIncorrectPassword(&download_, true);
EXPECT_TRUE(DownloadItemWarningData::HasIncorrectPassword(&download_));
}
TEST_F(DownloadItemWarningDataTest, HasShownLocalDecryptionPrompt) {
EXPECT_FALSE(
DownloadItemWarningData::HasShownLocalDecryptionPrompt(&download_));
DownloadItemWarningData::SetHasShownLocalDecryptionPrompt(&download_, true);
EXPECT_TRUE(
DownloadItemWarningData::HasShownLocalDecryptionPrompt(&download_));
}
TEST_F(DownloadItemWarningDataTest, DeepScanTrigger) {
EXPECT_EQ(DownloadItemWarningData::DownloadDeepScanTrigger(&download_),
DeepScanTrigger::TRIGGER_UNKNOWN);
DownloadItemWarningData::SetDeepScanTrigger(
&download_, DeepScanTrigger::TRIGGER_CONSUMER_PROMPT);
EXPECT_EQ(DownloadItemWarningData::DownloadDeepScanTrigger(&download_),
DeepScanTrigger::TRIGGER_CONSUMER_PROMPT);
}
TEST_F(DownloadItemWarningDataTest, FirstShownTimeAndSurface) {
EXPECT_EQ(DownloadItemWarningData::WarningFirstShownSurface(&download_),
std::nullopt);
EXPECT_TRUE(
DownloadItemWarningData::WarningFirstShownTime(&download_).is_null());
base::Time now = base::Time::Now();
FastForwardAndAddEvent(base::Seconds(0),
WarningSurface::DOWNLOAD_NOTIFICATION,
WarningAction::SHOWN);
FastForwardAndAddEvent(base::Seconds(5), WarningSurface::BUBBLE_MAINPAGE,
WarningAction::SHOWN);
EXPECT_EQ(*DownloadItemWarningData::WarningFirstShownSurface(&download_),
WarningSurface::DOWNLOAD_NOTIFICATION);
EXPECT_EQ(DownloadItemWarningData::WarningFirstShownTime(&download_), now);
}
TEST_F(DownloadItemWarningDataTest, EventToString) {
FastForwardAndAddEvent(base::Seconds(0), WarningSurface::BUBBLE_MAINPAGE,
WarningAction::SHOWN);
FastForwardAndAddEvent(base::Seconds(5), WarningSurface::BUBBLE_SUBPAGE,
WarningAction::CLOSE);
FastForwardAndAddEvent(base::Seconds(10), WarningSurface::DOWNLOAD_PROMPT,
WarningAction::CANCEL);
FastForwardAndAddEvent(base::Seconds(15), WarningSurface::DOWNLOADS_PAGE,
WarningAction::DISCARD);
std::vector<WarningActionEvent> events = GetEvents();
// The initial SHOWN event is not included.
EXPECT_EQ(events[0].ToString(), "BUBBLE_SUBPAGE:CLOSE:5000");
EXPECT_EQ(events[1].ToString(), "DOWNLOAD_PROMPT:CANCEL:15000");
EXPECT_EQ(events[2].ToString(), "DOWNLOADS_PAGE:DISCARD:30000");
}
|