1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
|
// Copyright (c) 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "platform/UserGestureIndicator.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "wtf/CurrentTime.h"
namespace blink {
static double s_currentTime = 1000.0;
static void advanceClock(double seconds) {
s_currentTime += seconds;
}
static double mockTimeFunction() {
return s_currentTime;
}
class TestUserGestureToken final : public UserGestureToken {
WTF_MAKE_NONCOPYABLE(TestUserGestureToken);
public:
static PassRefPtr<UserGestureToken> create(
Status status = PossiblyExistingGesture) {
return adoptRef(new TestUserGestureToken(status));
}
private:
TestUserGestureToken(Status status) : UserGestureToken(status) {}
};
// Checks for the initial state of UserGestureIndicator.
TEST(UserGestureIndicatorTest, InitialState) {
EXPECT_FALSE(UserGestureIndicator::utilizeUserGesture());
EXPECT_EQ(nullptr, UserGestureIndicator::currentToken());
EXPECT_FALSE(UserGestureIndicator::consumeUserGesture());
}
TEST(UserGestureIndicatorTest, ConstructedWithNewUserGesture) {
UserGestureIndicator userGestureScope(
TestUserGestureToken::create(UserGestureToken::NewGesture));
EXPECT_TRUE(UserGestureIndicator::utilizeUserGesture());
EXPECT_NE(nullptr, UserGestureIndicator::currentToken());
EXPECT_TRUE(UserGestureIndicator::consumeUserGesture());
}
TEST(UserGestureIndicatorTest, ConstructedWithUserGesture) {
UserGestureIndicator userGestureScope(TestUserGestureToken::create());
EXPECT_TRUE(UserGestureIndicator::utilizeUserGesture());
EXPECT_NE(nullptr, UserGestureIndicator::currentToken());
EXPECT_TRUE(UserGestureIndicator::consumeUserGesture());
}
TEST(UserGestureIndicatorTest, ConstructedWithNoUserGesture) {
UserGestureIndicator userGestureScope(nullptr);
EXPECT_FALSE(UserGestureIndicator::utilizeUserGesture());
EXPECT_EQ(nullptr, UserGestureIndicator::currentToken());
EXPECT_FALSE(UserGestureIndicator::consumeUserGesture());
}
// Check that after UserGestureIndicator destruction state will be cleared.
TEST(UserGestureIndicatorTest, DestructUserGestureIndicator) {
{
UserGestureIndicator userGestureScope(TestUserGestureToken::create());
EXPECT_TRUE(UserGestureIndicator::utilizeUserGesture());
EXPECT_NE(nullptr, UserGestureIndicator::currentToken());
}
EXPECT_FALSE(UserGestureIndicator::utilizeUserGesture());
EXPECT_EQ(nullptr, UserGestureIndicator::currentToken());
EXPECT_FALSE(UserGestureIndicator::consumeUserGesture());
}
// Tests creation of scoped UserGestureIndicator objects.
TEST(UserGestureIndicatorTest, ScopedNewUserGestureIndicators) {
// Root GestureIndicator and GestureToken.
UserGestureIndicator userGestureScope(
TestUserGestureToken::create(UserGestureToken::NewGesture));
EXPECT_TRUE(UserGestureIndicator::utilizeUserGesture());
EXPECT_NE(nullptr, UserGestureIndicator::currentToken());
{
// Construct inner UserGestureIndicator.
// It should share GestureToken with the root indicator.
UserGestureIndicator innerUserGesture(
TestUserGestureToken::create(UserGestureToken::NewGesture));
EXPECT_TRUE(UserGestureIndicator::utilizeUserGesture());
EXPECT_NE(nullptr, UserGestureIndicator::currentToken());
// Consume inner gesture.
EXPECT_TRUE(UserGestureIndicator::consumeUserGesture());
}
EXPECT_TRUE(UserGestureIndicator::utilizeUserGesture());
EXPECT_NE(nullptr, UserGestureIndicator::currentToken());
// Consume root gesture.
EXPECT_TRUE(UserGestureIndicator::consumeUserGesture());
EXPECT_FALSE(UserGestureIndicator::utilizeUserGesture());
EXPECT_NE(nullptr, UserGestureIndicator::currentToken());
}
TEST(UserGestureIndicatorTest, MultipleGesturesWithTheSameToken) {
UserGestureIndicator indicator(
TestUserGestureToken::create(UserGestureToken::NewGesture));
EXPECT_TRUE(UserGestureIndicator::processingUserGesture());
EXPECT_NE(nullptr, UserGestureIndicator::currentToken());
{
// Construct an inner indicator that shares the same token.
UserGestureIndicator innerIndicator(UserGestureIndicator::currentToken());
EXPECT_TRUE(UserGestureIndicator::processingUserGesture());
EXPECT_NE(nullptr, UserGestureIndicator::currentToken());
}
// Though the inner indicator was destroyed, the outer is still present (and
// the gesture hasn't been consumed), so it should still be processing a user
// gesture.
EXPECT_TRUE(UserGestureIndicator::processingUserGesture());
EXPECT_NE(nullptr, UserGestureIndicator::currentToken());
}
class UsedCallback : public UserGestureUtilizedCallback {
public:
UsedCallback() : m_usedCount(0) {}
void userGestureUtilized() override { m_usedCount++; }
unsigned getAndResetUsedCount() {
unsigned curCount = m_usedCount;
m_usedCount = 0;
return curCount;
}
private:
unsigned m_usedCount;
};
// Tests callback invocation.
TEST(UserGestureIndicatorTest, Callback) {
UsedCallback cb;
{
UserGestureIndicator userGestureScope(TestUserGestureToken::create());
UserGestureIndicator::currentToken()->setUserGestureUtilizedCallback(&cb);
EXPECT_EQ(0u, cb.getAndResetUsedCount());
// Untracked doesn't invoke the callback
EXPECT_TRUE(UserGestureIndicator::processingUserGesture());
EXPECT_EQ(0u, cb.getAndResetUsedCount());
// But processingUserGesture does
EXPECT_TRUE(UserGestureIndicator::utilizeUserGesture());
EXPECT_EQ(1u, cb.getAndResetUsedCount());
// But only the first time
EXPECT_TRUE(UserGestureIndicator::utilizeUserGesture());
EXPECT_TRUE(UserGestureIndicator::consumeUserGesture());
EXPECT_EQ(0u, cb.getAndResetUsedCount());
}
EXPECT_EQ(0u, cb.getAndResetUsedCount());
{
UserGestureIndicator userGestureScope(TestUserGestureToken::create());
UserGestureIndicator::currentToken()->setUserGestureUtilizedCallback(&cb);
// Consume also invokes the callback
EXPECT_TRUE(UserGestureIndicator::consumeUserGesture());
EXPECT_EQ(1u, cb.getAndResetUsedCount());
// But only once
EXPECT_FALSE(UserGestureIndicator::utilizeUserGesture());
EXPECT_FALSE(UserGestureIndicator::consumeUserGesture());
EXPECT_EQ(0u, cb.getAndResetUsedCount());
}
{
std::unique_ptr<UserGestureIndicator> userGestureScope(
new UserGestureIndicator(TestUserGestureToken::create()));
RefPtr<UserGestureToken> token = UserGestureIndicator::currentToken();
token->setUserGestureUtilizedCallback(&cb);
userGestureScope.reset();
// The callback should be cleared when the UseGestureIndicator is deleted.
EXPECT_FALSE(UserGestureIndicator::utilizeUserGesture());
EXPECT_EQ(0u, cb.getAndResetUsedCount());
}
// The callback isn't invoked outside the scope of the UGI
EXPECT_FALSE(UserGestureIndicator::utilizeUserGesture());
EXPECT_EQ(0u, cb.getAndResetUsedCount());
EXPECT_FALSE(UserGestureIndicator::consumeUserGesture());
EXPECT_EQ(0u, cb.getAndResetUsedCount());
}
TEST(UserGestureIndicatorTest, Timeouts) {
TimeFunction previous = setTimeFunctionsForTesting(mockTimeFunction);
{
// Token times out after 1 second.
RefPtr<UserGestureToken> token = TestUserGestureToken::create();
EXPECT_TRUE(token->hasGestures());
UserGestureIndicator userGestureScope(token.get());
EXPECT_TRUE(token->hasGestures());
advanceClock(0.75);
EXPECT_TRUE(token->hasGestures());
advanceClock(0.75);
EXPECT_FALSE(token->hasGestures());
}
{
// Timestamp is reset when a token is put in a UserGestureIndicator.
RefPtr<UserGestureToken> token = TestUserGestureToken::create();
EXPECT_TRUE(token->hasGestures());
advanceClock(0.75);
EXPECT_TRUE(token->hasGestures());
UserGestureIndicator userGestureScope(token.get());
advanceClock(0.75);
EXPECT_TRUE(token->hasGestures());
advanceClock(0.75);
EXPECT_FALSE(token->hasGestures());
}
setTimeFunctionsForTesting(previous);
}
} // namespace blink
|