• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include <errno.h>
2 
3 #include <bufferhub/BufferNode.h>
4 #include <gmock/gmock.h>
5 #include <gtest/gtest.h>
6 #include <ui/BufferHubDefs.h>
7 #include <ui/GraphicBufferMapper.h>
8 
9 namespace android {
10 namespace frameworks {
11 namespace bufferhub {
12 namespace V1_0 {
13 namespace implementation {
14 
15 namespace {
16 
17 using testing::NotNull;
18 
19 const uint32_t kWidth = 640;
20 const uint32_t kHeight = 480;
21 const uint32_t kLayerCount = 1;
22 const uint32_t kFormat = 1;
23 const uint64_t kUsage = 0;
24 const size_t kUserMetadataSize = 0;
25 
26 class BufferNodeTest : public ::testing::Test {
27 protected:
SetUp()28     void SetUp() override {
29         mBufferNode =
30                 new BufferNode(kWidth, kHeight, kLayerCount, kFormat, kUsage, kUserMetadataSize);
31         ASSERT_TRUE(mBufferNode->isValid());
32     }
33 
TearDown()34     void TearDown() override {
35         if (mBufferNode != nullptr) {
36             delete mBufferNode;
37         }
38     }
39 
40     BufferNode* mBufferNode = nullptr;
41 };
42 
TEST_F(BufferNodeTest,TestCreateBufferNode)43 TEST_F(BufferNodeTest, TestCreateBufferNode) {
44     EXPECT_EQ(mBufferNode->userMetadataSize(), kUserMetadataSize);
45     // Test the handle just allocated is good (i.e. able to be imported)
46     GraphicBufferMapper& mapper = GraphicBufferMapper::get();
47     const native_handle_t* outHandle;
48     status_t ret =
49             mapper.importBuffer(mBufferNode->bufferHandle(), mBufferNode->bufferDesc().width,
50                                 mBufferNode->bufferDesc().height, mBufferNode->bufferDesc().layers,
51                                 mBufferNode->bufferDesc().format, mBufferNode->bufferDesc().usage,
52                                 mBufferNode->bufferDesc().stride, &outHandle);
53     EXPECT_EQ(ret, OK);
54     EXPECT_THAT(outHandle, NotNull());
55 }
56 
TEST_F(BufferNodeTest,TestaddNewActiveClientsBitToMask_twoNewClients)57 TEST_F(BufferNodeTest, TestaddNewActiveClientsBitToMask_twoNewClients) {
58     uint32_t newClientStateMask1 = mBufferNode->addNewActiveClientsBitToMask();
59     EXPECT_EQ(mBufferNode->getActiveClientsBitMask(), newClientStateMask1);
60 
61     // Request and add a new client_state_mask again.
62     // Active clients bit mask should be the union of the two new
63     // client_state_masks.
64     uint32_t newClientStateMask2 = mBufferNode->addNewActiveClientsBitToMask();
65     EXPECT_EQ(mBufferNode->getActiveClientsBitMask(), newClientStateMask1 | newClientStateMask2);
66 }
67 
TEST_F(BufferNodeTest,TestaddNewActiveClientsBitToMask_32NewClients)68 TEST_F(BufferNodeTest, TestaddNewActiveClientsBitToMask_32NewClients) {
69     uint32_t newClientStateMask = 0U;
70     uint32_t currentMask = 0U;
71     uint32_t expectedMask = 0U;
72 
73     for (int i = 0; i < BufferHubDefs::kMaxNumberOfClients; ++i) {
74         newClientStateMask = mBufferNode->addNewActiveClientsBitToMask();
75         EXPECT_NE(newClientStateMask, 0U);
76         EXPECT_FALSE(newClientStateMask & currentMask);
77         expectedMask = currentMask | newClientStateMask;
78         currentMask = mBufferNode->getActiveClientsBitMask();
79         EXPECT_EQ(currentMask, expectedMask);
80     }
81 
82     // Method should fail upon requesting for more than maximum allowable clients.
83     newClientStateMask = mBufferNode->addNewActiveClientsBitToMask();
84     EXPECT_EQ(newClientStateMask, 0U);
85     EXPECT_EQ(errno, E2BIG);
86 }
87 
TEST_F(BufferNodeTest,TestRemoveActiveClientsBitFromMask)88 TEST_F(BufferNodeTest, TestRemoveActiveClientsBitFromMask) {
89     mBufferNode->addNewActiveClientsBitToMask();
90     uint32_t currentMask = mBufferNode->getActiveClientsBitMask();
91     uint32_t newClientStateMask = mBufferNode->addNewActiveClientsBitToMask();
92     EXPECT_NE(mBufferNode->getActiveClientsBitMask(), currentMask);
93 
94     mBufferNode->removeClientsBitFromMask(newClientStateMask);
95     EXPECT_EQ(mBufferNode->getActiveClientsBitMask(), currentMask);
96 
97     // Remove the test_mask again to the active client bit mask should not modify
98     // the value of active clients bit mask.
99     mBufferNode->removeClientsBitFromMask(newClientStateMask);
100     EXPECT_EQ(mBufferNode->getActiveClientsBitMask(), currentMask);
101 }
102 
103 } // namespace
104 
105 } // namespace implementation
106 } // namespace V1_0
107 } // namespace bufferhub
108 } // namespace frameworks
109 } // namespace android
110