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
|
// 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/ui/ash/wallpaper/wallpaper_ash.h"
#include <memory>
#include "ash/public/mojom/wallpaper.mojom.h"
#include "base/check_deref.h"
#include "base/memory/ptr_util.h"
#include "base/memory/raw_ptr.h"
#include "base/test/bind.h"
#include "base/test/test_future.h"
#include "chrome/browser/ash/login/users/fake_chrome_user_manager.h"
#include "chrome/browser/ash/profiles/profile_helper.h"
#include "chrome/browser/ash/wallpaper_handlers/test_wallpaper_fetcher_delegate.h"
#include "chrome/browser/ui/ash/wallpaper/test_wallpaper_controller.h"
#include "chrome/browser/ui/ash/wallpaper/wallpaper_ash.h"
#include "chrome/browser/ui/ash/wallpaper/wallpaper_controller_client_impl.h"
#include "chrome/test/base/testing_browser_process.h"
#include "chrome/test/base/testing_profile.h"
#include "chrome/test/base/testing_profile_manager.h"
#include "chromeos/ash/components/browser_context_helper/annotated_account_id.h"
#include "chromeos/ash/components/login/login_state/login_state.h"
#include "components/crash/core/common/crash_key.h"
#include "components/user_manager/scoped_user_manager.h"
#include "components/user_manager/user_manager.h"
#include "components/user_manager/user_names.h"
#include "content/public/test/browser_task_environment.h"
#include "content/public/test/test_utils.h"
#include "services/data_decoder/public/cpp/test_support/in_process_data_decoder.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/gfx/codec/jpeg_codec.h"
std::vector<uint8_t> CreateJpeg(int width = 100, int height = 100) {
const SkColor kRed = SkColorSetRGB(255, 0, 0);
constexpr int kQuality = 80;
SkBitmap bitmap;
bitmap.allocN32Pixels(width, height);
bitmap.eraseColor(kRed);
std::optional<std::vector<uint8_t>> jpg_data =
gfx::JPEGCodec::Encode(bitmap, kQuality);
if (!jpg_data) {
LOG(ERROR) << "Failed to encode a sample JPEG wallpaper image";
return {};
}
return jpg_data.value();
}
using ash::mojom::SetWallpaperResultPtr;
class WallpaperAshTest : public testing::Test {
public:
WallpaperAshTest()
: fake_user_manager_(std::make_unique<ash::FakeChromeUserManager>()),
testing_profile_manager_(TestingBrowserProcess::GetGlobal()) {}
WallpaperAshTest(const WallpaperAshTest&) = delete;
~WallpaperAshTest() override = default;
void SetUp() override {
// Log in user.
ash::LoginState::Initialize();
ASSERT_TRUE(testing_profile_manager_.SetUp());
auto* user = fake_user_manager_->AddUser(user_manager::StubAccountId());
fake_user_manager_->LoginUser(user->GetAccountId());
testing_profile_ = testing_profile_manager_.CreateTestingProfile("profile");
ash::AnnotatedAccountId::Set(testing_profile_, user->GetAccountId());
// Create Wallpaper Controller Client.
wallpaper_controller_client_ = std::make_unique<
WallpaperControllerClientImpl>(
CHECK_DEREF(TestingBrowserProcess::GetGlobal()->local_state()),
std::make_unique<wallpaper_handlers::TestWallpaperFetcherDelegate>());
wallpaper_controller_client_->InitForTesting(&test_wallpaper_controller_);
crash_reporter::InitializeCrashKeysForTesting();
}
void TearDown() override {
crash_reporter::ResetCrashKeysForTesting();
wallpaper_controller_client_.reset();
ash::LoginState::Shutdown();
}
SetWallpaperResultPtr SetWallpaper(
ash::mojom::WallpaperSettingsPtr wallpaper_settings,
const std::string& extension_id,
const std::string& extension_name) {
base::test::TestFuture<SetWallpaperResultPtr> future;
wallpaper_ash_.SetWallpaper(std::move(wallpaper_settings), extension_id,
extension_name, future.GetCallback());
return future.Take();
}
protected:
// We need to satisfy DCHECK_CURRENTLY_ON(BrowserThread::UI).
content::BrowserTaskEnvironment task_environment_;
WallpaperAsh wallpaper_ash_;
data_decoder::test::InProcessDataDecoder data_decoder_;
user_manager::TypedScopedUserManager<ash::FakeChromeUserManager>
fake_user_manager_;
TestingProfileManager testing_profile_manager_;
raw_ptr<TestingProfile> testing_profile_;
TestWallpaperController test_wallpaper_controller_;
std::unique_ptr<WallpaperControllerClientImpl> wallpaper_controller_client_;
};
TEST_F(WallpaperAshTest, SetWallpaper) {
ash::mojom::WallpaperSettingsPtr settings =
ash::mojom::WallpaperSettings::New();
settings->data = CreateJpeg();
test_wallpaper_controller_.SetCurrentUser(user_manager::StubAccountId());
const SetWallpaperResultPtr result =
SetWallpaper(std::move(settings), "extension_id", "extension_name");
ASSERT_TRUE(result->is_thumbnail_data());
ASSERT_FALSE(result->get_thumbnail_data().empty());
ASSERT_EQ(1, test_wallpaper_controller_.get_third_party_wallpaper_count());
}
TEST_F(WallpaperAshTest, SetWallpaper1x1) {
ash::mojom::WallpaperSettingsPtr settings =
ash::mojom::WallpaperSettings::New();
settings->data = CreateJpeg(1, 1);
test_wallpaper_controller_.SetCurrentUser(user_manager::StubAccountId());
const SetWallpaperResultPtr result =
SetWallpaper(std::move(settings), "extension_id", "extension_name");
ASSERT_TRUE(result->is_thumbnail_data());
ASSERT_FALSE(result->get_thumbnail_data().empty());
ASSERT_EQ(1, test_wallpaper_controller_.get_third_party_wallpaper_count());
}
TEST_F(WallpaperAshTest, SetWallpaper_InvalidWallpaper) {
ash::mojom::WallpaperSettingsPtr settings =
ash::mojom::WallpaperSettings::New();
test_wallpaper_controller_.SetCurrentUser(user_manager::StubAccountId());
// Created invalid data by not adding a wallpaper image to the settings data.
const SetWallpaperResultPtr result =
SetWallpaper(std::move(settings), "extension_id", "extension_name");
ASSERT_TRUE(result->is_error_message());
ASSERT_EQ("Decoding wallpaper data failed.", result->get_error_message());
ASSERT_EQ(0, test_wallpaper_controller_.get_third_party_wallpaper_count());
}
TEST_F(WallpaperAshTest, SetWallpaper_InvalidUser) {
ash::mojom::WallpaperSettingsPtr settings =
ash::mojom::WallpaperSettings::New();
settings->data = CreateJpeg();
// Setting the wallpaper fails because we haven't set the current user.
const SetWallpaperResultPtr result =
SetWallpaper(std::move(settings), "extension_id", "extension_name");
ASSERT_TRUE(result->is_error_message());
ASSERT_EQ("Setting the wallpaper failed due to user permissions.",
result->get_error_message());
ASSERT_EQ(0, test_wallpaper_controller_.get_third_party_wallpaper_count());
}
TEST_F(WallpaperAshTest, SetWallpaper_CrashKeys_OnSuccess) {
test_wallpaper_controller_.SetCurrentUser(user_manager::StubAccountId());
// Create valid settings.
ash::mojom::WallpaperSettingsPtr settings =
ash::mojom::WallpaperSettings::New();
settings->data = CreateJpeg();
// Invoke SetWallpaper(). It will respond with success.
base::test::TestFuture<SetWallpaperResultPtr> future;
wallpaper_ash_.SetWallpaper(std::move(settings), "extension_id",
"extension_name", future.GetCallback());
// Crash key is set when function starts running.
using crash_reporter::GetCrashKeyValue;
EXPECT_EQ(GetCrashKeyValue("extension-function-caller-1"), "extension_id");
// Crash key is cleared after function completes.
const SetWallpaperResultPtr result = future.Take();
ASSERT_FALSE(result->is_error_message());
EXPECT_EQ(GetCrashKeyValue("extension-function-caller-1"), "");
}
TEST_F(WallpaperAshTest, SetWallpaper_CrashKeys_OnError) {
test_wallpaper_controller_.SetCurrentUser(user_manager::StubAccountId());
// Create invalid data by not adding a wallpaper image to the settings data.
ash::mojom::WallpaperSettingsPtr settings =
ash::mojom::WallpaperSettings::New();
// Invoke SetWallpaper(). It will respond with an error.
base::test::TestFuture<SetWallpaperResultPtr> future;
wallpaper_ash_.SetWallpaper(std::move(settings), "extension_id",
"extension_name", future.GetCallback());
// Crash key is set when function starts running.
using crash_reporter::GetCrashKeyValue;
EXPECT_EQ(GetCrashKeyValue("extension-function-caller-1"), "extension_id");
// Crash key is cleared after function completes.
const SetWallpaperResultPtr result = future.Take();
ASSERT_TRUE(result->is_error_message());
EXPECT_EQ(GetCrashKeyValue("extension-function-caller-1"), "");
}
|