Index: tests/LayerAllocatorTest.cpp |
diff --git a/tests/LayerAllocatorTest.cpp b/tests/LayerAllocatorTest.cpp |
new file mode 100644 |
index 0000000000000000000000000000000000000000..d171129d81f1428848e89fb6b3e2a38bb2e8d30f |
--- /dev/null |
+++ b/tests/LayerAllocatorTest.cpp |
@@ -0,0 +1,69 @@ |
+/* |
+ * Copyright 2016 Google Inc. |
+ * |
+ * Use of this source code is governed by a BSD-style license that can be |
+ * found in the LICENSE file. |
+ */ |
+ |
+#include "Test.h" |
+ |
+#include "SkCanvas.h" |
+#include "SkRasterCanvasLayerAllocator.h" |
+ |
+class TestAllocator : public SkRasterCanvasLayerAllocator { |
+public: |
+ TestAllocator() : flushCount(0), allocated(false) {} |
+ virtual ~TestAllocator() {} |
+ |
+ void* getNativeContext(void* buffer) override; |
+ void flush() override; |
+ void free(void* buffer, void* nativeContext) override; |
+ |
+ int nativeLayerSentry; |
+ int nativeContextSentry; |
+ int flushCount; |
+ bool allocated; |
+ |
+protected: |
+ void* allocateLayer(const SkImageInfo& info, size_t* rowBytes) override; |
+}; |
+ |
+void* TestAllocator::getNativeContext(void* buffer) { |
+ return &this->nativeContextSentry; |
+} |
+ |
+void TestAllocator::flush() { |
+ flushCount++; |
+} |
+ |
+void TestAllocator::free(void* buffer, void* nativeContext) { |
+ SkASSERT(this->allocated); |
+ this->allocated = false; |
+} |
+ |
+void* TestAllocator::allocateLayer(const SkImageInfo& info, size_t* rowBytes) { |
+ SkASSERT(!this->allocated); |
+ this->allocated = true; |
+ return &this->nativeLayerSentry; |
+} |
+ |
+// Need to trigger: |
+// SkBitmapDevice::onCreateDevice() - hit in SkCanvas::internalSaveLayer() |
+ |
+DEF_TEST(TestAllocator, reporter) { |
+ TestAllocator* allocator = new TestAllocator; |
+ SkCanvas* canvas = allocator->CreateCanvas(SkImageInfo::MakeN32Premul(100, 100)); |
+ |
+ REPORTER_ASSERT(reporter, |
+ canvas->getTopLayerNative() == &allocator->nativeContextSentry); |
+ REPORTER_ASSERT(reporter, |
+ canvas->accessTopLayerPixels(nullptr, nullptr) == &allocator->nativeLayerSentry); |
+ |
+ canvas->saveLayer(nullptr, nullptr); |
+ |
+ REPORTER_ASSERT(reporter, |
+ canvas->getTopLayerNative() == &allocator->nativeContextSentry); |
+ REPORTER_ASSERT(reporter, |
+ canvas->accessTopLayerPixels(nullptr, nullptr) == &allocator->nativeLayerSentry); |
+} |
+ |