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

ash / wallpaper / wallpaper_file_manager_unittest.cc [blame]

// Copyright 2024 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/wallpaper/wallpaper_file_manager.h"

#include <string>

#include "ash/public/cpp/test/in_process_data_decoder.h"
#include "ash/public/cpp/wallpaper/wallpaper_types.h"
#include "base/check.h"
#include "base/files/file_path.h"
#include "base/files/scoped_temp_dir.h"
#include "base/notreached.h"
#include "base/test/task_environment.h"
#include "base/test/test_future.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/skia/include/core/SkColor.h"
#include "ui/gfx/image/image_skia.h"
#include "ui/gfx/image/image_unittest_util.h"

namespace ash {
namespace {

// jpg encoding/decoding sometimes changes the pixels slightly.
constexpr int kMaxPixelDeviation = 1;

class WallpaperFileManagerTest
    : public ::testing::Test,
      public testing::WithParamInterface<WallpaperType> {
 public:
  void SetUp() override { ASSERT_TRUE(scoped_temp_dir_.CreateUniqueTempDir()); }

  WallpaperType wallpaper_type() const { return GetParam(); }

  WallpaperFileManager& wallpaper_file_manager() {
    return wallpaper_file_manager_;
  }

  base::FilePath scoped_temp_dir_path() { return scoped_temp_dir_.GetPath(); }

 private:
  base::test::TaskEnvironment task_environment_;
  base::ScopedTempDir scoped_temp_dir_;
  InProcessDataDecoder decoder_;
  WallpaperFileManager wallpaper_file_manager_;
};

INSTANTIATE_TEST_SUITE_P(
    // empty to simplify gtest output
    ,
    WallpaperFileManagerTest,
    testing::Values(WallpaperType::kOnline,
                    WallpaperType::kDaily,
                    WallpaperType::kOnceGooglePhotos,
                    WallpaperType::kDailyGooglePhotos,
                    WallpaperType::kCustomized),
    [](const testing::TestParamInfo<WallpaperFileManagerTest::ParamType>& info)
        -> std::string {
      switch (info.param) {
        case WallpaperType::kOnline:
          return "Online";
        case WallpaperType::kDaily:
          return "Daily";
        case WallpaperType::kOnceGooglePhotos:
          return "OnceGooglePhotos";
        case WallpaperType::kDailyGooglePhotos:
          return "DailyGooglePhotos";
        case WallpaperType::kCustomized:
          return "Customized";
        default:
          NOTREACHED();
      }
    });

TEST_P(WallpaperFileManagerTest, LoadMissingWallpaper) {
  base::test::TestFuture<const gfx::ImageSkia&> load_wallpaper_future;

  wallpaper_file_manager().LoadWallpaper(
      wallpaper_type(), scoped_temp_dir_path(), "test_location",
      load_wallpaper_future.GetCallback());

  EXPECT_TRUE(load_wallpaper_future.Get().isNull());
}

TEST_P(WallpaperFileManagerTest, SaveAndLoadSameWallpaper) {
  const gfx::ImageSkia test_image =
      gfx::test::CreateImageSkia(10, SK_ColorYELLOW);

  base::test::TestFuture<const base::FilePath&> save_wallpaper_future;

  wallpaper_file_manager().SaveWallpaperToDisk(
      wallpaper_type(), scoped_temp_dir_path(), "test_file_name.jpg",
      WALLPAPER_LAYOUT_CENTER_CROPPED, test_image,
      save_wallpaper_future.GetCallback(), "wallpaper_files_id");

  ASSERT_FALSE(save_wallpaper_future.Get().empty());

  std::string location;
  switch (wallpaper_type()) {
    case WallpaperType::kOnline:
    case WallpaperType::kDaily:
      location = "https://example.com/test_file_name.jpg";
      break;
    case WallpaperType::kOnceGooglePhotos:
    case WallpaperType::kDailyGooglePhotos:
      location = "test_file_name.jpg";
      break;
    case WallpaperType::kCustomized:
      location = "original/wallpaper_files_id/test_file_name.jpg";
      break;
    case WallpaperType::kSeaPen:
    case WallpaperType::kDefault:
    case WallpaperType::kDevice:
    case WallpaperType::kPolicy:
    case WallpaperType::kOobe:
    case WallpaperType::kThirdParty:
    case WallpaperType::kOneShot:
    case WallpaperType::kCount:
      NOTREACHED();
  }

  base::test::TestFuture<const gfx::ImageSkia&> load_wallpaper_future;

  wallpaper_file_manager().LoadWallpaper(wallpaper_type(),
                                         scoped_temp_dir_path(), location,
                                         load_wallpaper_future.GetCallback());

  EXPECT_TRUE(gfx::test::AreImagesClose(gfx::Image(test_image),
                                        gfx::Image(load_wallpaper_future.Get()),
                                        kMaxPixelDeviation));
}

}  // namespace
}  // namespace ash