Back to index

unity  6.0.0
test_indicator_entry.cpp
Go to the documentation of this file.
00001 #include <UnityCore/IndicatorEntry.h>
00002 
00003 #include <gtest/gtest.h>
00004 
00005 using namespace std;
00006 using namespace unity;
00007 
00008 namespace
00009 {
00010 
00011 
00012 TEST(TestIndicatorEntry, TestConstruction)
00013 {
00014 
00015   indicator::Entry entry("id", "name_hint", "label", true, true,
00016                          1, "some icon", false, true, -1);
00017 
00018   EXPECT_EQ(entry.id(), "id");
00019   EXPECT_EQ(entry.label(), "label");
00020   EXPECT_EQ(entry.name_hint(), "name_hint");
00021   EXPECT_TRUE(entry.label_sensitive());
00022   EXPECT_TRUE(entry.label_visible());
00023   EXPECT_FALSE(entry.image_sensitive());
00024   EXPECT_TRUE(entry.image_visible());
00025   EXPECT_FALSE(entry.active());
00026   EXPECT_FALSE(entry.show_now());
00027   EXPECT_TRUE(entry.visible());
00028   EXPECT_EQ(entry.image_type(), 1);
00029   EXPECT_EQ(entry.image_data(), "some icon");
00030   EXPECT_EQ(entry.priority(), -1);
00031 }
00032 
00033 struct Counter : sigc::trackable
00034 {
00035   Counter() : count(0) {}
00036   void increment()
00037   {
00038     ++count;
00039   }
00040   int count;
00041 };
00042 
00043 template <typename T>
00044 struct ChangeRecorder : sigc::trackable
00045 {
00046   void value_changed(T const& value)
00047   {
00048     changed_values.push_back(value);
00049   }
00050   typedef std::vector<T> ChangedValues;
00051   ChangedValues changed_values;
00052 };
00053 
00054 TEST(TestIndicatorEntry, TestAssignment)
00055 {
00056 
00057   indicator::Entry entry("id", "name_hint", "label", true, true,
00058                          0, "some icon", false, true, 10);
00059   indicator::Entry other_entry("other_id", "other_name_hint", "other_label",
00060                                false, false, 2, "other icon", true, false, 5);
00061 
00062   Counter counter;
00063   entry.updated.connect(sigc::mem_fun(counter, &Counter::increment));
00064 
00065   entry = other_entry;
00066 
00067   EXPECT_EQ(entry.id(), "other_id");
00068   EXPECT_EQ(entry.name_hint(), "other_name_hint");
00069   EXPECT_EQ(entry.label(), "other_label");
00070   EXPECT_FALSE(entry.label_sensitive());
00071   EXPECT_FALSE(entry.label_visible());
00072   EXPECT_EQ(entry.image_type(), 2);
00073   EXPECT_EQ(entry.image_data(), "other icon");
00074   EXPECT_TRUE(entry.image_sensitive());
00075   EXPECT_FALSE(entry.image_visible());
00076   EXPECT_EQ(counter.count, 1);
00077   EXPECT_EQ(entry.priority(), 5);
00078 }
00079 
00080 TEST(TestIndicatorEntry, TestShowNowEvents)
00081 {
00082 
00083   indicator::Entry entry("id", "name_hint", "label", true, true,
00084                          0, "some icon", false, true, -1);
00085 
00086   ChangeRecorder<bool> recorder;
00087   Counter counter;
00088   entry.updated.connect(sigc::mem_fun(counter, &Counter::increment));
00089   entry.show_now_changed.connect(sigc::mem_fun(recorder, &ChangeRecorder<bool>::value_changed));
00090 
00091   // Setting show_now to the same value doesn't emit any events.
00092   entry.set_show_now(false);
00093   EXPECT_FALSE(entry.show_now());
00094   EXPECT_EQ(counter.count, 0);
00095   EXPECT_EQ(recorder.changed_values.size(), 0);
00096 
00097   // Setting to a different value does emit the events.
00098   entry.set_show_now(true);
00099   EXPECT_TRUE(entry.show_now());
00100   EXPECT_EQ(counter.count, 1);
00101   ASSERT_EQ(recorder.changed_values.size(), 1);
00102   EXPECT_TRUE(recorder.changed_values[0]);
00103 }
00104 
00105 TEST(TestIndicatorEntry, TestActiveEvents)
00106 {
00107 
00108   indicator::Entry entry("id", "name_hint", "label", true, true,
00109                          0, "some icon", false, true, -1);
00110 
00111   ChangeRecorder<bool> recorder;
00112   Counter counter;
00113   entry.updated.connect(sigc::mem_fun(counter, &Counter::increment));
00114   entry.active_changed.connect(sigc::mem_fun(recorder, &ChangeRecorder<bool>::value_changed));
00115 
00116   // Setting to the same value doesn't emit any events.
00117   entry.set_active(false);
00118   EXPECT_FALSE(entry.active());
00119   EXPECT_EQ(counter.count, 0);
00120   EXPECT_EQ(recorder.changed_values.size(), 0);
00121 
00122   // Setting to a different value does emit the events.
00123   entry.set_active(true);
00124   EXPECT_TRUE(entry.active());
00125   EXPECT_EQ(counter.count, 1);
00126   ASSERT_EQ(recorder.changed_values.size(), 1);
00127   EXPECT_TRUE(recorder.changed_values[0]);
00128 }
00129 
00130 struct ScrollRecorder : public ChangeRecorder<int>
00131 {
00132   ScrollRecorder(std::string const& name) : entry_name(name) {}
00133 
00134   void OnScroll(std::string const& name, int delta)
00135   {
00136     EXPECT_EQ(name, entry_name);
00137     value_changed(delta);
00138   }
00139 
00140   std::string entry_name;
00141 };
00142 
00143 TEST(TestIndicatorEntry, TestOnScroll)
00144 {
00145 
00146   indicator::Entry entry("id", "name_hint", "label", true, true,
00147                          0, "some icon", false, true, -1);
00148 
00149   ScrollRecorder recorder("id");
00150   entry.on_scroll.connect(sigc::mem_fun(recorder, &ScrollRecorder::OnScroll));
00151 
00152   entry.Scroll(10);
00153   entry.Scroll(-20);
00154 
00155   ASSERT_EQ(recorder.changed_values.size(), 2);
00156   EXPECT_EQ(recorder.changed_values[0], 10);
00157   EXPECT_EQ(recorder.changed_values[1], -20);
00158 }
00159 
00160 struct ShowMenuRecorder
00161 {
00162   void OnShowMenu(std::string const& a, unsigned int b, int c, int d, unsigned int e, unsigned int f)
00163   {
00164     name = a;
00165     xid = b;
00166     x = c;
00167     y = d;
00168     button = e;
00169     timestamp = f;
00170   }
00171   std::string name;
00172   unsigned int xid, timestamp, button;
00173   int x, y;
00174 };
00175 
00176 TEST(TestIndicatorEntry, TestOnShowMenu)
00177 {
00178 
00179   indicator::Entry entry("id", "name_hint", "label", true, true,
00180                          0, "some icon", false, true, -1);
00181 
00182   ShowMenuRecorder recorder;
00183   entry.on_show_menu.connect(sigc::mem_fun(recorder, &ShowMenuRecorder::OnShowMenu));
00184 
00185   entry.ShowMenu(10, 20, 1, 12345);
00186   EXPECT_EQ(recorder.name, "id");
00187   EXPECT_EQ(recorder.xid, 0);
00188   EXPECT_EQ(recorder.x, 10);
00189   EXPECT_EQ(recorder.y, 20);
00190   EXPECT_EQ(recorder.timestamp, 12345);
00191   EXPECT_EQ(recorder.button, 1);
00192 }
00193 
00194 TEST(TestIndicatorEntry, TestOnShowMenuXid)
00195 {
00196 
00197   indicator::Entry entry("xid", "name_hint", "label", true, true,
00198                          0, "some icon", false, true, -1);
00199 
00200   ShowMenuRecorder recorder;
00201   entry.on_show_menu.connect(sigc::mem_fun(recorder, &ShowMenuRecorder::OnShowMenu));
00202 
00203   entry.ShowMenu(88492615, 15, 25, 2, 123456);
00204   EXPECT_EQ(recorder.name, "xid");
00205   EXPECT_EQ(recorder.xid, 88492615);
00206   EXPECT_EQ(recorder.x, 15);
00207   EXPECT_EQ(recorder.y, 25);
00208   EXPECT_EQ(recorder.timestamp, 123456);
00209   EXPECT_EQ(recorder.button, 2);
00210 }
00211 
00212 TEST(TestIndicatorEntry, TestVisibility)
00213 {
00214 
00215   indicator::Entry entry("id", "name_hint", "label", true, true,
00216                          0, "some icon", false, false, -1);
00217 
00218   EXPECT_TRUE(entry.visible());
00219 
00220   entry.setLabel("", true, true);
00221   EXPECT_FALSE(entry.visible());
00222 
00223   entry.setLabel("valid-label", true, true);
00224   EXPECT_TRUE(entry.visible());
00225 
00226   entry.setLabel("invalid-label", true, false);
00227   EXPECT_FALSE(entry.visible());
00228 
00229   entry.setImage(1, "valid-image", true, true);
00230   EXPECT_TRUE(entry.visible());
00231 
00232   entry.setImage(1, "", true, true);
00233   EXPECT_FALSE(entry.visible());
00234 
00235   entry.setImage(1, "valid-image", true, true);
00236   EXPECT_TRUE(entry.visible());
00237 
00238   entry.setImage(0, "invalid-image-type", true, true);
00239   EXPECT_FALSE(entry.visible());
00240 
00241   entry.setLabel("valid-label", true, true);
00242   EXPECT_TRUE(entry.visible());
00243 
00244   entry.setLabel("invalid-label", true, false);
00245   EXPECT_FALSE(entry.visible());
00246 
00247   entry.setImage(1, "invalid-image", true, false);
00248   EXPECT_FALSE(entry.visible());
00249 
00250   entry.setLabel("valid-label", true, true);
00251   entry.setImage(1, "valid-image", true, true);
00252   EXPECT_TRUE(entry.visible());
00253 }
00254 
00255 TEST(TestIndicatorEntry, TestGeometry)
00256 {
00257 
00258   indicator::Entry entry("id", "name_hint", "label", true, true,
00259                          0, "some icon", false, true, -1);
00260 
00261   Counter counter;
00262   entry.updated.connect(sigc::mem_fun(counter, &Counter::increment));
00263   bool geo_changed = false;
00264   nux::Rect new_geo;
00265 
00266   entry.geometry_changed.connect([&] (nux::Rect const& geo) {
00267     geo_changed = true;
00268     new_geo = geo;
00269   });
00270 
00271   // Setting to the same value doesn't emit any events.
00272   entry.set_geometry(nux::Rect());
00273   EXPECT_EQ(entry.geometry(), nux::Rect());
00274   EXPECT_EQ(counter.count, 0);
00275 
00276   // Setting to a different value does emit the events.
00277   entry.set_geometry(nux::Rect(1, 2, 3, 4));
00278   EXPECT_EQ(entry.geometry(), nux::Rect(1, 2, 3, 4));
00279   EXPECT_TRUE(geo_changed);
00280   EXPECT_EQ(new_geo, nux::Rect(1, 2, 3, 4));
00281   EXPECT_EQ(counter.count, 1);
00282 }
00283 
00284 }