1/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <gtest/gtest.h>
18
19#include "RenderNode.h"
20#include "TreeInfo.h"
21#include "tests/common/TestUtils.h"
22#include "utils/Color.h"
23
24using namespace android;
25using namespace android::uirenderer;
26
27TEST(RenderNode, hasParents) {
28    auto child = TestUtils::createNode(0, 0, 200, 400,
29            [](RenderProperties& props, TestCanvas& canvas) {
30        canvas.drawColor(Color::Red_500, SkXfermode::kSrcOver_Mode);
31    });
32    auto parent = TestUtils::createNode(0, 0, 200, 400,
33            [&child](RenderProperties& props, TestCanvas& canvas) {
34        canvas.drawRenderNode(child.get());
35    });
36
37    TestUtils::syncHierarchyPropertiesAndDisplayList(parent);
38
39    EXPECT_TRUE(child->hasParents()) << "Child node has no parent";
40    EXPECT_FALSE(parent->hasParents()) << "Root node shouldn't have any parents";
41
42    TestUtils::recordNode(*parent, [](TestCanvas& canvas) {
43        canvas.drawColor(Color::Amber_500, SkXfermode::kSrcOver_Mode);
44    });
45
46    EXPECT_TRUE(child->hasParents()) << "Child should still have a parent";
47    EXPECT_FALSE(parent->hasParents()) << "Root node shouldn't have any parents";
48
49    TestUtils::syncHierarchyPropertiesAndDisplayList(parent);
50
51    EXPECT_FALSE(child->hasParents()) << "Child should be removed";
52    EXPECT_FALSE(parent->hasParents()) << "Root node shouldn't have any parents";
53}
54
55TEST(RenderNode, releasedCallback) {
56    class DecRefOnReleased : public GlFunctorLifecycleListener {
57    public:
58        DecRefOnReleased(int* refcnt) : mRefCnt(refcnt) {}
59        void onGlFunctorReleased(Functor* functor) override {
60            *mRefCnt -= 1;
61        }
62    private:
63        int* mRefCnt;
64    };
65
66    int refcnt = 0;
67    sp<DecRefOnReleased> listener(new DecRefOnReleased(&refcnt));
68    Functor noopFunctor;
69
70    auto node = TestUtils::createNode(0, 0, 200, 400,
71            [&](RenderProperties& props, TestCanvas& canvas) {
72        refcnt++;
73        canvas.callDrawGLFunction(&noopFunctor, listener.get());
74    });
75    TestUtils::syncHierarchyPropertiesAndDisplayList(node);
76    EXPECT_EQ(1, refcnt);
77
78    TestUtils::recordNode(*node, [&](TestCanvas& canvas) {
79        refcnt++;
80        canvas.callDrawGLFunction(&noopFunctor, listener.get());
81    });
82    EXPECT_EQ(2, refcnt);
83
84    TestUtils::syncHierarchyPropertiesAndDisplayList(node);
85    EXPECT_EQ(1, refcnt);
86
87    TestUtils::recordNode(*node, [](TestCanvas& canvas) {});
88    EXPECT_EQ(1, refcnt);
89    TestUtils::syncHierarchyPropertiesAndDisplayList(node);
90    EXPECT_EQ(0, refcnt);
91}
92