1 #include "Test.h"
2
3 #include "SkMallocPixelRef.h"
4 #include "SkPixelRef.h"
5
6 class TestListener : public SkPixelRef::GenIDChangeListener {
7 public:
TestListener(int * ptr)8 explicit TestListener(int* ptr) : fPtr(ptr) {}
onChange()9 virtual void onChange() SK_OVERRIDE { (*fPtr)++; }
10 private:
11 int* fPtr;
12 };
13
DEF_TEST(PixelRef_GenIDChange,r)14 DEF_TEST(PixelRef_GenIDChange, r) {
15 SkImageInfo info = SkImageInfo::MakeN32Premul(10, 10);
16
17 SkAutoTUnref<SkPixelRef> pixelRef(SkMallocPixelRef::NewAllocate(info, 0, NULL));
18
19 // Register a listener.
20 int count = 0;
21 pixelRef->addGenIDChangeListener(SkNEW_ARGS(TestListener, (&count)));
22 REPORTER_ASSERT(r, 0 == count);
23
24 // No one has looked at our pixelRef's generation ID, so invalidating it doesn't make sense.
25 // (An SkPixelRef tree falls in the forest but there's nobody around to hear it. Do we care?)
26 pixelRef->notifyPixelsChanged();
27 REPORTER_ASSERT(r, 0 == count);
28
29 // Force the generation ID to be calculated.
30 REPORTER_ASSERT(r, 0 != pixelRef->getGenerationID());
31
32 // Our listener was dropped in the first call to notifyPixelsChanged(). This is a no-op.
33 pixelRef->notifyPixelsChanged();
34 REPORTER_ASSERT(r, 0 == count);
35
36 // Force the generation ID to be recalculated, then add a listener.
37 REPORTER_ASSERT(r, 0 != pixelRef->getGenerationID());
38 pixelRef->addGenIDChangeListener(SkNEW_ARGS(TestListener, (&count)));
39 pixelRef->notifyPixelsChanged();
40 REPORTER_ASSERT(r, 1 == count);
41
42 // Quick check that NULL is safe.
43 REPORTER_ASSERT(r, 0 != pixelRef->getGenerationID());
44 pixelRef->addGenIDChangeListener(NULL);
45 pixelRef->notifyPixelsChanged();
46 }
47