Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1172)

Side by Side Diff: third_party/WebKit/Source/platform/UserGestureIndicatorTest.cpp

Issue 2408333004: Move persistent gesture state to Document, add DocumentUserGestureToken (Closed)
Patch Set: Re-add dropped null check Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2016 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "platform/UserGestureIndicator.h" 5 #include "platform/UserGestureIndicator.h"
6 6
7 #include "testing/gtest/include/gtest/gtest.h" 7 #include "testing/gtest/include/gtest/gtest.h"
8 8
9 namespace blink { 9 namespace blink {
10 10
11 class TestUserGestureToken final : public UserGestureToken {
12 WTF_MAKE_NONCOPYABLE(TestUserGestureToken);
13
14 public:
15 static PassRefPtr<UserGestureToken> create(
16 Status status = PossiblyExistingGesture) {
17 return adoptRef(new TestUserGestureToken(status));
18 }
19
20 ~TestUserGestureToken() final {}
21
22 private:
23 TestUserGestureToken(Status status) : UserGestureToken(status) {}
24 };
25
11 // Checks for the initial state of UserGestureIndicator. 26 // Checks for the initial state of UserGestureIndicator.
12 TEST(UserGestureIndicatorTest, InitialState) { 27 TEST(UserGestureIndicatorTest, InitialState) {
13 EXPECT_FALSE(UserGestureIndicator::utilizeUserGesture()); 28 EXPECT_FALSE(UserGestureIndicator::utilizeUserGesture());
14 EXPECT_FALSE(UserGestureIndicator::processedUserGestureSinceLoad());
15 EXPECT_EQ(nullptr, UserGestureIndicator::currentToken()); 29 EXPECT_EQ(nullptr, UserGestureIndicator::currentToken());
16 EXPECT_FALSE(UserGestureIndicator::consumeUserGesture()); 30 EXPECT_FALSE(UserGestureIndicator::consumeUserGesture());
17 } 31 }
18 32
19 TEST(UserGestureIndicatorTest, ConstructedWithNewUserGesture) { 33 TEST(UserGestureIndicatorTest, ConstructedWithNewUserGesture) {
20 UserGestureIndicator::clearProcessedUserGestureSinceLoad();
21 UserGestureIndicator userGestureScope( 34 UserGestureIndicator userGestureScope(
22 UserGestureToken::create(UserGestureToken::NewGesture)); 35 TestUserGestureToken::create(UserGestureToken::NewGesture));
23 36
24 EXPECT_TRUE(UserGestureIndicator::utilizeUserGesture()); 37 EXPECT_TRUE(UserGestureIndicator::utilizeUserGesture());
25 EXPECT_TRUE(UserGestureIndicator::processedUserGestureSinceLoad());
26 EXPECT_NE(nullptr, UserGestureIndicator::currentToken()); 38 EXPECT_NE(nullptr, UserGestureIndicator::currentToken());
27 39
28 EXPECT_TRUE(UserGestureIndicator::consumeUserGesture()); 40 EXPECT_TRUE(UserGestureIndicator::consumeUserGesture());
29 } 41 }
30 42
31 TEST(UserGestureIndicatorTest, ConstructedWithUserGesture) { 43 TEST(UserGestureIndicatorTest, ConstructedWithUserGesture) {
32 UserGestureIndicator::clearProcessedUserGestureSinceLoad(); 44 UserGestureIndicator userGestureScope(TestUserGestureToken::create());
33 UserGestureIndicator userGestureScope(UserGestureToken::create());
34 45
35 EXPECT_TRUE(UserGestureIndicator::utilizeUserGesture()); 46 EXPECT_TRUE(UserGestureIndicator::utilizeUserGesture());
36 EXPECT_TRUE(UserGestureIndicator::processedUserGestureSinceLoad());
37 EXPECT_NE(nullptr, UserGestureIndicator::currentToken()); 47 EXPECT_NE(nullptr, UserGestureIndicator::currentToken());
38 48
39 EXPECT_TRUE(UserGestureIndicator::consumeUserGesture()); 49 EXPECT_TRUE(UserGestureIndicator::consumeUserGesture());
40 } 50 }
41 51
42 TEST(UserGestureIndicatorTest, ConstructedWithNoUserGesture) { 52 TEST(UserGestureIndicatorTest, ConstructedWithNoUserGesture) {
43 UserGestureIndicator::clearProcessedUserGestureSinceLoad();
44 UserGestureIndicator userGestureScope(nullptr); 53 UserGestureIndicator userGestureScope(nullptr);
45 54
46 EXPECT_FALSE(UserGestureIndicator::utilizeUserGesture()); 55 EXPECT_FALSE(UserGestureIndicator::utilizeUserGesture());
47 EXPECT_FALSE(UserGestureIndicator::processedUserGestureSinceLoad());
48 EXPECT_EQ(nullptr, UserGestureIndicator::currentToken()); 56 EXPECT_EQ(nullptr, UserGestureIndicator::currentToken());
49 57
50 EXPECT_FALSE(UserGestureIndicator::consumeUserGesture()); 58 EXPECT_FALSE(UserGestureIndicator::consumeUserGesture());
51 } 59 }
52 60
53 // Check that after UserGestureIndicator destruction state will be cleared. 61 // Check that after UserGestureIndicator destruction state will be cleared.
54 TEST(UserGestureIndicatorTest, DestructUserGestureIndicator) { 62 TEST(UserGestureIndicatorTest, DestructUserGestureIndicator) {
55 { 63 {
56 UserGestureIndicator userGestureScope(UserGestureToken::create()); 64 UserGestureIndicator userGestureScope(TestUserGestureToken::create());
57 65
58 EXPECT_TRUE(UserGestureIndicator::utilizeUserGesture()); 66 EXPECT_TRUE(UserGestureIndicator::utilizeUserGesture());
59 EXPECT_TRUE(UserGestureIndicator::processedUserGestureSinceLoad());
60 EXPECT_NE(nullptr, UserGestureIndicator::currentToken()); 67 EXPECT_NE(nullptr, UserGestureIndicator::currentToken());
61 } 68 }
62 69
63 EXPECT_FALSE(UserGestureIndicator::utilizeUserGesture()); 70 EXPECT_FALSE(UserGestureIndicator::utilizeUserGesture());
64 EXPECT_EQ(nullptr, UserGestureIndicator::currentToken()); 71 EXPECT_EQ(nullptr, UserGestureIndicator::currentToken());
65 EXPECT_FALSE(UserGestureIndicator::consumeUserGesture()); 72 EXPECT_FALSE(UserGestureIndicator::consumeUserGesture());
66 } 73 }
67 74
68 // Tests creation of scoped UserGestureIndicator objects. 75 // Tests creation of scoped UserGestureIndicator objects.
69 TEST(UserGestureIndicatorTest, ScopedNewUserGestureIndicators) { 76 TEST(UserGestureIndicatorTest, ScopedNewUserGestureIndicators) {
70 // Root GestureIndicator and GestureToken. 77 // Root GestureIndicator and GestureToken.
71 UserGestureIndicator userGestureScope( 78 UserGestureIndicator userGestureScope(
72 UserGestureToken::create(UserGestureToken::NewGesture)); 79 TestUserGestureToken::create(UserGestureToken::NewGesture));
73 80
74 EXPECT_TRUE(UserGestureIndicator::utilizeUserGesture()); 81 EXPECT_TRUE(UserGestureIndicator::utilizeUserGesture());
75 EXPECT_TRUE(UserGestureIndicator::processedUserGestureSinceLoad());
76 EXPECT_NE(nullptr, UserGestureIndicator::currentToken()); 82 EXPECT_NE(nullptr, UserGestureIndicator::currentToken());
77 { 83 {
78 // Construct inner UserGestureIndicator. 84 // Construct inner UserGestureIndicator.
79 // It should share GestureToken with the root indicator. 85 // It should share GestureToken with the root indicator.
80 UserGestureIndicator innerUserGesture( 86 UserGestureIndicator innerUserGesture(
81 UserGestureToken::create(UserGestureToken::NewGesture)); 87 TestUserGestureToken::create(UserGestureToken::NewGesture));
82 88
83 EXPECT_TRUE(UserGestureIndicator::utilizeUserGesture()); 89 EXPECT_TRUE(UserGestureIndicator::utilizeUserGesture());
84 EXPECT_NE(nullptr, UserGestureIndicator::currentToken()); 90 EXPECT_NE(nullptr, UserGestureIndicator::currentToken());
85 91
86 // Consume inner gesture. 92 // Consume inner gesture.
87 EXPECT_TRUE(UserGestureIndicator::consumeUserGesture()); 93 EXPECT_TRUE(UserGestureIndicator::consumeUserGesture());
88 } 94 }
89 95
90 EXPECT_TRUE(UserGestureIndicator::utilizeUserGesture()); 96 EXPECT_TRUE(UserGestureIndicator::utilizeUserGesture());
91 EXPECT_NE(nullptr, UserGestureIndicator::currentToken()); 97 EXPECT_NE(nullptr, UserGestureIndicator::currentToken());
(...skipping 18 matching lines...) Expand all
110 116
111 private: 117 private:
112 unsigned m_usedCount; 118 unsigned m_usedCount;
113 }; 119 };
114 120
115 // Tests callback invocation. 121 // Tests callback invocation.
116 TEST(UserGestureIndicatorTest, Callback) { 122 TEST(UserGestureIndicatorTest, Callback) {
117 UsedCallback cb; 123 UsedCallback cb;
118 124
119 { 125 {
120 UserGestureIndicator userGestureScope(UserGestureToken::create()); 126 UserGestureIndicator userGestureScope(TestUserGestureToken::create());
121 UserGestureIndicator::currentToken()->setUserGestureUtilizedCallback(&cb); 127 UserGestureIndicator::currentToken()->setUserGestureUtilizedCallback(&cb);
122 EXPECT_EQ(0u, cb.getAndResetUsedCount()); 128 EXPECT_EQ(0u, cb.getAndResetUsedCount());
123 129
124 // Untracked doesn't invoke the callback 130 // Untracked doesn't invoke the callback
125 EXPECT_TRUE(UserGestureIndicator::processingUserGesture()); 131 EXPECT_TRUE(UserGestureIndicator::processingUserGesture());
126 EXPECT_EQ(0u, cb.getAndResetUsedCount()); 132 EXPECT_EQ(0u, cb.getAndResetUsedCount());
127 133
128 // But processingUserGesture does 134 // But processingUserGesture does
129 EXPECT_TRUE(UserGestureIndicator::utilizeUserGesture()); 135 EXPECT_TRUE(UserGestureIndicator::utilizeUserGesture());
130 EXPECT_EQ(1u, cb.getAndResetUsedCount()); 136 EXPECT_EQ(1u, cb.getAndResetUsedCount());
131 137
132 // But only the first time 138 // But only the first time
133 EXPECT_TRUE(UserGestureIndicator::utilizeUserGesture()); 139 EXPECT_TRUE(UserGestureIndicator::utilizeUserGesture());
134 EXPECT_TRUE(UserGestureIndicator::consumeUserGesture()); 140 EXPECT_TRUE(UserGestureIndicator::consumeUserGesture());
135 EXPECT_EQ(0u, cb.getAndResetUsedCount()); 141 EXPECT_EQ(0u, cb.getAndResetUsedCount());
136 } 142 }
137 EXPECT_EQ(0u, cb.getAndResetUsedCount()); 143 EXPECT_EQ(0u, cb.getAndResetUsedCount());
138 144
139 { 145 {
140 UserGestureIndicator userGestureScope(UserGestureToken::create()); 146 UserGestureIndicator userGestureScope(TestUserGestureToken::create());
141 UserGestureIndicator::currentToken()->setUserGestureUtilizedCallback(&cb); 147 UserGestureIndicator::currentToken()->setUserGestureUtilizedCallback(&cb);
142 148
143 // Consume also invokes the callback 149 // Consume also invokes the callback
144 EXPECT_TRUE(UserGestureIndicator::consumeUserGesture()); 150 EXPECT_TRUE(UserGestureIndicator::consumeUserGesture());
145 EXPECT_EQ(1u, cb.getAndResetUsedCount()); 151 EXPECT_EQ(1u, cb.getAndResetUsedCount());
146 152
147 // But only once 153 // But only once
148 EXPECT_FALSE(UserGestureIndicator::utilizeUserGesture()); 154 EXPECT_FALSE(UserGestureIndicator::utilizeUserGesture());
149 EXPECT_FALSE(UserGestureIndicator::consumeUserGesture()); 155 EXPECT_FALSE(UserGestureIndicator::consumeUserGesture());
150 EXPECT_EQ(0u, cb.getAndResetUsedCount()); 156 EXPECT_EQ(0u, cb.getAndResetUsedCount());
151 } 157 }
152 158
153 { 159 {
154 std::unique_ptr<UserGestureIndicator> userGestureScope( 160 std::unique_ptr<UserGestureIndicator> userGestureScope(
155 new UserGestureIndicator(UserGestureToken::create())); 161 new UserGestureIndicator(TestUserGestureToken::create()));
156 RefPtr<UserGestureToken> token = UserGestureIndicator::currentToken(); 162 RefPtr<UserGestureToken> token = UserGestureIndicator::currentToken();
157 token->setUserGestureUtilizedCallback(&cb); 163 token->setUserGestureUtilizedCallback(&cb);
158 userGestureScope.reset(); 164 userGestureScope.reset();
159 165
160 // The callback should be cleared when the UseGestureIndicator is deleted. 166 // The callback should be cleared when the UseGestureIndicator is deleted.
161 EXPECT_FALSE(UserGestureIndicator::utilizeUserGesture()); 167 EXPECT_FALSE(UserGestureIndicator::utilizeUserGesture());
162 EXPECT_EQ(0u, cb.getAndResetUsedCount()); 168 EXPECT_EQ(0u, cb.getAndResetUsedCount());
163 } 169 }
164 170
165 // The callback isn't invoked outside the scope of the UGI 171 // The callback isn't invoked outside the scope of the UGI
166 EXPECT_FALSE(UserGestureIndicator::utilizeUserGesture()); 172 EXPECT_FALSE(UserGestureIndicator::utilizeUserGesture());
167 EXPECT_EQ(0u, cb.getAndResetUsedCount()); 173 EXPECT_EQ(0u, cb.getAndResetUsedCount());
168 EXPECT_FALSE(UserGestureIndicator::consumeUserGesture()); 174 EXPECT_FALSE(UserGestureIndicator::consumeUserGesture());
169 EXPECT_EQ(0u, cb.getAndResetUsedCount()); 175 EXPECT_EQ(0u, cb.getAndResetUsedCount());
170 } 176 }
171 177
172 } // namespace blink 178 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698