1/*
2 * Copyright (C) 2011 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#define LOG_TAG "EGL_test"
18//#define LOG_NDEBUG 0
19
20#include <gtest/gtest.h>
21
22#include <utils/Log.h>
23
24#include <android-base/test_utils.h>
25
26#include "egl_cache.h"
27#include "egl_display.h"
28
29#include <memory>
30
31namespace android {
32
33class EGLCacheTest : public ::testing::Test {
34protected:
35    virtual void SetUp() {
36        mCache = egl_cache_t::get();
37    }
38
39    virtual void TearDown() {
40        mCache->setCacheFilename("");
41        mCache->terminate();
42    }
43
44    egl_cache_t* mCache;
45};
46
47TEST_F(EGLCacheTest, UninitializedCacheAlwaysMisses) {
48    uint8_t buf[4] = { 0xee, 0xee, 0xee, 0xee };
49    mCache->setBlob("abcd", 4, "efgh", 4);
50    ASSERT_EQ(0, mCache->getBlob("abcd", 4, buf, 4));
51    ASSERT_EQ(0xee, buf[0]);
52    ASSERT_EQ(0xee, buf[1]);
53    ASSERT_EQ(0xee, buf[2]);
54    ASSERT_EQ(0xee, buf[3]);
55}
56
57TEST_F(EGLCacheTest, InitializedCacheAlwaysHits) {
58    uint8_t buf[4] = { 0xee, 0xee, 0xee, 0xee };
59    mCache->initialize(egl_display_t::get(EGL_DEFAULT_DISPLAY));
60    mCache->setBlob("abcd", 4, "efgh", 4);
61    ASSERT_EQ(4, mCache->getBlob("abcd", 4, buf, 4));
62    ASSERT_EQ('e', buf[0]);
63    ASSERT_EQ('f', buf[1]);
64    ASSERT_EQ('g', buf[2]);
65    ASSERT_EQ('h', buf[3]);
66}
67
68TEST_F(EGLCacheTest, TerminatedCacheAlwaysMisses) {
69    uint8_t buf[4] = { 0xee, 0xee, 0xee, 0xee };
70    mCache->initialize(egl_display_t::get(EGL_DEFAULT_DISPLAY));
71    mCache->setBlob("abcd", 4, "efgh", 4);
72    mCache->terminate();
73    ASSERT_EQ(0, mCache->getBlob("abcd", 4, buf, 4));
74    ASSERT_EQ(0xee, buf[0]);
75    ASSERT_EQ(0xee, buf[1]);
76    ASSERT_EQ(0xee, buf[2]);
77    ASSERT_EQ(0xee, buf[3]);
78}
79
80class EGLCacheSerializationTest : public EGLCacheTest {
81
82protected:
83
84    virtual void SetUp() {
85        EGLCacheTest::SetUp();
86        mTempFile.reset(new TemporaryFile());
87    }
88
89    virtual void TearDown() {
90        mTempFile.reset(nullptr);
91        EGLCacheTest::TearDown();
92    }
93
94    std::unique_ptr<TemporaryFile> mTempFile;
95};
96
97TEST_F(EGLCacheSerializationTest, ReinitializedCacheContainsValues) {
98    uint8_t buf[4] = { 0xee, 0xee, 0xee, 0xee };
99    mCache->setCacheFilename(&mTempFile->path[0]);
100    mCache->initialize(egl_display_t::get(EGL_DEFAULT_DISPLAY));
101    mCache->setBlob("abcd", 4, "efgh", 4);
102    mCache->terminate();
103    mCache->initialize(egl_display_t::get(EGL_DEFAULT_DISPLAY));
104    ASSERT_EQ(4, mCache->getBlob("abcd", 4, buf, 4));
105    ASSERT_EQ('e', buf[0]);
106    ASSERT_EQ('f', buf[1]);
107    ASSERT_EQ('g', buf[2]);
108    ASSERT_EQ('h', buf[3]);
109}
110
111}
112