Back to index

unity  6.0.0
test_animator.cpp
Go to the documentation of this file.
00001 // -*- Mode: C++; indent-tabs-mode: nil; tab-width: 2 -*-
00002 /*
00003  * Copyright (C) 2011 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 <gtest/gtest.h>
00021 
00022 #include "Animator.h"
00023 #include "test_utils.h"
00024 
00025 using namespace std;
00026 using namespace unity;
00027 
00028 namespace
00029 {
00030 
00031 class TestAnimator : public ::testing::Test
00032 {
00033 public:
00034   TestAnimator() :
00035    test_animator_(100),
00036    n_steps_(0),
00037    current_progress_(0.0f),
00038    got_update_(false),
00039    half_reached_(false),
00040    started_(false),
00041    stopped_(false),
00042    ended_(false)
00043   {
00044     test_animator_.animation_started.connect([this]() {
00045       started_ = true;
00046     });
00047 
00048     test_animator_.animation_ended.connect([this]() {
00049       ended_ = true;
00050     });
00051 
00052     test_animator_.animation_stopped.connect([this](double progress) {
00053       stopped_ = true;
00054     });
00055 
00056     test_animator_.animation_updated.connect([this](double progress) {
00057       n_steps_++;
00058       current_progress_ = progress;
00059       got_update_ = true;
00060 
00061       if (progress >= 0.5f)
00062         half_reached_ = true;
00063     });
00064   }
00065 
00066 protected:
00067   void ResetValues()
00068   {
00069     n_steps_ = 0;
00070     current_progress_ = 0.0f;
00071     started_ = false;
00072     stopped_ = false;
00073     ended_ = false;
00074     got_update_ = false;
00075     half_reached_ = false;
00076   }
00077 
00078   Animator test_animator_;
00079   unsigned int n_steps_;
00080   double current_progress_;
00081   bool got_update_;
00082   bool half_reached_;
00083   bool started_;
00084   bool stopped_;
00085   bool ended_;
00086 };
00087 
00088 TEST_F(TestAnimator, ConstructDestroy)
00089 {
00090   bool stopped = false;
00091   double progress = 0.0f;
00092   
00093   {
00094     Animator tmp_animator(200, 25);
00095 
00096     EXPECT_EQ(tmp_animator.GetDuration(), 200);
00097     EXPECT_EQ(tmp_animator.GetRate(), 25);
00098 
00099     bool got_update = false;
00100     tmp_animator.animation_updated.connect([&progress, &got_update](double p) {
00101       progress = p;
00102       got_update = true;
00103     });
00104 
00105     tmp_animator.animation_stopped.connect([&stopped](double p) {
00106       stopped = true;
00107     });
00108 
00109     tmp_animator.Start();
00110 
00111     Utils::WaitUntil(got_update);
00112 
00113     EXPECT_EQ(tmp_animator.IsRunning(), true);
00114     EXPECT_GT(progress, 0.0f);
00115     EXPECT_EQ(stopped, false);
00116   }
00117 
00118   EXPECT_EQ(stopped, true);
00119 }
00120 
00121 TEST_F(TestAnimator, SetGetValues)
00122 {
00123   test_animator_.SetRate(30);
00124   EXPECT_EQ(test_animator_.GetRate(), 30);
00125 
00126   test_animator_.SetDuration(100);
00127   EXPECT_EQ(test_animator_.GetDuration(), 100);
00128 
00129   EXPECT_EQ(test_animator_.GetProgress(), 0.0f);
00130   EXPECT_EQ(test_animator_.IsRunning(), false);
00131 }
00132 
00133 TEST_F(TestAnimator, SimulateStep)
00134 {
00135   test_animator_.DoStep();
00136   EXPECT_EQ(test_animator_.IsRunning(), false);
00137   EXPECT_EQ(n_steps_, 1);
00138   EXPECT_GT(test_animator_.GetProgress(), 0.0f);
00139   ResetValues();
00140 }
00141 
00142 TEST_F(TestAnimator, SimulateAnimation)
00143 {
00144   test_animator_.SetRate(20);
00145   test_animator_.SetDuration(200);
00146   long long start_time = g_get_monotonic_time() / 1000;
00147   test_animator_.Start();
00148 
00149   EXPECT_EQ(started_, true);
00150   EXPECT_EQ(test_animator_.IsRunning(), true);
00151   
00152   Utils::WaitUntil(got_update_);
00153   EXPECT_GT(test_animator_.GetProgress(), 0.0f);
00154   EXPECT_EQ(test_animator_.GetProgress(), current_progress_);
00155   EXPECT_EQ(n_steps_, 1);
00156 
00157   Utils::WaitUntil(ended_);
00158   EXPECT_EQ(stopped_, false);
00159   EXPECT_EQ(ended_, true);
00160 
00161   unsigned int expected_steps = (test_animator_.GetDuration() / 1000.0f) * test_animator_.GetRate();
00162   EXPECT_EQ(n_steps_, ceil(expected_steps));
00163 
00164   long long actual_time = g_get_monotonic_time() / 1000;
00165   long long expected_end = start_time+test_animator_.GetDuration();
00166   EXPECT_GE(actual_time, expected_end);
00167   EXPECT_LE(actual_time-expected_end, test_animator_.GetRate()*2);
00168 
00169   ResetValues();
00170 }
00171 
00172 TEST_F(TestAnimator, SimulateStoppedAnimation)
00173 {
00174   test_animator_.SetRate(30);
00175   test_animator_.SetDuration(100);
00176   test_animator_.Start();
00177   EXPECT_EQ(started_, true);
00178   EXPECT_EQ(test_animator_.IsRunning(), true);
00179 
00180   Utils::WaitUntil(half_reached_);
00181   EXPECT_GT(test_animator_.GetProgress(), 0.5f);
00182   EXPECT_EQ(test_animator_.GetProgress(), current_progress_);
00183   EXPECT_EQ(test_animator_.IsRunning(), true);
00184 
00185   test_animator_.Stop();
00186   EXPECT_EQ(test_animator_.IsRunning(), false);
00187   EXPECT_LT(test_animator_.GetProgress(), 1.0f);
00188   EXPECT_EQ(stopped_, true);
00189   EXPECT_EQ(ended_, true);
00190 
00191   ResetValues();
00192 }
00193 
00194 TEST_F(TestAnimator, SimulateStoppedAndContinueAnimation)
00195 {
00196   test_animator_.SetRate(30);
00197   test_animator_.SetDuration(100);
00198   test_animator_.Start();
00199   EXPECT_EQ(started_, true);
00200   EXPECT_EQ(test_animator_.IsRunning(), true);
00201 
00202   Utils::WaitUntil(half_reached_);
00203   test_animator_.Stop();
00204 
00205   EXPECT_LT(test_animator_.GetProgress(), 1.0f);
00206   EXPECT_EQ(stopped_, true);
00207   EXPECT_EQ(ended_, true);
00208   stopped_ = false;
00209   ended_ = false;
00210 
00211   long long start_time = g_get_monotonic_time() / 1000;
00212   long long expected_end = start_time + (test_animator_.GetDuration() / current_progress_);
00213   test_animator_.Start(test_animator_.GetProgress());
00214   Utils::WaitUntil(ended_);
00215   EXPECT_EQ(stopped_, false);
00216   EXPECT_EQ(ended_, true);
00217 
00218   long long actual_time = g_get_monotonic_time() / 1000;
00219   EXPECT_LT(expected_end-actual_time, test_animator_.GetDuration());
00220 
00221   ResetValues();
00222 }
00223 
00224 TEST_F(TestAnimator, SimulateOneTimeDurationStart)
00225 {
00226   unsigned int default_duration = 100;
00227 
00228   test_animator_.SetRate(30);
00229   test_animator_.SetDuration(default_duration);
00230 
00231   unsigned int one_time_duration = 75;
00232   test_animator_.Start(one_time_duration);
00233   EXPECT_EQ(started_, true);
00234   EXPECT_EQ(test_animator_.IsRunning(), true);
00235 
00236   Utils::WaitUntil(half_reached_);
00237   EXPECT_LT(test_animator_.GetProgress(), 1.0f);
00238   EXPECT_EQ(test_animator_.GetDuration(), one_time_duration);
00239   EXPECT_EQ(ended_, false);
00240 
00241   Utils::WaitUntil(ended_);
00242   EXPECT_EQ(stopped_, false);
00243   EXPECT_EQ(ended_, true);
00244 
00245   EXPECT_EQ(test_animator_.GetDuration(), default_duration);
00246 
00247   ResetValues();
00248 }
00249 
00250 TEST_F(TestAnimator, SimulateOneTimeDurationStartStop)
00251 {
00252   unsigned int default_duration = 100;
00253 
00254   test_animator_.SetRate(30);
00255   test_animator_.SetDuration(default_duration);
00256 
00257   unsigned int one_time_duration = 75;
00258   test_animator_.Start(one_time_duration);
00259   EXPECT_EQ(started_, true);
00260   EXPECT_EQ(test_animator_.IsRunning(), true);
00261 
00262   Utils::WaitUntil(half_reached_);
00263   EXPECT_EQ(test_animator_.GetDuration(), one_time_duration);
00264   EXPECT_EQ(ended_, false);
00265 
00266   test_animator_.Stop();
00267   EXPECT_EQ(stopped_, true);
00268   EXPECT_EQ(ended_, true);
00269   EXPECT_EQ(test_animator_.GetDuration(), default_duration);
00270 
00271   ResetValues();
00272 }
00273 
00274 TEST_F(TestAnimator, SimulateZeroDuration)
00275 {
00276   test_animator_.SetRate(30);
00277   test_animator_.SetDuration(0);
00278 
00279   EXPECT_EQ(started_, false);
00280   EXPECT_EQ(ended_, false);
00281   EXPECT_EQ(test_animator_.IsRunning(), false);
00282 
00283   long long start_time = g_get_monotonic_time() / 1000;
00284   test_animator_.Start();
00285   EXPECT_EQ(started_, true);
00286 
00287   Utils::WaitUntil(ended_);
00288   EXPECT_EQ(ended_, true);
00289 
00290   long long end_time = g_get_monotonic_time() / 1000;
00291   EXPECT_LT(end_time - start_time, test_animator_.GetRate()*2);
00292 }
00293 
00294 
00295 } // Namespace