Index: Source/core/html/StableStateTest.cpp |
diff --git a/Source/core/html/StableStateTest.cpp b/Source/core/html/StableStateTest.cpp |
new file mode 100644 |
index 0000000000000000000000000000000000000000..b95236876b4a74c87fca20fab7910a40554ab0d3 |
--- /dev/null |
+++ b/Source/core/html/StableStateTest.cpp |
@@ -0,0 +1,110 @@ |
+// Copyright 2014 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "config.h" |
+#include "core/html/StableState.h" |
+ |
+#include "wtf/Functional.h" |
+ |
+#include <gmock/gmock.h> |
+#include <gtest/gtest.h> |
+ |
+using WebCore::StableState; |
+ |
+namespace { |
+ |
+class MockAwaiter : public StableState::Awaiter { |
+public: |
+ MOCK_METHOD0(didAwaitStableState, void()); |
+}; |
+ |
+TEST(StableState, Await) |
+{ |
+ MockAwaiter awaiter; |
+ EXPECT_CALL(awaiter, didAwaitStableState()).Times(0); |
+ StableState::await(awaiter); |
+} |
+ |
+TEST(StableState, AwaitProvide) |
+{ |
+ MockAwaiter awaiter; |
+ EXPECT_CALL(awaiter, didAwaitStableState()).Times(1); |
+ StableState::await(awaiter); |
+ StableState::provide(); |
+} |
+ |
+TEST(StableState, AwaitProvideTwice) |
+{ |
+ MockAwaiter awaiter; |
+ EXPECT_CALL(awaiter, didAwaitStableState()).Times(1); |
+ StableState::await(awaiter); |
+ StableState::provide(); |
+ StableState::provide(); |
+} |
+ |
+TEST(StableState, AwaitTwiceProvide) |
+{ |
+ MockAwaiter awaiter; |
+ EXPECT_CALL(awaiter, didAwaitStableState()).Times(1); |
+ StableState::await(awaiter); |
+ StableState::await(awaiter); |
+ StableState::provide(); |
+} |
+ |
+TEST(StableState, AwaitProvideAwaitProvide) |
+{ |
+ MockAwaiter awaiter; |
+ EXPECT_CALL(awaiter, didAwaitStableState()).Times(2); |
+ StableState::await(awaiter); |
+ StableState::provide(); |
+ StableState::await(awaiter); |
+ StableState::provide(); |
+} |
+ |
+TEST(StableState, TwoAwaiters) |
+{ |
+ MockAwaiter awaiter1; |
+ MockAwaiter awaiter2; |
+ EXPECT_CALL(awaiter1, didAwaitStableState()).Times(1); |
+ EXPECT_CALL(awaiter2, didAwaitStableState()).Times(1); |
+ StableState::await(awaiter1); |
+ StableState::await(awaiter2); |
+ StableState::provide(); |
+} |
+ |
+// Test that a class can await a stable state and invoke one of two callbacks |
+// without having state bits by using a self-deleting StableState::Awaiter |
+// object. Deleting the awaiter inside the callback must not crash or influence |
+// any other awaiter. |
+ |
+class MockCallbacks { |
+public: |
+ MOCK_METHOD0(callback1, void()); |
+ MOCK_METHOD0(callback2, void()); |
+}; |
+ |
+class ClosureAwaiter : public StableState::Awaiter { |
+public: |
+ ClosureAwaiter(const Closure& closure) : m_closure(closure) { } |
+ virtual void didAwaitStableState() OVERRIDE |
+ { |
+ m_closure(); |
+ delete this; |
+ } |
+private: |
+ Closure m_closure; |
+}; |
+ |
+TEST(StableState, ClosureAwaiter) |
+{ |
+ MockCallbacks callbacks; |
+ EXPECT_CALL(callbacks, callback1()).Times(2); |
+ EXPECT_CALL(callbacks, callback2()).Times(1); |
+ StableState::await(*new ClosureAwaiter(bind(&MockCallbacks::callback1, &callbacks))); |
+ StableState::await(*new ClosureAwaiter(bind(&MockCallbacks::callback2, &callbacks))); |
+ StableState::await(*new ClosureAwaiter(bind(&MockCallbacks::callback1, &callbacks))); |
+ StableState::provide(); |
+} |
+ |
+} // namespace |