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

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

Issue 2588403002: TestingPlatformSupport: register Platform instance correctly (Closed)
Patch Set: new plan 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..b886dd237307d6285afbf7aa9a7ec19e8d9c58e8 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,16 @@ class TestingPlatformMockScheduler : public WebScheduler {
void removePendingNavigation(WebScheduler::NavigatingFrameType) override {}
};
+// A base class to override Platform methods for testing. You can override
+// compositorSupport() method via a TestingPlatformSupport::Config parameter.
+// Other virtual Platform methods that are overriden in this class will call
+// the original Platform methods that can be obtained by Platform::current()
+// in the consturctor.
+// Or you may to consider to implement your own inheritance of this class to
+// override arbitorary virtual methods.
+// You should use ScopedTestingPlatformSupport to hold any instance of this
+// class and inheritances in order to setup and shutdown Platform instance
+// correctly. See also comments on ScopedTestingPlatformSupport.
haraken 2017/01/12 11:21:01 Slightly simpler: // A base class to override Pla
Takashi Toyoshima 2017/01/12 12:13:01 Actually, existing TestingPlatformSupport and Test
class TestingPlatformSupport : public Platform {
WTF_MAKE_NONCOPYABLE(TestingPlatformSupport);
@@ -142,6 +156,8 @@ class TestingPlatformSupport : public Platform {
std::unique_ptr<TestingInterfaceProvider> m_interfaceProvider;
};
+// This class adds mocked scheduler support to TestingPlatformSupport. See also
+// comments on TestingPlatformSupport to use this class correctly.
class TestingPlatformSupportWithMockScheduler : public TestingPlatformSupport {
WTF_MAKE_NONCOPYABLE(TestingPlatformSupportWithMockScheduler);
@@ -188,7 +204,58 @@ class TestingPlatformSupportWithMockScheduler : public TestingPlatformSupport {
std::unique_ptr<WebThread> m_thread;
};
-class ScopedUnittestsEnvironmentSetup {
+// A template class to hold an instance of TestingPlatformSupport class and
+// inheritances. This class also sets up the instance to be available through
+// Platform::current(), and safely reset it to the previous Platform instance
+// on its destruction. Instances could be nested.
haraken 2017/01/12 11:21:01 Slightly simpler: // ScopedTestingPlatformSupport
Takashi Toyoshima 2017/01/12 12:13:01 Done.
+//
+// Usage:
+//
+// #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 origianl MyTestingPlatformSupport should be restored.
haraken 2017/01/12 11:21:01 original
Takashi Toyoshima 2017/01/12 12:13:02 Done.
+// }
+template <class T, typename... Args>
+class ScopedTestingPlatformSupport final {
haraken 2017/01/12 11:21:01 This looks much simpler!
Takashi Toyoshima 2017/01/12 12:13:01 Acknowledged.
+ 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 +266,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