#include "chrome/common/notifications/notification_image_retainer.h"
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/run_loop.h"
#include "base/test/scoped_path_override.h"
#include "base/test/task_environment.h"
#include "chrome/common/chrome_paths.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "ui/gfx/image/image.h"
namespace {
constexpr base::TimeDelta kDeletionDelay = base::Seconds(12);
}
class NotificationImageRetainerTest : public ::testing::Test {
public:
NotificationImageRetainerTest()
: task_environment_(base::test::TaskEnvironment::TimeSource::MOCK_TIME),
user_data_dir_override_(chrome::DIR_USER_DATA) {}
NotificationImageRetainerTest(const NotificationImageRetainerTest&) = delete;
NotificationImageRetainerTest& operator=(
const NotificationImageRetainerTest&) = delete;
~NotificationImageRetainerTest() override = default;
protected:
base::test::TaskEnvironment task_environment_;
private:
base::ScopedPathOverride user_data_dir_override_;
};
TEST_F(NotificationImageRetainerTest, RegisterTemporaryImage) {
auto image_retainer = std::make_unique<NotificationImageRetainer>(
task_environment_.GetMainThreadTaskRunner(),
task_environment_.GetMockTickClock());
SkBitmap icon;
icon.allocN32Pixels(64, 64);
icon.eraseARGB(255, 100, 150, 200);
gfx::Image image = gfx::Image::CreateFrom1xBitmap(icon);
base::FilePath temp_file = image_retainer->RegisterTemporaryImage(image);
ASSERT_FALSE(temp_file.empty());
ASSERT_TRUE(base::PathExists(temp_file));
task_environment_.FastForwardBy(kDeletionDelay);
ASSERT_FALSE(base::PathExists(temp_file));
image_retainer.reset();
ASSERT_TRUE(base::PathExists(temp_file.DirName()));
}
TEST_F(NotificationImageRetainerTest, DeleteFilesInBatch) {
auto image_retainer = std::make_unique<NotificationImageRetainer>(
task_environment_.GetMainThreadTaskRunner(),
task_environment_.GetMockTickClock());
SkBitmap icon;
icon.allocN32Pixels(64, 64);
icon.eraseARGB(255, 100, 150, 200);
gfx::Image image = gfx::Image::CreateFrom1xBitmap(icon);
base::FilePath temp_file1 = image_retainer->RegisterTemporaryImage(image);
ASSERT_FALSE(temp_file1.empty());
ASSERT_TRUE(base::PathExists(temp_file1));
task_environment_.FastForwardBy(base::Seconds(1));
base::FilePath temp_file2 = image_retainer->RegisterTemporaryImage(image);
ASSERT_FALSE(temp_file2.empty());
ASSERT_TRUE(base::PathExists(temp_file2));
task_environment_.FastForwardBy(base::Seconds(1));
base::FilePath temp_file3 = image_retainer->RegisterTemporaryImage(image);
ASSERT_FALSE(temp_file3.empty());
ASSERT_TRUE(base::PathExists(temp_file3));
task_environment_.FastForwardBy(kDeletionDelay);
ASSERT_FALSE(base::PathExists(temp_file1));
ASSERT_TRUE(base::PathExists(temp_file2));
ASSERT_TRUE(base::PathExists(temp_file3));
task_environment_.FastForwardBy(kDeletionDelay);
ASSERT_FALSE(base::PathExists(temp_file2));
ASSERT_FALSE(base::PathExists(temp_file3));
}
TEST_F(NotificationImageRetainerTest, CleanupFilesFromPrevSessions) {
auto image_retainer = std::make_unique<NotificationImageRetainer>(
task_environment_.GetMainThreadTaskRunner(),
task_environment_.GetMockTickClock());
const base::FilePath& image_dir = image_retainer->image_dir();
ASSERT_TRUE(base::CreateDirectory(image_dir));
base::FilePath temp_file1;
ASSERT_TRUE(base::CreateTemporaryFileInDir(image_dir, &temp_file1));
base::FilePath temp_file2;
ASSERT_TRUE(base::CreateTemporaryFileInDir(image_dir, &temp_file2));
ASSERT_TRUE(base::PathExists(temp_file1));
ASSERT_TRUE(base::PathExists(temp_file2));
SkBitmap icon;
icon.allocN32Pixels(64, 64);
icon.eraseARGB(255, 100, 150, 200);
gfx::Image image = gfx::Image::CreateFrom1xBitmap(icon);
base::FilePath temp_file3 = image_retainer->RegisterTemporaryImage(image);
ASSERT_FALSE(temp_file3.empty());
ASSERT_TRUE(base::PathExists(temp_file3));
image_retainer->CleanupFilesFromPrevSessions();
task_environment_.RunUntilIdle();
ASSERT_FALSE(base::PathExists(temp_file1));
ASSERT_FALSE(base::PathExists(temp_file2));
ASSERT_TRUE(base::PathExists(temp_file3));
task_environment_.FastForwardBy(kDeletionDelay);
ASSERT_FALSE(base::PathExists(temp_file3));
ASSERT_TRUE(base::PathExists(image_dir));
}