photo_view_unittest.cc 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. // Copyright 2020 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "ash/ambient/ui/photo_view.h"
  5. #include "ash/ambient/ambient_constants.h"
  6. #include "ash/ambient/ambient_controller.h"
  7. #include "ash/ambient/test/ambient_ash_test_base.h"
  8. #include "ash/ambient/ui/ambient_container_view.h"
  9. #include "ash/assistant/ui/assistant_view_ids.h"
  10. #include "ash/public/cpp/ambient/proto/photo_cache_entry.pb.h"
  11. #include "ui/display/manager/display_manager.h"
  12. #include "ui/display/test/display_manager_test_api.h"
  13. #include "ui/gfx/geometry/rect.h"
  14. #include "ui/gfx/image/image_skia.h"
  15. #include "ui/views/controls/image_view.h"
  16. namespace ash {
  17. using AmbientPhotoViewTest = AmbientAshTestBase;
  18. // Test that a new topic s rendered every cycle.
  19. TEST_F(AmbientPhotoViewTest, ShouldRefreshImagesEveryCycle) {
  20. UpdateDisplay("600x800");
  21. ShowAmbientScreen();
  22. gfx::ImageSkia image_1 = GetAmbientBackgroundImageView()->GetCurrentImage();
  23. ASSERT_FALSE(image_1.isNull());
  24. // It takes 2 cycles to refresh both AmbientBackgroundImageViews owned by the
  25. // PhotoView, guaranteeing that the images for both should have changed.
  26. FastForwardToNextImage();
  27. FastForwardToNextImage();
  28. gfx::ImageSkia image_2 = GetAmbientBackgroundImageView()->GetCurrentImage();
  29. ASSERT_FALSE(image_2.isNull());
  30. EXPECT_FALSE(image_2.BackedBySameObjectAs(image_1));
  31. FastForwardToNextImage();
  32. FastForwardToNextImage();
  33. gfx::ImageSkia image_3 = GetAmbientBackgroundImageView()->GetCurrentImage();
  34. ASSERT_FALSE(image_3.isNull());
  35. EXPECT_FALSE(image_3.BackedBySameObjectAs(image_2));
  36. }
  37. // Test that image is scaled to fill screen width when image is portrait and
  38. // screen is portrait. The top and bottom of the image will be cut off, as
  39. // the height of the image is taller than the height of the screen.
  40. TEST_F(AmbientPhotoViewTest, ShouldResizePortraitImageForPortraitScreen) {
  41. SetPhotoOrientation(/*portrait=*/true);
  42. SetDecodedPhotoSize(/*width=*/10, /*height=*/20);
  43. UpdateDisplay("600x800");
  44. ShowAmbientScreen();
  45. FastForwardToNextImage();
  46. auto* image_view = GetAmbientBackgroundImageView();
  47. // Image should be full width. Image height should extend above and below the
  48. // visible part of the screen.
  49. ASSERT_EQ(image_view->GetImageBoundsInScreenForTesting(),
  50. gfx::Rect(/*x=*/0, /*y=*/-200, /*width=*/600, /*height=*/1200));
  51. ASSERT_EQ(image_view->GetRelatedImageBoundsInScreenForTesting(), gfx::Rect());
  52. }
  53. // Test that two landscape images are scaled and filed to fill screen when the
  54. // screen is portrait.
  55. TEST_F(AmbientPhotoViewTest, ShouldResizeLandscapeImageForPortraitScreen) {
  56. SetDecodedPhotoSize(/*width=*/30, /*height=*/20);
  57. UpdateDisplay("600x808");
  58. ShowAmbientScreen();
  59. FastForwardToNextImage();
  60. auto* image_view = GetAmbientBackgroundImageView();
  61. ASSERT_EQ(image_view->GetImageBoundsInScreenForTesting(),
  62. gfx::Rect(/*x=*/0, /*y=*/0, /*width=*/600, /*height=*/400));
  63. ASSERT_EQ(image_view->GetRelatedImageBoundsInScreenForTesting(),
  64. gfx::Rect(/*x=*/0, /*y=*/408, /*width=*/600, /*height=*/400));
  65. }
  66. // Test that two portrait images are scaled and tiled to fill screen when the
  67. // screen is landscape.
  68. TEST_F(AmbientPhotoViewTest, ShouldTileTwoPortraitImagesForLandscapeScreen) {
  69. SetPhotoOrientation(/*portrait=*/true);
  70. SetDecodedPhotoSize(/*width=*/10, /*height=*/20);
  71. UpdateDisplay("808x600");
  72. ShowAmbientScreen();
  73. FastForwardToNextImage();
  74. auto* image_view = GetAmbientBackgroundImageView();
  75. // Will tile two portrait images. Each image will fill 400x600 area with 8px
  76. // spaing in between.
  77. // Image should be full width. Image height should extend above and below the
  78. // visible part of the view.
  79. ASSERT_EQ(image_view->GetImageBoundsInScreenForTesting(),
  80. gfx::Rect(/*x=*/0, /*y=*/-100, /*width=*/400, /*height=*/800));
  81. ASSERT_EQ(image_view->GetRelatedImageBoundsInScreenForTesting(),
  82. gfx::Rect(/*x=*/408, /*y=*/-100, /*width=*/400, /*height=*/800));
  83. }
  84. // Test that two portrait images are scaled and tiled to fill screen when the
  85. // screen is landscape. For odd screen width, the first image will take one more
  86. // pixel.
  87. TEST_F(AmbientPhotoViewTest,
  88. ShouldTileTwoPortraitImagesForLandscapeScreenWithOddWidth) {
  89. SetPhotoOrientation(/*portrait=*/true);
  90. SetDecodedPhotoSize(/*width=*/10, /*height=*/20);
  91. constexpr int kScreenWidth = 809;
  92. constexpr int kScreenHeight = 600;
  93. std::string display_size = base::NumberToString(kScreenWidth) + "x" +
  94. base::NumberToString(kScreenHeight);
  95. UpdateDisplay(display_size);
  96. ShowAmbientScreen();
  97. FastForwardToNextImage();
  98. auto* image_view = GetAmbientBackgroundImageView();
  99. // Will tile two portrait images.
  100. // The first image will fill 401x802 area with 8px spaing in between. Image
  101. // should be full width. Image height should extend above and below the
  102. // visible part of the view.
  103. const int image_width = (kScreenWidth - kMarginLeftOfRelatedImageDip + 1) / 2;
  104. const int image_height = 20 * image_width / 10;
  105. const int y = (kScreenHeight - image_height) / 2;
  106. ASSERT_EQ(image_view->GetImageBoundsInScreenForTesting(),
  107. gfx::Rect(/*x=*/0, /*y=*/y, /*width=*/image_width,
  108. /*height=*/image_height));
  109. ASSERT_EQ(image_view->GetRelatedImageBoundsInScreenForTesting(),
  110. gfx::Rect(/*x=*/409, /*y=*/-100, /*width=*/400, /*height=*/800));
  111. }
  112. // Test that only show one landscape image when screen is landscape.
  113. TEST_F(AmbientPhotoViewTest,
  114. ShouldNotTileTwoLandscapeImagesForLandscapeScreen) {
  115. SetDecodedPhotoSize(/*width=*/20, /*height=*/10);
  116. UpdateDisplay("800x600");
  117. ShowAmbientScreen();
  118. FastForwardToNextImage();
  119. auto* image_view = GetAmbientBackgroundImageView();
  120. // Show one landscape image.
  121. // Image should be full height. Image width should extend equally to the left
  122. // and right of the visible part of the screen.
  123. ASSERT_EQ(image_view->GetImageBoundsInScreenForTesting(),
  124. gfx::Rect(/*x=*/-200, /*y=*/0, /*width=*/1200, /*height=*/600));
  125. ASSERT_EQ(image_view->GetRelatedImageBoundsInScreenForTesting(), gfx::Rect());
  126. }
  127. // Test that only have one available image will not be tiled when screen is
  128. // landscape.
  129. TEST_F(AmbientPhotoViewTest,
  130. ShouldNotTileIfRelatedImageIsNullForLandscapeScreen) {
  131. SetPhotoOrientation(/*portrait=*/true);
  132. SetDecodedPhotoSize(/*width=*/10, /*height=*/20);
  133. UpdateDisplay("800x600");
  134. ShowAmbientScreen();
  135. FastForwardToNextImage();
  136. auto* image_view = GetAmbientBackgroundImageView();
  137. // Remove the related image.
  138. image_view->ResetRelatedImageForTesting();
  139. // Trigger layout.
  140. UpdateDisplay("808x600");
  141. // Only show one portrait image.
  142. // Image should be full height. Image should have equal empty space left and
  143. // right.
  144. ASSERT_EQ(image_view->GetImageBoundsInScreenForTesting(),
  145. gfx::Rect(/*x=*/254, /*y=*/0, /*width=*/300, /*height=*/600));
  146. ASSERT_EQ(image_view->GetRelatedImageBoundsInScreenForTesting(), gfx::Rect());
  147. }
  148. // Test that image is scaled to fill screen height when the image is landscape
  149. // and no related image when the screen is landscape.
  150. // The image will be zoomed in and the left and right will be cut off, as the
  151. // width of the image is greater than the width of the screen.
  152. TEST_F(AmbientPhotoViewTest, ShouldResizeLandscapeImageForLandscapeScreen) {
  153. SetDecodedPhotoSize(/*width=*/30, /*height=*/20);
  154. UpdateDisplay("800x600");
  155. ShowAmbientScreen();
  156. FastForwardToNextImage();
  157. auto* image_view = GetAmbientBackgroundImageView();
  158. // Remove the related image.
  159. image_view->ResetRelatedImageForTesting();
  160. // Trigger layout.
  161. UpdateDisplay("808x600");
  162. // Image should be full height. Image width should extend equally to the left
  163. // and right of the visible part of the screen.
  164. ASSERT_EQ(image_view->GetImageBoundsInScreenForTesting(),
  165. gfx::Rect(/*x=*/-46, /*y=*/0, /*width=*/900, /*height=*/600));
  166. ASSERT_EQ(image_view->GetRelatedImageBoundsInScreenForTesting(), gfx::Rect());
  167. }
  168. // Test that when rotates to portrait screen, will dynamically only show one
  169. // portrait image.
  170. TEST_F(AmbientPhotoViewTest,
  171. ShouldNotTilePortraitImagesWhenRotateToPortraitScreen) {
  172. SetPhotoOrientation(/*portrait=*/true);
  173. SetDecodedPhotoSize(/*width=*/10, /*height=*/20);
  174. UpdateDisplay("808x600");
  175. ShowAmbientScreen();
  176. FastForwardToNextImage();
  177. auto* image_view = GetAmbientBackgroundImageView();
  178. // Will tile two portrait images. Each image will fill 400x600 area with 8px
  179. // spaing in between.
  180. // Image should be full width. Image height should extend above and below the
  181. // visible part of the view.
  182. ASSERT_EQ(image_view->GetImageBoundsInScreenForTesting(),
  183. gfx::Rect(/*x=*/0, /*y=*/-100, /*width=*/400, /*height=*/800));
  184. ASSERT_EQ(image_view->GetRelatedImageBoundsInScreenForTesting(),
  185. gfx::Rect(/*x=*/408, /*y=*/-100, /*width=*/400, /*height=*/800));
  186. // Rotate screen.
  187. UpdateDisplay("600x808");
  188. // Only one image will show.
  189. // Image should be full width. Image height should extend above and below the
  190. // visible part of the screen.
  191. ASSERT_EQ(image_view->GetImageBoundsInScreenForTesting(),
  192. gfx::Rect(/*x=*/0, /*y=*/-196, /*width=*/600, /*height=*/1200));
  193. ASSERT_EQ(image_view->GetRelatedImageBoundsInScreenForTesting(), gfx::Rect());
  194. }
  195. // Test that when rotates to portrait screen, will dynamically show two paired
  196. // landscape images.
  197. TEST_F(AmbientPhotoViewTest,
  198. ShouldTileLandscapeImagesWhenRotateToPortraitScreen) {
  199. SetDecodedPhotoSize(/*width=*/20, /*height=*/10);
  200. UpdateDisplay("808x600");
  201. ShowAmbientScreen();
  202. FastForwardToNextImage();
  203. auto* image_view = GetAmbientBackgroundImageView();
  204. // Will show one landscape image.
  205. // Image should be full height. Image height should extend left and right the
  206. // visible part of the view.
  207. ASSERT_EQ(image_view->GetImageBoundsInScreenForTesting(),
  208. gfx::Rect(/*x=*/-196, /*y=*/0, /*width=*/1200, /*height=*/600));
  209. ASSERT_EQ(image_view->GetRelatedImageBoundsInScreenForTesting(), gfx::Rect());
  210. // Rotate screen.
  211. UpdateDisplay("600x808");
  212. // Will show paired landscape images.
  213. // Image should be full height. Image height should extend left and right the
  214. // visible part of the screen.
  215. ASSERT_EQ(image_view->GetImageBoundsInScreenForTesting(),
  216. gfx::Rect(/*x=*/-100, /*y=*/0, /*width=*/800, /*height=*/400));
  217. ASSERT_EQ(image_view->GetRelatedImageBoundsInScreenForTesting(),
  218. gfx::Rect(/*x=*/-100, /*y=*/408, /*width=*/800, /*height=*/400));
  219. }
  220. // Test that when rotates to portrait screen, will rotate Geo landscape images.
  221. TEST_F(AmbientPhotoViewTest,
  222. ShouldRotateGeoLandscapeImageWhenRotateToPortraitScreen) {
  223. SetDecodedPhotoSize(/*width=*/20, /*height=*/10);
  224. SetPhotoTopicType(::ambient::TopicType::kGeo);
  225. UpdateDisplay("808x600");
  226. ShowAmbientScreen();
  227. FastForwardToNextImage();
  228. auto* image_view = GetAmbientBackgroundImageView();
  229. // Will show one landscape image.
  230. // Image should be full height. Image height should extend left and right the
  231. // visible part of the view.
  232. ASSERT_EQ(image_view->GetImageBoundsInScreenForTesting(),
  233. gfx::Rect(/*x=*/-196, /*y=*/0, /*width=*/1200, /*height=*/600));
  234. ASSERT_EQ(image_view->GetRelatedImageBoundsInScreenForTesting(), gfx::Rect());
  235. // Rotate screen.
  236. int64_t internal_display_id =
  237. display::test::DisplayManagerTestApi(display_manager())
  238. .SetFirstDisplayAsInternalDisplay();
  239. display_manager()->SetDisplayRotation(internal_display_id,
  240. display::Display::Rotation::ROTATE_90,
  241. display::Display::RotationSource::USER);
  242. // Will show one rotated image.
  243. // Image should be full width. Image height should extend above and below the
  244. // visible part of the screen.
  245. ASSERT_EQ(image_view->GetImageBoundsInScreenForTesting(),
  246. gfx::Rect(/*x=*/0, /*y=*/-196, /*width=*/600, /*height=*/1200));
  247. ASSERT_EQ(image_view->GetRelatedImageBoundsInScreenForTesting(), gfx::Rect());
  248. }
  249. // Test that when rotates to landscape screen, will dynamically tile two
  250. // portrait images.
  251. TEST_F(AmbientPhotoViewTest, ShouldTileWhenRotateToLandscapeScreen) {
  252. SetPhotoOrientation(/*portrait=*/true);
  253. SetDecodedPhotoSize(/*width=*/10, /*height=*/20);
  254. UpdateDisplay("600x808");
  255. ShowAmbientScreen();
  256. FastForwardToNextImage();
  257. auto* image_view = GetAmbientBackgroundImageView();
  258. // Only one image will show.
  259. // Image should be full width. Image height should extend above and below the
  260. // visible part of the screen.
  261. ASSERT_EQ(image_view->GetImageBoundsInScreenForTesting(),
  262. gfx::Rect(/*x=*/0, /*y=*/-196, /*width=*/600, /*height=*/1200));
  263. ASSERT_EQ(image_view->GetRelatedImageBoundsInScreenForTesting(), gfx::Rect());
  264. // Rotate screen.
  265. UpdateDisplay("808x600");
  266. // Will tile two portrait images. Each image will fill 400x600 area with 8px
  267. // spaing in between.
  268. // Image should be full width. Image height should extend above and below the
  269. // visible part of the view.
  270. ASSERT_EQ(image_view->GetImageBoundsInScreenForTesting(),
  271. gfx::Rect(/*x=*/0, /*y=*/-100, /*width=*/400, /*height=*/800));
  272. ASSERT_EQ(image_view->GetRelatedImageBoundsInScreenForTesting(),
  273. gfx::Rect(/*x=*/408, /*y=*/-100, /*width=*/400, /*height=*/800));
  274. }
  275. // Test that two protrat images will resize when bounds changes in landscape.
  276. TEST_F(AmbientPhotoViewTest, ShouldResizeTiledPortraitImagesWhenBoundsChanged) {
  277. SetPhotoOrientation(/*portrait=*/true);
  278. SetDecodedPhotoSize(/*width=*/10, /*height=*/20);
  279. UpdateDisplay("808x600");
  280. ShowAmbientScreen();
  281. FastForwardToNextImage();
  282. auto* image_view = GetAmbientBackgroundImageView();
  283. // Will tile two portrait images. Each image will fill 400x600 area with 8px
  284. // spaing in between.
  285. // Image should be full width. Image height should extend above and below the
  286. // visible part of the view.
  287. ASSERT_EQ(image_view->GetImageBoundsInScreenForTesting(),
  288. gfx::Rect(/*x=*/0, /*y=*/-100, /*width=*/400, /*height=*/800));
  289. ASSERT_EQ(image_view->GetRelatedImageBoundsInScreenForTesting(),
  290. gfx::Rect(/*x=*/408, /*y=*/-100, /*width=*/400, /*height=*/800));
  291. // Bounds changes so that image will be shown in portrait view.
  292. UpdateDisplay("508x200");
  293. // Will tile two portrait images. Each image will fill 250x200 area with 8px
  294. // spaing in between.
  295. // Image should be full height. Image should have equal empty space left and
  296. // right.
  297. ASSERT_EQ(image_view->GetImageBoundsInScreenForTesting(),
  298. gfx::Rect(/*x=*/75, /*y=*/0, /*width=*/100, /*height=*/200));
  299. ASSERT_EQ(
  300. image_view->GetRelatedImageBoundsInScreenForTesting(),
  301. gfx::Rect(/*x=*/(258 + 75), /*y=*/0, /*width=*/100, /*height=*/200));
  302. // Bounds changes so that image will be shown in portrait view.
  303. UpdateDisplay("308x200");
  304. // Will tile two portrait images. Each image will fill 150x200 area with 8px
  305. // spaing in between.
  306. // Image should be full width. Image height should extend above and below the
  307. // visible part of the view.
  308. ASSERT_EQ(image_view->GetImageBoundsInScreenForTesting(),
  309. gfx::Rect(/*x=*/0, /*y=*/-50, /*width=*/150, /*height=*/300));
  310. ASSERT_EQ(image_view->GetRelatedImageBoundsInScreenForTesting(),
  311. gfx::Rect(/*x=*/158, /*y=*/-50, /*width=*/150, /*height=*/300));
  312. // Bounds changes to exact aspect ratio of the image.
  313. UpdateDisplay("208x200");
  314. // Will tile two portrait images. Each image will fill 100x200 area with 8px
  315. // spaing in between.
  316. // Image should be full width and height.
  317. ASSERT_EQ(image_view->GetImageBoundsInScreenForTesting(),
  318. gfx::Rect(/*x=*/0, /*y=*/0, /*width=*/100, /*height=*/200));
  319. ASSERT_EQ(image_view->GetRelatedImageBoundsInScreenForTesting(),
  320. gfx::Rect(/*x=*/108, /*y=*/0, /*width=*/100, /*height=*/200));
  321. }
  322. } // namespace ash