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

ash / public / cpp / wallpaper / wallpaper_info_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/public/cpp/wallpaper/wallpaper_info.h"

#include "ash/constants/ash_features.h"
#include "ash/public/cpp/wallpaper/google_photos_wallpaper_params.h"
#include "ash/public/cpp/wallpaper/online_wallpaper_params.h"
#include "ash/public/cpp/wallpaper/wallpaper_types.h"
#include "ash/webui/personalization_app/proto/backdrop_wallpaper.pb.h"
#include "base/test/scoped_feature_list.h"
#include "base/time/time.h"
#include "base/values.h"
#include "google_apis/gaia/gaia_id.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace ash {
namespace {

constexpr char kUser1[] = "user1@test.com";
const AccountId kAccountId1 =
    AccountId::FromUserEmailGaiaId(kUser1, GaiaId(kUser1));

const uint64_t kAssetId = 1;
const uint64_t kUnitId = 1;

using WallpaperInfoTest = ::testing::Test;

TEST_F(WallpaperInfoTest, FromDictReturnsNullOptForInvalidValues) {
  {
    // Invalid type.
    WallpaperInfo actual_info =
        WallpaperInfo(std::string(), WALLPAPER_LAYOUT_CENTER_CROPPED,
                      WallpaperType::kCount, base::Time::Now(), std::string());
    base::Value::Dict dict = actual_info.ToDict();
    EXPECT_FALSE(WallpaperInfo::FromDict(dict));
  }
  {
    // Invalid layout.
    WallpaperInfo actual_info =
        WallpaperInfo(std::string(), NUM_WALLPAPER_LAYOUT,
                      WallpaperType::kOnline, base::Time::Now(), std::string());
    base::Value::Dict dict = actual_info.ToDict();
    EXPECT_FALSE(WallpaperInfo::FromDict(dict));
  }
}

TEST_F(WallpaperInfoTest, ToAndFromDict) {
  {
    // WallpaperType::kOnline
    OnlineWallpaperParams params = OnlineWallpaperParams(
        kAccountId1,
        /*collection_id=*/std::string(), WALLPAPER_LAYOUT_CENTER_CROPPED,
        /*preview_mode=*/false, /*from_user=*/false,
        /*daily_refresh_enabled=*/false, kUnitId,
        /*variants=*/
        {{kAssetId, GURL("https://example.com/image.png"),
          backdrop::Image::IMAGE_TYPE_UNKNOWN}});
    WallpaperInfo actual_info = WallpaperInfo(params, params.variants[0]);
    base::Value::Dict dict = actual_info.ToDict();
    std::optional<WallpaperInfo> expected_info = WallpaperInfo::FromDict(dict);
    EXPECT_TRUE(actual_info.MatchesAsset(expected_info.value()));
  }
  {
    // WallpaperType::kOnceGooglePhotos
    GooglePhotosWallpaperParams params = GooglePhotosWallpaperParams(
        kAccountId1, "id", /*daily_refresh_enabled=*/false,
        WALLPAPER_LAYOUT_CENTER_CROPPED, /*preview_mode=*/false, "dedup_key");
    WallpaperInfo actual_info = WallpaperInfo(params);
    base::Value::Dict dict = actual_info.ToDict();
    std::optional<WallpaperInfo> expected_info = WallpaperInfo::FromDict(dict);
    EXPECT_TRUE(actual_info.MatchesAsset(expected_info.value()));
  }
  {
    // WallpaperType::kCustomized
    WallpaperInfo actual_info = WallpaperInfo(
        std::string(), WALLPAPER_LAYOUT_CENTER_CROPPED,
        WallpaperType::kCustomized, base::Time::Now(), std::string());
    base::Value::Dict dict = actual_info.ToDict();
    std::optional<WallpaperInfo> expected_info = WallpaperInfo::FromDict(dict);
    EXPECT_TRUE(actual_info.MatchesAsset(expected_info.value()));
  }
}

}  // namespace
}  // namespace ash