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