File: photo_view_unittest.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; 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,806; 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 (427 lines) | stat: -rw-r--r-- 16,824 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
// Copyright 2020 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/ui/photo_view.h"

#include "ash/ambient/ambient_constants.h"
#include "ash/ambient/ambient_controller.h"
#include "ash/ambient/ambient_ui_settings.h"
#include "ash/ambient/test/ambient_ash_test_base.h"
#include "ash/ambient/ui/ambient_container_view.h"
#include "ash/assistant/ui/assistant_view_ids.h"
#include "ash/public/cpp/ambient/proto/photo_cache_entry.pb.h"
#include "ash/webui/personalization_app/mojom/personalization_app.mojom-shared.h"
#include "base/strings/string_number_conversions.h"
#include "ui/display/manager/display_manager.h"
#include "ui/display/test/display_manager_test_api.h"
#include "ui/gfx/geometry/rect.h"
#include "ui/gfx/image/image_skia.h"
#include "ui/views/controls/image_view.h"

namespace ash {

class AmbientPhotoViewTest : public AmbientAshTestBase {
 protected:
  void SetUp() override {
    AmbientAshTestBase::SetUp();
    SetAmbientTheme(personalization_app::mojom::AmbientTheme::kSlideshow);
  }
};

// Test that a new topic s rendered every cycle.
TEST_F(AmbientPhotoViewTest, ShouldRefreshImagesEveryCycle) {
  UpdateDisplay("600x800");

  SetAmbientShownAndWaitForWidgets();
  gfx::ImageSkia image_1 = GetAmbientBackgroundImageView()->GetCurrentImage();
  ASSERT_FALSE(image_1.isNull());

  // It takes 2 cycles to refresh both AmbientBackgroundImageViews owned by the
  // PhotoView, guaranteeing that the images for both should have changed.
  FastForwardByPhotoRefreshInterval();
  FastForwardByPhotoRefreshInterval();
  gfx::ImageSkia image_2 = GetAmbientBackgroundImageView()->GetCurrentImage();
  ASSERT_FALSE(image_2.isNull());
  EXPECT_FALSE(image_2.BackedBySameObjectAs(image_1));

  FastForwardByPhotoRefreshInterval();
  FastForwardByPhotoRefreshInterval();
  gfx::ImageSkia image_3 = GetAmbientBackgroundImageView()->GetCurrentImage();
  ASSERT_FALSE(image_3.isNull());
  EXPECT_FALSE(image_3.BackedBySameObjectAs(image_2));
}

// Test that image is scaled to fill screen width when image is portrait and
// screen is portrait. The top and bottom of the image will be cut off, as
// the height of the image is taller than the height of the screen.
TEST_F(AmbientPhotoViewTest, ShouldResizePortraitImageForPortraitScreen) {
  SetPhotoOrientation(/*portrait=*/true);
  SetDecodedPhotoSize(/*width=*/10, /*height=*/20);

  UpdateDisplay("600x800");

  SetAmbientShownAndWaitForWidgets();

  FastForwardByPhotoRefreshInterval();

  auto* image_view = GetAmbientBackgroundImageView();

  // Image should be full width. Image height should extend above and below the
  // visible part of the screen.
  ASSERT_EQ(image_view->GetImageBoundsInScreenForTesting(),
            gfx::Rect(/*x=*/0, /*y=*/-200, /*width=*/600, /*height=*/1200));
  ASSERT_EQ(image_view->GetRelatedImageBoundsInScreenForTesting(), gfx::Rect());
}

// Test that two landscape images are scaled and filed to fill screen when the
// screen is portrait.
TEST_F(AmbientPhotoViewTest, ShouldResizeLandscapeImageForPortraitScreen) {
  SetDecodedPhotoSize(/*width=*/30, /*height=*/20);

  UpdateDisplay("600x808");

  SetAmbientShownAndWaitForWidgets();

  FastForwardByPhotoRefreshInterval();

  auto* image_view = GetAmbientBackgroundImageView();

  ASSERT_EQ(image_view->GetImageBoundsInScreenForTesting(),
            gfx::Rect(/*x=*/0, /*y=*/0, /*width=*/600, /*height=*/400));
  ASSERT_EQ(image_view->GetRelatedImageBoundsInScreenForTesting(),
            gfx::Rect(/*x=*/0, /*y=*/408, /*width=*/600, /*height=*/400));
}

// Test that two portrait images are scaled and tiled to fill screen when the
// screen is landscape.
TEST_F(AmbientPhotoViewTest, ShouldTileTwoPortraitImagesForLandscapeScreen) {
  SetPhotoOrientation(/*portrait=*/true);
  SetDecodedPhotoSize(/*width=*/10, /*height=*/20);

  UpdateDisplay("808x600");

  SetAmbientShownAndWaitForWidgets();

  FastForwardByPhotoRefreshInterval();

  auto* image_view = GetAmbientBackgroundImageView();

  // Will tile two portrait images. Each image will fill 400x600 area with 8px
  // spaing in between.
  // Image should be full width. Image height should extend above and below the
  // visible part of the view.
  ASSERT_EQ(image_view->GetImageBoundsInScreenForTesting(),
            gfx::Rect(/*x=*/0, /*y=*/-100, /*width=*/400, /*height=*/800));
  ASSERT_EQ(image_view->GetRelatedImageBoundsInScreenForTesting(),
            gfx::Rect(/*x=*/408, /*y=*/-100, /*width=*/400, /*height=*/800));
}

// Test that two portrait images are scaled and tiled to fill screen when the
// screen is landscape. For odd screen width, the first image will take one more
// pixel.
TEST_F(AmbientPhotoViewTest,
       ShouldTileTwoPortraitImagesForLandscapeScreenWithOddWidth) {
  SetPhotoOrientation(/*portrait=*/true);
  SetDecodedPhotoSize(/*width=*/10, /*height=*/20);

  constexpr int kScreenWidth = 809;
  constexpr int kScreenHeight = 600;
  std::string display_size = base::NumberToString(kScreenWidth) + "x" +
                             base::NumberToString(kScreenHeight);
  UpdateDisplay(display_size);

  SetAmbientShownAndWaitForWidgets();

  FastForwardByPhotoRefreshInterval();

  auto* image_view = GetAmbientBackgroundImageView();

  // Will tile two portrait images.
  // The first image will fill 401x802 area with 8px spaing in between. Image
  // should be full width. Image height should extend above and below the
  // visible part of the view.
  const int image_width = (kScreenWidth - kMarginLeftOfRelatedImageDip + 1) / 2;
  const int image_height = 20 * image_width / 10;
  const int y = (kScreenHeight - image_height) / 2;
  ASSERT_EQ(image_view->GetImageBoundsInScreenForTesting(),
            gfx::Rect(/*x=*/0, /*y=*/y, /*width=*/image_width,
                      /*height=*/image_height));
  ASSERT_EQ(image_view->GetRelatedImageBoundsInScreenForTesting(),
            gfx::Rect(/*x=*/409, /*y=*/-100, /*width=*/400, /*height=*/800));
}

// Test that only show one landscape image when screen is landscape.
TEST_F(AmbientPhotoViewTest,
       ShouldNotTileTwoLandscapeImagesForLandscapeScreen) {
  SetDecodedPhotoSize(/*width=*/20, /*height=*/10);

  UpdateDisplay("800x600");

  SetAmbientShownAndWaitForWidgets();

  FastForwardByPhotoRefreshInterval();

  auto* image_view = GetAmbientBackgroundImageView();

  // Show one landscape image.
  // Image should be full height. Image width should extend equally to the left
  // and right of the visible part of the screen.
  ASSERT_EQ(image_view->GetImageBoundsInScreenForTesting(),
            gfx::Rect(/*x=*/-200, /*y=*/0, /*width=*/1200, /*height=*/600));
  ASSERT_EQ(image_view->GetRelatedImageBoundsInScreenForTesting(), gfx::Rect());
}

// Test that only have one available image will not be tiled when screen is
// landscape.
TEST_F(AmbientPhotoViewTest,
       ShouldNotTileIfRelatedImageIsNullForLandscapeScreen) {
  SetPhotoOrientation(/*portrait=*/true);
  SetDecodedPhotoSize(/*width=*/10, /*height=*/20);

  UpdateDisplay("800x600");

  SetAmbientShownAndWaitForWidgets();

  FastForwardByPhotoRefreshInterval();

  auto* image_view = GetAmbientBackgroundImageView();

  // Remove the related image.
  image_view->ResetRelatedImageForTesting();

  // Trigger layout.
  UpdateDisplay("808x600");

  // Only show one portrait image.
  // Image should be full height. Image should have equal empty space left and
  // right.
  ASSERT_EQ(image_view->GetImageBoundsInScreenForTesting(),
            gfx::Rect(/*x=*/254, /*y=*/0, /*width=*/300, /*height=*/600));
  ASSERT_EQ(image_view->GetRelatedImageBoundsInScreenForTesting(), gfx::Rect());
}

// Test that image is scaled to fill screen height when the image is landscape
// and no related image when the screen is landscape.
// The image will be zoomed in and the left and right will be cut off, as the
// width of the image is greater than the width of the screen.
TEST_F(AmbientPhotoViewTest, ShouldResizeLandscapeImageForLandscapeScreen) {
  SetDecodedPhotoSize(/*width=*/30, /*height=*/20);

  UpdateDisplay("800x600");

  SetAmbientShownAndWaitForWidgets();

  FastForwardByPhotoRefreshInterval();

  auto* image_view = GetAmbientBackgroundImageView();

  // Remove the related image.
  image_view->ResetRelatedImageForTesting();

  // Trigger layout.
  UpdateDisplay("808x600");

  // Image should be full height. Image width should extend equally to the left
  // and right of the visible part of the screen.
  ASSERT_EQ(image_view->GetImageBoundsInScreenForTesting(),
            gfx::Rect(/*x=*/-46, /*y=*/0, /*width=*/900, /*height=*/600));
  ASSERT_EQ(image_view->GetRelatedImageBoundsInScreenForTesting(), gfx::Rect());
}

// Test that when rotates to portrait screen, will dynamically only show one
// portrait image.
TEST_F(AmbientPhotoViewTest,
       ShouldNotTilePortraitImagesWhenRotateToPortraitScreen) {
  SetPhotoOrientation(/*portrait=*/true);
  SetDecodedPhotoSize(/*width=*/10, /*height=*/20);

  UpdateDisplay("808x600");

  SetAmbientShownAndWaitForWidgets();

  FastForwardByPhotoRefreshInterval();

  auto* image_view = GetAmbientBackgroundImageView();

  // Will tile two portrait images. Each image will fill 400x600 area with 8px
  // spaing in between.
  // Image should be full width. Image height should extend above and below the
  // visible part of the view.
  ASSERT_EQ(image_view->GetImageBoundsInScreenForTesting(),
            gfx::Rect(/*x=*/0, /*y=*/-100, /*width=*/400, /*height=*/800));
  ASSERT_EQ(image_view->GetRelatedImageBoundsInScreenForTesting(),
            gfx::Rect(/*x=*/408, /*y=*/-100, /*width=*/400, /*height=*/800));

  // Rotate screen.
  UpdateDisplay("600x808");
  // Only one image will show.
  // Image should be full width. Image height should extend above and below the
  // visible part of the screen.
  ASSERT_EQ(image_view->GetImageBoundsInScreenForTesting(),
            gfx::Rect(/*x=*/0, /*y=*/-196, /*width=*/600, /*height=*/1200));
  ASSERT_EQ(image_view->GetRelatedImageBoundsInScreenForTesting(), gfx::Rect());
}

// Test that when rotates to portrait screen, will dynamically show two paired
// landscape images.
TEST_F(AmbientPhotoViewTest,
       ShouldTileLandscapeImagesWhenRotateToPortraitScreen) {
  SetDecodedPhotoSize(/*width=*/20, /*height=*/10);

  UpdateDisplay("808x600");

  SetAmbientShownAndWaitForWidgets();

  FastForwardByPhotoRefreshInterval();

  auto* image_view = GetAmbientBackgroundImageView();

  // Will show one landscape image.
  // Image should be full height. Image height should extend left and right the
  // visible part of the view.
  ASSERT_EQ(image_view->GetImageBoundsInScreenForTesting(),
            gfx::Rect(/*x=*/-196, /*y=*/0, /*width=*/1200, /*height=*/600));
  ASSERT_EQ(image_view->GetRelatedImageBoundsInScreenForTesting(), gfx::Rect());

  // Rotate screen.
  UpdateDisplay("600x808");
  // Will show paired landscape images.
  // Image should be full height. Image height should extend left and right the
  // visible part of the screen.
  ASSERT_EQ(image_view->GetImageBoundsInScreenForTesting(),
            gfx::Rect(/*x=*/-100, /*y=*/0, /*width=*/800, /*height=*/400));
  ASSERT_EQ(image_view->GetRelatedImageBoundsInScreenForTesting(),
            gfx::Rect(/*x=*/-100, /*y=*/408, /*width=*/800, /*height=*/400));
}

// Test that when rotates to portrait screen, will rotate Geo landscape images.
TEST_F(AmbientPhotoViewTest,
       ShouldRotateGeoLandscapeImageWhenRotateToPortraitScreen) {
  SetDecodedPhotoSize(/*width=*/20, /*height=*/10);
  SetPhotoTopicType(::ambient::TopicType::kGeo);

  UpdateDisplay("808x600");

  SetAmbientShownAndWaitForWidgets();

  FastForwardByPhotoRefreshInterval();

  auto* image_view = GetAmbientBackgroundImageView();

  // Will show one landscape image.
  // Image should be full height. Image height should extend left and right the
  // visible part of the view.
  ASSERT_EQ(image_view->GetImageBoundsInScreenForTesting(),
            gfx::Rect(/*x=*/-196, /*y=*/0, /*width=*/1200, /*height=*/600));
  ASSERT_EQ(image_view->GetRelatedImageBoundsInScreenForTesting(), gfx::Rect());

  // Rotate screen.
  int64_t internal_display_id =
      display::test::DisplayManagerTestApi(display_manager())
          .SetFirstDisplayAsInternalDisplay();
  display_manager()->SetDisplayRotation(internal_display_id,
                                        display::Display::Rotation::ROTATE_90,
                                        display::Display::RotationSource::USER);
  // Will show one rotated image.
  // Image should be full width. Image height should extend above and below the
  // visible part of the screen.
  ASSERT_EQ(image_view->GetImageBoundsInScreenForTesting(),
            gfx::Rect(/*x=*/0, /*y=*/-196, /*width=*/600, /*height=*/1200));
  ASSERT_EQ(image_view->GetRelatedImageBoundsInScreenForTesting(), gfx::Rect());
}

// Test that when rotates to landscape screen, will dynamically tile two
// portrait images.
TEST_F(AmbientPhotoViewTest, ShouldTileWhenRotateToLandscapeScreen) {
  SetPhotoOrientation(/*portrait=*/true);
  SetDecodedPhotoSize(/*width=*/10, /*height=*/20);

  UpdateDisplay("600x808");

  SetAmbientShownAndWaitForWidgets();

  FastForwardByPhotoRefreshInterval();

  auto* image_view = GetAmbientBackgroundImageView();

  // Only one image will show.
  // Image should be full width. Image height should extend above and below the
  // visible part of the screen.
  ASSERT_EQ(image_view->GetImageBoundsInScreenForTesting(),
            gfx::Rect(/*x=*/0, /*y=*/-196, /*width=*/600, /*height=*/1200));
  ASSERT_EQ(image_view->GetRelatedImageBoundsInScreenForTesting(), gfx::Rect());

  // Rotate screen.
  UpdateDisplay("808x600");

  // Will tile two portrait images. Each image will fill 400x600 area with 8px
  // spaing in between.
  // Image should be full width. Image height should extend above and below the
  // visible part of the view.
  ASSERT_EQ(image_view->GetImageBoundsInScreenForTesting(),
            gfx::Rect(/*x=*/0, /*y=*/-100, /*width=*/400, /*height=*/800));
  ASSERT_EQ(image_view->GetRelatedImageBoundsInScreenForTesting(),
            gfx::Rect(/*x=*/408, /*y=*/-100, /*width=*/400, /*height=*/800));
}

// Test that two protrat images will resize when bounds changes in landscape.
TEST_F(AmbientPhotoViewTest, ShouldResizeTiledPortraitImagesWhenBoundsChanged) {
  SetPhotoOrientation(/*portrait=*/true);
  SetDecodedPhotoSize(/*width=*/10, /*height=*/20);

  UpdateDisplay("808x600");

  SetAmbientShownAndWaitForWidgets();

  FastForwardByPhotoRefreshInterval();

  auto* image_view = GetAmbientBackgroundImageView();

  // Will tile two portrait images. Each image will fill 400x600 area with 8px
  // spaing in between.
  // Image should be full width. Image height should extend above and below the
  // visible part of the view.
  ASSERT_EQ(image_view->GetImageBoundsInScreenForTesting(),
            gfx::Rect(/*x=*/0, /*y=*/-100, /*width=*/400, /*height=*/800));
  ASSERT_EQ(image_view->GetRelatedImageBoundsInScreenForTesting(),
            gfx::Rect(/*x=*/408, /*y=*/-100, /*width=*/400, /*height=*/800));

  // Bounds changes so that image will be shown in portrait view.
  UpdateDisplay("508x200");

  // Will tile two portrait images. Each image will fill 250x200 area with 8px
  // spaing in between.
  // Image should be full height. Image should have equal empty space left and
  // right.
  ASSERT_EQ(image_view->GetImageBoundsInScreenForTesting(),
            gfx::Rect(/*x=*/75, /*y=*/0, /*width=*/100, /*height=*/200));
  ASSERT_EQ(
      image_view->GetRelatedImageBoundsInScreenForTesting(),
      gfx::Rect(/*x=*/(258 + 75), /*y=*/0, /*width=*/100, /*height=*/200));

  // Bounds changes so that image will be shown in portrait view.
  UpdateDisplay("308x200");

  // Will tile two portrait images. Each image will fill 150x200 area with 8px
  // spaing in between.
  // Image should be full width. Image height should extend above and below the
  // visible part of the view.
  ASSERT_EQ(image_view->GetImageBoundsInScreenForTesting(),
            gfx::Rect(/*x=*/0, /*y=*/-50, /*width=*/150, /*height=*/300));
  ASSERT_EQ(image_view->GetRelatedImageBoundsInScreenForTesting(),
            gfx::Rect(/*x=*/158, /*y=*/-50, /*width=*/150, /*height=*/300));

  // Bounds changes to exact aspect ratio of the image.
  UpdateDisplay("208x200");

  // Will tile two portrait images. Each image will fill 100x200 area with 8px
  // spaing in between.
  // Image should be full width and height.
  ASSERT_EQ(image_view->GetImageBoundsInScreenForTesting(),
            gfx::Rect(/*x=*/0, /*y=*/0, /*width=*/100, /*height=*/200));
  ASSERT_EQ(image_view->GetRelatedImageBoundsInScreenForTesting(),
            gfx::Rect(/*x=*/108, /*y=*/0, /*width=*/100, /*height=*/200));
}

}  // namespace ash