Formatting automated-tests
[platform/core/uifw/dali-adaptor.git] / automated-tests / src / dali-adaptor-internal / utc-Dali-CompressedTextures.cpp
1 /*
2  * Copyright (c) 2020 Samsung Electronics Co., Ltd.
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
18 // EXTERNAL INCLUDES
19 #include <dali-test-suite-utils.h>
20 #include <dali/internal/imaging/common/loader-astc.h>
21 #include <dali/internal/imaging/common/loader-ktx.h>
22 #include <stdlib.h>
23 #include <iostream>
24 #include <vector>
25
26 // INTERNAL INCLUDES
27 #include "image-loaders.h"
28
29 using namespace Dali;
30
31 // Pre-define loader functions for each image type being tested (as they are reused in different tests).
32 static const LoadFunctions KtxLoaders(TizenPlatform::LoadKtxHeader, TizenPlatform::LoadBitmapFromKtx);
33 static const LoadFunctions AstcLoaders(TizenPlatform::LoadAstcHeader, TizenPlatform::LoadBitmapFromAstc);
34
35 /**
36  * This class encapsulates knowledge of testing compressed files.
37  * It requires a few input parameters per test to confirm if the file was read and understood.
38  * The fixture guarantees that each test performed is setup and closed individually, therefore run order does not matter.
39  */
40 class KtxTestFixture
41 {
42 public:
43   /**
44      * Constructor.
45      * Sets up the fixture.
46      */
47   KtxTestFixture(void)
48   {
49   }
50
51   /**
52      * Destructor.
53      */
54   ~KtxTestFixture()
55   {
56   }
57
58   /**
59      * This struct contains any per-test parameters.
60      * This should be added to if more properties of a file/format should be tested.
61      */
62   struct TestEntry
63   {
64     LoadFunctions loadFunctions;  ///< Used to parse the header of a given type of image.
65     std::string   filename;       ///< Name of the compressed texture KTX file to load.
66     int           expectedWidth;  ///< The width the texture should be.
67     int           expectedHeight; ///< The height the KTX texture should be.
68
69     TestEntry(const LoadFunctions& newLoadFunctions, std::string newFilename, int newExpectedWidth, int newExpectedHeight)
70     : loadFunctions(newLoadFunctions),
71       filename(newFilename),
72       expectedWidth(newExpectedWidth),
73       expectedHeight(newExpectedHeight)
74     {
75     }
76   };
77
78 private:
79   typedef std::vector<TestEntry> TestContainer;
80
81 public:
82   /**
83      * Adds a test to be performed.
84      * @param[in] testEntry A TestEntry struct containing all the details to perform one test.
85      */
86   void AddTest(TestEntry testEntry)
87   {
88     mTests.push_back(testEntry);
89   }
90
91   /**
92      * Runs all tests created with "AddTest".
93      * This will create failures upon failing tests.
94      */
95   void RunTests()
96   {
97     for(TestContainer::iterator testIterator = mTests.begin(); testIterator != mTests.end(); ++testIterator)
98     {
99       const TestEntry& currentTest = *testIterator;
100
101       RunTest(currentTest);
102     }
103   }
104
105 private:
106   /**
107      * Sets up, Runs and Closes-down an individual test.
108      * @param[in] testEntry A TestEntry struct containing all the details to perform one test.
109      */
110   void RunTest(const TestEntry& testEntry)
111   {
112     FILE*         fileDescriptor = fopen(testEntry.filename.c_str(), "rb");
113     AutoCloseFile autoClose(fileDescriptor);
114     DALI_TEST_CHECK(fileDescriptor != NULL);
115
116     // Check the header file.
117     unsigned int                   width(0), height(0);
118     const Dali::ImageLoader::Input input(fileDescriptor);
119
120     // Use the given loader to parse the image header.
121     DALI_TEST_CHECK(testEntry.loadFunctions.header(input, width, height));
122
123     DALI_TEST_EQUALS(width, testEntry.expectedWidth, TEST_LOCATION);
124     DALI_TEST_EQUALS(height, testEntry.expectedHeight, TEST_LOCATION);
125   }
126
127 private:
128   TestContainer mTests; ///< Holds all tests to be run.
129 };
130
131 // KTX files (KTX is a wrapper, so can contain different compressed texture types):
132
133 int UtcDaliKtxLoaderETC(void)
134 {
135   KtxTestFixture fixture;
136
137   fixture.AddTest(KtxTestFixture::TestEntry(KtxLoaders, TEST_IMAGE_DIR "/fractal-compressed-ETC1_RGB8_OES-45x80.ktx", 45u, 80u));
138   fixture.AddTest(KtxTestFixture::TestEntry(KtxLoaders, TEST_IMAGE_DIR "/fractal-compressed-RGB8_ETC2-45x80.ktx", 45u, 80u));
139
140   fixture.RunTests();
141
142   END_TEST;
143 }
144
145 int UtcDaliKtxLoaderPVRTC(void)
146 {
147   KtxTestFixture fixture;
148
149   fixture.AddTest(KtxTestFixture::TestEntry(KtxLoaders, TEST_IMAGE_DIR "/fractal-compressed-RGB_PVRTC_4BPPV1_IMG-32x64.ktx", 32u, 64u));
150
151   fixture.RunTests();
152
153   END_TEST;
154 }
155
156 int UtcDaliKtxLoaderEAC(void)
157 {
158   KtxTestFixture fixture;
159
160   fixture.AddTest(KtxTestFixture::TestEntry(KtxLoaders, TEST_IMAGE_DIR "/fractal-compressed-R11_EAC-45x80.ktx", 45u, 80u));
161
162   fixture.RunTests();
163
164   END_TEST;
165 }
166
167 int UtcDaliKtxLoaderASTC(void)
168 {
169   KtxTestFixture fixture;
170
171   fixture.AddTest(KtxTestFixture::TestEntry(KtxLoaders, TEST_IMAGE_DIR "/fractal-compressed-RGBA_ASTC_4x4_KHR-32x64.ktx", 32u, 64u));
172   fixture.AddTest(KtxTestFixture::TestEntry(KtxLoaders, TEST_IMAGE_DIR "/fractal-compressed-SRBG8_ALPHA8_ASTC_4x4_KHR-32x64.ktx", 32u, 64u));
173
174   fixture.RunTests();
175
176   END_TEST;
177 }
178
179 // ASTC (Native) files:
180 int UtcDaliAstcLoader(void)
181 {
182   KtxTestFixture fixture;
183
184   fixture.AddTest(KtxTestFixture::TestEntry(AstcLoaders, TEST_IMAGE_DIR "/fractal-compressed-RGBA_ASTC_4x4_KHR-32x64.astc", 32u, 64u));
185
186   fixture.RunTests();
187
188   END_TEST;
189 }