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
|
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ash/ambient/ambient_backup_photo_downloader.h"
#include <utility>
#include <vector>
#include "ash/ambient/ambient_access_token_controller.h"
#include "ash/ambient/ambient_photo_cache.h"
#include "ash/ambient/ambient_photo_cache_settings.h"
#include "ash/ambient/test/test_ambient_client.h"
#include "ash/public/cpp/ambient/proto/photo_cache_entry.pb.h"
#include "ash/public/cpp/image_util.h"
#include "ash/public/cpp/test/in_process_data_decoder.h"
#include "ash/test/ash_test_util.h"
#include "base/files/scoped_temp_dir.h"
#include "base/task/task_traits.h"
#include "base/task/thread_pool.h"
#include "base/test/task_environment.h"
#include "base/test/test_future.h"
#include "net/http/http_status_code.h"
#include "services/device/public/cpp/test/test_wake_lock_provider.h"
#include "services/network/test/test_url_loader_factory.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/skia/include/core/SkColor.h"
#include "ui/gfx/image/image.h"
#include "ui/gfx/image/image_skia.h"
#include "ui/gfx/image/image_unittest_util.h"
#include "ui/gfx/image/image_util.h"
#include "url/gurl.h"
namespace ash {
namespace {
constexpr int kMaxPixelDeviation = 1;
constexpr char kTestUrl[] = "https://test.com";
gfx::Image ReadImageFromCache(int cache_index) {
base::test::TestFuture<::ambient::PhotoCacheEntry> read_cache_future;
ambient_photo_cache::ReadPhotoCache(ambient_photo_cache::Store::kBackup,
cache_index,
read_cache_future.GetCallback());
base::test::TestFuture<const gfx::ImageSkia&> decode_image_future;
image_util::DecodeImageData(decode_image_future.GetCallback(),
data_decoder::mojom::ImageCodec::kDefault,
read_cache_future.Get().primary_photo().image());
return gfx::Image(decode_image_future.Get());
}
class AmbientBackupPhotoDownloaderTest : public ::testing::Test {
protected:
AmbientBackupPhotoDownloaderTest() : ambient_client_(&wake_lock_provider_) {
ambient_photo_cache::SetFileTaskRunner(
base::ThreadPool::CreateSequencedTaskRunner({base::MayBlock()}));
}
// ::testing::Test
void SetUp() override {
ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
ambient_client_.SetAutomaticalyIssueToken(true);
SetAmbientBackupPhotoCacheRootDirForTesting(temp_dir_.GetPath());
}
base::test::TaskEnvironment task_environment_;
base::ScopedTempDir temp_dir_;
InProcessDataDecoder decoder_;
device::TestWakeLockProvider wake_lock_provider_;
TestAmbientClient ambient_client_;
AmbientAccessTokenController token_controller_;
};
TEST_F(AmbientBackupPhotoDownloaderTest, Success) {
gfx::ImageSkia test_image;
ambient_client_.test_url_loader_factory().AddResponse(
kTestUrl, CreateEncodedImageForTesting(
gfx::Size(100, 100), SK_ColorBLACK,
data_decoder::mojom::ImageCodec::kDefault, &test_image));
base::test::TestFuture<bool> downloader_future;
AmbientBackupPhotoDownloader downloader(token_controller_, 0,
gfx::Size(100, 100), kTestUrl,
downloader_future.GetCallback());
ASSERT_TRUE(downloader_future.Get());
EXPECT_TRUE(gfx::test::AreImagesClose(
ReadImageFromCache(0), gfx::Image(test_image), kMaxPixelDeviation));
}
TEST_F(AmbientBackupPhotoDownloaderTest, SuccessWithResizeSameAspectRatio) {
gfx::ImageSkia test_image;
ambient_client_.test_url_loader_factory().AddResponse(
kTestUrl, CreateEncodedImageForTesting(
gfx::Size(100, 100), SK_ColorGREEN,
data_decoder::mojom::ImageCodec::kDefault, &test_image));
base::test::TestFuture<bool> downloader_future;
AmbientBackupPhotoDownloader downloader(token_controller_, 0,
gfx::Size(50, 50), kTestUrl,
downloader_future.GetCallback());
ASSERT_TRUE(downloader_future.Get());
gfx::ImageSkia expected_image =
CreateSolidColorTestImage(gfx::Size(50, 50), SK_ColorGREEN);
EXPECT_TRUE(gfx::test::AreImagesClose(
ReadImageFromCache(0), gfx::Image(expected_image), kMaxPixelDeviation));
}
TEST_F(AmbientBackupPhotoDownloaderTest, SuccessWithResizeLandscapeToPortrait) {
gfx::ImageSkia test_image;
ambient_client_.test_url_loader_factory().AddResponse(
kTestUrl, CreateEncodedImageForTesting(
gfx::Size(200, 100), SK_ColorGREEN,
data_decoder::mojom::ImageCodec::kDefault, &test_image));
base::test::TestFuture<bool> downloader_future;
AmbientBackupPhotoDownloader downloader(token_controller_, 0,
gfx::Size(25, 50), kTestUrl,
downloader_future.GetCallback());
ASSERT_TRUE(downloader_future.Get());
gfx::ImageSkia expected_image =
CreateSolidColorTestImage(gfx::Size(100, 50), SK_ColorGREEN);
EXPECT_TRUE(gfx::test::AreImagesClose(
ReadImageFromCache(0), gfx::Image(expected_image), kMaxPixelDeviation));
}
TEST_F(AmbientBackupPhotoDownloaderTest, SuccessWithResizePortraitToLandscape) {
gfx::ImageSkia test_image;
ambient_client_.test_url_loader_factory().AddResponse(
kTestUrl, CreateEncodedImageForTesting(
gfx::Size(100, 200), SK_ColorGREEN,
data_decoder::mojom::ImageCodec::kDefault, &test_image));
base::test::TestFuture<bool> downloader_future;
AmbientBackupPhotoDownloader downloader(token_controller_, 0,
gfx::Size(50, 25), kTestUrl,
downloader_future.GetCallback());
ASSERT_TRUE(downloader_future.Get());
gfx::ImageSkia expected_image =
CreateSolidColorTestImage(gfx::Size(50, 100), SK_ColorGREEN);
EXPECT_TRUE(gfx::test::AreImagesClose(
ReadImageFromCache(0), gfx::Image(expected_image), kMaxPixelDeviation));
}
TEST_F(AmbientBackupPhotoDownloaderTest, FailedServerResponse) {
ambient_client_.test_url_loader_factory().AddResponse(kTestUrl, "",
net::HTTP_BAD_REQUEST);
base::test::TestFuture<bool> downloader_future;
AmbientBackupPhotoDownloader downloader(token_controller_, 0,
gfx::Size(100, 100), kTestUrl,
downloader_future.GetCallback());
EXPECT_FALSE(downloader_future.Get());
}
} // namespace
} // namespace ash
|