Home | History | Annotate | Download | only in android
      1 // Copyright 2014 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #include "base/time/time.h"
      6 #include "testing/gtest/include/gtest/gtest.h"
      7 #include "ui/gfx/android/scroller.h"
      8 
      9 namespace gfx {
     10 
     11 namespace {
     12 
     13 const float kDefaultStartX = 7.f;
     14 const float kDefaultStartY = 25.f;
     15 const float kDefaultDeltaX = -20.f;
     16 const float kDefaultDeltaY = 73.f;
     17 const float kDefaultVelocityX = -35.f;
     18 const float kDefaultVelocityY = 22.f;
     19 const float kEpsilon = 1e-3f;
     20 
     21 Scroller::Config DefaultConfig() {
     22   return Scroller::Config();
     23 }
     24 
     25 }  // namespace
     26 
     27 class ScrollerTest : public testing::Test {};
     28 
     29 TEST_F(ScrollerTest, Scroll) {
     30   Scroller scroller(DefaultConfig());
     31   base::TimeTicks start_time = base::TimeTicks::Now();
     32 
     33   // Start a scroll and verify initialized values.
     34   scroller.StartScroll(kDefaultStartX,
     35                        kDefaultStartY,
     36                        kDefaultDeltaX,
     37                        kDefaultDeltaY,
     38                        start_time);
     39 
     40   EXPECT_EQ(kDefaultStartX, scroller.GetStartX());
     41   EXPECT_EQ(kDefaultStartY, scroller.GetStartY());
     42   EXPECT_EQ(kDefaultStartX, scroller.GetCurrX());
     43   EXPECT_EQ(kDefaultStartY, scroller.GetCurrY());
     44   EXPECT_EQ(kDefaultStartX + kDefaultDeltaX, scroller.GetFinalX());
     45   EXPECT_EQ(kDefaultStartY + kDefaultDeltaY, scroller.GetFinalY());
     46   EXPECT_FALSE(scroller.IsFinished());
     47   EXPECT_EQ(base::TimeDelta(), scroller.GetTimePassed());
     48 
     49   // Advance halfway through the scroll.
     50   const base::TimeDelta scroll_duration = scroller.GetDuration();
     51   scroller.ComputeScrollOffset(start_time + scroll_duration / 2);
     52 
     53   // Ensure we've moved in the direction of the delta, but have yet to reach
     54   // the target.
     55   EXPECT_GT(kDefaultStartX, scroller.GetCurrX());
     56   EXPECT_LT(kDefaultStartY, scroller.GetCurrY());
     57   EXPECT_LT(scroller.GetFinalX(), scroller.GetCurrX());
     58   EXPECT_GT(scroller.GetFinalY(), scroller.GetCurrY());
     59   EXPECT_FALSE(scroller.IsFinished());
     60 
     61   // Ensure our velocity is non-zero and in the same direction as the delta.
     62   EXPECT_GT(0.f, scroller.GetCurrVelocityX() * kDefaultDeltaX);
     63   EXPECT_GT(0.f, scroller.GetCurrVelocityY() * kDefaultDeltaY);
     64   EXPECT_TRUE(scroller.IsScrollingInDirection(kDefaultDeltaX, kDefaultDeltaY));
     65 
     66   // Repeated offset computations at the same timestamp should yield identical
     67   // results.
     68   float curr_x = scroller.GetCurrX();
     69   float curr_y = scroller.GetCurrY();
     70   float curr_velocity_x = scroller.GetCurrVelocityX();
     71   float curr_velocity_y = scroller.GetCurrVelocityY();
     72   scroller.ComputeScrollOffset(start_time + scroll_duration / 2);
     73   EXPECT_EQ(curr_x, scroller.GetCurrX());
     74   EXPECT_EQ(curr_y, scroller.GetCurrY());
     75   EXPECT_EQ(curr_velocity_x, scroller.GetCurrVelocityX());
     76   EXPECT_EQ(curr_velocity_y, scroller.GetCurrVelocityY());
     77 
     78   // Advance to the end.
     79   scroller.ComputeScrollOffset(start_time + scroll_duration);
     80   EXPECT_EQ(scroller.GetFinalX(), scroller.GetCurrX());
     81   EXPECT_EQ(scroller.GetFinalY(), scroller.GetCurrY());
     82   EXPECT_TRUE(scroller.IsFinished());
     83   EXPECT_EQ(scroll_duration, scroller.GetTimePassed());
     84   EXPECT_NEAR(0.f, scroller.GetCurrVelocityX(), kEpsilon);
     85   EXPECT_NEAR(0.f, scroller.GetCurrVelocityY(), kEpsilon);
     86 
     87   // Try to advance further; nothing should change.
     88   scroller.ComputeScrollOffset(start_time + scroll_duration * 2);
     89   EXPECT_EQ(scroller.GetFinalX(), scroller.GetCurrX());
     90   EXPECT_EQ(scroller.GetFinalY(), scroller.GetCurrY());
     91   EXPECT_TRUE(scroller.IsFinished());
     92   EXPECT_EQ(scroll_duration, scroller.GetTimePassed());
     93 }
     94 
     95 TEST_F(ScrollerTest, Fling) {
     96   Scroller scroller(DefaultConfig());
     97   base::TimeTicks start_time = base::TimeTicks::Now();
     98 
     99   // Start a fling and verify initialized values.
    100   scroller.Fling(kDefaultStartX,
    101                  kDefaultStartY,
    102                  kDefaultVelocityX,
    103                  kDefaultVelocityY,
    104                  INT_MIN,
    105                  INT_MAX,
    106                  INT_MIN,
    107                  INT_MAX,
    108                  start_time);
    109 
    110   EXPECT_EQ(kDefaultStartX, scroller.GetStartX());
    111   EXPECT_EQ(kDefaultStartY, scroller.GetStartY());
    112   EXPECT_EQ(kDefaultStartX, scroller.GetCurrX());
    113   EXPECT_EQ(kDefaultStartY, scroller.GetCurrY());
    114   EXPECT_GT(kDefaultStartX, scroller.GetFinalX());
    115   EXPECT_LT(kDefaultStartY, scroller.GetFinalY());
    116   EXPECT_FALSE(scroller.IsFinished());
    117   EXPECT_EQ(base::TimeDelta(), scroller.GetTimePassed());
    118 
    119   // Advance halfway through the fling.
    120   const base::TimeDelta scroll_duration = scroller.GetDuration();
    121   scroller.ComputeScrollOffset(start_time + scroll_duration / 2);
    122 
    123   // Ensure we've moved in the direction of the velocity, but have yet to reach
    124   // the target.
    125   EXPECT_GT(kDefaultStartX, scroller.GetCurrX());
    126   EXPECT_LT(kDefaultStartY, scroller.GetCurrY());
    127   EXPECT_LT(scroller.GetFinalX(), scroller.GetCurrX());
    128   EXPECT_GT(scroller.GetFinalY(), scroller.GetCurrY());
    129   EXPECT_FALSE(scroller.IsFinished());
    130 
    131   // Ensure our velocity is non-zero and in the same direction as the original
    132   // velocity.
    133   EXPECT_LT(0.f, scroller.GetCurrVelocityX() * kDefaultVelocityX);
    134   EXPECT_LT(0.f, scroller.GetCurrVelocityY() * kDefaultVelocityY);
    135   EXPECT_TRUE(
    136       scroller.IsScrollingInDirection(kDefaultVelocityX, kDefaultVelocityY));
    137 
    138   // Repeated offset computations at the same timestamp should yield identical
    139   // results.
    140   float curr_x = scroller.GetCurrX();
    141   float curr_y = scroller.GetCurrY();
    142   float curr_velocity_x = scroller.GetCurrVelocityX();
    143   float curr_velocity_y = scroller.GetCurrVelocityY();
    144   scroller.ComputeScrollOffset(start_time + scroll_duration / 2);
    145   EXPECT_EQ(curr_x, scroller.GetCurrX());
    146   EXPECT_EQ(curr_y, scroller.GetCurrY());
    147   EXPECT_EQ(curr_velocity_x, scroller.GetCurrVelocityX());
    148   EXPECT_EQ(curr_velocity_y, scroller.GetCurrVelocityY());
    149 
    150   // Advance to the end.
    151   scroller.ComputeScrollOffset(start_time + scroll_duration);
    152   EXPECT_EQ(scroller.GetFinalX(), scroller.GetCurrX());
    153   EXPECT_EQ(scroller.GetFinalY(), scroller.GetCurrY());
    154   EXPECT_TRUE(scroller.IsFinished());
    155   EXPECT_EQ(scroll_duration, scroller.GetTimePassed());
    156   EXPECT_NEAR(0.f, scroller.GetCurrVelocityX(), kEpsilon);
    157   EXPECT_NEAR(0.f, scroller.GetCurrVelocityY(), kEpsilon);
    158 
    159   // Try to advance further; nothing should change.
    160   scroller.ComputeScrollOffset(start_time + scroll_duration * 2);
    161   EXPECT_EQ(scroller.GetFinalX(), scroller.GetCurrX());
    162   EXPECT_EQ(scroller.GetFinalY(), scroller.GetCurrY());
    163   EXPECT_TRUE(scroller.IsFinished());
    164   EXPECT_EQ(scroll_duration, scroller.GetTimePassed());
    165 }
    166 
    167 }  // namespace gfx
    168