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