Index: third_party/WebKit/Source/platform/testing/TestingPlatformSupport.h |
diff --git a/third_party/WebKit/Source/platform/testing/TestingPlatformSupport.h b/third_party/WebKit/Source/platform/testing/TestingPlatformSupport.h |
index 159c0febae33681c850788625010da4a68eee369..cf90b8125da1be9c5630a3cada6d21abeaf07418 100644 |
--- a/third_party/WebKit/Source/platform/testing/TestingPlatformSupport.h |
+++ b/third_party/WebKit/Source/platform/testing/TestingPlatformSupport.h |
@@ -37,8 +37,12 @@ |
#include "public/platform/WebCompositorSupport.h" |
#include "public/platform/WebScheduler.h" |
#include "public/platform/WebThread.h" |
+#include "wtf/Allocator.h" |
+#include "wtf/Assertions.h" |
+#include "wtf/PtrUtil.h" |
#include "wtf/Vector.h" |
#include <memory> |
+#include <utility> |
namespace base { |
class SimpleTestTickClock; |
@@ -107,6 +111,9 @@ class TestingPlatformMockScheduler : public WebScheduler { |
void removePendingNavigation(WebScheduler::NavigatingFrameType) override {} |
}; |
+// A base class to override Platform methods for testing. You can override the |
+// behavior by subclassing TestingPlatformSupport or using |
+// ScopedTestingPlatformSupport (see below). |
class TestingPlatformSupport : public Platform { |
WTF_MAKE_NONCOPYABLE(TestingPlatformSupport); |
@@ -142,6 +149,8 @@ class TestingPlatformSupport : public Platform { |
std::unique_ptr<TestingInterfaceProvider> m_interfaceProvider; |
}; |
+// This class adds mocked scheduler support to TestingPlatformSupport. See also |
+// ScopedTestingPlatformSupport to use this class correctly. |
class TestingPlatformSupportWithMockScheduler : public TestingPlatformSupport { |
WTF_MAKE_NONCOPYABLE(TestingPlatformSupportWithMockScheduler); |
@@ -188,7 +197,54 @@ class TestingPlatformSupportWithMockScheduler : public TestingPlatformSupport { |
std::unique_ptr<WebThread> m_thread; |
}; |
-class ScopedUnittestsEnvironmentSetup { |
+// ScopedTestingPlatformSupport<MyTestingPlatformSupport> can be used to |
+// override Platform::current() with MyTestingPlatformSupport, like this: |
+// |
+// #include "platform/testing/TestingPlatformSupport.h" |
+// |
+// TEST_F(SampleTest, sampleTest) { |
+// ScopedTestingPlatformSupport<MyTestingPlatformSupport> platform; |
+// ... |
+// // You can call methods of MyTestingPlatformSupport. |
+// EXPECT_TRUE(platform->myMethodIsCalled()); |
+// |
+// // Another instance can be nested. |
+// { |
+// // Constructor's arguments can be passed like this. |
+// Arg arg; |
+// ScopedTestingPlatformSupport<MyAnotherTestingPlatformSupport, const Arg&> |
+// another_platform(args); |
+// ... |
+// } |
+// |
+// // Here the original MyTestingPlatformSupport should be restored. |
+// } |
+template <class T, typename... Args> |
+class ScopedTestingPlatformSupport final { |
+ STACK_ALLOCATED(); |
+ |
+ public: |
+ explicit ScopedTestingPlatformSupport(Args&&... args) { |
+ m_testingPlatformSupport = WTF::makeUnique<T>(std::forward<Args>(args)...); |
+ m_originalPlatform = Platform::current(); |
+ DCHECK(m_originalPlatform); |
+ Platform::setCurrentPlatformForTesting(m_testingPlatformSupport.get()); |
+ } |
+ ~ScopedTestingPlatformSupport() { |
+ DCHECK_EQ(m_testingPlatformSupport.get(), Platform::current()); |
+ m_testingPlatformSupport.reset(); |
+ Platform::setCurrentPlatformForTesting(m_originalPlatform); |
+ } |
+ |
+ const T* operator->() const { return m_testingPlatformSupport.get(); } |
+ T* operator->() { return m_testingPlatformSupport.get(); } |
+ |
+ private: |
+ std::unique_ptr<T> m_testingPlatformSupport; |
+ Platform* m_originalPlatform; |
+}; |
+ |
+class ScopedUnittestsEnvironmentSetup final { |
WTF_MAKE_NONCOPYABLE(ScopedUnittestsEnvironmentSetup); |
public: |
@@ -199,7 +255,7 @@ class ScopedUnittestsEnvironmentSetup { |
class DummyPlatform; |
std::unique_ptr<base::TestDiscardableMemoryAllocator> |
m_discardableMemoryAllocator; |
- std::unique_ptr<DummyPlatform> m_platform; |
+ std::unique_ptr<DummyPlatform> m_dummyPlatform; |
std::unique_ptr<cc_blink::WebCompositorSupportImpl> m_compositorSupport; |
TestingPlatformSupport::Config m_testingPlatformConfig; |
std::unique_ptr<TestingPlatformSupport> m_testingPlatformSupport; |