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

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

Issue 2401123002: UserGestureIndicator is a mess. Clean it up. (Closed)
Patch Set: Callback cleanup, comments Created 4 years, 2 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 EXPECT_FALSE(UserGestureIndicator::utilizeUserGesture()); 13 EXPECT_FALSE(UserGestureIndicator::utilizeUserGesture());
14 EXPECT_FALSE(UserGestureIndicator::processedUserGestureSinceLoad()); 14 EXPECT_FALSE(UserGestureIndicator::processedUserGestureSinceLoad());
15 EXPECT_EQ(nullptr, UserGestureIndicator::currentToken()); 15 EXPECT_EQ(nullptr, UserGestureIndicator::currentToken());
16 EXPECT_FALSE(UserGestureIndicator::consumeUserGesture()); 16 EXPECT_FALSE(UserGestureIndicator::consumeUserGesture());
17 } 17 }
18 18
19 TEST(UserGestureIndicatorTest, ConstructedWithNewUserGesture) { 19 TEST(UserGestureIndicatorTest, ConstructedWithNewUserGesture) {
20 UserGestureIndicator::clearProcessedUserGestureSinceLoad(); 20 UserGestureIndicator::clearProcessedUserGestureSinceLoad();
21 UserGestureIndicator userGestureScope(DefinitelyProcessingNewUserGesture); 21 UserGestureIndicator userGestureScope(
22 UserGestureToken::create(UserGestureToken::NewGesture));
22 23
23 EXPECT_TRUE(UserGestureIndicator::utilizeUserGesture()); 24 EXPECT_TRUE(UserGestureIndicator::utilizeUserGesture());
24 EXPECT_TRUE(UserGestureIndicator::processedUserGestureSinceLoad()); 25 EXPECT_TRUE(UserGestureIndicator::processedUserGestureSinceLoad());
25 EXPECT_NE(nullptr, UserGestureIndicator::currentToken()); 26 EXPECT_NE(nullptr, UserGestureIndicator::currentToken());
26 27
27 EXPECT_TRUE(UserGestureIndicator::consumeUserGesture()); 28 EXPECT_TRUE(UserGestureIndicator::consumeUserGesture());
28 } 29 }
29 30
30 TEST(UserGestureIndicatorTest, ConstructedWithUserGesture) { 31 TEST(UserGestureIndicatorTest, ConstructedWithUserGesture) {
31 UserGestureIndicator::clearProcessedUserGestureSinceLoad(); 32 UserGestureIndicator::clearProcessedUserGestureSinceLoad();
32 UserGestureIndicator userGestureScope(DefinitelyProcessingUserGesture); 33 UserGestureIndicator userGestureScope(UserGestureToken::create());
33 34
34 EXPECT_TRUE(UserGestureIndicator::utilizeUserGesture()); 35 EXPECT_TRUE(UserGestureIndicator::utilizeUserGesture());
35 EXPECT_TRUE(UserGestureIndicator::processedUserGestureSinceLoad()); 36 EXPECT_TRUE(UserGestureIndicator::processedUserGestureSinceLoad());
36 EXPECT_NE(nullptr, UserGestureIndicator::currentToken()); 37 EXPECT_NE(nullptr, UserGestureIndicator::currentToken());
37 38
38 EXPECT_TRUE(UserGestureIndicator::consumeUserGesture()); 39 EXPECT_TRUE(UserGestureIndicator::consumeUserGesture());
39 } 40 }
40 41
41 TEST(UserGestureIndicatorTest, ConstructedWithNoUserGesture) { 42 TEST(UserGestureIndicatorTest, ConstructedWithNoUserGesture) {
42 UserGestureIndicator::clearProcessedUserGestureSinceLoad(); 43 UserGestureIndicator::clearProcessedUserGestureSinceLoad();
43 UserGestureIndicator userGestureScope(DefinitelyNotProcessingUserGesture); 44 UserGestureIndicator userGestureScope(nullptr);
44 45
45 EXPECT_FALSE(UserGestureIndicator::utilizeUserGesture()); 46 EXPECT_FALSE(UserGestureIndicator::utilizeUserGesture());
46 EXPECT_FALSE(UserGestureIndicator::processedUserGestureSinceLoad()); 47 EXPECT_FALSE(UserGestureIndicator::processedUserGestureSinceLoad());
47 EXPECT_NE(nullptr, UserGestureIndicator::currentToken()); 48 EXPECT_EQ(nullptr, UserGestureIndicator::currentToken());
48 49
49 EXPECT_FALSE(UserGestureIndicator::consumeUserGesture()); 50 EXPECT_FALSE(UserGestureIndicator::consumeUserGesture());
50 } 51 }
51 52
52 // Check that after UserGestureIndicator destruction state will be cleared. 53 // Check that after UserGestureIndicator destruction state will be cleared.
53 TEST(UserGestureIndicatorTest, DestructUserGestureIndicator) { 54 TEST(UserGestureIndicatorTest, DestructUserGestureIndicator) {
54 { 55 {
55 UserGestureIndicator userGestureScope(DefinitelyProcessingUserGesture); 56 UserGestureIndicator userGestureScope(UserGestureToken::create());
56 57
57 EXPECT_TRUE(UserGestureIndicator::utilizeUserGesture()); 58 EXPECT_TRUE(UserGestureIndicator::utilizeUserGesture());
58 EXPECT_TRUE(UserGestureIndicator::processedUserGestureSinceLoad()); 59 EXPECT_TRUE(UserGestureIndicator::processedUserGestureSinceLoad());
59 EXPECT_NE(nullptr, UserGestureIndicator::currentToken()); 60 EXPECT_NE(nullptr, UserGestureIndicator::currentToken());
60 } 61 }
61 62
62 EXPECT_FALSE(UserGestureIndicator::utilizeUserGesture()); 63 EXPECT_FALSE(UserGestureIndicator::utilizeUserGesture());
63 EXPECT_EQ(nullptr, UserGestureIndicator::currentToken()); 64 EXPECT_EQ(nullptr, UserGestureIndicator::currentToken());
64 EXPECT_FALSE(UserGestureIndicator::consumeUserGesture()); 65 EXPECT_FALSE(UserGestureIndicator::consumeUserGesture());
65 } 66 }
66 67
67 // Tests creation of scoped UserGestureIndicator objects. 68 // Tests creation of scoped UserGestureIndicator objects.
68 TEST(UserGestureIndicatorTest, ScopedNewUserGestureIndicators) { 69 TEST(UserGestureIndicatorTest, ScopedNewUserGestureIndicators) {
69 // Root GestureIndicator and GestureToken. 70 // Root GestureIndicator and GestureToken.
70 UserGestureIndicator userGestureScope(DefinitelyProcessingNewUserGesture); 71 UserGestureIndicator userGestureScope(
72 UserGestureToken::create(UserGestureToken::NewGesture));
71 73
72 EXPECT_TRUE(UserGestureIndicator::utilizeUserGesture()); 74 EXPECT_TRUE(UserGestureIndicator::utilizeUserGesture());
73 EXPECT_TRUE(UserGestureIndicator::processedUserGestureSinceLoad()); 75 EXPECT_TRUE(UserGestureIndicator::processedUserGestureSinceLoad());
74 EXPECT_NE(nullptr, UserGestureIndicator::currentToken()); 76 EXPECT_NE(nullptr, UserGestureIndicator::currentToken());
75 { 77 {
76 // Construct inner UserGestureIndicator. 78 // Construct inner UserGestureIndicator.
77 // It should share GestureToken with the root indicator. 79 // It should share GestureToken with the root indicator.
78 UserGestureIndicator innerUserGesture(DefinitelyProcessingNewUserGesture); 80 UserGestureIndicator innerUserGesture(
81 UserGestureToken::create(UserGestureToken::NewGesture));
79 82
80 EXPECT_TRUE(UserGestureIndicator::utilizeUserGesture()); 83 EXPECT_TRUE(UserGestureIndicator::utilizeUserGesture());
81 EXPECT_NE(nullptr, UserGestureIndicator::currentToken()); 84 EXPECT_NE(nullptr, UserGestureIndicator::currentToken());
82 85
83 // Consume inner gesture. 86 // Consume inner gesture.
84 EXPECT_TRUE(UserGestureIndicator::consumeUserGesture()); 87 EXPECT_TRUE(UserGestureIndicator::consumeUserGesture());
85 } 88 }
86 89
87 EXPECT_TRUE(UserGestureIndicator::utilizeUserGesture()); 90 EXPECT_TRUE(UserGestureIndicator::utilizeUserGesture());
88 EXPECT_NE(nullptr, UserGestureIndicator::currentToken()); 91 EXPECT_NE(nullptr, UserGestureIndicator::currentToken());
(...skipping 18 matching lines...) Expand all
107 110
108 private: 111 private:
109 unsigned m_usedCount; 112 unsigned m_usedCount;
110 }; 113 };
111 114
112 // Tests callback invocation. 115 // Tests callback invocation.
113 TEST(UserGestureIndicatorTest, Callback) { 116 TEST(UserGestureIndicatorTest, Callback) {
114 UsedCallback cb; 117 UsedCallback cb;
115 118
116 { 119 {
117 UserGestureIndicator userGestureScope(DefinitelyProcessingUserGesture, &cb); 120 UserGestureIndicator userGestureScope(UserGestureToken::create());
121 UserGestureIndicator::currentToken()->setUserGestureUtilizedCallback(&cb);
118 EXPECT_EQ(0u, cb.getAndResetUsedCount()); 122 EXPECT_EQ(0u, cb.getAndResetUsedCount());
119 123
120 // Untracked doesn't invoke the callback 124 // Untracked doesn't invoke the callback
121 EXPECT_TRUE(UserGestureIndicator::processingUserGesture()); 125 EXPECT_TRUE(UserGestureIndicator::processingUserGesture());
122 EXPECT_EQ(0u, cb.getAndResetUsedCount()); 126 EXPECT_EQ(0u, cb.getAndResetUsedCount());
123 127
124 // But processingUserGesture does 128 // But processingUserGesture does
125 EXPECT_TRUE(UserGestureIndicator::utilizeUserGesture()); 129 EXPECT_TRUE(UserGestureIndicator::utilizeUserGesture());
126 EXPECT_EQ(1u, cb.getAndResetUsedCount()); 130 EXPECT_EQ(1u, cb.getAndResetUsedCount());
127 131
128 // But only the first time 132 // But only the first time
129 EXPECT_TRUE(UserGestureIndicator::utilizeUserGesture()); 133 EXPECT_TRUE(UserGestureIndicator::utilizeUserGesture());
130 EXPECT_TRUE(UserGestureIndicator::consumeUserGesture()); 134 EXPECT_TRUE(UserGestureIndicator::consumeUserGesture());
131 EXPECT_EQ(0u, cb.getAndResetUsedCount()); 135 EXPECT_EQ(0u, cb.getAndResetUsedCount());
132 } 136 }
133 EXPECT_EQ(0u, cb.getAndResetUsedCount()); 137 EXPECT_EQ(0u, cb.getAndResetUsedCount());
134 138
135 { 139 {
136 UserGestureIndicator userGestureScope(DefinitelyProcessingUserGesture, &cb); 140 UserGestureIndicator userGestureScope(UserGestureToken::create());
141 UserGestureIndicator::currentToken()->setUserGestureUtilizedCallback(&cb);
137 142
138 // Consume also invokes the callback 143 // Consume also invokes the callback
139 EXPECT_TRUE(UserGestureIndicator::consumeUserGesture()); 144 EXPECT_TRUE(UserGestureIndicator::consumeUserGesture());
140 EXPECT_EQ(1u, cb.getAndResetUsedCount()); 145 EXPECT_EQ(1u, cb.getAndResetUsedCount());
141 146
142 // But only once 147 // But only once
143 EXPECT_FALSE(UserGestureIndicator::utilizeUserGesture()); 148 EXPECT_FALSE(UserGestureIndicator::utilizeUserGesture());
144 EXPECT_FALSE(UserGestureIndicator::consumeUserGesture()); 149 EXPECT_FALSE(UserGestureIndicator::consumeUserGesture());
145 EXPECT_EQ(0u, cb.getAndResetUsedCount()); 150 EXPECT_EQ(0u, cb.getAndResetUsedCount());
146 } 151 }
147 152
148 { 153 {
149 UserGestureIndicator userGestureScope(DefinitelyNotProcessingUserGesture, 154 std::unique_ptr<UserGestureIndicator> userGestureScope(
150 &cb); 155 new UserGestureIndicator(UserGestureToken::create()));
156 RefPtr<UserGestureToken> token = UserGestureIndicator::currentToken();
157 token->setUserGestureUtilizedCallback(&cb);
158 userGestureScope.reset();
151 159
152 // Callback not invoked when there isn't actually a user gesture 160 // The callback should be cleared when the UseGestureIndicator is deleted.
153 EXPECT_FALSE(UserGestureIndicator::processingUserGesture()); 161 EXPECT_FALSE(UserGestureIndicator::utilizeUserGesture());
154 EXPECT_EQ(0u, cb.getAndResetUsedCount()); 162 EXPECT_EQ(0u, cb.getAndResetUsedCount());
155 } 163 }
156 164
157 // The callback isn't invoked outside the scope of the UGI 165 // The callback isn't invoked outside the scope of the UGI
158 EXPECT_FALSE(UserGestureIndicator::utilizeUserGesture()); 166 EXPECT_FALSE(UserGestureIndicator::utilizeUserGesture());
159 EXPECT_EQ(0u, cb.getAndResetUsedCount()); 167 EXPECT_EQ(0u, cb.getAndResetUsedCount());
160 EXPECT_FALSE(UserGestureIndicator::consumeUserGesture()); 168 EXPECT_FALSE(UserGestureIndicator::consumeUserGesture());
161 EXPECT_EQ(0u, cb.getAndResetUsedCount()); 169 EXPECT_EQ(0u, cb.getAndResetUsedCount());
162 } 170 }
163 171
164 } // namespace blink 172 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698