Back to index

unity  6.0.0
test_switcher_controller.cpp
Go to the documentation of this file.
00001 /*
00002  * Copyright 2012 Canonical Ltd.
00003  *
00004  * This program is free software: you can redistribute it and/or modify it
00005  * under the terms of the GNU General Public License version 3, as published
00006  * by the  Free Software Foundation.
00007  *
00008  * This program is distributed in the hope that it will be useful, but
00009  * WITHOUT ANY WARRANTY; without even the implied warranties of
00010  * MERCHANTABILITY, SATISFACTORY QUALITY or FITNESS FOR A PARTICULAR
00011  * PURPOSE.  See the GNU General Public License for more details.
00012  *
00013  * You should have received a copy of the GNU General Public License
00014  * version 3 along with this program.  If not, see
00015  * <http://www.gnu.org/licenses/>
00016  *
00017  * Authored by: Marco Trevisan (TreviƱo) <3v1n0@ubuntu.com>
00018  *
00019  */
00020 
00021 #include <gtest/gtest.h>
00022 #include <time.h>
00023 
00024 #include "test_utils.h"
00025 
00026 #include "SwitcherController.h"
00027 #include "TimeUtil.h"
00028 
00029 
00030 using namespace unity::switcher;
00031 
00032 namespace
00033 {
00034 
00035 unsigned int DEFAULT_LAZY_CONSTRUCT_TIMEOUT = 20;
00036 
00037 class MockSwitcherController : public Controller
00038 {
00039 public:
00040   MockSwitcherController()
00041     : Controller()
00042     , window_constructed_(false)
00043     , view_constructed_(false)
00044     , view_shown_(false)
00045     , detail_timeout_reached_(false)
00046   {};
00047 
00048   MockSwitcherController(unsigned int load_timeout)
00049     : Controller(load_timeout)
00050     , window_constructed_(false)
00051     , view_constructed_(false)
00052     , view_shown_(false)
00053     , detail_timeout_reached_(false)
00054   {};
00055 
00056   virtual void ConstructWindow()
00057   {
00058     window_constructed_ = true;
00059   }
00060 
00061   virtual void ConstructView()
00062   {
00063     view_constructed_ = true;
00064   }
00065 
00066   virtual void ShowView()
00067   {
00068     view_shown_ = true;
00069   }
00070 
00071   virtual bool OnDetailTimer()
00072   {
00073     detail_timeout_reached_ = true;
00074     clock_gettime(CLOCK_MONOTONIC, &detail_timespec_);
00075     return false;
00076   }
00077 
00078   unsigned int GetConstructTimeout() const
00079   {
00080     return construct_timeout_;
00081   }
00082 
00083   void FakeSelectionChange()
00084   {
00085     unity::launcher::AbstractLauncherIcon::Ptr icon(nullptr);
00086     OnModelSelectionChanged(icon);
00087   }
00088 
00089   bool window_constructed_;
00090   bool view_constructed_;
00091   bool view_shown_;
00092   bool detail_timeout_reached_;
00093   struct timespec detail_timespec_;
00094 };
00095 
00096 TEST(TestSwitcherController, Construction)
00097 {
00098   Controller controller;
00099   EXPECT_FALSE(controller.Visible());
00100 }
00101 
00102 TEST(TestSwitcherController, LazyConstructionTimeoutLength)
00103 {
00104   MockSwitcherController controller;
00105   EXPECT_EQ(controller.GetConstructTimeout(), DEFAULT_LAZY_CONSTRUCT_TIMEOUT);
00106 }
00107 
00108 TEST(TestSwitcherController, LazyWindowConstruction)
00109 {
00110   // Setting the timeout to a lower value to speed-up the test
00111   MockSwitcherController controller(2);
00112 
00113   EXPECT_EQ(controller.GetConstructTimeout(), 2);
00114 
00115   g_timeout_add_seconds(controller.GetConstructTimeout()/2, [] (gpointer data) -> gboolean {
00116     auto controller = static_cast<MockSwitcherController*>(data);
00117     EXPECT_FALSE(controller->window_constructed_);
00118 
00119     return FALSE;
00120   }, &controller);
00121 
00122   Utils::WaitUntil(controller.window_constructed_, controller.GetConstructTimeout() + 1);
00123   EXPECT_TRUE(controller.window_constructed_);
00124 }
00125 
00126 TEST(TestSwitcherController, InitialDetailTimeout)
00127 {
00128   MockSwitcherController controller;
00129   std::vector<unity::launcher::AbstractLauncherIcon::Ptr> results;
00130   struct timespec current;
00131 
00132   controller.initial_detail_timeout_length = 2000;
00133   controller.detail_timeout_length = 20000;
00134   clock_gettime(CLOCK_MONOTONIC, &current);
00135 
00136   controller.Show(ShowMode::ALL, SortMode::LAUNCHER_ORDER, false, results);
00137 
00138   Utils::WaitUntil(controller.detail_timeout_reached_, 3);
00139   ASSERT_TRUE(controller.detail_timeout_reached_);
00140   EXPECT_TRUE(unity::TimeUtil::TimeDelta(&controller.detail_timespec_, &current) >= 2000);
00141 }
00142 
00143 TEST(TestSwitcherController, DetailTimeout)
00144 {
00145   MockSwitcherController controller;
00146   struct timespec current;
00147 
00148   controller.detail_timeout_length = 1000;
00149   controller.initial_detail_timeout_length = 10000;
00150   clock_gettime(CLOCK_MONOTONIC, &current);
00151 
00152   controller.FakeSelectionChange();
00153 
00154   Utils::WaitUntil(controller.detail_timeout_reached_, 2);
00155   ASSERT_TRUE(controller.detail_timeout_reached_);
00156   EXPECT_TRUE(unity::TimeUtil::TimeDelta(&controller.detail_timespec_, &current) >= 1000);
00157 }
00158 
00159 TEST(TestSwitcherController, ShowSwitcher)
00160 {
00161   MockSwitcherController controller;
00162   std::vector<unity::launcher::AbstractLauncherIcon::Ptr> results;
00163 
00164   controller.Show(ShowMode::ALL, SortMode::LAUNCHER_ORDER, false, results);
00165 
00166   Utils::WaitUntil(controller.view_shown_, 2);
00167   ASSERT_TRUE(controller.view_shown_);
00168 }
00169 
00170 }