Back to index

unity  6.0.0
test_lens.cpp
Go to the documentation of this file.
00001 #include <boost/lexical_cast.hpp>
00002 #include <gtest/gtest.h>
00003 #include <glib-object.h>
00004 
00005 #include <UnityCore/CheckOptionFilter.h>
00006 #include <UnityCore/GLibWrapper.h>
00007 #include <UnityCore/Lens.h>
00008 #include <UnityCore/MultiRangeFilter.h>
00009 #include <UnityCore/MusicPreviews.h>
00010 #include <UnityCore/RadioOptionFilter.h>
00011 #include <UnityCore/RatingsFilter.h>
00012 
00013 #include "test_utils.h"
00014 
00015 using namespace std;
00016 using namespace unity::dash;
00017 
00018 namespace
00019 {
00020 
00021 static const string lens_name = "com.canonical.Unity.Test";
00022 static const string lens_path = "/com/canonical/unity/testlens";
00023 
00024 class TestLens : public ::testing::Test
00025 {
00026 public:
00027   TestLens()
00028     : lens_(new Lens("testlens", lens_name, lens_path,
00029                      "Test Lens", "gtk-apply")),
00030       n_categories_(0)
00031     , n_filters_(0)
00032   {
00033     WaitForConnected();
00034 
00035     Categories::Ptr categories = lens_->categories;
00036     categories->category_added.connect(sigc::mem_fun(this, &TestLens::OnCategoryAdded));
00037 
00038     Filters::Ptr filters = lens_->filters;
00039     filters->filter_added.connect(sigc::mem_fun(this, &TestLens::OnFilterAdded));
00040   }
00041 
00042   void OnCategoryAdded(Category const& category)
00043   {
00044     n_categories_++;
00045   }
00046 
00047   void OnFilterAdded(Filter::Ptr filter)
00048   {
00049     n_filters_++;
00050   }
00051  
00052   void WaitForConnected()
00053   {
00054     bool timeout_reached = false;
00055     guint32 timeout_id = Utils::ScheduleTimeout(&timeout_reached);
00056 
00057     while (!lens_->connected && !timeout_reached)
00058     {
00059       g_main_context_iteration(g_main_context_get_thread_default(), TRUE);
00060     }
00061     if (lens_->connected)
00062       g_source_remove(timeout_id);
00063 
00064     EXPECT_FALSE(timeout_reached);
00065   }
00066 
00067   template<typename Adaptor>
00068   void WaitForModel(Model<Adaptor>* model, unsigned int n_rows)
00069   {
00070     bool timeout_reached = false;
00071     guint32 timeout_id = Utils::ScheduleTimeout(&timeout_reached);
00072     
00073     while (model->count != n_rows && !timeout_reached)
00074     {
00075       g_main_context_iteration(g_main_context_get_thread_default(), TRUE);
00076     }
00077     if (model->count == n_rows)
00078       g_source_remove(timeout_id);
00079 
00080     EXPECT_FALSE(timeout_reached);
00081   }
00082 
00083   std::string PopulateAndGetFirstResultURI()
00084   {
00085     Results::Ptr results = lens_->results;
00086 
00087     lens_->Search("Test Search String");
00088     WaitForModel<Result>(results.get(), 5);
00089     
00090     Result result = results->RowAtIndex(0);
00091     return result.uri;
00092   }
00093 
00094   Lens::Ptr lens_;
00095   unsigned int n_categories_;
00096   unsigned int n_filters_;
00097 };
00098 
00099 TEST_F(TestLens, TestConnection)
00100 {
00101   EXPECT_EQ(lens_->id, "testlens");
00102   EXPECT_EQ(lens_->dbus_name, lens_name);
00103   EXPECT_EQ(lens_->dbus_path, lens_path);
00104   EXPECT_EQ(lens_->name, "Test Lens");
00105   EXPECT_EQ(lens_->icon_hint, "gtk-apply");
00106 }
00107 
00108 TEST_F(TestLens, TestSynchronization)
00109 {
00110   EXPECT_EQ(lens_->search_hint, "Search Test Lens");
00111   EXPECT_TRUE(lens_->visible);
00112   EXPECT_TRUE(lens_->search_in_global);
00113 }
00114 
00115 TEST_F(TestLens, TestCategories)
00116 {
00117   Categories::Ptr categories = lens_->categories;
00118   WaitForModel<Category>(categories.get(), 3);
00119 
00120   EXPECT_EQ(categories->count, (unsigned int)3);
00121   EXPECT_EQ(n_categories_, 3);
00122   
00123   Category category = categories->RowAtIndex(0);
00124   EXPECT_EQ(category.name, "Category1");
00125   EXPECT_EQ(category.icon_hint, "gtk-apply");
00126   EXPECT_EQ(category.index, (unsigned int)0);
00127   EXPECT_EQ(category.renderer_name, "tile-vertical");
00128 
00129   category = categories->RowAtIndex(1);
00130   EXPECT_EQ(category.name, "Category2");
00131   EXPECT_EQ(category.icon_hint, "gtk-cancel");
00132   EXPECT_EQ(category.index, (unsigned int)1);
00133   EXPECT_EQ(category.renderer_name, "tile-horizontal");
00134 
00135   category = categories->RowAtIndex(2);
00136   EXPECT_EQ(category.name, "Category3");
00137   EXPECT_EQ(category.icon_hint, "gtk-close");
00138   EXPECT_EQ(category.index, (unsigned int)2);
00139   EXPECT_EQ(category.renderer_name, "flow");
00140 }
00141 
00142 TEST_F(TestLens, TestSearch)
00143 {
00144   Results::Ptr results = lens_->results;
00145 
00146   lens_->Search("Test Search String");
00147   WaitForModel<Result>(results.get(), 5);
00148 
00149   for (unsigned int i = 0; i < 5; i++)
00150   {
00151     std::string name("Test Search String" + boost::lexical_cast<std::string>(i));
00152 
00153     Result result = results->RowAtIndex(i);
00154     
00155     std::string uri = result.uri;
00156     EXPECT_TRUE(uri.find("file:///test"));
00157     EXPECT_EQ(result.icon_hint, "gtk-apply");
00158     EXPECT_EQ(result.category_index, i);
00159     EXPECT_EQ(result.mimetype, "text/html");
00160     EXPECT_EQ(result.name, name);
00161     EXPECT_EQ(result.comment, "kamstrup likes ponies");
00162     EXPECT_EQ(result.dnd_uri, "file:///test");
00163   }
00164 }
00165 
00166 TEST_F(TestLens, TestGlobalSearch)
00167 {
00168   Results::Ptr results = lens_->global_results;
00169 
00170   lens_->GlobalSearch("Test Global Search String");
00171   WaitForModel<Result>(results.get(), 10);
00172 
00173   for (unsigned int i = 0; i < 10; i++)
00174   {
00175     std::string name("Test Global Search String" + boost::lexical_cast<std::string>(i));
00176 
00177     Result result = results->RowAtIndex(i);
00178 
00179     std::string uri = result.uri;
00180     EXPECT_TRUE(uri.find("file:///test"));
00181     EXPECT_EQ(result.icon_hint, "gtk-apply");
00182     EXPECT_EQ(result.category_index, i);
00183     EXPECT_EQ(result.mimetype, "text/html");
00184     EXPECT_EQ(result.name, name);
00185     EXPECT_EQ(result.comment, "kamstrup likes ponies");
00186     EXPECT_EQ(result.dnd_uri, "file:///test");
00187   }
00188 }
00189 
00190 TEST_F(TestLens, TestActivation)
00191 {
00192   std::string uri = PopulateAndGetFirstResultURI();
00193   
00194   bool activated = false;
00195   auto activated_cb = [&activated, &uri] (std::string const& uri_,
00196                                           HandledType handled,
00197                                           Lens::Hints const& hints)
00198   {
00199     EXPECT_EQ(uri, uri_);
00200     EXPECT_EQ(handled, HandledType::HIDE_DASH);
00201     EXPECT_EQ(hints.size(), 0);
00202     activated = true;
00203   };
00204   lens_->activated.connect(sigc::slot<void, std::string const&, HandledType,Lens::Hints const&>(activated_cb));  
00205   
00206   lens_->Activate(uri);
00207   Utils::WaitUntil(activated);
00208 }
00209 
00210 TEST_F(TestLens, TestPreview)
00211 {
00212   // FIXME: fix up when unity-core supports current preview protocol
00213   /*
00214   std::string uri = PopulateAndGetFirstResultURI();
00215   bool previewed = false;
00216 
00217   auto preview_cb = [&previewed, &uri] (std::string const& uri_,
00218                                   Preview::Ptr preview)
00219   {
00220     EXPECT_EQ(uri, uri_);
00221     EXPECT_EQ(preview->renderer_name, "preview-generic");
00222 
00223     TrackPreview::Ptr track_preview = std::static_pointer_cast<TrackPreview>(preview);
00224     EXPECT_EQ(track_preview->number, (unsigned int)1);
00225     EXPECT_EQ(track_preview->title, "Animus Vox");
00226     EXPECT_EQ(track_preview->artist, "The Glitch Mob");
00227     EXPECT_EQ(track_preview->album, "Drink The Sea");
00228     EXPECT_EQ(track_preview->length, (unsigned int)404);
00229     EXPECT_EQ(track_preview->album_cover, "file://music/the/track");
00230     EXPECT_EQ(track_preview->primary_action_name, "Play");
00231     EXPECT_EQ(track_preview->primary_action_icon_hint, "");
00232     EXPECT_EQ(track_preview->primary_action_uri, "play://music/the/track");
00233     EXPECT_EQ(track_preview->play_action_uri, "preview://music/the/track");
00234     EXPECT_EQ(track_preview->pause_action_uri, "pause://music/the/track");
00235     EXPECT_EQ(track_preview->genres.size(), (unsigned int)1);
00236     previewed = true;
00237   };
00238   lens_->preview_ready.connect(sigc::slot<void, std::string const&, Preview::Ptr>(preview_cb));
00239 
00240   lens_->Preview(uri);
00241   Utils::WaitUntil(previewed);
00242   */
00243 }
00244 
00245 TEST_F(TestLens, TestFilterSync)
00246 {
00247   Filters::Ptr filters = lens_->filters;
00248   WaitForModel<FilterAdaptor>(filters.get(), 4);
00249 
00250   EXPECT_EQ(filters->count, (unsigned int)4);
00251   EXPECT_EQ(n_filters_, 4);
00252 
00253 }
00254 
00255 TEST_F(TestLens, TestFilterRadioOption)
00256 {
00257   Filters::Ptr filters = lens_->filters;
00258   WaitForModel<FilterAdaptor>(filters.get(), 4);
00259 
00260   RadioOptionFilter::Ptr filter = static_pointer_cast<RadioOptionFilter>(filters->FilterAtIndex(0));
00261   EXPECT_EQ(filter->id, "when");
00262   EXPECT_EQ(filter->name, "When");
00263   EXPECT_EQ(filter->icon_hint, "");
00264   EXPECT_EQ(filter->renderer_name, "filter-radiooption");
00265   EXPECT_TRUE(filter->visible);
00266   EXPECT_FALSE(filter->collapsed);
00267   EXPECT_FALSE(filter->filtering);
00268 
00269   RadioOptionFilter::RadioOptions options = filter->options;
00270   EXPECT_EQ(options.size(), (unsigned int)3);
00271   
00272   EXPECT_EQ(options[0]->id, "today");
00273   EXPECT_EQ(options[0]->name, "Today");
00274   EXPECT_EQ(options[0]->icon_hint, "");
00275   EXPECT_FALSE(options[0]->active);
00276 
00277   EXPECT_EQ(options[1]->id, "yesterday");
00278   EXPECT_EQ(options[1]->name, "Yesterday");
00279   EXPECT_EQ(options[1]->icon_hint, "");
00280   EXPECT_FALSE(options[1]->active);
00281 
00282   EXPECT_EQ(options[2]->id, "lastweek");
00283   EXPECT_EQ(options[2]->name, "Last Week");
00284   EXPECT_EQ(options[2]->icon_hint, "");
00285   EXPECT_FALSE(options[2]->active);
00286 }
00287 
00288 TEST_F(TestLens, TestFilterRadioOptionLogic)
00289 {
00290   Filters::Ptr filters = lens_->filters;
00291   WaitForModel<FilterAdaptor>(filters.get(), 4);
00292 
00293   RadioOptionFilter::Ptr filter = static_pointer_cast<RadioOptionFilter>(filters->FilterAtIndex(0));
00294   RadioOptionFilter::RadioOptions options = filter->options;
00295 
00296   EXPECT_FALSE (filter->filtering);
00297   EXPECT_FALSE (options[0]->active);
00298   EXPECT_FALSE (options[1]->active);
00299   EXPECT_FALSE (options[2]->active);
00300 
00301   options[0]->active = true;
00302   options[0]->active = false;
00303   EXPECT_FALSE (filter->filtering);
00304   EXPECT_FALSE (options[0]->active);
00305   EXPECT_FALSE (options[1]->active);
00306   EXPECT_FALSE (options[2]->active);
00307 
00308   options[0]->active = true;
00309   EXPECT_TRUE (filter->filtering);
00310   EXPECT_TRUE (options[0]->active);
00311   EXPECT_FALSE (options[1]->active);
00312   EXPECT_FALSE (options[2]->active);
00313 
00314   options[1]->active = true;
00315   EXPECT_TRUE (filter->filtering);
00316   EXPECT_FALSE (options[0]->active);
00317   EXPECT_TRUE (options[1]->active);
00318   EXPECT_FALSE (options[2]->active);
00319 
00320   options[2]->active = true;
00321   EXPECT_TRUE (filter->filtering);
00322   EXPECT_FALSE (options[0]->active);
00323   EXPECT_FALSE (options[1]->active);
00324   EXPECT_TRUE (options[2]->active);
00325 
00326   filter->Clear();
00327   EXPECT_FALSE (filter->filtering);
00328   EXPECT_FALSE (options[0]->active);
00329   EXPECT_FALSE (options[1]->active);
00330   EXPECT_FALSE (options[2]->active);
00331 }
00332 
00333 TEST_F(TestLens, TestFilterCheckOption)
00334 {
00335   Filters::Ptr filters = lens_->filters;
00336   WaitForModel<FilterAdaptor>(filters.get(), 4);
00337 
00338   CheckOptionFilter::Ptr filter = static_pointer_cast<CheckOptionFilter>(filters->FilterAtIndex(1));
00339   EXPECT_EQ(filter->id, "type");
00340   EXPECT_EQ(filter->name, "Type");
00341   EXPECT_EQ(filter->icon_hint, "");
00342   EXPECT_EQ(filter->renderer_name, "filter-checkoption");
00343   EXPECT_TRUE(filter->visible);
00344   EXPECT_FALSE(filter->collapsed);
00345   EXPECT_FALSE(filter->filtering);
00346 
00347   CheckOptionFilter::CheckOptions options = filter->options;
00348   EXPECT_EQ(options.size(), (unsigned int)3);
00349   
00350   EXPECT_EQ(options[0]->id, "apps");
00351   EXPECT_EQ(options[0]->name, "Apps");
00352   EXPECT_EQ(options[0]->icon_hint, "gtk-apps");
00353   EXPECT_FALSE(options[0]->active);
00354 
00355   EXPECT_EQ(options[1]->id, "files");
00356   EXPECT_EQ(options[1]->name, "Files");
00357   EXPECT_EQ(options[1]->icon_hint, "gtk-files");
00358   EXPECT_FALSE(options[1]->active);
00359 
00360   EXPECT_EQ(options[2]->id, "music");
00361   EXPECT_EQ(options[2]->name, "Music");
00362   EXPECT_EQ(options[2]->icon_hint, "gtk-music");
00363   EXPECT_FALSE(options[2]->active);
00364 }
00365 
00366 TEST_F(TestLens, TestFilterCheckOptionLogic)
00367 {
00368   Filters::Ptr filters = lens_->filters;
00369   WaitForModel<FilterAdaptor>(filters.get(), 4);
00370 
00371   CheckOptionFilter::Ptr filter = static_pointer_cast<CheckOptionFilter>(filters->FilterAtIndex(1));
00372   CheckOptionFilter::CheckOptions options = filter->options;
00373 
00374   EXPECT_FALSE (filter->filtering);
00375   EXPECT_FALSE (options[0]->active);
00376   EXPECT_FALSE (options[1]->active);
00377   EXPECT_FALSE (options[2]->active);
00378 
00379   options[0]->active = true;
00380   options[0]->active = false;
00381   EXPECT_FALSE (filter->filtering);
00382   EXPECT_FALSE (options[0]->active);
00383   EXPECT_FALSE (options[1]->active);
00384   EXPECT_FALSE (options[2]->active);
00385 
00386   options[0]->active = true;
00387   EXPECT_TRUE (filter->filtering);
00388   EXPECT_TRUE (options[0]->active);
00389   EXPECT_FALSE (options[1]->active);
00390   EXPECT_FALSE (options[2]->active);
00391 
00392   options[1]->active = true;
00393   EXPECT_TRUE (filter->filtering);
00394   EXPECT_TRUE (options[0]->active);
00395   EXPECT_TRUE (options[1]->active);
00396   EXPECT_FALSE (options[2]->active);
00397 
00398   options[2]->active = true;
00399   EXPECT_TRUE (filter->filtering);
00400   EXPECT_TRUE (options[0]->active);
00401   EXPECT_TRUE (options[1]->active);
00402   EXPECT_TRUE (options[2]->active);
00403 
00404   filter->Clear();
00405   EXPECT_FALSE (filter->filtering);
00406   EXPECT_FALSE (options[0]->active);
00407   EXPECT_FALSE (options[1]->active);
00408   EXPECT_FALSE (options[2]->active);
00409 }
00410 
00411 TEST_F(TestLens, TestFilterRatings)
00412 {
00413   Filters::Ptr filters = lens_->filters;
00414   WaitForModel<FilterAdaptor>(filters.get(), 4);
00415 
00416   RatingsFilter::Ptr filter = static_pointer_cast<RatingsFilter>(filters->FilterAtIndex(2));
00417   EXPECT_EQ(filter->id, "ratings");
00418   EXPECT_EQ(filter->name, "Ratings");
00419   EXPECT_EQ(filter->icon_hint, "");
00420   std::string tmp = filter->renderer_name;
00421   EXPECT_EQ(filter->renderer_name, "filter-ratings");
00422   EXPECT_TRUE(filter->visible);
00423   EXPECT_FALSE(filter->collapsed);
00424   EXPECT_FALSE(filter->filtering);
00425 
00426   EXPECT_FLOAT_EQ(filter->rating, 0.0f);
00427   filter->rating = 0.5f;
00428   EXPECT_FLOAT_EQ(filter->rating, 0.5f);
00429 }
00430 
00431 TEST_F(TestLens, TestFilterMultiRange)
00432 {
00433   Filters::Ptr filters = lens_->filters;
00434   WaitForModel<FilterAdaptor>(filters.get(), 4);
00435 
00436   MultiRangeFilter::Ptr filter = static_pointer_cast<MultiRangeFilter>(filters->FilterAtIndex(3));
00437   EXPECT_EQ(filter->id, "size");
00438   EXPECT_EQ(filter->name, "Size");
00439   EXPECT_EQ(filter->icon_hint, "");
00440   std::string tmp = filter->renderer_name;
00441   EXPECT_EQ(filter->renderer_name, "filter-multirange");
00442   EXPECT_TRUE(filter->visible);
00443   EXPECT_TRUE(filter->collapsed);
00444   EXPECT_FALSE(filter->filtering);
00445 
00446   MultiRangeFilter::Options options = filter->options;
00447   EXPECT_EQ(options.size(), (unsigned int)4);
00448   
00449   EXPECT_EQ(options[0]->id, "1MB");
00450   EXPECT_EQ(options[0]->name, "1MB");
00451   EXPECT_EQ(options[0]->icon_hint, "");
00452   EXPECT_FALSE(options[0]->active);
00453 
00454   EXPECT_EQ(options[1]->id, "10MB");
00455   EXPECT_EQ(options[1]->name, "10MB");
00456   EXPECT_EQ(options[1]->icon_hint, "");
00457   EXPECT_FALSE(options[1]->active);
00458 
00459   EXPECT_EQ(options[2]->id, "100MB");
00460   EXPECT_EQ(options[2]->name, "100MB");
00461   EXPECT_EQ(options[2]->icon_hint, "");
00462   EXPECT_FALSE(options[2]->active);
00463 }
00464 
00465 TEST_F(TestLens, TestFilterMultiRangeLogic)
00466 {
00467   Filters::Ptr filters = lens_->filters;
00468   WaitForModel<FilterAdaptor>(filters.get(), 4);
00469 
00470   MultiRangeFilter::Ptr filter = static_pointer_cast<MultiRangeFilter>(filters->FilterAtIndex(3));
00471   MultiRangeFilter::Options options = filter->options;
00472 
00473   EXPECT_FALSE (filter->filtering);
00474   EXPECT_FALSE (options[0]->active);
00475   EXPECT_FALSE (options[1]->active);
00476   EXPECT_FALSE (options[2]->active);
00477   EXPECT_FALSE (options[3]->active);
00478 
00479   options[0]->active = true;
00480   options[3]->active = true;
00481   EXPECT_TRUE (filter->filtering);
00482   EXPECT_TRUE (options[0]->active);
00483   EXPECT_TRUE (options[1]->active);
00484   EXPECT_TRUE (options[2]->active);
00485   EXPECT_TRUE (options[3]->active);
00486 
00487   options[0]->active = true;
00488   options[2]->active = false;
00489   EXPECT_TRUE (filter->filtering);
00490   EXPECT_TRUE (options[0]->active);
00491   EXPECT_TRUE (options[1]->active);
00492   EXPECT_TRUE (options[2]->active);
00493   EXPECT_FALSE (options[3]->active);
00494 
00495   options[0]->active = false;
00496   EXPECT_TRUE (filter->filtering);
00497   EXPECT_FALSE (options[0]->active);
00498   EXPECT_TRUE (options[1]->active);
00499   EXPECT_TRUE (options[2]->active);
00500   EXPECT_FALSE (options[3]->active);
00501 
00502   filter->Clear();
00503   EXPECT_FALSE (filter->filtering);
00504   EXPECT_FALSE (options[0]->active);
00505   EXPECT_FALSE (options[1]->active);
00506   EXPECT_FALSE (options[2]->active);
00507   EXPECT_FALSE (options[3]->active);
00508 }
00509 
00510 
00511 }