Back to index

unity  6.0.0
test_glib_source.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 #include "TimeUtil.h"
00024 #include "test_utils.h"
00025 
00026 #include <UnityCore/GLibSource.h>
00027 
00028 
00029 using namespace unity::glib;
00030 
00031 namespace
00032 {
00033 
00034 bool callback_called = false;
00035 unsigned int callback_call_count = 0;
00036 
00037 bool OnSourceCallbackStop()
00038 {
00039   callback_called = true;
00040   ++callback_call_count;
00041 
00042   return false;
00043 }
00044 
00045 bool OnSourceCallbackContinue()
00046 {
00047   callback_called = true;
00048   ++callback_call_count;
00049 
00050   return true;
00051 }
00052 
00053 
00054 // GLib Source tests
00055 
00056 TEST(TestGLibSource, ID)
00057 {
00058   Idle source;
00059   EXPECT_EQ(source.Id(), 0);
00060 }
00061 
00062 TEST(TestGLibSource, Running)
00063 {
00064   Idle source;
00065   EXPECT_FALSE(source.IsRunning());
00066 }
00067 
00068 TEST(TestGLibSource, Priority)
00069 {
00070   Idle source;
00071 
00072   source.SetPriority(Source::Priority::HIGH);
00073   EXPECT_EQ(source.GetPriority(), Source::Priority::HIGH);
00074 
00075   source.SetPriority(Source::Priority::DEFAULT);
00076   EXPECT_EQ(source.GetPriority(), Source::Priority::DEFAULT);
00077 
00078   source.SetPriority(Source::Priority::HIGH_IDLE);
00079   EXPECT_EQ(source.GetPriority(), Source::Priority::HIGH_IDLE);
00080 
00081   source.SetPriority(Source::Priority::DEFAULT_IDLE);
00082   EXPECT_EQ(source.GetPriority(), Source::Priority::DEFAULT_IDLE);
00083 
00084   source.SetPriority(Source::Priority::LOW);
00085   EXPECT_EQ(source.GetPriority(), Source::Priority::LOW);
00086 }
00087 
00088 
00089 // GLib Timeout tests
00090 
00091 TEST(TestGLibTimeout, Construction)
00092 {
00093   Timeout timeout(1000, &OnSourceCallbackContinue);
00094   EXPECT_NE(timeout.Id(), 0);
00095   EXPECT_TRUE(timeout.IsRunning());
00096   EXPECT_EQ(timeout.GetPriority(), Source::Priority::DEFAULT);
00097 }
00098 
00099 TEST(TestGLibTimeout, ConstructionEmptyCallback)
00100 {
00101   Timeout timeout(1000, Source::SourceCallback());
00102   EXPECT_NE(timeout.Id(), 0);
00103   EXPECT_TRUE(timeout.IsRunning());
00104   EXPECT_EQ(timeout.GetPriority(), Source::Priority::DEFAULT);
00105 }
00106 
00107 TEST(TestGLibTimeout, DelayedRunConstruction)
00108 {
00109   Timeout timeout(1000);
00110   EXPECT_EQ(timeout.Id(), 0);
00111   EXPECT_FALSE(timeout.IsRunning());
00112   EXPECT_EQ(timeout.GetPriority(), Source::Priority::DEFAULT);
00113 }
00114 
00115 TEST(TestGLibTimeout, Destroy)
00116 {
00117   callback_called = false;
00118   callback_call_count = 0;
00119   bool removed_called = false;
00120 
00121   {
00122     Timeout timeout(1000, &OnSourceCallbackContinue);
00123     timeout.removed.connect([&] (unsigned int id) { removed_called = true; });
00124   }
00125 
00126   EXPECT_TRUE(removed_called);
00127   EXPECT_EQ(callback_call_count, 0);
00128 }
00129 
00130 TEST(TestGLibTimeout, OneShotRun)
00131 {
00132   callback_called = false;
00133   callback_call_count = 0;
00134   struct timespec pre, post;
00135 
00136   Timeout timeout(100, &OnSourceCallbackStop);
00137   clock_gettime(CLOCK_MONOTONIC, &pre);
00138   timeout.removed.connect([&] (unsigned int id) { clock_gettime(CLOCK_MONOTONIC, &post); });
00139 
00140   Utils::WaitForTimeoutMSec(500);
00141   EXPECT_FALSE(timeout.IsRunning());
00142   EXPECT_TRUE(callback_called);
00143   EXPECT_EQ(callback_call_count, 1);
00144   int time_delta = unity::TimeUtil::TimeDelta(&post, &pre);
00145   EXPECT_GE(time_delta, 100);
00146   EXPECT_LT(time_delta, 110);
00147 }
00148 
00149 TEST(TestGLibTimeout, MultipleShotsRun)
00150 {
00151   callback_called = false;
00152   callback_call_count = 0;
00153   struct timespec pre, post;
00154 
00155   {
00156   Timeout timeout(100, &OnSourceCallbackContinue);
00157   clock_gettime(CLOCK_MONOTONIC, &pre);
00158   timeout.removed.connect([&] (unsigned int id) { clock_gettime(CLOCK_MONOTONIC, &post); });
00159 
00160   Utils::WaitForTimeoutMSec(650);
00161   EXPECT_TRUE(timeout.IsRunning());
00162   }
00163 
00164   EXPECT_TRUE(callback_called);
00165   EXPECT_EQ(callback_call_count, 6);
00166   int time_delta = unity::TimeUtil::TimeDelta(&post, &pre);
00167   EXPECT_GE(time_delta, 600);
00168   EXPECT_LT(time_delta, 700);
00169 }
00170 
00171 TEST(TestGLibTimeout, OneShotRunWithEmptyCallback)
00172 {
00173   struct timespec pre, post;
00174   Timeout timeout(100, Source::SourceCallback());
00175   clock_gettime(CLOCK_MONOTONIC, &pre);
00176   timeout.removed.connect([&] (unsigned int id) { clock_gettime(CLOCK_MONOTONIC, &post); });
00177 
00178   Utils::WaitForTimeoutMSec(500);
00179   EXPECT_FALSE(timeout.IsRunning());
00180   int time_delta = unity::TimeUtil::TimeDelta(&post, &pre);
00181   EXPECT_GE(time_delta, 100);
00182   EXPECT_LT(time_delta, 110);
00183 }
00184 
00185 TEST(TestGLibTimeout, Removal)
00186 {
00187   callback_called = false;
00188   callback_call_count = 0;
00189   bool removed_called = false;
00190 
00191   Timeout timeout(300, &OnSourceCallbackContinue);
00192   timeout.removed.connect([&] (unsigned int id) { removed_called = true; });
00193 
00194   Utils::WaitForTimeoutMSec(100);
00195   timeout.Remove();
00196 
00197   Utils::WaitForTimeoutMSec(300);
00198 
00199   EXPECT_NE(timeout.Id(), 0);
00200   EXPECT_FALSE(timeout.IsRunning());
00201   EXPECT_TRUE(removed_called);
00202   EXPECT_FALSE(callback_called);
00203   EXPECT_EQ(callback_call_count, 0);
00204 }
00205 
00206 TEST(TestGLibTimeout, Running)
00207 {
00208   callback_called = false;
00209   callback_call_count = 0;
00210 
00211   Timeout timeout(300);
00212   EXPECT_FALSE(timeout.IsRunning());
00213 
00214   timeout.Run(&OnSourceCallbackStop);
00215   EXPECT_TRUE(timeout.IsRunning());
00216   Utils::WaitForTimeoutMSec(600);
00217 
00218   EXPECT_FALSE(timeout.IsRunning());
00219   EXPECT_TRUE(callback_called);
00220   EXPECT_EQ(callback_call_count, 1);
00221 }
00222 
00223 TEST(TestGLibTimeout, RemoveOnCallback)
00224 {
00225   bool local_callback_called = false;
00226   unsigned int local_callback_call_count = 0;
00227 
00228   Timeout timeout(10, [&] {
00229     local_callback_called = true;
00230     ++local_callback_call_count;
00231     timeout.Remove();
00232 
00233     // this function would be called more than once if we had not removed the source.
00234     return true;
00235   });
00236 
00237   Utils::WaitForTimeoutMSec(100);
00238 
00239   ASSERT_EQ(timeout.IsRunning(), false);
00240   EXPECT_EQ(local_callback_called, true);
00241   EXPECT_EQ(local_callback_call_count, 1);
00242 }
00243 
00244 TEST(TestGLibTimeout, RemovePtrOnCallback)
00245 {
00246   bool local_callback_called = false;
00247   unsigned int local_callback_call_count = 0;
00248 
00249   Source::UniquePtr timeout(new Timeout(10, [&] {
00250     local_callback_called = true;
00251     ++local_callback_call_count;
00252     timeout.reset();
00253 
00254     // this function would be called more than once if we had not removed the source.
00255     return true;
00256   }));
00257 
00258   Utils::WaitForTimeoutMSec(100);
00259 
00260   ASSERT_EQ(timeout, nullptr);
00261   EXPECT_EQ(local_callback_called, true);
00262   EXPECT_EQ(local_callback_call_count, 1);
00263 }
00264 
00265 // GLib TimeoutSeconds tests
00266 
00267 TEST(TestGLibTimeoutSeconds, Construction)
00268 {
00269   TimeoutSeconds timeout(1, &OnSourceCallbackContinue);
00270   EXPECT_NE(timeout.Id(), 0);
00271   EXPECT_TRUE(timeout.IsRunning());
00272   EXPECT_EQ(timeout.GetPriority(), Source::Priority::DEFAULT);
00273 }
00274 
00275 TEST(TestGLibTimeoutSeconds, DelayedRunConstruction)
00276 {
00277   TimeoutSeconds timeout(1);
00278   EXPECT_EQ(timeout.Id(), 0);
00279   EXPECT_FALSE(timeout.IsRunning());
00280   EXPECT_EQ(timeout.GetPriority(), Source::Priority::DEFAULT);
00281 }
00282 
00283 TEST(TestGLibTimeoutSeconds, Destroy)
00284 {
00285   callback_called = false;
00286   callback_call_count = 0;
00287   bool removed_called = false;
00288 
00289   {
00290     TimeoutSeconds timeout(1, &OnSourceCallbackContinue);
00291     timeout.removed.connect([&] (unsigned int id) { removed_called = true; });
00292   }
00293 
00294   EXPECT_TRUE(removed_called);
00295   EXPECT_EQ(callback_call_count, 0);
00296 }
00297 
00298 TEST(TestGLibTimeoutSeconds, OneShotRun)
00299 {
00300   callback_called = false;
00301   callback_call_count = 0;
00302   struct timespec pre, post;
00303 
00304   TimeoutSeconds timeout(1, &OnSourceCallbackStop);
00305   clock_gettime(CLOCK_MONOTONIC, &pre);
00306   timeout.removed.connect([&] (unsigned int id) { clock_gettime(CLOCK_MONOTONIC, &post); });
00307 
00308   Utils::WaitForTimeoutMSec(2000);
00309   EXPECT_FALSE(timeout.IsRunning());
00310   EXPECT_TRUE(callback_called);
00311   EXPECT_EQ(callback_call_count, 1);
00312   int time_delta = unity::TimeUtil::TimeDelta(&post, &pre);
00313   EXPECT_GE(time_delta, 500);
00314   EXPECT_LT(time_delta, 2000);
00315 }
00316 
00317 TEST(TestGLibTimeoutSeconds, MultipleShotsRun)
00318 {
00319   callback_called = false;
00320   callback_call_count = 0;
00321   struct timespec pre, post;
00322 
00323   {
00324   TimeoutSeconds timeout(1, &OnSourceCallbackContinue);
00325   clock_gettime(CLOCK_MONOTONIC, &pre);
00326   timeout.removed.connect([&] (unsigned int id) { clock_gettime(CLOCK_MONOTONIC, &post); });
00327 
00328   Utils::WaitForTimeoutMSec(3999);
00329   EXPECT_TRUE(timeout.IsRunning());
00330   }
00331 
00332   EXPECT_TRUE(callback_called);
00333   EXPECT_GE(callback_call_count, 3);
00334   EXPECT_LE(callback_call_count, 4);
00335   int time_delta = unity::TimeUtil::TimeDelta(&post, &pre);
00336   EXPECT_GE(time_delta, 3500);
00337   EXPECT_LT(time_delta, 5000);
00338 }
00339 
00340 // GLib Idle tests
00341 
00342 TEST(TestGLibIdle, Construction)
00343 {
00344   Idle idle(&OnSourceCallbackStop);
00345   EXPECT_NE(idle.Id(), 0);
00346   EXPECT_EQ(idle.GetPriority(), Source::Priority::DEFAULT_IDLE);
00347 }
00348 
00349 TEST(TestGLibIdle, Destroy)
00350 {
00351   callback_called = false;
00352   callback_call_count = 0;
00353   bool removed_called = false;
00354 
00355   {
00356     Idle idle(&OnSourceCallbackContinue);
00357     idle.removed.connect([&] (unsigned int id) { removed_called = true; });
00358   }
00359 
00360   EXPECT_TRUE(removed_called);
00361   EXPECT_EQ(callback_call_count, 0);
00362 }
00363 
00364 TEST(TestGLibIdle, OneShotRun)
00365 {
00366   callback_called = false;
00367   callback_call_count = 0;
00368   long long pre = 0;
00369   long long post = 0;
00370 
00371   Idle idle(&OnSourceCallbackStop);
00372   pre = g_get_monotonic_time();
00373   idle.removed.connect([&] (unsigned int id) { post = g_get_monotonic_time(); });
00374 
00375   Utils::WaitForTimeoutMSec(100);
00376   EXPECT_FALSE(idle.IsRunning());
00377   EXPECT_TRUE(callback_called);
00378   EXPECT_EQ(callback_call_count, 1);
00379   EXPECT_LT(pre, post);
00380 }
00381 
00382 TEST(TestGLibIdle, MultipleShotsRun)
00383 {
00384   callback_called = false;
00385   callback_call_count = 0;
00386   struct timespec pre, post;
00387 
00388   {
00389   Idle idle(&OnSourceCallbackContinue);
00390   clock_gettime(CLOCK_MONOTONIC, &pre);
00391   idle.removed.connect([&] (unsigned int id) { clock_gettime(CLOCK_MONOTONIC, &post); });
00392 
00393   Utils::WaitForTimeoutMSec(100);
00394   EXPECT_TRUE(idle.IsRunning());
00395   }
00396 
00397   EXPECT_TRUE(callback_called);
00398   EXPECT_GT(callback_call_count, 1);
00399   int time_delta = unity::TimeUtil::TimeDelta(&post, &pre);
00400   EXPECT_GE(time_delta, 100);
00401   EXPECT_LT(time_delta, 200);
00402 }
00403 
00404 TEST(TestGLibIdle, Removal)
00405 {
00406   callback_called = false;
00407   callback_call_count = 0;
00408   bool removed_called = false;
00409 
00410   Idle idle(&OnSourceCallbackContinue);
00411   idle.removed.connect([&] (unsigned int id) { removed_called = true; });
00412   idle.Remove();
00413 
00414   Utils::WaitForTimeoutMSec(300);
00415 
00416   EXPECT_NE(idle.Id(), 0);
00417   EXPECT_FALSE(idle.IsRunning());
00418   EXPECT_TRUE(removed_called);
00419   EXPECT_FALSE(callback_called);
00420   EXPECT_EQ(callback_call_count, 0);
00421 }
00422 
00423 TEST(TestGLibIdle, Running)
00424 {
00425   callback_called = false;
00426   callback_call_count = 0;
00427 
00428   Idle idle;
00429   EXPECT_FALSE(idle.IsRunning());
00430 
00431   idle.Run(&OnSourceCallbackStop);
00432   EXPECT_TRUE(idle.IsRunning());
00433   Utils::WaitForTimeoutMSec(200);
00434 
00435   EXPECT_FALSE(idle.IsRunning());
00436   EXPECT_TRUE(callback_called);
00437   EXPECT_EQ(callback_call_count, 1);
00438 }
00439 
00440 TEST(TestGLibIdle, RemoveOnCallback)
00441 {
00442   bool local_callback_called = false;
00443   unsigned int local_callback_call_count = 0;
00444 
00445   Idle idle([&] {
00446     local_callback_called = true;
00447     ++local_callback_call_count;
00448     idle.Remove();
00449 
00450     // this function would be called more than once if we had not removed the source.
00451     return true;
00452   });
00453 
00454   Utils::WaitForTimeoutMSec(100);
00455 
00456   ASSERT_EQ(idle.IsRunning(), false);
00457   EXPECT_EQ(local_callback_called, true);
00458   EXPECT_EQ(local_callback_call_count, 1);
00459 }
00460 
00461 TEST(TestGLibIdle, RemovePtrOnCallback)
00462 {
00463   bool local_callback_called = false;
00464   unsigned int local_callback_call_count = 0;
00465 
00466   Source::UniquePtr idle(new Idle([&] {
00467     local_callback_called = true;
00468     ++local_callback_call_count;
00469     idle.reset();
00470 
00471     // this function would be called more than once if we had not removed the source.
00472     return true;
00473   }));
00474 
00475   Utils::WaitForTimeoutMSec(100);
00476 
00477   ASSERT_EQ(idle, nullptr);
00478   EXPECT_EQ(local_callback_called, true);
00479   EXPECT_EQ(local_callback_call_count, 1);
00480 }
00481 
00482 // Test GLibSource Manager
00483 
00484 class MockSourceManager : public SourceManager
00485 {
00486 public:
00487   SourcesMap GetSources()
00488   {
00489     return sources_;
00490   }
00491 };
00492 
00493 TEST(TestGLibSourceManager, Construction)
00494 {
00495   SourceManager manager;
00496 }
00497 
00498 TEST(TestGLibSourceManager, AddingAnonymousSources)
00499 {
00500   MockSourceManager manager;
00501 
00502   manager.Add(new Timeout(1));
00503   manager.Add(new Timeout(1, &OnSourceCallbackContinue));
00504   manager.Add(new Idle());
00505   manager.Add(new Idle(&OnSourceCallbackContinue));
00506 
00507   EXPECT_EQ(manager.GetSources().size(), 4);
00508 }
00509 
00510 TEST(TestGLibSourceManager, AddingNamedSources)
00511 {
00512   MockSourceManager manager;
00513 
00514   Source* timeout_1 = new Timeout(1);
00515   manager.Add(timeout_1, "timeout-1");
00516   ASSERT_EQ(manager.GetSource("timeout-1").get(), timeout_1);
00517 
00518   Source* timeout_2 = new Timeout(1, &OnSourceCallbackContinue);
00519   manager.Add(timeout_2, "timeout-2");
00520   ASSERT_EQ(manager.GetSource("timeout-2").get(), timeout_2);
00521 
00522   Source* idle_1 = new Idle();
00523   manager.Add(idle_1, "idle-1");
00524   ASSERT_EQ(manager.GetSource("idle-1").get(), idle_1);
00525 
00526   Source* idle_2 = new Idle(&OnSourceCallbackContinue);
00527   manager.Add(idle_2, "idle-2");
00528   ASSERT_EQ(manager.GetSource("idle-2").get(), idle_2);
00529 
00530   EXPECT_EQ(manager.GetSources().size(), 4);
00531 }
00532 
00533 TEST(TestGLibSourceManager, AddingDuplicatedSources)
00534 {
00535   MockSourceManager manager;
00536   bool ret = false;
00537 
00538   Source::Ptr timeout(new Timeout(1));
00539 
00540   ret = manager.Add(timeout);
00541   EXPECT_EQ(manager.GetSource(timeout->Id()), timeout);
00542   EXPECT_EQ(ret, true);
00543 
00544   ret = manager.Add(timeout);
00545   EXPECT_EQ(manager.GetSource(timeout->Id()), timeout);
00546   EXPECT_EQ(ret, false);
00547 
00548   EXPECT_EQ(manager.GetSources().size(), 1);
00549 }
00550 
00551 TEST(TestGLibSourceManager, AddingDuplicatedNamedSources)
00552 {
00553   MockSourceManager manager;
00554   bool ret = false;
00555 
00556   Source::Ptr timeout_1(new Timeout(1, &OnSourceCallbackContinue));
00557   Source::Ptr timeout_2(new Timeout(2));
00558 
00559   ret = manager.Add(timeout_1, "timeout");
00560   EXPECT_EQ(manager.GetSource("timeout"), timeout_1);
00561   EXPECT_EQ(ret, true);
00562 
00563   ret = manager.Add(timeout_2, "timeout");
00564   EXPECT_EQ(manager.GetSource("timeout"), timeout_2);
00565   EXPECT_EQ(ret, true);
00566 
00567   EXPECT_FALSE(timeout_1->IsRunning());
00568   EXPECT_EQ(manager.GetSources().size(), 1);
00569 }
00570 
00571 TEST(TestGLibSourceManager, RemovingSourcesById)
00572 {
00573   MockSourceManager manager;
00574 
00575   Source::Ptr timeout1(new Timeout(1));
00576   Source::Ptr timeout2(new Timeout(2, &OnSourceCallbackContinue));
00577   Source::Ptr idle1(new Idle());
00578   Source::Ptr idle2(new Idle(&OnSourceCallbackContinue));
00579 
00580   manager.Add(timeout1);
00581   manager.Add(timeout2);
00582   manager.Add(idle1);
00583   manager.Add(idle2);
00584 
00585   manager.Remove(timeout1->Id());
00586   EXPECT_FALSE(timeout1->IsRunning());
00587   EXPECT_EQ(manager.GetSources().size(), 3);
00588 
00589   manager.Remove(timeout2->Id());
00590   EXPECT_FALSE(timeout2->IsRunning());
00591   EXPECT_EQ(manager.GetSource(timeout2->Id()), nullptr);
00592   EXPECT_EQ(manager.GetSources().size(), 2);
00593 
00594   manager.Remove(idle1->Id());
00595   EXPECT_FALSE(idle1->IsRunning());
00596   EXPECT_EQ(manager.GetSources().size(), 1);
00597 
00598   manager.Remove(idle2->Id());
00599   EXPECT_FALSE(idle2->IsRunning());
00600   EXPECT_EQ(manager.GetSource(idle2->Id()), nullptr);
00601   EXPECT_EQ(manager.GetSources().size(), 0);
00602 }
00603 
00604 TEST(TestGLibSourceManager, RemovingSourcesByNick)
00605 {
00606   MockSourceManager manager;
00607 
00608   Source::Ptr timeout1(new Timeout(1));
00609   Source::Ptr timeout2(new Timeout(2, &OnSourceCallbackContinue));
00610   Source::Ptr idle1(new Idle());
00611   Source::Ptr idle2(new Idle(&OnSourceCallbackContinue));
00612 
00613   manager.Add(timeout1, "timeout-1");
00614   manager.Add(timeout2, "timeout-2");
00615   manager.Add(idle1, "idle-1");
00616   manager.Add(idle2, "idle-2");
00617 
00618   manager.Remove("timeout-1");
00619   EXPECT_FALSE(timeout1->IsRunning());
00620   EXPECT_EQ(manager.GetSource("timeout-1"), nullptr);
00621   EXPECT_EQ(manager.GetSources().size(), 3);
00622 
00623   manager.Remove("timeout-2");
00624   EXPECT_FALSE(timeout2->IsRunning());
00625   EXPECT_EQ(manager.GetSource("timeout-2"), nullptr);
00626   EXPECT_EQ(manager.GetSources().size(), 2);
00627 
00628   manager.Remove("idle-1");
00629   EXPECT_FALSE(idle1->IsRunning());
00630   EXPECT_EQ(manager.GetSource("idle-1"), nullptr);
00631   EXPECT_EQ(manager.GetSources().size(), 1);
00632 
00633   manager.Remove("idle-2");
00634   EXPECT_FALSE(idle2->IsRunning());
00635   EXPECT_EQ(manager.GetSource("idle-2"), nullptr);
00636   EXPECT_EQ(manager.GetSources().size(), 0);
00637 }
00638 
00639 TEST(TestGLibSourceManager, RemovesOnDisconnection)
00640 {
00641   MockSourceManager manager;
00642 
00643   Source::Ptr timeout(new Timeout(1, &OnSourceCallbackContinue));
00644   Source::Ptr idle(new Idle(&OnSourceCallbackContinue));
00645 
00646   manager.Add(timeout);
00647   manager.Add(idle);
00648 
00649   ASSERT_EQ(manager.GetSources().size(), 2);
00650   EXPECT_EQ(timeout, manager.GetSource(timeout->Id()));
00651   EXPECT_EQ(idle, manager.GetSource(idle->Id()));
00652 
00653   timeout->Remove();
00654   EXPECT_EQ(manager.GetSources().size(), 1);
00655   EXPECT_EQ(manager.GetSource(timeout->Id()), nullptr);
00656 
00657   idle->Remove();
00658   EXPECT_EQ(manager.GetSources().size(), 0);
00659   EXPECT_EQ(manager.GetSource(idle->Id()), nullptr);
00660 }
00661 
00662 TEST(TestGLibSourceManager, DisconnectsOnRemoval)
00663 {
00664   Source::Ptr timeout(new Timeout(1, &OnSourceCallbackContinue));
00665   Source::Ptr idle(new Idle(&OnSourceCallbackContinue));
00666 
00667   {
00668     SourceManager manager;
00669     manager.Add(timeout);
00670     manager.Add(idle, "test-idle");
00671     ASSERT_TRUE(timeout->IsRunning());
00672     ASSERT_TRUE(idle->IsRunning());
00673   }
00674 
00675   EXPECT_FALSE(timeout->IsRunning());
00676   EXPECT_FALSE(idle->IsRunning());
00677 }
00678 
00679 TEST(TestGLibSourceManager, RemoveSourceOnCallback)
00680 {
00681   SourceManager manager;
00682   bool local_callback_called = false;
00683   unsigned int local_callback_call_count = 0;
00684 
00685   Source::Ptr idle(new Idle());
00686   manager.Add(idle, "test-idle");
00687   idle->Run([&] {
00688     local_callback_called = true;
00689     ++local_callback_call_count;
00690     manager.Remove("test-idle");
00691     // this function would be called more than once if we had not removed the source.
00692     return true;
00693   });
00694 
00695   Utils::WaitForTimeoutMSec(100);
00696 
00697   ASSERT_EQ(idle->IsRunning(), false);
00698   EXPECT_EQ(local_callback_called, true);
00699   EXPECT_EQ(local_callback_call_count, 1);
00700 }
00701 
00702 }