Back to index

unity  6.0.0
test_indicator.cpp
Go to the documentation of this file.
00001 // -*- Mode: C++; indent-tabs-mode: nil; tab-width: 2 -*-
00002 /*
00003  * Copyright (C) 2012 Canonical Ltd
00004  *
00005  * This program is free software: you can redistribute it and/or modify
00006  * it under the terms of the GNU General Public License version 3 as
00007  * published by the Free Software Foundation.
00008  *
00009  * This program is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  * GNU General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
00016  *
00017  * Authored by: Marco Trevisan (TreviƱo) <3v1n0@ubuntu.com>
00018  */
00019 
00020 #include <UnityCore/Indicator.h>
00021 
00022 #include <gtest/gtest.h>
00023 
00024 using namespace std;
00025 using namespace unity;
00026 using namespace indicator;
00027 
00028 namespace
00029 {
00030 
00031 TEST(TestIndicator, Construction)
00032 {
00033   Indicator indicator("indicator-test");
00034 
00035   EXPECT_EQ(indicator.name(), "indicator-test");
00036   EXPECT_FALSE(indicator.IsAppmenu());
00037   EXPECT_EQ(indicator.GetEntry("test-entry"), nullptr);
00038   EXPECT_TRUE(indicator.GetEntries().empty());
00039 }
00040 
00041 TEST(TestIndicator, Syncing)
00042 {
00043   Indicator::Entries sync_data;
00044   Entry* entry;
00045 
00046   Indicator indicator("indicator-test");
00047 
00048   // Connecting to signals
00049   Indicator::Entries added;
00050   indicator.on_entry_added.connect([&added] (Entry::Ptr const& e) {
00051     added.push_back(e);
00052   });
00053 
00054   std::vector<std::string> removed;
00055   sigc::connection removed_connection = indicator.on_entry_removed.connect([&removed]
00056   (std::string const& en_id) {
00057     removed.push_back(en_id);
00058   });
00059 
00060   entry = new Entry("test-entry-1", "name-hint", "label", true, true, 0, "icon",
00061                     true, true, -1);
00062   Entry::Ptr entry1(entry);
00063   sync_data.push_back(entry1);
00064 
00065   entry = new Entry("test-entry-2", "name-hint", "label", true, true, 0, "icon",
00066                     true, true, -1);
00067   Entry::Ptr entry2(entry);
00068   sync_data.push_back(entry2);
00069 
00070   entry = new Entry("test-entry-3", "name-hint", "label", true, true, 0, "icon",
00071                     true, true, -1);
00072   Entry::Ptr entry3(entry);
00073   sync_data.push_back(entry3);
00074 
00075   // Sync the indicator, adding 3 entries
00076   indicator.Sync(sync_data);
00077   EXPECT_EQ(indicator.GetEntries().size(), 3);
00078   EXPECT_EQ(indicator.GetEntry("test-entry-2"), entry2);
00079   EXPECT_EQ(added.size(), 3);
00080   EXPECT_EQ(added.front()->id(), "test-entry-1");
00081   EXPECT_EQ(added.back()->id(), "test-entry-3");
00082   EXPECT_EQ(removed.size(), 0);
00083 
00084   added.clear();
00085   removed.clear();
00086 
00087   // Sync the indicator removing an entry
00088   sync_data.remove(entry2);
00089   EXPECT_EQ(sync_data.size(), 2);
00090 
00091   indicator.Sync(sync_data);
00092   EXPECT_EQ(indicator.GetEntries().size(), 2);
00093   EXPECT_EQ(indicator.GetEntry("test-entry-2"), nullptr);
00094   EXPECT_EQ(added.size(), 0);
00095   EXPECT_EQ(removed.size(), 1);
00096   EXPECT_EQ(removed.front(), entry2->id());
00097 
00098   // Sync the indicator removing an entry and adding a new one
00099   entry = new Entry("test-entry-4", "name-hint", "label", true, true, 0, "icon",
00100                     true, true, -1);
00101   Entry::Ptr entry4(entry);
00102   sync_data.push_back(entry4);
00103   sync_data.remove(entry3);
00104   EXPECT_EQ(sync_data.size(), 2);
00105 
00106   added.clear();
00107   removed.clear();
00108 
00109   indicator.Sync(sync_data);
00110   EXPECT_EQ(indicator.GetEntries().size(), 2);
00111   EXPECT_EQ(added.size(), 1);
00112   EXPECT_EQ(added.front(), entry4);
00113   EXPECT_EQ(removed.size(), 1);
00114   EXPECT_EQ(removed.front(), entry3->id());
00115 
00116   // Remove all the indicators
00117   added.clear();
00118   removed.clear();
00119 
00120   sync_data.clear();
00121   indicator.Sync(sync_data);
00122   EXPECT_EQ(indicator.GetEntries().size(), 0);
00123   EXPECT_EQ(added.size(), 0);
00124   EXPECT_EQ(removed.size(), 2);
00125 
00126   removed_connection.disconnect();
00127 }
00128 
00129 TEST(TestIndicator, ChildrenSignals)
00130 {
00131   Indicator::Entries sync_data;
00132   Entry* entry;
00133 
00134   Indicator indicator("indicator-test");
00135 
00136   entry = new Entry("test-entry-1", "name-hint", "label", true, true, 0, "icon",
00137                     true, true, -1);
00138   Entry::Ptr entry1(entry);
00139   sync_data.push_back(entry1);
00140 
00141   indicator.Sync(sync_data);
00142 
00143   std::string show_entry;
00144   int show_x, show_y;
00145   unsigned int show_xid, show_button, show_timestamp;
00146 
00147   // Connecting to signals
00148   indicator.on_show_menu.connect([&] (std::string const& eid, unsigned int xid,
00149                                       int x, int y, unsigned int button,
00150                                       unsigned int timestamp) {
00151     show_entry = eid;
00152     show_xid = xid;
00153     show_x = x;
00154     show_y = y;
00155     show_button = button;
00156     show_timestamp = timestamp;
00157   });
00158 
00159   entry1->ShowMenu(123456789, 50, 100, 2, 1328058770);
00160   EXPECT_EQ(show_entry, "test-entry-1");
00161   EXPECT_EQ(show_xid, 123456789);
00162   EXPECT_EQ(show_x, 50);
00163   EXPECT_EQ(show_y, 100);
00164   EXPECT_EQ(show_button, 2);
00165   EXPECT_EQ(show_timestamp, 1328058770);
00166 
00167   // Check if a removed entry still emits a signal to the old indicator
00168   show_entry = "invalid-entry";
00169   sync_data.remove(entry1);
00170   indicator.Sync(sync_data);
00171 
00172   entry1->ShowMenu(123456789, 50, 100, 2, 1328058770);
00173   EXPECT_EQ(show_entry, "invalid-entry");
00174 
00175   // Checking secondary activate signal
00176   entry = new Entry("test-entry-2", "name-hint", "label", true, true, 0, "icon",
00177                       true, true, -1);
00178   sync_data.push_back(Entry::Ptr(entry));
00179   indicator.Sync(sync_data);
00180 
00181   std::string secondary_activated;
00182   unsigned int secondary_timestamp;
00183   indicator.on_secondary_activate.connect([&] (std::string const& eid,
00184                                                unsigned int timestamp) {
00185     secondary_activated = eid;
00186     secondary_timestamp = timestamp;
00187   });
00188 
00189   entry->SecondaryActivate(1328060717);
00190   EXPECT_EQ(secondary_activated, "test-entry-2");
00191   EXPECT_EQ(secondary_timestamp, 1328060717);
00192 
00193   // Checking scroll signal
00194   std::string scrolled_entry;
00195   int scrolled_delta;
00196 
00197   indicator.on_scroll.connect([&] (std::string const& eid, int delta) {
00198     scrolled_entry = eid;
00199     scrolled_delta = delta;
00200   });
00201 
00202   entry->Scroll(-5);
00203   EXPECT_EQ(scrolled_entry, "test-entry-2");
00204   EXPECT_EQ(scrolled_delta, -5);
00205 }
00206 
00207 }