test_gui.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /*M///////////////////////////////////////////////////////////////////////////////////////
  2. //
  3. // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
  4. //
  5. // By downloading, copying, installing or using the software you agree to this license.
  6. // If you do not agree to this license, do not download, install,
  7. // copy or use the software.
  8. //
  9. //
  10. // License Agreement
  11. // For Open Source Computer Vision Library
  12. //
  13. // Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
  14. // Copyright (C) 2009, Willow Garage Inc., all rights reserved.
  15. // Third party copyrights are property of their respective owners.
  16. //
  17. // Redistribution and use in source and binary forms, with or without modification,
  18. // are permitted provided that the following conditions are met:
  19. //
  20. // * Redistribution's of source code must retain the above copyright notice,
  21. // this list of conditions and the following disclaimer.
  22. //
  23. // * Redistribution's in binary form must reproduce the above copyright notice,
  24. // this list of conditions and the following disclaimer in the documentation
  25. // and/or other materials provided with the distribution.
  26. //
  27. // * The name of the copyright holders may not be used to endorse or promote products
  28. // derived from this software without specific prior written permission.
  29. //
  30. // This software is provided by the copyright holders and contributors "as is" and
  31. // any express or implied warranties, including, but not limited to, the implied
  32. // warranties of merchantability and fitness for a particular purpose are disclaimed.
  33. // In no event shall the Intel Corporation or contributors be liable for any direct,
  34. // indirect, incidental, special, exemplary, or consequential damages
  35. // (including, but not limited to, procurement of substitute goods or services;
  36. // loss of use, data, or profits; or business interruption) however caused
  37. // and on any theory of liability, whether in contract, strict liability,
  38. // or tort (including negligence or otherwise) arising in any way out of
  39. // the use of this software, even if advised of the possibility of such damage.
  40. //
  41. //M*/
  42. #include "test_precomp.hpp"
  43. namespace opencv_test { namespace {
  44. inline void verify_size(const std::string &nm, const cv::Mat &img)
  45. {
  46. EXPECT_NO_THROW(imshow(nm, img));
  47. EXPECT_EQ(-1, waitKey(200));
  48. // see https://github.com/opencv/opencv/issues/25550
  49. // Wayland backend is not supported getWindowImageRect().
  50. string framework;
  51. EXPECT_NO_THROW(framework = currentUIFramework());
  52. if(framework == "WAYLAND")
  53. {
  54. return;
  55. }
  56. Rect rc;
  57. EXPECT_NO_THROW(rc = getWindowImageRect(nm));
  58. EXPECT_EQ(rc.size(), img.size());
  59. }
  60. #if (!defined(ENABLE_PLUGINS) \
  61. && !defined HAVE_GTK \
  62. && !defined HAVE_QT \
  63. && !defined HAVE_WIN32UI \
  64. && !defined HAVE_COCOA \
  65. && !defined HAVE_WAYLAND \
  66. )
  67. TEST(Highgui_GUI, DISABLED_regression)
  68. #else
  69. TEST(Highgui_GUI, regression)
  70. #endif
  71. {
  72. const std::string window_name("opencv_highgui_test_window");
  73. const cv::Size image_size(800, 600);
  74. EXPECT_NO_THROW(destroyAllWindows());
  75. ASSERT_NO_THROW(namedWindow(window_name));
  76. const vector<int> channels = {1, 3, 4};
  77. const vector<int> depths = {CV_8U, CV_8S, CV_16U, CV_16S, CV_32F, CV_64F};
  78. for(int cn : channels)
  79. {
  80. SCOPED_TRACE(cn);
  81. for(int depth : depths)
  82. {
  83. SCOPED_TRACE(depth);
  84. double min_val = 0.;
  85. double max_val = 256.;
  86. switch(depth)
  87. {
  88. case CV_8S:
  89. min_val = static_cast<double>(-0x7F);
  90. max_val = static_cast<double>(0x7F + 1);
  91. break;
  92. case CV_16S:
  93. min_val = static_cast<double>(-0x7FFF);
  94. max_val = static_cast<double>(0x7FFF + 1);
  95. break;
  96. case CV_16U:
  97. max_val = static_cast<double>(0xFFFF + 1);
  98. break;
  99. case CV_32F:
  100. case CV_64F:
  101. max_val = 1.0;
  102. break;
  103. }
  104. Mat m = cvtest::randomMat(TS::ptr()->get_rng(), image_size, CV_MAKE_TYPE(depth, cn), min_val, max_val, false);
  105. verify_size(window_name, m);
  106. Mat bgr(image_size, CV_MAKE_TYPE(depth, cn));
  107. int b_g = image_size.width / 3, g_r = b_g * 2;
  108. if (cn > 1)
  109. {
  110. bgr.colRange(0, b_g).setTo(cv::Scalar(max_val, min_val, min_val));
  111. bgr.colRange(b_g, g_r).setTo(cv::Scalar(min_val, max_val, min_val));
  112. bgr.colRange(g_r, image_size.width).setTo(cv::Scalar(min_val, min_val, max_val));
  113. }
  114. else
  115. {
  116. bgr.colRange(0, b_g).setTo(cv::Scalar::all(min_val));
  117. bgr.colRange(b_g, g_r).setTo(cv::Scalar::all((min_val + max_val) / 2));
  118. bgr.colRange(g_r, image_size.width).setTo(cv::Scalar::all(max_val));
  119. }
  120. verify_size(window_name, bgr);
  121. }
  122. }
  123. EXPECT_NO_THROW(destroyAllWindows());
  124. }
  125. //==================================================================================================
  126. static void Foo(int, void* counter)
  127. {
  128. if (counter)
  129. {
  130. int *counter_int = static_cast<int*>(counter);
  131. (*counter_int)++;
  132. }
  133. }
  134. #if (!defined(ENABLE_PLUGINS) \
  135. && !defined HAVE_GTK \
  136. && !defined HAVE_QT \
  137. && !defined HAVE_WIN32UI \
  138. && !defined HAVE_WAYLAND \
  139. ) \
  140. || defined(__APPLE__) /* test fails on Mac (cocoa) */ \
  141. || defined HAVE_FRAMEBUFFER /* trackbar is not supported */
  142. TEST(Highgui_GUI, DISABLED_trackbar_unsafe)
  143. #else
  144. TEST(Highgui_GUI, trackbar_unsafe)
  145. #endif
  146. {
  147. int value = 50;
  148. int callback_count = 0;
  149. const std::string window_name("trackbar_test_window");
  150. const std::string trackbar_name("trackbar");
  151. EXPECT_NO_THROW(destroyAllWindows());
  152. ASSERT_NO_THROW(namedWindow(window_name));
  153. EXPECT_EQ((int)1, createTrackbar(trackbar_name, window_name, &value, 100, Foo, &callback_count));
  154. EXPECT_EQ(value, getTrackbarPos(trackbar_name, window_name));
  155. EXPECT_GE(callback_count, 0);
  156. EXPECT_LE(callback_count, 1);
  157. int callback_count_base = callback_count;
  158. EXPECT_NO_THROW(setTrackbarPos(trackbar_name, window_name, 90));
  159. EXPECT_EQ(callback_count_base + 1, callback_count);
  160. EXPECT_EQ(90, value);
  161. EXPECT_EQ(90, getTrackbarPos(trackbar_name, window_name));
  162. EXPECT_NO_THROW(destroyAllWindows());
  163. }
  164. static
  165. void testTrackbarCallback(int pos, void* param)
  166. {
  167. CV_Assert(param);
  168. int* status = (int*)param;
  169. status[0] = pos;
  170. status[1]++;
  171. }
  172. #if (!defined(ENABLE_PLUGINS) \
  173. && !defined HAVE_GTK \
  174. && !defined HAVE_QT \
  175. && !defined HAVE_WIN32UI \
  176. && !defined HAVE_WAYLAND \
  177. ) \
  178. || defined(__APPLE__) /* test fails on Mac (cocoa) */ \
  179. || defined HAVE_FRAMEBUFFER /* trackbar is not supported */
  180. TEST(Highgui_GUI, DISABLED_trackbar)
  181. #else
  182. TEST(Highgui_GUI, trackbar)
  183. #endif
  184. {
  185. int status[2] = {-1, 0}; // pos, counter
  186. const std::string window_name("trackbar_test_window");
  187. const std::string trackbar_name("trackbar");
  188. EXPECT_NO_THROW(destroyAllWindows());
  189. ASSERT_NO_THROW(namedWindow(window_name));
  190. EXPECT_EQ((int)1, createTrackbar(trackbar_name, window_name, NULL, 100, testTrackbarCallback, status));
  191. EXPECT_EQ(0, getTrackbarPos(trackbar_name, window_name));
  192. int callback_count = status[1];
  193. EXPECT_GE(callback_count, 0);
  194. EXPECT_LE(callback_count, 1);
  195. int callback_count_base = callback_count;
  196. EXPECT_NO_THROW(setTrackbarPos(trackbar_name, window_name, 90));
  197. callback_count = status[1];
  198. EXPECT_EQ(callback_count_base + 1, callback_count);
  199. int value = status[0];
  200. EXPECT_EQ(90, value);
  201. EXPECT_EQ(90, getTrackbarPos(trackbar_name, window_name));
  202. EXPECT_NO_THROW(destroyAllWindows());
  203. }
  204. // See https://github.com/opencv/opencv/issues/25560
  205. #if (!defined(ENABLE_PLUGINS) \
  206. && !defined HAVE_GTK \
  207. && !defined HAVE_QT \
  208. && !defined HAVE_WIN32UI \
  209. && !defined HAVE_WAYLAND)
  210. TEST(Highgui_GUI, DISABLED_small_width_image)
  211. #else
  212. TEST(Highgui_GUI, small_width_image)
  213. #endif
  214. {
  215. const std::string window_name("trackbar_test_window");
  216. cv::Mat src(1,1,CV_8UC3,cv::Scalar(0));
  217. EXPECT_NO_THROW(destroyAllWindows());
  218. ASSERT_NO_THROW(namedWindow(window_name));
  219. ASSERT_NO_THROW(imshow(window_name, src));
  220. EXPECT_NO_THROW(waitKey(10));
  221. EXPECT_NO_THROW(destroyAllWindows());
  222. }
  223. TEST(Highgui_GUI, currentUIFramework)
  224. {
  225. auto framework = currentUIFramework();
  226. std::cout << "UI framework: \"" << framework << "\"" << std::endl;
  227. #if (!defined(ENABLE_PLUGINS) \
  228. && !defined HAVE_GTK \
  229. && !defined HAVE_QT \
  230. && !defined HAVE_WIN32UI \
  231. && !defined HAVE_COCOA \
  232. && !defined HAVE_WAYLAND \
  233. )
  234. EXPECT_TRUE(framework.empty());
  235. #elif !defined(ENABLE_PLUGINS)
  236. EXPECT_GT(framework.size(), 0); // builtin backends
  237. #endif
  238. }
  239. }} // namespace