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
|
// Copyright 2017 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/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 {
// This value has to stay in sync with that in notification_image_retainer.cc.
constexpr base::TimeDelta kDeletionDelay = base::Seconds(12);
} // namespace
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));
// Fast-forward the task runner so that the file deletion task posted in
// RegisterTemporaryImage() finishes running.
task_environment_.FastForwardBy(kDeletionDelay);
// The temp file should be deleted now.
ASSERT_FALSE(base::PathExists(temp_file));
// The destruction of the image retainer object won't delete the image
// directory.
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);
// Create 1st image file on disk.
base::FilePath temp_file1 = image_retainer->RegisterTemporaryImage(image);
ASSERT_FALSE(temp_file1.empty());
ASSERT_TRUE(base::PathExists(temp_file1));
// Simulate ticking of the clock so that the next image file has a different
// registration time.
task_environment_.FastForwardBy(base::Seconds(1));
// Create 2nd image file on disk.
base::FilePath temp_file2 = image_retainer->RegisterTemporaryImage(image);
ASSERT_FALSE(temp_file2.empty());
ASSERT_TRUE(base::PathExists(temp_file2));
// Simulate ticking of the clock so that the next image file has a different
// registration time.
task_environment_.FastForwardBy(base::Seconds(1));
// Create 3rd image file on disk.
base::FilePath temp_file3 = image_retainer->RegisterTemporaryImage(image);
ASSERT_FALSE(temp_file3.empty());
ASSERT_TRUE(base::PathExists(temp_file3));
// Fast-forward the task runner by kDeletionDelay. The first temp file should
// be deleted now, while the other two should still be around.
task_environment_.FastForwardBy(kDeletionDelay);
ASSERT_FALSE(base::PathExists(temp_file1));
ASSERT_TRUE(base::PathExists(temp_file2));
ASSERT_TRUE(base::PathExists(temp_file3));
// Fast-forward the task runner again. The second and the third temp files
// are deleted simultaneously.
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));
// Create two temp files as if they were created in previous sessions.
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));
// Create a new temp file in the current session. This file will be scheduled
// to delete after kDeletionDelay seconds.
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));
// Schedule a file cleanup task.
image_retainer->CleanupFilesFromPrevSessions();
// Now the file cleanup task finishes running.
task_environment_.RunUntilIdle();
// The two temp files from previous sessions should be deleted now.
ASSERT_FALSE(base::PathExists(temp_file1));
ASSERT_FALSE(base::PathExists(temp_file2));
// The temp file created in this session should still be around.
ASSERT_TRUE(base::PathExists(temp_file3));
// Fast-forward the task runner so that the file deletion task posted in
// RegisterTemporaryImage() finishes running.
task_environment_.FastForwardBy(kDeletionDelay);
// The temp file created in this session should be deleted now.
ASSERT_FALSE(base::PathExists(temp_file3));
// The image directory should still be around.
ASSERT_TRUE(base::PathExists(image_dir));
}
|