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
|
// Copyright 2024 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/android/download_controller.h"
#include "base/json/values_util.h"
#include "base/test/scoped_feature_list.h"
#include "chrome/common/pref_names.h"
#include "chrome/test/base/testing_profile.h"
#include "components/download/public/common/download_item.h"
#include "components/download/public/common/mock_download_item.h"
#include "components/prefs/pref_service.h"
#include "components/prefs/scoped_user_pref_update.h"
#include "components/safe_browsing/core/common/features.h"
#include "content/public/browser/download_item_utils.h"
#include "content/public/test/browser_task_environment.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
using ::testing::IsEmpty;
using ::testing::Return;
class DownloadControllerTest : public testing::Test {
public:
DownloadControllerTest() {
content::DownloadItemUtils::AttachInfoForTesting(&item_, &profile_,
nullptr);
}
DownloadControllerTest(const DownloadControllerTest&) = delete;
DownloadControllerTest& operator=(const DownloadControllerTest&) = delete;
download::MockDownloadItem* item() { return &item_; }
Profile* profile() { return &profile_; }
bool ShouldShowAppVerificationPrompt(download::DownloadItem* item) {
return controller_.ShouldShowAppVerificationPrompt(item);
}
private:
content::BrowserTaskEnvironment task_environment_;
TestingProfile profile_;
DownloadController controller_;
download::MockDownloadItem item_;
};
TEST_F(DownloadControllerTest, ShouldShowAppVerificationPrompt) {
EXPECT_FALSE(ShouldShowAppVerificationPrompt(item()));
EXPECT_CALL(*item(), GetDangerType())
.WillRepeatedly(Return(download::DOWNLOAD_DANGER_TYPE_USER_VALIDATED));
EXPECT_TRUE(ShouldShowAppVerificationPrompt(item()));
{
ScopedListPrefUpdate update(
profile()->GetPrefs(), prefs::kDownloadAppVerificationPromptTimestamps);
update->Append(base::TimeToValue(base::Time::Now()));
update->Append(base::TimeToValue(base::Time::Now()));
update->Append(base::TimeToValue(base::Time::Now()));
}
EXPECT_FALSE(ShouldShowAppVerificationPrompt(item()));
}
TEST_F(DownloadControllerTest, CleanupOldTimestamps) {
EXPECT_CALL(*item(), GetDangerType())
.WillOnce(Return(download::DOWNLOAD_DANGER_TYPE_USER_VALIDATED));
{
ScopedListPrefUpdate update(
profile()->GetPrefs(), prefs::kDownloadAppVerificationPromptTimestamps);
base::Time long_ago = base::Time::Now() - base::Days(500);
update->Append(base::TimeToValue(long_ago));
update->Append(base::TimeToValue(long_ago));
update->Append(base::TimeToValue(long_ago));
}
EXPECT_TRUE(ShouldShowAppVerificationPrompt(item()));
EXPECT_THAT(profile()->GetPrefs()->GetList(
prefs::kDownloadAppVerificationPromptTimestamps),
IsEmpty());
}
|