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

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

Issue 1799253002: Stricter user gestures for touch - measure and warn (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Tweaks Created 4 years, 8 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
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 // Checks for the initial state of UserGestureIndicator. 11 // Checks for the initial state of UserGestureIndicator.
12 TEST(UserGestureIndicatorTest, InitialState) 12 TEST(UserGestureIndicatorTest, InitialState)
13 { 13 {
14 EXPECT_FALSE(UserGestureIndicator::processingUserGesture()); 14 EXPECT_FALSE(UserGestureIndicator::utilizeUserGesture());
15 EXPECT_FALSE(UserGestureIndicator::processedUserGestureSinceLoad()); 15 EXPECT_FALSE(UserGestureIndicator::processedUserGestureSinceLoad());
16 EXPECT_EQ(nullptr, UserGestureIndicator::currentToken()); 16 EXPECT_EQ(nullptr, UserGestureIndicator::currentToken());
17 EXPECT_FALSE(UserGestureIndicator::consumeUserGesture()); 17 EXPECT_FALSE(UserGestureIndicator::consumeUserGesture());
18 } 18 }
19 19
20 TEST(UserGestureIndicatorTest, ConstructedWithNewUserGesture) 20 TEST(UserGestureIndicatorTest, ConstructedWithNewUserGesture)
21 { 21 {
22 UserGestureIndicator::clearProcessedUserGestureSinceLoad(); 22 UserGestureIndicator::clearProcessedUserGestureSinceLoad();
23 UserGestureIndicator userGestureScope(DefinitelyProcessingNewUserGesture); 23 UserGestureIndicator userGestureScope(DefinitelyProcessingNewUserGesture);
24 24
25 EXPECT_TRUE(UserGestureIndicator::processingUserGesture()); 25 EXPECT_TRUE(UserGestureIndicator::utilizeUserGesture());
26 EXPECT_TRUE(UserGestureIndicator::processedUserGestureSinceLoad()); 26 EXPECT_TRUE(UserGestureIndicator::processedUserGestureSinceLoad());
27 EXPECT_NE(nullptr, UserGestureIndicator::currentToken()); 27 EXPECT_NE(nullptr, UserGestureIndicator::currentToken());
28 28
29 EXPECT_TRUE(UserGestureIndicator::consumeUserGesture()); 29 EXPECT_TRUE(UserGestureIndicator::consumeUserGesture());
30 } 30 }
31 31
32 TEST(UserGestureIndicatorTest, ConstructedWithUserGesture) 32 TEST(UserGestureIndicatorTest, ConstructedWithUserGesture)
33 { 33 {
34 UserGestureIndicator::clearProcessedUserGestureSinceLoad(); 34 UserGestureIndicator::clearProcessedUserGestureSinceLoad();
35 UserGestureIndicator userGestureScope(DefinitelyProcessingUserGesture); 35 UserGestureIndicator userGestureScope(DefinitelyProcessingUserGesture);
36 36
37 EXPECT_TRUE(UserGestureIndicator::processingUserGesture()); 37 EXPECT_TRUE(UserGestureIndicator::utilizeUserGesture());
38 EXPECT_TRUE(UserGestureIndicator::processedUserGestureSinceLoad()); 38 EXPECT_TRUE(UserGestureIndicator::processedUserGestureSinceLoad());
39 EXPECT_NE(nullptr, UserGestureIndicator::currentToken()); 39 EXPECT_NE(nullptr, UserGestureIndicator::currentToken());
40 40
41 EXPECT_TRUE(UserGestureIndicator::consumeUserGesture()); 41 EXPECT_TRUE(UserGestureIndicator::consumeUserGesture());
42 } 42 }
43 43
44 TEST(UserGestureIndicatorTest, ConstructedWithNoUserGesture) 44 TEST(UserGestureIndicatorTest, ConstructedWithNoUserGesture)
45 { 45 {
46 UserGestureIndicator::clearProcessedUserGestureSinceLoad(); 46 UserGestureIndicator::clearProcessedUserGestureSinceLoad();
47 UserGestureIndicator userGestureScope(DefinitelyNotProcessingUserGesture); 47 UserGestureIndicator userGestureScope(DefinitelyNotProcessingUserGesture);
48 48
49 EXPECT_FALSE(UserGestureIndicator::processingUserGesture()); 49 EXPECT_FALSE(UserGestureIndicator::utilizeUserGesture());
50 EXPECT_FALSE(UserGestureIndicator::processedUserGestureSinceLoad()); 50 EXPECT_FALSE(UserGestureIndicator::processedUserGestureSinceLoad());
51 EXPECT_NE(nullptr, UserGestureIndicator::currentToken()); 51 EXPECT_NE(nullptr, UserGestureIndicator::currentToken());
52 52
53 EXPECT_FALSE(UserGestureIndicator::consumeUserGesture()); 53 EXPECT_FALSE(UserGestureIndicator::consumeUserGesture());
54 } 54 }
55 55
56 // Check that after UserGestureIndicator destruction state will be cleared. 56 // Check that after UserGestureIndicator destruction state will be cleared.
57 TEST(UserGestureIndicatorTest, DestructUserGestureIndicator) 57 TEST(UserGestureIndicatorTest, DestructUserGestureIndicator)
58 { 58 {
59 { 59 {
60 UserGestureIndicator userGestureScope(DefinitelyProcessingUserGesture); 60 UserGestureIndicator userGestureScope(DefinitelyProcessingUserGesture);
61 61
62 EXPECT_TRUE(UserGestureIndicator::processingUserGesture()); 62 EXPECT_TRUE(UserGestureIndicator::utilizeUserGesture());
63 EXPECT_TRUE(UserGestureIndicator::processedUserGestureSinceLoad()); 63 EXPECT_TRUE(UserGestureIndicator::processedUserGestureSinceLoad());
64 EXPECT_NE(nullptr, UserGestureIndicator::currentToken()); 64 EXPECT_NE(nullptr, UserGestureIndicator::currentToken());
65 } 65 }
66 66
67 EXPECT_FALSE(UserGestureIndicator::processingUserGesture()); 67 EXPECT_FALSE(UserGestureIndicator::utilizeUserGesture());
68 EXPECT_EQ(nullptr, UserGestureIndicator::currentToken()); 68 EXPECT_EQ(nullptr, UserGestureIndicator::currentToken());
69 EXPECT_FALSE(UserGestureIndicator::consumeUserGesture()); 69 EXPECT_FALSE(UserGestureIndicator::consumeUserGesture());
70 } 70 }
71 71
72 // Tests creation of scoped UserGestureIndicator objects. 72 // Tests creation of scoped UserGestureIndicator objects.
73 TEST(UserGestureIndicatorTest, ScopedNewUserGestureIndicators) 73 TEST(UserGestureIndicatorTest, ScopedNewUserGestureIndicators)
74 { 74 {
75 // Root GestureIndicator and GestureToken. 75 // Root GestureIndicator and GestureToken.
76 UserGestureIndicator userGestureScope(DefinitelyProcessingNewUserGesture); 76 UserGestureIndicator userGestureScope(DefinitelyProcessingNewUserGesture);
77 77
78 EXPECT_TRUE(UserGestureIndicator::processingUserGesture()); 78 EXPECT_TRUE(UserGestureIndicator::utilizeUserGesture());
79 EXPECT_TRUE(UserGestureIndicator::processedUserGestureSinceLoad()); 79 EXPECT_TRUE(UserGestureIndicator::processedUserGestureSinceLoad());
80 EXPECT_NE(nullptr, UserGestureIndicator::currentToken()); 80 EXPECT_NE(nullptr, UserGestureIndicator::currentToken());
81 { 81 {
82 // Construct inner UserGestureIndicator. 82 // Construct inner UserGestureIndicator.
83 // It should share GestureToken with the root indicator. 83 // It should share GestureToken with the root indicator.
84 UserGestureIndicator innerUserGesture(DefinitelyProcessingNewUserGesture ); 84 UserGestureIndicator innerUserGesture(DefinitelyProcessingNewUserGesture );
85 85
86 EXPECT_TRUE(UserGestureIndicator::processingUserGesture()); 86 EXPECT_TRUE(UserGestureIndicator::utilizeUserGesture());
87 EXPECT_NE(nullptr, UserGestureIndicator::currentToken()); 87 EXPECT_NE(nullptr, UserGestureIndicator::currentToken());
88 88
89 // Consume inner gesture. 89 // Consume inner gesture.
90 EXPECT_TRUE(UserGestureIndicator::consumeUserGesture()); 90 EXPECT_TRUE(UserGestureIndicator::consumeUserGesture());
91 } 91 }
92 92
93 EXPECT_TRUE(UserGestureIndicator::processingUserGesture()); 93 EXPECT_TRUE(UserGestureIndicator::utilizeUserGesture());
94 EXPECT_NE(nullptr, UserGestureIndicator::currentToken()); 94 EXPECT_NE(nullptr, UserGestureIndicator::currentToken());
95 95
96 // Consume root gesture. 96 // Consume root gesture.
97 EXPECT_TRUE(UserGestureIndicator::consumeUserGesture()); 97 EXPECT_TRUE(UserGestureIndicator::consumeUserGesture());
98 EXPECT_FALSE(UserGestureIndicator::processingUserGesture()); 98 EXPECT_FALSE(UserGestureIndicator::utilizeUserGesture());
99 EXPECT_NE(nullptr, UserGestureIndicator::currentToken()); 99 EXPECT_NE(nullptr, UserGestureIndicator::currentToken());
100 } 100 }
101 101
102 class UsedCallback : public UserGestureUtilizedCallback {
103 public:
104 UsedCallback() : m_usedCount(0)
105 {
106 }
107
108 void userGestureUtilized() override
109 {
110 m_usedCount++;
111 }
112
113 unsigned getAndResetUsedCount()
114 {
115 unsigned curCount = m_usedCount;
116 m_usedCount = 0;
117 return curCount;
118 }
119
120 private:
121 unsigned m_usedCount;
122 };
123
124 // Tests callback invocation.
125 TEST(UserGestureIndicatorTest, Callback)
126 {
127 UsedCallback cb;
128
129 {
130 UserGestureIndicator userGestureScope(DefinitelyProcessingUserGesture, & cb);
131 EXPECT_EQ(0u, cb.getAndResetUsedCount());
132
133 // Untracked doesn't invoke the callback
134 EXPECT_TRUE(UserGestureIndicator::processingUserGesture());
135 EXPECT_EQ(0u, cb.getAndResetUsedCount());
136
137 // But processingUserGesture does
138 EXPECT_TRUE(UserGestureIndicator::utilizeUserGesture());
139 EXPECT_EQ(1u, cb.getAndResetUsedCount());
140
141 // But only the first time
142 EXPECT_TRUE(UserGestureIndicator::utilizeUserGesture());
143 EXPECT_TRUE(UserGestureIndicator::consumeUserGesture());
144 EXPECT_EQ(0u, cb.getAndResetUsedCount());
145 }
146 EXPECT_EQ(0u, cb.getAndResetUsedCount());
147
148 {
149 UserGestureIndicator userGestureScope(DefinitelyProcessingUserGesture, & cb);
150
151 // Consume also invokes the callback
152 EXPECT_TRUE(UserGestureIndicator::consumeUserGesture());
153 EXPECT_EQ(1u, cb.getAndResetUsedCount());
154
155 // But only once
156 EXPECT_FALSE(UserGestureIndicator::utilizeUserGesture());
157 EXPECT_FALSE(UserGestureIndicator::consumeUserGesture());
158 EXPECT_EQ(0u, cb.getAndResetUsedCount());
159 }
160
161 {
162 UserGestureIndicator userGestureScope(DefinitelyNotProcessingUserGesture , &cb);
163
164 // Callback not invoked when there isn't actually a user gesture
165 EXPECT_FALSE(UserGestureIndicator::processingUserGesture());
166 EXPECT_EQ(0u, cb.getAndResetUsedCount());
167 }
168
169 // The callback isn't invoked outside the scope of the UGI
170 EXPECT_FALSE(UserGestureIndicator::utilizeUserGesture());
171 EXPECT_EQ(0u, cb.getAndResetUsedCount());
172 EXPECT_FALSE(UserGestureIndicator::consumeUserGesture());
173 EXPECT_EQ(0u, cb.getAndResetUsedCount());
174 }
175
102 } // namespace blink 176 } // namespace blink
OLDNEW
« no previous file with comments | « third_party/WebKit/Source/platform/UserGestureIndicator.cpp ('k') | tools/metrics/histograms/histograms.xml » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698