Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 Loading... | |
| 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 RefPtr<UserGestureToken> token = UserGestureToken::create(); |
| 121 token->setUserGestureUtilizedCallback(&cb); | |
| 122 UserGestureIndicator userGestureScope(token); | |
| 118 EXPECT_EQ(0u, cb.getAndResetUsedCount()); | 123 EXPECT_EQ(0u, cb.getAndResetUsedCount()); |
| 119 | 124 |
| 120 // Untracked doesn't invoke the callback | 125 // Untracked doesn't invoke the callback |
| 121 EXPECT_TRUE(UserGestureIndicator::processingUserGesture()); | 126 EXPECT_TRUE(UserGestureIndicator::processingUserGesture()); |
| 122 EXPECT_EQ(0u, cb.getAndResetUsedCount()); | 127 EXPECT_EQ(0u, cb.getAndResetUsedCount()); |
| 123 | 128 |
| 124 // But processingUserGesture does | 129 // But processingUserGesture does |
| 125 EXPECT_TRUE(UserGestureIndicator::utilizeUserGesture()); | 130 EXPECT_TRUE(UserGestureIndicator::utilizeUserGesture()); |
| 126 EXPECT_EQ(1u, cb.getAndResetUsedCount()); | 131 EXPECT_EQ(1u, cb.getAndResetUsedCount()); |
| 127 | 132 |
| 128 // But only the first time | 133 // But only the first time |
| 129 EXPECT_TRUE(UserGestureIndicator::utilizeUserGesture()); | 134 EXPECT_TRUE(UserGestureIndicator::utilizeUserGesture()); |
| 130 EXPECT_TRUE(UserGestureIndicator::consumeUserGesture()); | 135 EXPECT_TRUE(UserGestureIndicator::consumeUserGesture()); |
| 131 EXPECT_EQ(0u, cb.getAndResetUsedCount()); | 136 EXPECT_EQ(0u, cb.getAndResetUsedCount()); |
| 132 } | 137 } |
| 133 EXPECT_EQ(0u, cb.getAndResetUsedCount()); | 138 EXPECT_EQ(0u, cb.getAndResetUsedCount()); |
| 134 | 139 |
| 135 { | 140 { |
| 136 UserGestureIndicator userGestureScope(DefinitelyProcessingUserGesture, &cb); | 141 RefPtr<UserGestureToken> token = UserGestureToken::create(); |
| 142 token->setUserGestureUtilizedCallback(&cb); | |
| 143 UserGestureIndicator userGestureScope(token); | |
| 137 | 144 |
| 138 // Consume also invokes the callback | 145 // Consume also invokes the callback |
| 139 EXPECT_TRUE(UserGestureIndicator::consumeUserGesture()); | 146 EXPECT_TRUE(UserGestureIndicator::consumeUserGesture()); |
| 140 EXPECT_EQ(1u, cb.getAndResetUsedCount()); | 147 EXPECT_EQ(1u, cb.getAndResetUsedCount()); |
| 141 | 148 |
| 142 // But only once | 149 // But only once |
| 143 EXPECT_FALSE(UserGestureIndicator::utilizeUserGesture()); | 150 EXPECT_FALSE(UserGestureIndicator::utilizeUserGesture()); |
| 144 EXPECT_FALSE(UserGestureIndicator::consumeUserGesture()); | 151 EXPECT_FALSE(UserGestureIndicator::consumeUserGesture()); |
| 145 EXPECT_EQ(0u, cb.getAndResetUsedCount()); | 152 EXPECT_EQ(0u, cb.getAndResetUsedCount()); |
| 146 } | 153 } |
| 147 | 154 |
| 148 { | |
| 149 UserGestureIndicator userGestureScope(DefinitelyNotProcessingUserGesture, | |
| 150 &cb); | |
| 151 | |
| 152 // Callback not invoked when there isn't actually a user gesture | |
| 153 EXPECT_FALSE(UserGestureIndicator::processingUserGesture()); | |
| 154 EXPECT_EQ(0u, cb.getAndResetUsedCount()); | |
| 155 } | |
| 156 | |
| 157 // The callback isn't invoked outside the scope of the UGI | 155 // The callback isn't invoked outside the scope of the UGI |
|
Rick Byers
2016/10/11 18:00:19
With the separation from the token, this is probab
Nate Chapin
2016/10/11 19:17:55
Done (with a unique_ptr instead of with nested sco
| |
| 158 EXPECT_FALSE(UserGestureIndicator::utilizeUserGesture()); | 156 EXPECT_FALSE(UserGestureIndicator::utilizeUserGesture()); |
| 159 EXPECT_EQ(0u, cb.getAndResetUsedCount()); | 157 EXPECT_EQ(0u, cb.getAndResetUsedCount()); |
| 160 EXPECT_FALSE(UserGestureIndicator::consumeUserGesture()); | 158 EXPECT_FALSE(UserGestureIndicator::consumeUserGesture()); |
| 161 EXPECT_EQ(0u, cb.getAndResetUsedCount()); | 159 EXPECT_EQ(0u, cb.getAndResetUsedCount()); |
| 162 } | 160 } |
| 163 | 161 |
| 164 } // namespace blink | 162 } // namespace blink |
| OLD | NEW |