test_tb_widget_value.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. // ================================================================================
  2. // == This file is a part of Turbo Badger. (C) 2011-2014, Emil Segerås ==
  3. // == See tb_core.h for more information. ==
  4. // ================================================================================
  5. #include "tb_test.h"
  6. #include "tb_editfield.h"
  7. #include "tb_widgets_common.h"
  8. #include "tb_select.h"
  9. #include "tb_inline_select.h"
  10. #include "tb_widget_value.h"
  11. #ifdef TB_UNIT_TESTING
  12. using namespace tb;
  13. TB_TEST_GROUP(tb_widget_value_text)
  14. {
  15. TBWidgetValue widget_val(TBIDC("test value text"));
  16. TBWidget *a, *b, *c;
  17. TB_TEST(Init)
  18. {
  19. TB_VERIFY(a = new TBEditField);
  20. TB_VERIFY(b = new TBEditField);
  21. TB_VERIFY(c = new TBEditField);
  22. }
  23. TB_TEST(connect)
  24. {
  25. // Set the initial value, no widgets connected yet.
  26. widget_val.SetText("turbo badger");
  27. // Connecting widgets should give them the current value.
  28. a->Connect(&widget_val);
  29. b->Connect(&widget_val);
  30. c->Connect(&widget_val);
  31. TB_VERIFY_STR(a->GetText(), "turbo badger");
  32. TB_VERIFY_STR(b->GetText(), "turbo badger");
  33. TB_VERIFY_STR(c->GetText(), "turbo badger");
  34. }
  35. TB_TEST(change_value)
  36. {
  37. // Changing the value should change all widgets
  38. widget_val.SetText("Emil");
  39. TB_VERIFY_STR(a->GetText(), "Emil");
  40. TB_VERIFY_STR(b->GetText(), "Emil");
  41. TB_VERIFY_STR(c->GetText(), "Emil");
  42. }
  43. TB_TEST(change_widget)
  44. {
  45. // When a widget change, all the other widgets should also change.
  46. a->SetText("A");
  47. TB_VERIFY_STR(b->GetText(), "A");
  48. TB_VERIFY_STR(c->GetText(), "A");
  49. b->SetText("B");
  50. TB_VERIFY_STR(a->GetText(), "B");
  51. TB_VERIFY_STR(c->GetText(), "B");
  52. c->SetText("C");
  53. TB_VERIFY_STR(a->GetText(), "C");
  54. TB_VERIFY_STR(b->GetText(), "C");
  55. // The value itself should also have changed.
  56. TB_VERIFY_STR(widget_val.GetText(), "C");
  57. }
  58. TB_TEST(Shutdown)
  59. {
  60. delete a;
  61. delete b;
  62. delete c;
  63. }
  64. }
  65. TB_TEST_GROUP(tb_widget_value_int)
  66. {
  67. TBWidgetValue widget_val(TBIDC("test value int"));
  68. TBSlider *a;
  69. TBScrollBar *b;
  70. TBInlineSelect *c;
  71. TB_TEST(Init)
  72. {
  73. TB_VERIFY(a = new TBSlider);
  74. TB_VERIFY(b = new TBScrollBar);
  75. TB_VERIFY(c = new TBInlineSelect);
  76. a->SetLimits(0, 1000);
  77. b->SetLimits(0, 1000, 1);
  78. c->SetLimits(0, 1000);
  79. }
  80. TB_TEST(connect)
  81. {
  82. // Set the initial value, no widgets connected yet.
  83. widget_val.SetInt(42);
  84. // Connecting widgets should give them the current value.
  85. a->Connect(&widget_val);
  86. b->Connect(&widget_val);
  87. c->Connect(&widget_val);
  88. TB_VERIFY(a->GetValue() == 42);
  89. TB_VERIFY(b->GetValue() == 42);
  90. TB_VERIFY(c->GetValue() == 42);
  91. }
  92. TB_TEST(change_value)
  93. {
  94. // Changing the value should change all widgets
  95. widget_val.SetInt(123);
  96. TB_VERIFY(a->GetValue() == 123);
  97. TB_VERIFY(b->GetValue() == 123);
  98. TB_VERIFY(c->GetValue() == 123);
  99. }
  100. TB_TEST(change_widget)
  101. {
  102. // When a widget change, all the other widgets should also change.
  103. a->SetValue(1);
  104. TB_VERIFY(b->GetValue() == 1);
  105. TB_VERIFY(c->GetValue() == 1);
  106. b->SetValue(2);
  107. TB_VERIFY(a->GetValue() == 2);
  108. TB_VERIFY(c->GetValue() == 2);
  109. c->SetValue(3);
  110. TB_VERIFY(a->GetValue() == 3);
  111. TB_VERIFY(b->GetValue() == 3);
  112. // The value itself should also have changed.
  113. TB_VERIFY(widget_val.GetInt() == 3);
  114. }
  115. TB_TEST(Shutdown)
  116. {
  117. delete a;
  118. delete b;
  119. delete c;
  120. }
  121. }
  122. TB_TEST_GROUP(tb_widget_value_listener)
  123. {
  124. TBWidgetValue widget_val(TBIDC("test value check"));
  125. TBCheckBox *a, *b;
  126. /** Listen to changes and update val to any changed value. */
  127. class MyListener : public TBValueGroupListener
  128. {
  129. public:
  130. int change_counter;
  131. TBValue val;
  132. MyListener() : change_counter(0) {}
  133. virtual void OnValueChanged(const TBValueGroup *group, const TBWidgetValue *value)
  134. {
  135. val = value->GetValue();
  136. change_counter++;
  137. }
  138. };
  139. MyListener listener;
  140. TB_TEST(Init)
  141. {
  142. TB_VERIFY(a = new TBCheckBox);
  143. TB_VERIFY(b = new TBCheckBox);
  144. }
  145. TB_TEST(Setup) { g_value_group.AddListener(&listener); }
  146. TB_TEST(Cleanup) { g_value_group.RemoveListener(&listener); }
  147. TB_TEST(Shutdown)
  148. {
  149. delete a;
  150. delete b;
  151. }
  152. TB_TEST(change_with_no_widgets)
  153. {
  154. // Set the initial value, no widgets connected yet.
  155. widget_val.SetInt(1);
  156. // The listener should have registered the change
  157. TB_VERIFY(listener.val.GetInt() == 1);
  158. TB_VERIFY(listener.change_counter == 1);
  159. }
  160. TB_TEST(change_with_widgets)
  161. {
  162. a->Connect(&widget_val);
  163. b->Connect(&widget_val);
  164. // Change the value to 0
  165. widget_val.SetInt(0);
  166. // The listener should have registered the change, once.
  167. TB_VERIFY(listener.val.GetInt() == 0);
  168. TB_VERIFY(listener.change_counter == 2);
  169. }
  170. TB_TEST(change_widget)
  171. {
  172. // Change one of the widgets
  173. a->SetValue(1);
  174. // The listener should have registered the change, once.
  175. TB_VERIFY(listener.val.GetInt() == 1);
  176. TB_VERIFY(listener.change_counter == 3);
  177. }
  178. }
  179. #endif // TB_UNIT_TESTING