File: google_photos_wallpaper_handlers_unittest.cc

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (452 lines) | stat: -rw-r--r-- 18,043 bytes parent folder | download | duplicates (5)
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
// Copyright 2025 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/ash/wallpaper_handlers/google_photos_wallpaper_handlers.h"

#include <memory>
#include <string>
#include <string_view>
#include <vector>

#include "ash/webui/personalization_app/mojom/personalization_app.mojom.h"
#include "base/json/json_reader.h"
#include "base/memory/raw_ptr.h"
#include "base/values.h"
#include "chrome/browser/ash/login/users/fake_chrome_user_manager.h"
#include "chrome/browser/ash/settings/scoped_cros_settings_test_helper.h"
#include "chrome/browser/ash/wallpaper_handlers/wallpaper_fetcher_delegate.h"
#include "chrome/test/base/scoped_testing_local_state.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 "components/user_manager/scoped_user_manager.h"
#include "content/public/browser/browser_context.h"
#include "content/public/test/browser_task_environment.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/icu/source/i18n/unicode/timezone.h"

namespace wallpaper_handlers {

namespace {

constexpr char kFakeTestEmail[] = "fakeemail@personalization";

constexpr char kGooglePhotosResumeTokenOnlyResponse[] =
    "{\"resumeToken\": \"token\"}";
constexpr char kGooglePhotosResumeToken[] = "token";

constexpr char kGooglePhotosPhotosFullResponse[] =
    "{"
    "   \"item\": [ {"
    "      \"itemId\": {"
    "         \"mediaKey\": \"photoId\""
    "      },"
    "      \"dedupKey\": \"dedupKey\","
    "      \"filename\": \"photoName.png\","
    "      \"creationTimestamp\": \"2021-12-31T07:07:07.000Z\","
    "      \"photo\": {"
    "         \"servingUrl\": \"https://www.google.com/\""
    "      },"
    "      \"locationFeature\": {"
    "         \"name\": [ {"
    "            \"text\": \"home\""
    "         } ]"
    "      }"
    "   } ],"
    "   \"resumeToken\": \"token\""
    "}";

constexpr char kGooglePhotosPhotosSingleItemResponse[] =
    "{"
    "   \"item\": {"
    "      \"itemId\": {"
    "         \"mediaKey\": \"photoId\""
    "      },"
    "      \"dedupKey\": \"dedupKey\","
    "      \"filename\": \"photoName.png\","
    "      \"creationTimestamp\": \"2021-12-31T07:07:07.000Z\","
    "      \"photo\": {"
    "         \"servingUrl\": \"https://www.google.com/\""
    "      },"
    "      \"locationFeature\": {"
    "         \"name\": [ {"
    "            \"text\": \"home\""
    "         } ]"
    "      }"
    "   }"
    "}";

constexpr char kGooglePhotosAlbumsFullResponse[] =
    "{"
    "   \"collection\": [ {"
    "      \"collectionId\": {"
    "         \"mediaKey\": \"albumId\""
    "      },"
    "      \"coverItemServingUrl\": \"https://www.google.com/\","
    "      \"name\": \"title\","
    "      \"numPhotos\": \"1\","
    "      \"latestModificationTimestamp\": \"2021-12-31T07:07:07.000Z\""
    "   } ],"
    "   \"resumeToken\": \"token\""
    "}";

// Parses `json` as a value dictionary. A test calling this function will fail
// if `json` is not appropriately formatted.
base::Value::Dict JsonToDict(std::string_view json) {
  std::optional<base::Value> parsed_json = base::JSONReader::Read(json);
  EXPECT_TRUE(parsed_json.has_value() && parsed_json->is_dict());
  return std::move(*parsed_json).TakeDict();
}

// Returns a non-null pointer to the photo in a hypothetical Google Photos
// photos query response. A test calling this function will fail if the response
// does not contain exactly one photo.
base::Value::Dict* GetPhotoFromGooglePhotosPhotosResponse(
    base::Value::Dict* response) {
  EXPECT_TRUE(response);
  auto* photos = response->FindList("item");
  EXPECT_TRUE(photos && photos->size() == 1);
  auto* photo = photos->front().GetIfDict();
  EXPECT_TRUE(photo);
  return photo;
}

// Returns a non-null pointer to the album in a hypothetical Google Photos
// albums query response. A test calling this function will fail if the response
// does not contain exactly one album.
base::Value::Dict* GetAlbumFromGooglePhotosAlbumsResponse(
    base::Value::Dict* response) {
  EXPECT_TRUE(response);
  auto* albums = response->FindList("collection");
  EXPECT_TRUE(albums && albums->size() == 1);
  auto* album = albums->front().GetIfDict();
  EXPECT_TRUE(album);
  return album;
}

using ash::personalization_app::mojom::FetchGooglePhotosAlbumsResponse;
using ash::personalization_app::mojom::FetchGooglePhotosPhotosResponse;
using ash::personalization_app::mojom::GooglePhotosAlbum;
using ash::personalization_app::mojom::GooglePhotosAlbumPtr;
using ash::personalization_app::mojom::GooglePhotosEnablementState;
using ash::personalization_app::mojom::GooglePhotosPhoto;
using ash::personalization_app::mojom::GooglePhotosPhotoPtr;

}  // namespace

class GooglePhotosFetcherTestBase : public testing::Test {
 public:
  GooglePhotosFetcherTestBase()
      : scoped_user_manager_(std::make_unique<ash::FakeChromeUserManager>()),
        profile_manager_(TestingBrowserProcess::GetGlobal()) {}

  GooglePhotosFetcherTestBase(const GooglePhotosFetcherTestBase&) = delete;
  GooglePhotosFetcherTestBase& operator=(const GooglePhotosFetcherTestBase&) =
      delete;
  ~GooglePhotosFetcherTestBase() override = default;

  TestingProfile* profile() { return profile_; }

 protected:
  // testing::Test:
  void SetUp() override {
    ASSERT_TRUE(profile_manager_.SetUp());
    profile_ = profile_manager_.CreateTestingProfile(kFakeTestEmail);
  }

 private:
  content::BrowserTaskEnvironment task_environment_;
  user_manager::ScopedUserManager scoped_user_manager_;
  TestingProfileManager profile_manager_;
  raw_ptr<TestingProfile> profile_;
};

class GooglePhotosEnabledFetcherTest : public GooglePhotosFetcherTestBase {
 public:
  GooglePhotosEnablementState ParseResponse(base::Value::Dict* response) {
    return google_photos_enabled_fetcher_->ParseResponse(response);
  }

  std::optional<size_t> GetResultCount(
      const GooglePhotosEnablementState& result) {
    return google_photos_enabled_fetcher_->GetResultCount(result);
  }

 protected:
  void SetUp() override {
    GooglePhotosFetcherTestBase::SetUp();
    google_photos_enabled_fetcher_ =
        std::make_unique<WallpaperFetcherDelegateImpl>()
            ->CreateGooglePhotosEnabledFetcher(profile());
  }

 private:
  std::unique_ptr<GooglePhotosEnabledFetcher> google_photos_enabled_fetcher_;
};

TEST_F(GooglePhotosEnabledFetcherTest, ParseGooglePhotosEnabled) {
  // Parse an absent response (simulating a fetching error).
  auto result = GooglePhotosEnablementState::kError;
  EXPECT_EQ(ParseResponse(nullptr), result);
  EXPECT_EQ(GetResultCount(result), std::nullopt);

  // Parse a response without an enabled state.
  base::Value::Dict response;
  EXPECT_EQ(ParseResponse(&response), result);
  EXPECT_EQ(GetResultCount(result), std::nullopt);

  // Parse a response with an unknown enabled state.
  response.SetByDottedPath("status.userState", "UNKNOWN_STATUS_STATE");
  EXPECT_EQ(ParseResponse(&response), result);
  EXPECT_EQ(GetResultCount(result), std::nullopt);

  // Parse a response indicating that the user cannot access Google Photos data.
  response.SetByDottedPath("status.userState", "USER_DASHER_DISABLED");
  result = GooglePhotosEnablementState::kDisabled;
  EXPECT_EQ(ParseResponse(&response), result);
  EXPECT_EQ(GetResultCount(result), std::make_optional<size_t>(1u));

  // Parse a response indicating that the user can access Google Photos data.
  response.SetByDottedPath("status.userState", "USER_PERMITTED");
  result = GooglePhotosEnablementState::kEnabled;
  EXPECT_EQ(ParseResponse(&response), result);
  EXPECT_EQ(GetResultCount(result), std::make_optional<size_t>(1u));
}

class GooglePhotosPhotosFetcherTest : public GooglePhotosFetcherTestBase {
 public:
  GooglePhotosPhotosCbkArgs ParseResponse(const base::Value::Dict* response) {
    return google_photos_photos_fetcher_->ParseResponse(response);
  }

  std::optional<size_t> GetResultCount(
      const GooglePhotosPhotosCbkArgs& result) {
    return google_photos_photos_fetcher_->GetResultCount(result);
  }

 protected:
  void SetUp() override {
    GooglePhotosFetcherTestBase::SetUp();
    google_photos_photos_fetcher_ =
        std::make_unique<WallpaperFetcherDelegateImpl>()
            ->CreateGooglePhotosPhotosFetcher(profile());
  }

 private:
  std::unique_ptr<GooglePhotosPhotosFetcher> google_photos_photos_fetcher_;
};

TEST_F(GooglePhotosPhotosFetcherTest, ParseGooglePhotosPhotosAbsentPhoto) {
  // Parse an absent response (simulating a fetching error).
  auto result = FetchGooglePhotosPhotosResponse::New();
  EXPECT_EQ(ParseResponse(nullptr), result);
  EXPECT_EQ(GetResultCount(result), std::nullopt);

  // Parse a response with no resume token or photos.
  base::Value::Dict empty_response;
  EXPECT_EQ(ParseResponse(&empty_response), result);
  EXPECT_EQ(GetResultCount(result), std::nullopt);

  // Parse a response with a resume token and no photos.
  auto response = JsonToDict(kGooglePhotosResumeTokenOnlyResponse);
  result = FetchGooglePhotosPhotosResponse::New(std::nullopt,
                                                kGooglePhotosResumeToken);
  EXPECT_EQ(ParseResponse(&response), result);
  EXPECT_EQ(GetResultCount(result), std::optional<size_t>());
}

TEST_F(GooglePhotosPhotosFetcherTest, ParsePhotosInvalidPhoto) {
  auto result = FetchGooglePhotosPhotosResponse::New(
      std::vector<GooglePhotosPhotoPtr>(), kGooglePhotosResumeToken);

  // Parse one-photo responses where one of the photo's fields is missing.
  for (const auto* const path : {"itemId.mediaKey", "filename",
                                 "creationTimestamp", "photo.servingUrl"}) {
    auto response = JsonToDict(kGooglePhotosPhotosFullResponse);
    auto* photo = GetPhotoFromGooglePhotosPhotosResponse(&response);
    photo->RemoveByDottedPath(path);
    EXPECT_EQ(ParseResponse(&response), result);
    EXPECT_EQ(GetResultCount(result), std::make_optional<size_t>(0u));
  }

  // Parse one-photo responses where one of the photo's fields has an invalid
  // value.
  std::vector<std::pair<std::string, std::string>> invalid_field_test_cases = {
      {"creationTimestamp", ""},
      {"creationTimestamp", "Bad timestamp"},
      {"creationTimestamp", "2021T07:07:07.000Z"},
      {"creationTimestamp", "31T07:07:07.000Z"},
      {"creationTimestamp", "12T07:07:07.000Z"},
      {"creationTimestamp", "12-31T07:07:07.000Z"},
      {"creationTimestamp", "2021-31T07:07:07.000Z"},
      {"creationTimestamp", "2021-12T07:07:07.000Z"},
      {"creationTimestamp", "-2021-12-31T07:07:07.000Z"}};
  for (const auto& kv : invalid_field_test_cases) {
    auto response = JsonToDict(kGooglePhotosPhotosFullResponse);
    auto* photo = GetPhotoFromGooglePhotosPhotosResponse(&response);
    photo->SetByDottedPath(kv.first, kv.second);
    EXPECT_EQ(ParseResponse(&response), result);
    EXPECT_EQ(GetResultCount(result), std::make_optional<size_t>(0u));
  }
}

TEST_F(GooglePhotosPhotosFetcherTest, ParsePhotosValidPhoto) {
  // Ensure that photo timestamps resolve to the same date on all testing
  // platforms.
  icu::TimeZone::adoptDefault(icu::TimeZone::createTimeZone("UTC"));

  // Parse a response with a valid photo and a resume token.
  auto valid_photos_vector = std::vector<GooglePhotosPhotoPtr>();
  valid_photos_vector.push_back(GooglePhotosPhoto::New(
      "photoId", "dedupKey", "photoName", u"Friday, December 31, 2021",
      GURL("https://www.google.com/"), "home"));
  auto response = JsonToDict(kGooglePhotosPhotosFullResponse);
  auto result = FetchGooglePhotosPhotosResponse::New(
      mojo::Clone(valid_photos_vector), kGooglePhotosResumeToken);
  EXPECT_EQ(ParseResponse(&response), result);
  EXPECT_EQ(GetResultCount(result),
            std::make_optional<size_t>(valid_photos_vector.size()));

  // Parse a response with a valid photo and no resume token.
  response.Remove("resumeToken");
  result = FetchGooglePhotosPhotosResponse::New(
      mojo::Clone(valid_photos_vector), std::nullopt);
  EXPECT_EQ(ParseResponse(&response), result);
  EXPECT_EQ(GetResultCount(result),
            std::make_optional<size_t>(valid_photos_vector.size()));

  // Parse a response with a single valid photo not in a list.
  response = JsonToDict(kGooglePhotosPhotosSingleItemResponse);
  EXPECT_EQ(ParseResponse(&response), result);
  EXPECT_EQ(GetResultCount(result),
            std::make_optional<size_t>(valid_photos_vector.size()));

  // Parse a response with a valid photo and no dedup key.
  auto valid_photos_vector_without_dedup_key =
      std::vector<GooglePhotosPhotoPtr>();
  valid_photos_vector_without_dedup_key.push_back(GooglePhotosPhoto::New(
      "photoId", std::nullopt, "photoName", u"Friday, December 31, 2021",
      GURL("https://www.google.com/"), "home"));
  response.RemoveByDottedPath("item.dedupKey");
  result = FetchGooglePhotosPhotosResponse::New(
      mojo::Clone(valid_photos_vector_without_dedup_key), std::nullopt);
  EXPECT_EQ(result, ParseResponse(&response));
  EXPECT_EQ(
      GetResultCount(result),
      std::make_optional<size_t>(valid_photos_vector_without_dedup_key.size()));

  // Parse a response with a valid photo and no location.
  auto valid_photos_vector_without_location =
      std::vector<GooglePhotosPhotoPtr>();
  valid_photos_vector_without_location.push_back(GooglePhotosPhoto::New(
      "photoId", std::nullopt, "photoName", u"Friday, December 31, 2021",
      GURL("https://www.google.com/"), std::nullopt));
  auto* name_list = response.FindListByDottedPath("item.locationFeature.name");
  EXPECT_FALSE(name_list->empty());
  name_list->clear();
  result = FetchGooglePhotosPhotosResponse::New(
      mojo::Clone(valid_photos_vector_without_location), std::nullopt);
  EXPECT_EQ(result, ParseResponse(&response));
  EXPECT_EQ(
      GetResultCount(result),
      std::make_optional<size_t>(valid_photos_vector_without_location.size()));
}

class GooglePhotosAlbumsFetcherTest : public GooglePhotosFetcherTestBase {
 public:
  GooglePhotosAlbumsCbkArgs ParseResponse(const base::Value::Dict* response) {
    return google_photos_albums_fetcher_->ParseResponse(response);
  }

  std::optional<size_t> GetResultCount(
      const GooglePhotosAlbumsCbkArgs& result) {
    return google_photos_albums_fetcher_->GetResultCount(result);
  }

 protected:
  void SetUp() override {
    GooglePhotosFetcherTestBase::SetUp();
    google_photos_albums_fetcher_ =
        std::make_unique<WallpaperFetcherDelegateImpl>()
            ->CreateGooglePhotosAlbumsFetcher(profile());
  }

 private:
  std::unique_ptr<GooglePhotosAlbumsFetcher> google_photos_albums_fetcher_;
};

TEST_F(GooglePhotosAlbumsFetcherTest, ParseAlbumsAbsentAlbum) {
  // Parse an absent response (simulating a fetching error).
  auto result = FetchGooglePhotosAlbumsResponse::New();
  EXPECT_EQ(ParseResponse(nullptr), result);
  EXPECT_EQ(GetResultCount(result), std::nullopt);

  // Parse a response with no resume token or albums.
  base::Value::Dict empty_response;
  EXPECT_EQ(ParseResponse(&empty_response), result);

  // Parse a response with a resume token and no albums.
  auto response = JsonToDict(kGooglePhotosResumeTokenOnlyResponse);
  result = FetchGooglePhotosAlbumsResponse::New(std::nullopt,
                                                kGooglePhotosResumeToken);
  EXPECT_EQ(ParseResponse(&response), result);
  EXPECT_EQ(GetResultCount(result), std::nullopt);
}

TEST_F(GooglePhotosAlbumsFetcherTest, ParseAlbumsInvalidAlbum) {
  auto result = FetchGooglePhotosAlbumsResponse::New(
      std::vector<GooglePhotosAlbumPtr>(), kGooglePhotosResumeToken);

  // Parse one-album responses where one of the album's fields is missing.
  for (const auto* const path :
       {"collectionId.mediaKey", "name", "numPhotos", "coverItemServingUrl"}) {
    auto response = JsonToDict(kGooglePhotosAlbumsFullResponse);
    auto* album = GetAlbumFromGooglePhotosAlbumsResponse(&response);
    album->RemoveByDottedPath(path);
    EXPECT_EQ(ParseResponse(&response), result);
    EXPECT_EQ(GetResultCount(result), std::make_optional<size_t>(0u));
  }

  // Parse one-album responses where one of the album's fields has an invalid
  // value.
  std::vector<std::pair<std::string, std::string>> invalid_field_test_cases = {
      {"numPhotos", ""},
      {"numPhotos", "NaN"},
      {"numPhotos", "-1"},
      {"numPhotos", "0"}};
  for (const auto& kv : invalid_field_test_cases) {
    auto response = JsonToDict(kGooglePhotosAlbumsFullResponse);
    auto* album = GetAlbumFromGooglePhotosAlbumsResponse(&response);
    album->SetByDottedPath(kv.first, kv.second);
    EXPECT_EQ(ParseResponse(&response), result);
    EXPECT_EQ(GetResultCount(result), std::make_optional<size_t>(0u));
  }
}

TEST_F(GooglePhotosAlbumsFetcherTest, ParseAlbumsValidAlbum) {
  // Parse a response with a valid album and a resume token.
  auto response = JsonToDict(kGooglePhotosAlbumsFullResponse);
  auto valid_albums_vector = std::vector<GooglePhotosAlbumPtr>();
  base::Time timestamp;
  EXPECT_TRUE(
      base::Time::FromUTCString("2021-12-31T07:07:07.000Z", &timestamp));
  valid_albums_vector.push_back(GooglePhotosAlbum::New(
      "albumId", "title", 1, GURL("https://www.google.com/"), timestamp,
      /*is_shared=*/false));
  auto result = FetchGooglePhotosAlbumsResponse::New(
      mojo::Clone(valid_albums_vector), kGooglePhotosResumeToken);
  EXPECT_EQ(ParseResponse(&response), result);
  EXPECT_EQ(GetResultCount(result),
            std::make_optional<size_t>(valid_albums_vector.size()));

  // Parse a response with a valid album and no resume token.
  response.Remove("resumeToken");
  result = FetchGooglePhotosAlbumsResponse::New(
      mojo::Clone(valid_albums_vector), std::nullopt);
  EXPECT_EQ(ParseResponse(&response), result);
  EXPECT_EQ(GetResultCount(result),
            std::make_optional<size_t>(valid_albums_vector.size()));
}

}  // namespace wallpaper_handlers