test_gui.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. Rect rc;
  49. EXPECT_NO_THROW(rc = getWindowImageRect(nm));
  50. EXPECT_EQ(rc.size(), img.size());
  51. }
  52. #if (!defined(ENABLE_PLUGINS) \
  53. && !defined HAVE_GTK \
  54. && !defined HAVE_QT \
  55. && !defined HAVE_WIN32UI \
  56. && !defined HAVE_COCOA \
  57. )
  58. TEST(Highgui_GUI, DISABLED_regression)
  59. #else
  60. TEST(Highgui_GUI, regression)
  61. #endif
  62. {
  63. const std::string window_name("opencv_highgui_test_window");
  64. const cv::Size image_size(800, 600);
  65. EXPECT_NO_THROW(destroyAllWindows());
  66. ASSERT_NO_THROW(namedWindow(window_name));
  67. const vector<int> channels = {1, 3, 4};
  68. const vector<int> depths = {CV_8U, CV_8S, CV_16U, CV_16S, CV_32F, CV_64F};
  69. for(int cn : channels)
  70. {
  71. SCOPED_TRACE(cn);
  72. for(int depth : depths)
  73. {
  74. SCOPED_TRACE(depth);
  75. double min_val = 0.;
  76. double max_val = 256.;
  77. switch(depth)
  78. {
  79. case CV_8S:
  80. min_val = static_cast<double>(-0x7F);
  81. max_val = static_cast<double>(0x7F + 1);
  82. break;
  83. case CV_16S:
  84. min_val = static_cast<double>(-0x7FFF);
  85. max_val = static_cast<double>(0x7FFF + 1);
  86. break;
  87. case CV_16U:
  88. max_val = static_cast<double>(0xFFFF + 1);
  89. break;
  90. case CV_32F:
  91. case CV_64F:
  92. max_val = 1.0;
  93. break;
  94. }
  95. Mat m = cvtest::randomMat(TS::ptr()->get_rng(), image_size, CV_MAKE_TYPE(depth, cn), min_val, max_val, false);
  96. verify_size(window_name, m);
  97. Mat bgr(image_size, CV_MAKE_TYPE(depth, cn));
  98. int b_g = image_size.width / 3, g_r = b_g * 2;
  99. if (cn > 1)
  100. {
  101. bgr.colRange(0, b_g).setTo(cv::Scalar(max_val, min_val, min_val));
  102. bgr.colRange(b_g, g_r).setTo(cv::Scalar(min_val, max_val, min_val));
  103. bgr.colRange(g_r, image_size.width).setTo(cv::Scalar(min_val, min_val, max_val));
  104. }
  105. else
  106. {
  107. bgr.colRange(0, b_g).setTo(cv::Scalar::all(min_val));
  108. bgr.colRange(b_g, g_r).setTo(cv::Scalar::all((min_val + max_val) / 2));
  109. bgr.colRange(g_r, image_size.width).setTo(cv::Scalar::all(max_val));
  110. }
  111. verify_size(window_name, bgr);
  112. }
  113. }
  114. EXPECT_NO_THROW(destroyAllWindows());
  115. }
  116. //==================================================================================================
  117. static void Foo(int, void* counter)
  118. {
  119. if (counter)
  120. {
  121. int *counter_int = static_cast<int*>(counter);
  122. (*counter_int)++;
  123. }
  124. }
  125. #if (!defined(ENABLE_PLUGINS) \
  126. && !defined HAVE_GTK \
  127. && !defined HAVE_QT \
  128. && !defined HAVE_WIN32UI \
  129. ) \
  130. || defined(__APPLE__) // test fails on Mac (cocoa)
  131. TEST(Highgui_GUI, DISABLED_trackbar_unsafe)
  132. #else
  133. TEST(Highgui_GUI, trackbar_unsafe)
  134. #endif
  135. {
  136. int value = 50;
  137. int callback_count = 0;
  138. const std::string window_name("trackbar_test_window");
  139. const std::string trackbar_name("trackbar");
  140. EXPECT_NO_THROW(destroyAllWindows());
  141. ASSERT_NO_THROW(namedWindow(window_name));
  142. EXPECT_EQ((int)1, createTrackbar(trackbar_name, window_name, &value, 100, Foo, &callback_count));
  143. EXPECT_EQ(value, getTrackbarPos(trackbar_name, window_name));
  144. EXPECT_GE(callback_count, 0);
  145. EXPECT_LE(callback_count, 1);
  146. int callback_count_base = callback_count;
  147. EXPECT_NO_THROW(setTrackbarPos(trackbar_name, window_name, 90));
  148. EXPECT_EQ(callback_count_base + 1, callback_count);
  149. EXPECT_EQ(90, value);
  150. EXPECT_EQ(90, getTrackbarPos(trackbar_name, window_name));
  151. EXPECT_NO_THROW(destroyAllWindows());
  152. }
  153. static
  154. void testTrackbarCallback(int pos, void* param)
  155. {
  156. CV_Assert(param);
  157. int* status = (int*)param;
  158. status[0] = pos;
  159. status[1]++;
  160. }
  161. #if (!defined(ENABLE_PLUGINS) \
  162. && !defined HAVE_GTK \
  163. && !defined HAVE_QT \
  164. && !defined HAVE_WIN32UI \
  165. ) \
  166. || defined(__APPLE__) // test fails on Mac (cocoa)
  167. TEST(Highgui_GUI, DISABLED_trackbar)
  168. #else
  169. TEST(Highgui_GUI, trackbar)
  170. #endif
  171. {
  172. int status[2] = {-1, 0}; // pos, counter
  173. const std::string window_name("trackbar_test_window");
  174. const std::string trackbar_name("trackbar");
  175. EXPECT_NO_THROW(destroyAllWindows());
  176. ASSERT_NO_THROW(namedWindow(window_name));
  177. EXPECT_EQ((int)1, createTrackbar(trackbar_name, window_name, NULL, 100, testTrackbarCallback, status));
  178. EXPECT_EQ(0, getTrackbarPos(trackbar_name, window_name));
  179. int callback_count = status[1];
  180. EXPECT_GE(callback_count, 0);
  181. EXPECT_LE(callback_count, 1);
  182. int callback_count_base = callback_count;
  183. EXPECT_NO_THROW(setTrackbarPos(trackbar_name, window_name, 90));
  184. callback_count = status[1];
  185. EXPECT_EQ(callback_count_base + 1, callback_count);
  186. int value = status[0];
  187. EXPECT_EQ(90, value);
  188. EXPECT_EQ(90, getTrackbarPos(trackbar_name, window_name));
  189. EXPECT_NO_THROW(destroyAllWindows());
  190. }
  191. }} // namespace