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

Unified Diff: third_party/WebKit/Source/platform/testing/TestingPlatformSupport.h

Issue 2588403002: TestingPlatformSupport: register Platform instance correctly (Closed)
Patch Set: review #32 Created 3 years, 11 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 side-by-side diff with in-line comments
Download patch
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;

Powered by Google App Engine
This is Rietveld 408576698