90825462acd2a5a1aca031cfd2c5935a30aa85ab
[platform/core/uifw/dali-core.git] / automated-tests / src / dali / utc-Dali-NativeImage.cpp
1 /*
2  * Copyright (c) 2015 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 #include <iostream>
19 #include <algorithm>
20 #include <stdlib.h>
21 #include <dali/public-api/dali-core.h>
22 #include <dali-test-suite-utils.h>
23 #include <test-native-image.h>
24
25 using namespace Dali;
26
27 void utc_dali_native_image_startup(void)
28 {
29   test_return_value = TET_UNDEF;
30 }
31
32 void utc_dali_native_image_cleanup(void)
33 {
34   test_return_value = TET_PASS;
35 }
36
37 int UtcDaliNativeImageNew(void)
38 {
39   TestApplication application;
40
41   tet_infoline("UtcDaliNativeImageNew - NativeImage::New(NativeImageInterface&)");
42
43   // invoke default handle constructor
44   NativeImage image;
45   TestNativeImagePointer nativeImage = TestNativeImage::New(16, 16);
46
47   DALI_TEST_CHECK( !image );
48
49   // initialise handle
50   image = NativeImage::New(*(nativeImage.Get()));
51
52   DALI_TEST_CHECK( image );
53   END_TEST;
54 }
55
56 int UtcDaliNativeImageCopyConstructor(void)
57 {
58   TestApplication application;
59
60   tet_infoline("UtcDaliNativeImageCopyConstructor - NativeImage::NativeImage( const NativeImage& )");
61
62   NativeImage image1;
63   DALI_TEST_CHECK( !image1 );
64
65   TestNativeImagePointer nativeImage = TestNativeImage::New(16, 16);
66   image1 = NativeImage::New(*(nativeImage.Get()));
67   NativeImage image2( image1 );
68
69   DALI_TEST_CHECK( image2 );
70   DALI_TEST_EQUALS( image1, image2, TEST_LOCATION );
71
72   END_TEST;
73 }
74
75 int UtcDaliNativeImageDownCast(void)
76 {
77   TestApplication application;
78   tet_infoline("Testing Dali::Image::DownCast()");
79
80   TestNativeImagePointer nativeImage = TestNativeImage::New(16, 16);
81   NativeImage image = NativeImage::New(*(nativeImage.Get()));
82
83   BaseHandle object(image);
84
85   NativeImage image2 = NativeImage::DownCast(object);
86   DALI_TEST_CHECK(image2);
87
88   NativeImage image3 = DownCast< NativeImage >(object);
89   DALI_TEST_CHECK(image3);
90
91   BaseHandle unInitializedObject;
92   NativeImage image4 = NativeImage::DownCast(unInitializedObject);
93   DALI_TEST_CHECK(!image4);
94
95   NativeImage image5 = DownCast< NativeImage >(unInitializedObject);
96   DALI_TEST_CHECK(!image5);
97
98   Image image6 = NativeImage::New(*(nativeImage.Get()));
99   NativeImage image7 = NativeImage::DownCast(image6);
100   DALI_TEST_CHECK(image7);
101   END_TEST;
102 }
103
104 int UtcDaliNativeImageCreateGlTextureN(void)
105 {
106   TestApplication application;
107   tet_infoline( "Testing Dali::NativeImage::GenerateGlTexture()" );
108
109   NativeImage image;
110   try
111   {
112     image.CreateGlTexture();
113     tet_printf( "Assertion test failed - no Exception\n" );
114     tet_result( TET_FAIL );
115   }
116   catch( Dali::DaliException& e )
117   {
118     DALI_TEST_PRINT_ASSERT( e );
119     DALI_TEST_ASSERT( e, "image &&", TEST_LOCATION );
120   }
121   END_TEST;
122 }
123
124 int UtcDaliNativeImageCreateGlTextureP(void)
125 {
126   TestApplication application;
127   tet_infoline( "Testing Dali::NativeImage::GenerateGlTexture()" );
128
129   TestNativeImagePointer imageInterface = TestNativeImage::New( 16, 16 );
130   NativeImage image = NativeImage::New( *(imageInterface.Get()) );
131   DALI_TEST_CHECK( image );
132
133   image.CreateGlTexture();
134
135   application.SendNotification();
136   application.Render(16);
137
138   DALI_TEST_EQUALS( imageInterface->mExtensionCreateCalls, 1, TEST_LOCATION );
139   DALI_TEST_EQUALS( imageInterface->mTargetTextureCalls, 1, TEST_LOCATION );
140
141   END_TEST;
142 }
143
144 int UtcDaliNativeImageContextLoss(void)
145 {
146   TestApplication application;
147   tet_infoline( "Testing Dali::NativeImage behaviour through a context lost/regained cycle." );
148
149   // Build an image that is expected to have a GL texture created for it and
150   // recreated in a GL context recovery:
151   TestNativeImagePointer eagerImageInterface = TestNativeImage::New( 16, 16 );
152   NativeImage eagerImage = NativeImage::New( *(eagerImageInterface.Get()) );
153   DALI_TEST_CHECK( eagerImage );
154
155   // Build a regular lazy-allocated image for comparison:
156   TestNativeImagePointer lazyImageInterface = TestNativeImage::New( 16, 16 );
157   NativeImage lazyImage = NativeImage::New( *(lazyImageInterface.Get()) );
158   DALI_TEST_CHECK( lazyImage );
159
160   eagerImage.CreateGlTexture();
161
162   application.SendNotification();
163   application.Render(16);
164
165   // Cycle through a context loss and regain, asserting that the texture is
166   // not reallocated if it already existed before the cycle and is never allocated
167   // throughout the cycle if of the regular lazy kind:
168
169   // Call render thread context destroyed / created functions:
170   application.ResetContext();
171
172   // Call event thread function:
173   application.GetCore().RecoverFromContextLoss();
174
175   // Run update/render loop
176   application.SendNotification();
177   application.Render(16);
178
179   DALI_TEST_EQUALS( eagerImageInterface->mExtensionCreateCalls, 1, TEST_LOCATION );
180   DALI_TEST_EQUALS( eagerImageInterface->mTargetTextureCalls, 1, TEST_LOCATION );
181
182   DALI_TEST_EQUALS( lazyImageInterface->mExtensionCreateCalls, 0, TEST_LOCATION );
183   DALI_TEST_EQUALS( lazyImageInterface->mTargetTextureCalls, 0, TEST_LOCATION );
184
185   END_TEST;
186 }