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 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
|
// 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.
#ifdef UNSAFE_BUFFERS_BUILD
// TODO(crbug.com/40285824): Remove this and convert code to safer constructs.
#pragma allow_unsafe_buffers
#endif
#include "chrome/browser/ash/wallpaper/wallpaper_drivefs_delegate_impl.h"
#include <memory>
#include <vector>
#include "ash/shell.h"
#include "ash/wallpaper/wallpaper_controller_impl.h"
#include "base/barrier_closure.h"
#include "base/files/file.h"
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/files/scoped_temp_dir.h"
#include "base/memory/ref_counted_memory.h"
#include "base/memory/scoped_refptr.h"
#include "base/run_loop.h"
#include "base/test/bind.h"
#include "base/threading/thread_restrictions.h"
#include "base/time/time.h"
#include "chrome/browser/ash/drive/drive_integration_service.h"
#include "chrome/browser/ash/drive/drive_integration_service_browser_test_base.h"
#include "chrome/browser/ash/drive/file_system_util.h"
#include "chrome/browser/ash/profiles/profile_helper.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser.h"
#include "chromeos/ash/components/drivefs/fake_drivefs.h"
#include "chromeos/ash/components/drivefs/mojom/drivefs.mojom.h"
#include "components/account_id/account_id.h"
#include "components/user_manager/user.h"
#include "content/public/test/browser_test.h"
#include "mojo/public/cpp/bindings/remote.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "third_party/skia/include/core/SkColor.h"
#include "ui/gfx/codec/jpeg_codec.h"
#include "ui/gfx/image/image_skia.h"
#include "ui/gfx/image/image_unittest_util.h"
namespace ash {
namespace {
scoped_refptr<base::RefCountedBytes> EncodeImage(const gfx::ImageSkia& image) {
std::optional<std::vector<uint8_t>> data =
gfx::JPEGCodec::Encode(*(image.bitmap()), /*quality=*/90);
return base::MakeRefCounted<base::RefCountedBytes>(std::move(data).value());
}
WallpaperDriveFsDelegate* GetWallpaperDriveFsDelegate() {
Shell* shell = Shell::Get();
auto* wallpaper_controller = shell->wallpaper_controller();
return wallpaper_controller->drivefs_delegate_for_testing();
}
// Saves a test wallpaper file. If `target` is empty, will default to the
// DriveFS wallpaper path.
void SaveTestWallpaperFile(const AccountId& account_id, base::FilePath target) {
ASSERT_FALSE(target.empty()) << "target FilePath is required to be non-empty";
base::ScopedAllowBlockingForTesting allow_blocking;
if (!base::DirectoryExists(target.DirName())) {
ASSERT_TRUE(base::CreateDirectory(target.DirName()));
}
auto data = EncodeImage(gfx::test::CreateImageSkia(/*size=*/16));
ASSERT_TRUE(base::WriteFile(target, base::span(*data)));
}
} // namespace
class WallpaperDriveFsDelegateImplBrowserTest
: public drive::DriveIntegrationServiceBrowserTestBase {
public:
WallpaperDriveFsDelegateImplBrowserTest() = default;
WallpaperDriveFsDelegateImplBrowserTest(
const WallpaperDriveFsDelegateImplBrowserTest&) = delete;
WallpaperDriveFsDelegateImplBrowserTest& operator=(
const WallpaperDriveFsDelegateImplBrowserTest&) = delete;
~WallpaperDriveFsDelegateImplBrowserTest() override = default;
const AccountId& GetAccountId() const {
user_manager::User* user =
ProfileHelper::Get()->GetUserByProfile(browser()->profile());
DCHECK(user);
return user->GetAccountId();
}
bool SaveWallpaperSync(const AccountId& account_id,
const base::FilePath& source) {
base::RunLoop loop;
bool out = false;
GetWallpaperDriveFsDelegate()->SaveWallpaper(
account_id, source,
base::BindLambdaForTesting([&out, &loop](bool success) {
out = success;
loop.Quit();
}));
loop.Run();
return out;
}
base::Time GetWallpaperModificationTimeSync(const AccountId& account_id) {
base::RunLoop loop;
base::Time out;
GetWallpaperDriveFsDelegate()->GetWallpaperModificationTime(
account_id, base::BindLambdaForTesting([&out, &loop](base::Time time) {
out = time;
loop.Quit();
}));
loop.Run();
return out;
}
std::vector<drivefs::mojom::FileChangePtr> CreateWallpaperFileChange() {
std::vector<drivefs::mojom::FileChangePtr> file_changes;
drive::DriveIntegrationService* drive_integration_service =
drive::util::GetIntegrationServiceByProfile(browser()->profile());
base::FilePath fake_wallpaper_notification_path(
&base::FilePath::kSeparators[0]);
EXPECT_TRUE(
drive_integration_service->GetMountPointPath().AppendRelativePath(
GetWallpaperDriveFsDelegate()->GetWallpaperPath(GetAccountId()),
&fake_wallpaper_notification_path));
drivefs::mojom::FileChangePtr wallpaper_change =
drivefs::mojom::FileChange::New(
fake_wallpaper_notification_path,
drivefs::mojom::FileChange::Type::kModify);
file_changes.push_back(std::move(wallpaper_change));
return file_changes;
}
};
IN_PROC_BROWSER_TEST_F(WallpaperDriveFsDelegateImplBrowserTest,
EmptyBaseTimeIfNoDriveFs) {
InitTestFileMountRoot(browser()->profile());
SaveTestWallpaperFile(
GetAccountId(),
GetWallpaperDriveFsDelegate()->GetWallpaperPath(GetAccountId()));
drive::DriveIntegrationService* drive_integration_service =
drive::util::GetIntegrationServiceByProfile(browser()->profile());
ASSERT_TRUE(drive_integration_service);
drive_integration_service->SetEnabled(false);
const base::Time modification_time =
GetWallpaperModificationTimeSync(GetAccountId());
EXPECT_EQ(modification_time, base::Time())
<< "DriveFS disabled should result in empty time";
}
IN_PROC_BROWSER_TEST_F(WallpaperDriveFsDelegateImplBrowserTest,
RespondsWithModifiedAtTime) {
InitTestFileMountRoot(browser()->profile());
base::ScopedAllowBlockingForTesting allow_blocking;
const base::FilePath drivefs_wallpaper_path =
GetWallpaperDriveFsDelegate()->GetWallpaperPath(GetAccountId());
ASSERT_FALSE(base::PathExists(drivefs_wallpaper_path));
SaveTestWallpaperFile(GetAccountId(), drivefs_wallpaper_path);
base::File::Info file_info;
ASSERT_TRUE(base::GetFileInfo(drivefs_wallpaper_path, &file_info));
const base::Time drivefs_modification_time =
GetWallpaperModificationTimeSync(GetAccountId());
EXPECT_EQ(drivefs_modification_time, file_info.last_modified)
<< "modification_time matches file info time";
}
IN_PROC_BROWSER_TEST_F(WallpaperDriveFsDelegateImplBrowserTest, SaveWallpaper) {
InitTestFileMountRoot(browser()->profile());
base::FilePath drivefs_wallpaper_path =
GetWallpaperDriveFsDelegate()->GetWallpaperPath(GetAccountId());
ASSERT_FALSE(drivefs_wallpaper_path.empty());
base::ScopedAllowBlockingForTesting scoped_allow_blocking;
// Write a jpg file to a tmp directory. This file will be copied into DriveFS.
base::ScopedTempDir scoped_temp_dir;
ASSERT_TRUE(scoped_temp_dir.CreateUniqueTempDir());
base::FilePath source_jpg = scoped_temp_dir.GetPath().Append("source.jpg");
SaveTestWallpaperFile(GetAccountId(), source_jpg);
// `SaveWallpaper` should succeed while DriveFS is enabled.
EXPECT_TRUE(SaveWallpaperSync(GetAccountId(), source_jpg));
// source.jpg was copied to DriveFS wallpaper path.
EXPECT_TRUE(base::PathExists(drivefs_wallpaper_path));
}
IN_PROC_BROWSER_TEST_F(WallpaperDriveFsDelegateImplBrowserTest,
SaveWallpaperDriveFsDisabled) {
InitTestFileMountRoot(browser()->profile());
base::ScopedAllowBlockingForTesting scoped_allow_blocking;
// Write a jpg file to a tmp directory. This file will be copied into DriveFS.
base::ScopedTempDir scoped_temp_dir;
ASSERT_TRUE(scoped_temp_dir.CreateUniqueTempDir());
base::FilePath source_jpg = scoped_temp_dir.GetPath().Append("source.jpg");
SaveTestWallpaperFile(GetAccountId(), source_jpg);
base::FilePath drivefs_wallpaper_path =
GetWallpaperDriveFsDelegate()->GetWallpaperPath(GetAccountId());
ASSERT_FALSE(drivefs_wallpaper_path.empty());
// Call `SaveWallpaper` while DriveFS is disabled. No file should be written.
auto* drive_integration_service =
drive::util::GetIntegrationServiceByProfile(browser()->profile());
drive_integration_service->SetEnabled(false);
EXPECT_FALSE(SaveWallpaperSync(GetAccountId(), source_jpg));
EXPECT_FALSE(base::PathExists(drivefs_wallpaper_path));
}
IN_PROC_BROWSER_TEST_F(WallpaperDriveFsDelegateImplBrowserTest,
WaitForWallpaperChange) {
InitTestFileMountRoot(browser()->profile());
base::RunLoop loop;
GetWallpaperDriveFsDelegate()->WaitForWallpaperChange(
GetAccountId(), base::BindLambdaForTesting([&loop](bool success) {
EXPECT_TRUE(success);
loop.Quit();
}));
// Send the fake wallpaper file change notification.
drivefs::FakeDriveFs* fake_drivefs =
GetFakeDriveFsForProfile(browser()->profile());
fake_drivefs->delegate()->OnFilesChanged(CreateWallpaperFileChange());
loop.Run();
}
IN_PROC_BROWSER_TEST_F(WallpaperDriveFsDelegateImplBrowserTest,
WaitForWallpaperChangeWithDriveFsDisabled) {
drive::DriveIntegrationService* drive_integration_service =
drive::util::GetIntegrationServiceByProfile(browser()->profile());
drive_integration_service->SetEnabled(false);
base::RunLoop loop;
// Responds immediately with `success=false` even without receiving any file
// change notifications because DriveFS is disabled.
GetWallpaperDriveFsDelegate()->WaitForWallpaperChange(
GetAccountId(), base::BindLambdaForTesting([&loop](bool success) {
EXPECT_FALSE(success);
loop.Quit();
}));
loop.Run();
}
IN_PROC_BROWSER_TEST_F(WallpaperDriveFsDelegateImplBrowserTest,
WaitForWallpaperChangeMultipleTimes) {
InitTestFileMountRoot(browser()->profile());
base::RunLoop loop;
// Make sure that closure is called twice before `loop` quits.
base::RepeatingClosure barrier =
base::BarrierClosure(/*num_closures=*/2, loop.QuitClosure());
// The first `WaitForWallpaperChange` call should respond with false when
// `WaitForWallpaperChange` is called a second time before receiving a file
// change notification.
GetWallpaperDriveFsDelegate()->WaitForWallpaperChange(
GetAccountId(), base::BindLambdaForTesting([&barrier](bool success) {
EXPECT_FALSE(success);
barrier.Run();
}));
// This call to `WaitForWallpaperChange` should succeed upon receiving the
// file change notification.
GetWallpaperDriveFsDelegate()->WaitForWallpaperChange(
GetAccountId(), base::BindLambdaForTesting([&barrier](bool success) {
EXPECT_TRUE(success);
barrier.Run();
}));
// Send the fake wallpaper file change notification.
drivefs::FakeDriveFs* fake_drivefs =
GetFakeDriveFsForProfile(browser()->profile());
fake_drivefs->delegate()->OnFilesChanged(CreateWallpaperFileChange());
loop.Run();
}
} // namespace ash
|