x11_cursor_loader_unittest.cc 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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 "ui/base/x/x11_cursor_loader.h"
  5. #undef Bool
  6. #include "base/memory/ref_counted_memory.h"
  7. #include "base/sys_byteorder.h"
  8. #include "testing/gtest/include/gtest/gtest.h"
  9. namespace ui {
  10. namespace {
  11. std::vector<XCursorLoader::Image> ParseFile(std::vector<uint32_t>* data,
  12. uint32_t preferred_size) {
  13. for (uint32_t& i : *data)
  14. i = base::ByteSwapToLE32(i);
  15. std::vector<uint8_t> vec(data->size() * sizeof(uint32_t));
  16. memcpy(vec.data(), data->data(), vec.size());
  17. return ParseCursorFile(base::RefCountedBytes::TakeVector(&vec),
  18. preferred_size);
  19. }
  20. } // namespace
  21. TEST(XCursorLoaderTest, Basic) {
  22. std::vector<uint32_t> file{
  23. // magic number
  24. 0x72756358,
  25. // bytes in header
  26. 28,
  27. // version
  28. 1,
  29. // number of TOC entries
  30. 1,
  31. // type (image)
  32. 0xfffd0002,
  33. // subtype (image size)
  34. 1,
  35. // position
  36. 28,
  37. // bytes in header
  38. 16,
  39. // chunk type (image)
  40. 0xfffd0002,
  41. // chunk subtype (image size)
  42. 1,
  43. // chunk type version
  44. 1,
  45. // image width
  46. 1,
  47. // image height
  48. 1,
  49. // xhot
  50. 1234,
  51. // yhot
  52. 5678,
  53. // delay
  54. 123,
  55. // chunk data (ARGB image)
  56. 0xff123456,
  57. };
  58. auto images = ParseFile(&file, 1);
  59. ASSERT_EQ(images.size(), 1ul);
  60. EXPECT_EQ(images[0].frame_delay.InMilliseconds(), 123);
  61. EXPECT_EQ(images[0].bitmap.width(), 1);
  62. EXPECT_EQ(images[0].bitmap.height(), 1);
  63. EXPECT_EQ(images[0].hotspot.x(), 1234);
  64. EXPECT_EQ(images[0].hotspot.y(), 5678);
  65. EXPECT_EQ(images[0].bitmap.getColor(0, 0), 0xff123456);
  66. }
  67. TEST(XCursorLoaderTest, BestSize) {
  68. std::vector<uint32_t> file{
  69. // magic number
  70. 0x72756358,
  71. // bytes in header
  72. 28,
  73. // version
  74. 1,
  75. // number of TOC entries
  76. 3,
  77. // type (image)
  78. 0xfffd0002,
  79. // subtype (image size)
  80. 1,
  81. // position
  82. 52,
  83. // type (image)
  84. 0xfffd0002,
  85. // subtype (image size)
  86. 2,
  87. // position
  88. 92,
  89. // type (image)
  90. 0xfffd0002,
  91. // subtype (image size)
  92. 3,
  93. // position
  94. 144,
  95. // bytes in header
  96. 16,
  97. // chunk type (image)
  98. 0xfffd0002,
  99. // chunk subtype (image size)
  100. 1,
  101. // chunk type version
  102. 1,
  103. // image width
  104. 1,
  105. // image height
  106. 1,
  107. // xhot
  108. 0,
  109. // yhot
  110. 0,
  111. // delay
  112. 0,
  113. // chunk data (ARGB image)
  114. 0xffffffff,
  115. // bytes in header
  116. 16,
  117. // chunk type (image)
  118. 0xfffd0002,
  119. // chunk subtype (image size)
  120. 2,
  121. // chunk type version
  122. 1,
  123. // image width
  124. 2,
  125. // image height
  126. 2,
  127. // xhot
  128. 0,
  129. // yhot
  130. 0,
  131. // delay
  132. 0,
  133. // chunk data (ARGB image)
  134. 0xffffffff,
  135. 0xffffffff,
  136. 0xffffffff,
  137. 0xffffffff,
  138. // bytes in header
  139. 16,
  140. // chunk type (image)
  141. 0xfffd0002,
  142. // chunk subtype (image size)
  143. 3,
  144. // chunk type version
  145. 1,
  146. // image width
  147. 3,
  148. // image height
  149. 3,
  150. // xhot
  151. 0,
  152. // yhot
  153. 0,
  154. // delay
  155. 0,
  156. // chunk data (ARGB image)
  157. 0xffffffff,
  158. 0xffffffff,
  159. 0xffffffff,
  160. 0xffffffff,
  161. 0xffffffff,
  162. 0xffffffff,
  163. 0xffffffff,
  164. 0xffffffff,
  165. 0xffffffff,
  166. };
  167. auto images = ParseFile(&file, 2);
  168. ASSERT_EQ(images.size(), 1ul);
  169. EXPECT_EQ(images[0].bitmap.width(), 2);
  170. EXPECT_EQ(images[0].bitmap.height(), 2);
  171. }
  172. TEST(XCursorLoaderTest, Animated) {
  173. std::vector<uint32_t> file{
  174. // magic number
  175. 0x72756358,
  176. // bytes in header
  177. 28,
  178. // version
  179. 1,
  180. // number of TOC entries
  181. 2,
  182. // type (image)
  183. 0xfffd0002,
  184. // subtype (image size)
  185. 1,
  186. // position
  187. 40,
  188. // type (image)
  189. 0xfffd0002,
  190. // subtype (image size)
  191. 1,
  192. // position
  193. 80,
  194. // bytes in header
  195. 16,
  196. // chunk type (image)
  197. 0xfffd0002,
  198. // chunk subtype (image size)
  199. 1,
  200. // chunk type version
  201. 1,
  202. // image width
  203. 1,
  204. // image height
  205. 1,
  206. // xhot
  207. 0,
  208. // yhot
  209. 0,
  210. // delay
  211. 500,
  212. // chunk data (ARGB image)
  213. 0xff123456,
  214. // bytes in header
  215. 16,
  216. // chunk type (image)
  217. 0xfffd0002,
  218. // chunk subtype (image size)
  219. 1,
  220. // chunk type version
  221. 1,
  222. // image width
  223. 1,
  224. // image height
  225. 1,
  226. // xhot
  227. 0,
  228. // yhot
  229. 0,
  230. // delay
  231. 500,
  232. // chunk data (ARGB image)
  233. 0xff123456,
  234. };
  235. auto images = ParseFile(&file, 1);
  236. ASSERT_EQ(images.size(), 2ul);
  237. EXPECT_EQ(images[0].frame_delay.InMilliseconds(), 500);
  238. EXPECT_EQ(images[1].frame_delay.InMilliseconds(), 500);
  239. }
  240. } // namespace ui