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

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

Issue 2850983002: Remove UserGestureUtilizedCallback, it's unused outside of tests (Closed)
Patch Set: Created 3 years, 7 months 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
« no previous file with comments | « third_party/WebKit/Source/platform/UserGestureIndicator.cpp ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 "platform/wtf/CurrentTime.h" 7 #include "platform/wtf/CurrentTime.h"
8 #include "testing/gtest/include/gtest/gtest.h" 8 #include "testing/gtest/include/gtest/gtest.h"
9 9
10 namespace blink { 10 namespace blink {
(...skipping 16 matching lines...) Expand all
27 Status status = kPossiblyExistingGesture) { 27 Status status = kPossiblyExistingGesture) {
28 return AdoptRef(new TestUserGestureToken(status)); 28 return AdoptRef(new TestUserGestureToken(status));
29 } 29 }
30 30
31 private: 31 private:
32 TestUserGestureToken(Status status) : UserGestureToken(status) {} 32 TestUserGestureToken(Status status) : UserGestureToken(status) {}
33 }; 33 };
34 34
35 // Checks for the initial state of UserGestureIndicator. 35 // Checks for the initial state of UserGestureIndicator.
36 TEST(UserGestureIndicatorTest, InitialState) { 36 TEST(UserGestureIndicatorTest, InitialState) {
37 EXPECT_FALSE(UserGestureIndicator::UtilizeUserGesture()); 37 EXPECT_FALSE(UserGestureIndicator::ProcessingUserGesture());
38 EXPECT_EQ(nullptr, UserGestureIndicator::CurrentToken()); 38 EXPECT_EQ(nullptr, UserGestureIndicator::CurrentToken());
39 EXPECT_FALSE(UserGestureIndicator::ConsumeUserGesture()); 39 EXPECT_FALSE(UserGestureIndicator::ConsumeUserGesture());
40 } 40 }
41 41
42 TEST(UserGestureIndicatorTest, ConstructedWithNewUserGesture) { 42 TEST(UserGestureIndicatorTest, ConstructedWithNewUserGesture) {
43 UserGestureIndicator user_gesture_scope( 43 UserGestureIndicator user_gesture_scope(
44 TestUserGestureToken::Create(UserGestureToken::kNewGesture)); 44 TestUserGestureToken::Create(UserGestureToken::kNewGesture));
45 45
46 EXPECT_TRUE(UserGestureIndicator::UtilizeUserGesture()); 46 EXPECT_TRUE(UserGestureIndicator::ProcessingUserGesture());
47 EXPECT_NE(nullptr, UserGestureIndicator::CurrentToken()); 47 EXPECT_NE(nullptr, UserGestureIndicator::CurrentToken());
48 48
49 EXPECT_TRUE(UserGestureIndicator::ConsumeUserGesture()); 49 EXPECT_TRUE(UserGestureIndicator::ConsumeUserGesture());
50 } 50 }
51 51
52 TEST(UserGestureIndicatorTest, ConstructedWithUserGesture) { 52 TEST(UserGestureIndicatorTest, ConstructedWithUserGesture) {
53 UserGestureIndicator user_gesture_scope(TestUserGestureToken::Create()); 53 UserGestureIndicator user_gesture_scope(TestUserGestureToken::Create());
54 54
55 EXPECT_TRUE(UserGestureIndicator::UtilizeUserGesture()); 55 EXPECT_TRUE(UserGestureIndicator::ProcessingUserGesture());
56 EXPECT_NE(nullptr, UserGestureIndicator::CurrentToken()); 56 EXPECT_NE(nullptr, UserGestureIndicator::CurrentToken());
57 57
58 EXPECT_TRUE(UserGestureIndicator::ConsumeUserGesture()); 58 EXPECT_TRUE(UserGestureIndicator::ConsumeUserGesture());
59 } 59 }
60 60
61 TEST(UserGestureIndicatorTest, ConstructedWithNoUserGesture) { 61 TEST(UserGestureIndicatorTest, ConstructedWithNoUserGesture) {
62 UserGestureIndicator user_gesture_scope(nullptr); 62 UserGestureIndicator user_gesture_scope(nullptr);
63 63
64 EXPECT_FALSE(UserGestureIndicator::UtilizeUserGesture()); 64 EXPECT_FALSE(UserGestureIndicator::ProcessingUserGesture());
65 EXPECT_EQ(nullptr, UserGestureIndicator::CurrentToken()); 65 EXPECT_EQ(nullptr, UserGestureIndicator::CurrentToken());
66 66
67 EXPECT_FALSE(UserGestureIndicator::ConsumeUserGesture()); 67 EXPECT_FALSE(UserGestureIndicator::ConsumeUserGesture());
68 } 68 }
69 69
70 // Check that after UserGestureIndicator destruction state will be cleared. 70 // Check that after UserGestureIndicator destruction state will be cleared.
71 TEST(UserGestureIndicatorTest, DestructUserGestureIndicator) { 71 TEST(UserGestureIndicatorTest, DestructUserGestureIndicator) {
72 { 72 {
73 UserGestureIndicator user_gesture_scope(TestUserGestureToken::Create()); 73 UserGestureIndicator user_gesture_scope(TestUserGestureToken::Create());
74 74
75 EXPECT_TRUE(UserGestureIndicator::UtilizeUserGesture()); 75 EXPECT_TRUE(UserGestureIndicator::ProcessingUserGesture());
76 EXPECT_NE(nullptr, UserGestureIndicator::CurrentToken()); 76 EXPECT_NE(nullptr, UserGestureIndicator::CurrentToken());
77 } 77 }
78 78
79 EXPECT_FALSE(UserGestureIndicator::UtilizeUserGesture()); 79 EXPECT_FALSE(UserGestureIndicator::ProcessingUserGesture());
80 EXPECT_EQ(nullptr, UserGestureIndicator::CurrentToken()); 80 EXPECT_EQ(nullptr, UserGestureIndicator::CurrentToken());
81 EXPECT_FALSE(UserGestureIndicator::ConsumeUserGesture()); 81 EXPECT_FALSE(UserGestureIndicator::ConsumeUserGesture());
82 } 82 }
83 83
84 // Tests creation of scoped UserGestureIndicator objects. 84 // Tests creation of scoped UserGestureIndicator objects.
85 TEST(UserGestureIndicatorTest, ScopedNewUserGestureIndicators) { 85 TEST(UserGestureIndicatorTest, ScopedNewUserGestureIndicators) {
86 // Root GestureIndicator and GestureToken. 86 // Root GestureIndicator and GestureToken.
87 UserGestureIndicator user_gesture_scope( 87 UserGestureIndicator user_gesture_scope(
88 TestUserGestureToken::Create(UserGestureToken::kNewGesture)); 88 TestUserGestureToken::Create(UserGestureToken::kNewGesture));
89 89
90 EXPECT_TRUE(UserGestureIndicator::UtilizeUserGesture()); 90 EXPECT_TRUE(UserGestureIndicator::ProcessingUserGesture());
91 EXPECT_NE(nullptr, UserGestureIndicator::CurrentToken()); 91 EXPECT_NE(nullptr, UserGestureIndicator::CurrentToken());
92 { 92 {
93 // Construct inner UserGestureIndicator. 93 // Construct inner UserGestureIndicator.
94 // It should share GestureToken with the root indicator. 94 // It should share GestureToken with the root indicator.
95 UserGestureIndicator inner_user_gesture( 95 UserGestureIndicator inner_user_gesture(
96 TestUserGestureToken::Create(UserGestureToken::kNewGesture)); 96 TestUserGestureToken::Create(UserGestureToken::kNewGesture));
97 97
98 EXPECT_TRUE(UserGestureIndicator::UtilizeUserGesture()); 98 EXPECT_TRUE(UserGestureIndicator::ProcessingUserGesture());
99 EXPECT_NE(nullptr, UserGestureIndicator::CurrentToken()); 99 EXPECT_NE(nullptr, UserGestureIndicator::CurrentToken());
100 100
101 // Consume inner gesture. 101 // Consume inner gesture.
102 EXPECT_TRUE(UserGestureIndicator::ConsumeUserGesture()); 102 EXPECT_TRUE(UserGestureIndicator::ConsumeUserGesture());
103 } 103 }
104 104
105 EXPECT_TRUE(UserGestureIndicator::UtilizeUserGesture()); 105 EXPECT_TRUE(UserGestureIndicator::ProcessingUserGesture());
106 EXPECT_NE(nullptr, UserGestureIndicator::CurrentToken()); 106 EXPECT_NE(nullptr, UserGestureIndicator::CurrentToken());
107 107
108 // Consume root gesture. 108 // Consume root gesture.
109 EXPECT_TRUE(UserGestureIndicator::ConsumeUserGesture()); 109 EXPECT_TRUE(UserGestureIndicator::ConsumeUserGesture());
110 EXPECT_FALSE(UserGestureIndicator::UtilizeUserGesture()); 110 EXPECT_FALSE(UserGestureIndicator::ProcessingUserGesture());
111 EXPECT_NE(nullptr, UserGestureIndicator::CurrentToken()); 111 EXPECT_NE(nullptr, UserGestureIndicator::CurrentToken());
112 } 112 }
113 113
114 TEST(UserGestureIndicatorTest, MultipleGesturesWithTheSameToken) { 114 TEST(UserGestureIndicatorTest, MultipleGesturesWithTheSameToken) {
115 UserGestureIndicator indicator( 115 UserGestureIndicator indicator(
116 TestUserGestureToken::Create(UserGestureToken::kNewGesture)); 116 TestUserGestureToken::Create(UserGestureToken::kNewGesture));
117 EXPECT_TRUE(UserGestureIndicator::ProcessingUserGesture()); 117 EXPECT_TRUE(UserGestureIndicator::ProcessingUserGesture());
118 EXPECT_NE(nullptr, UserGestureIndicator::CurrentToken()); 118 EXPECT_NE(nullptr, UserGestureIndicator::CurrentToken());
119 { 119 {
120 // Construct an inner indicator that shares the same token. 120 // Construct an inner indicator that shares the same token.
121 UserGestureIndicator inner_indicator(UserGestureIndicator::CurrentToken()); 121 UserGestureIndicator inner_indicator(UserGestureIndicator::CurrentToken());
122 EXPECT_TRUE(UserGestureIndicator::ProcessingUserGesture()); 122 EXPECT_TRUE(UserGestureIndicator::ProcessingUserGesture());
123 EXPECT_NE(nullptr, UserGestureIndicator::CurrentToken()); 123 EXPECT_NE(nullptr, UserGestureIndicator::CurrentToken());
124 } 124 }
125 // Though the inner indicator was destroyed, the outer is still present (and 125 // Though the inner indicator was destroyed, the outer is still present (and
126 // the gesture hasn't been consumed), so it should still be processing a user 126 // the gesture hasn't been consumed), so it should still be processing a user
127 // gesture. 127 // gesture.
128 EXPECT_TRUE(UserGestureIndicator::ProcessingUserGesture()); 128 EXPECT_TRUE(UserGestureIndicator::ProcessingUserGesture());
129 EXPECT_NE(nullptr, UserGestureIndicator::CurrentToken()); 129 EXPECT_NE(nullptr, UserGestureIndicator::CurrentToken());
130 } 130 }
131 131
132 class UsedCallback : public UserGestureUtilizedCallback {
133 public:
134 UsedCallback() : used_count_(0) {}
135
136 void UserGestureUtilized() override { used_count_++; }
137
138 unsigned GetAndResetUsedCount() {
139 unsigned cur_count = used_count_;
140 used_count_ = 0;
141 return cur_count;
142 }
143
144 private:
145 unsigned used_count_;
146 };
147
148 // Tests callback invocation.
149 TEST(UserGestureIndicatorTest, Callback) {
150 UsedCallback cb;
151
152 {
153 UserGestureIndicator user_gesture_scope(TestUserGestureToken::Create());
154 UserGestureIndicator::CurrentToken()->SetUserGestureUtilizedCallback(&cb);
155 EXPECT_EQ(0u, cb.GetAndResetUsedCount());
156
157 // Untracked doesn't invoke the callback
158 EXPECT_TRUE(UserGestureIndicator::ProcessingUserGesture());
159 EXPECT_EQ(0u, cb.GetAndResetUsedCount());
160
161 // But processingUserGesture does
162 EXPECT_TRUE(UserGestureIndicator::UtilizeUserGesture());
163 EXPECT_EQ(1u, cb.GetAndResetUsedCount());
164
165 // But only the first time
166 EXPECT_TRUE(UserGestureIndicator::UtilizeUserGesture());
167 EXPECT_TRUE(UserGestureIndicator::ConsumeUserGesture());
168 EXPECT_EQ(0u, cb.GetAndResetUsedCount());
169 }
170 EXPECT_EQ(0u, cb.GetAndResetUsedCount());
171
172 {
173 UserGestureIndicator user_gesture_scope(TestUserGestureToken::Create());
174 UserGestureIndicator::CurrentToken()->SetUserGestureUtilizedCallback(&cb);
175
176 // Consume also invokes the callback
177 EXPECT_TRUE(UserGestureIndicator::ConsumeUserGesture());
178 EXPECT_EQ(1u, cb.GetAndResetUsedCount());
179
180 // But only once
181 EXPECT_FALSE(UserGestureIndicator::UtilizeUserGesture());
182 EXPECT_FALSE(UserGestureIndicator::ConsumeUserGesture());
183 EXPECT_EQ(0u, cb.GetAndResetUsedCount());
184 }
185
186 {
187 std::unique_ptr<UserGestureIndicator> user_gesture_scope(
188 new UserGestureIndicator(TestUserGestureToken::Create()));
189 RefPtr<UserGestureToken> token = UserGestureIndicator::CurrentToken();
190 token->SetUserGestureUtilizedCallback(&cb);
191 user_gesture_scope.reset();
192
193 // The callback should be cleared when the UseGestureIndicator is deleted.
194 EXPECT_FALSE(UserGestureIndicator::UtilizeUserGesture());
195 EXPECT_EQ(0u, cb.GetAndResetUsedCount());
196 }
197
198 // The callback isn't invoked outside the scope of the UGI
199 EXPECT_FALSE(UserGestureIndicator::UtilizeUserGesture());
200 EXPECT_EQ(0u, cb.GetAndResetUsedCount());
201 EXPECT_FALSE(UserGestureIndicator::ConsumeUserGesture());
202 EXPECT_EQ(0u, cb.GetAndResetUsedCount());
203 }
204
205 TEST(UserGestureIndicatorTest, Timeouts) { 132 TEST(UserGestureIndicatorTest, Timeouts) {
206 TimeFunction previous = SetTimeFunctionsForTesting(MockTimeFunction); 133 TimeFunction previous = SetTimeFunctionsForTesting(MockTimeFunction);
207 134
208 { 135 {
209 // Token times out after 1 second. 136 // Token times out after 1 second.
210 RefPtr<UserGestureToken> token = TestUserGestureToken::Create(); 137 RefPtr<UserGestureToken> token = TestUserGestureToken::Create();
211 EXPECT_TRUE(token->HasGestures()); 138 EXPECT_TRUE(token->HasGestures());
212 UserGestureIndicator user_gesture_scope(token.Get()); 139 UserGestureIndicator user_gesture_scope(token.Get());
213 EXPECT_TRUE(token->HasGestures()); 140 EXPECT_TRUE(token->HasGestures());
214 AdvanceClock(0.75); 141 AdvanceClock(0.75);
(...skipping 12 matching lines...) Expand all
227 AdvanceClock(0.75); 154 AdvanceClock(0.75);
228 EXPECT_TRUE(token->HasGestures()); 155 EXPECT_TRUE(token->HasGestures());
229 AdvanceClock(0.75); 156 AdvanceClock(0.75);
230 EXPECT_FALSE(token->HasGestures()); 157 EXPECT_FALSE(token->HasGestures());
231 } 158 }
232 159
233 SetTimeFunctionsForTesting(previous); 160 SetTimeFunctionsForTesting(previous);
234 } 161 }
235 162
236 } // namespace blink 163 } // namespace blink
OLDNEW
« no previous file with comments | « third_party/WebKit/Source/platform/UserGestureIndicator.cpp ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698