Merge branch 'devel/master' into tizen
[platform/core/uifw/dali-core.git] / automated-tests / src / dali / utc-Dali-Image.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 #include <iostream>
19 #include <algorithm>
20 #include <stdlib.h>
21 #include <dali/public-api/dali-core.h>
22 #include <dali/integration-api/bitmap.h>
23 #include <dali-test-suite-utils.h>
24 #include <test-native-image.h>
25
26 using namespace Dali;
27
28 void utc_dali_image_startup(void)
29 {
30   test_return_value = TET_UNDEF;
31 }
32
33 void utc_dali_image_cleanup(void)
34 {
35   test_return_value = TET_PASS;
36 }
37
38 static const char* gTestImageFilename = "icon_wrt.png";
39
40 namespace
41 {
42 void LoadBitmapResource(TestPlatformAbstraction& platform)
43 {
44   Integration::ResourceRequest* request = platform.GetRequest();
45   Integration::Bitmap* bitmap = Integration::Bitmap::New( Integration::Bitmap::BITMAP_2D_PACKED_PIXELS, ResourcePolicy::OWNED_DISCARD );
46   Integration::ResourcePointer resource(bitmap);
47   bitmap->GetPackedPixelsProfile()->ReserveBuffer(Pixel::RGBA8888, 80, 80, 80, 80);
48
49   if(request)
50   {
51     platform.SetResourceLoaded(request->GetId(), request->GetType()->id, resource);
52   }
53 }
54
55 }
56
57 int UtcDaliImageDownCast(void)
58 {
59   TestApplication application;
60   tet_infoline("Testing Dali::Image::DownCast()");
61
62   Image image = ResourceImage::New(gTestImageFilename);
63
64   BaseHandle object(image);
65
66   Image image2 = Image::DownCast(object);
67   DALI_TEST_CHECK(image2);
68
69   Image image3 = DownCast< Image >(object);
70   DALI_TEST_CHECK(image3);
71
72   BaseHandle unInitializedObject;
73   Image image4 = Image::DownCast(unInitializedObject);
74   DALI_TEST_CHECK(!image4);
75
76   Image image5 = DownCast< Image >(unInitializedObject);
77   DALI_TEST_CHECK(!image5);
78   END_TEST;
79 }
80
81 int UtcDaliImageGetWidthHeight(void)
82 {
83   TestApplication application;
84
85   tet_infoline("UtcDaliImageGetWidthHeight - Image::GetWidth() & Image::GetHeight");
86
87   Vector2 testSize(8.0f, 16.0f);
88   application.GetPlatform().SetClosestImageSize(testSize);
89   PrepareResourceImage( application, testSize.x, testSize.y, Pixel::RGBA8888 );
90   Image image1 = ResourceImage::New(gTestImageFilename);
91   DALI_TEST_EQUALS( image1.GetWidth(), testSize.x, TEST_LOCATION );
92   DALI_TEST_EQUALS( image1.GetHeight(), testSize.y, TEST_LOCATION );
93
94   testSize = Vector2(128.0f, 256.0f);
95   PrepareResourceImage( application, testSize.x, testSize.y, Pixel::RGBA8888 );
96   Image image2 = ResourceImage::New( gTestImageFilename, ImageDimensions(testSize.x, testSize.y), FittingMode::SCALE_TO_FILL, SamplingMode::DEFAULT );
97   DALI_TEST_EQUALS( image2.GetWidth(), testSize.x, TEST_LOCATION );
98   DALI_TEST_EQUALS( image2.GetHeight(), testSize.y, TEST_LOCATION );
99
100   testSize = Vector2(16.0f, 32.0f);
101   Image image3 = FrameBufferImage::New(testSize.x, testSize.y);
102   DALI_TEST_EQUALS(image3.GetWidth(), testSize.x, TEST_LOCATION);
103   DALI_TEST_EQUALS(image3.GetHeight(), testSize.y, TEST_LOCATION);
104
105   testSize = Vector2(32.0f, 64.0f);
106   PrepareResourceImage( application, testSize.x, testSize.y, Pixel::RGBA8888 );
107   TestNativeImagePointer nativeImage = TestNativeImage::New(testSize.x, testSize.y);
108   Image image4 = NativeImage::New(*(nativeImage.Get()));
109   DALI_TEST_EQUALS(image4.GetWidth(), testSize.x, TEST_LOCATION);
110   DALI_TEST_EQUALS(image4.GetHeight(), testSize.y, TEST_LOCATION);
111
112   END_TEST;
113 }
114
115 int UtcDaliImageDiscard01(void)
116 {
117   TestApplication application;
118   tet_infoline("UtcDaliImageDiscard01 - no actors");
119
120   {
121     Image image = ResourceImage::New(gTestImageFilename);
122
123     // Load image
124     application.SendNotification();
125     application.Render(16);
126     std::vector<GLuint> ids;
127     ids.push_back( 23 );
128     application.GetGlAbstraction().SetNextTextureIds( ids );
129     TestPlatformAbstraction& platform = application.GetPlatform();
130     LoadBitmapResource( platform );
131     application.Render(16);
132     application.SendNotification();
133   } // Drop image handle
134
135   application.SendNotification();
136   application.Render(16);
137   application.Render(16);
138   application.SendNotification();
139
140   // Shouldn't have been sent to GL...
141   const std::vector<GLuint>& texIds = application.GetGlAbstraction().GetNextTextureIds();
142   DALI_TEST_CHECK( texIds.size() == 1 );
143   DALI_TEST_CHECK( texIds[0] == 23 );
144   END_TEST;
145 }