blob: ce2575e830ceaa36963c802282dea797f46bde30 [file] [log] [blame]
commit-bot@chromium.org50a30432013-10-24 17:44:27 +00001#include "Test.h"
2#include "TestClassDef.h"
3
4#include "SkPixelRef.h"
5#include "SkMallocPixelRef.h"
6
7namespace {
8
9class TestListener : public SkPixelRef::GenIDChangeListener {
10public:
11 explicit TestListener(int* ptr) : fPtr(ptr) {}
12 void onChange() SK_OVERRIDE { (*fPtr)++; }
13private:
14 int* fPtr;
15};
16
17} // namespace
18
19DEF_TEST(PixelRef_GenIDChange, r) {
reed@google.com5b132b22013-12-06 18:51:08 +000020 SkMallocPixelRef pixelRef(NULL, 0, NULL); // We don't really care about the pixels here.
commit-bot@chromium.org50a30432013-10-24 17:44:27 +000021
22 // Register a listener.
23 int count = 0;
reed@google.com5b132b22013-12-06 18:51:08 +000024 pixelRef.addGenIDChangeListener(SkNEW_ARGS(TestListener, (&count)));
commit-bot@chromium.org50a30432013-10-24 17:44:27 +000025 REPORTER_ASSERT(r, 0 == count);
26
27 // No one has looked at our pixelRef's generation ID, so invalidating it doesn't make sense.
28 // (An SkPixelRef tree falls in the forest but there's nobody around to hear it. Do we care?)
reed@google.com5b132b22013-12-06 18:51:08 +000029 pixelRef.notifyPixelsChanged();
commit-bot@chromium.org50a30432013-10-24 17:44:27 +000030 REPORTER_ASSERT(r, 0 == count);
31
32 // Force the generation ID to be calculated.
reed@google.com5b132b22013-12-06 18:51:08 +000033 REPORTER_ASSERT(r, 0 != pixelRef.getGenerationID());
commit-bot@chromium.org50a30432013-10-24 17:44:27 +000034
35 // Our listener was dropped in the first call to notifyPixelsChanged(). This is a no-op.
reed@google.com5b132b22013-12-06 18:51:08 +000036 pixelRef.notifyPixelsChanged();
commit-bot@chromium.org50a30432013-10-24 17:44:27 +000037 REPORTER_ASSERT(r, 0 == count);
38
39 // Force the generation ID to be recalculated, then add a listener.
reed@google.com5b132b22013-12-06 18:51:08 +000040 REPORTER_ASSERT(r, 0 != pixelRef.getGenerationID());
41 pixelRef.addGenIDChangeListener(SkNEW_ARGS(TestListener, (&count)));
42 pixelRef.notifyPixelsChanged();
commit-bot@chromium.org50a30432013-10-24 17:44:27 +000043 REPORTER_ASSERT(r, 1 == count);
44
45 // Quick check that NULL is safe.
reed@google.com5b132b22013-12-06 18:51:08 +000046 REPORTER_ASSERT(r, 0 != pixelRef.getGenerationID());
47 pixelRef.addGenIDChangeListener(NULL);
48 pixelRef.notifyPixelsChanged();
commit-bot@chromium.org50a30432013-10-24 17:44:27 +000049}