Parallelize PlatformAbstraction image loading tests
[platform/core/uifw/dali-adaptor.git] / automated-tests / src / dali-platform-abstraction / utc-image-loading-cancel-all-loads.cpp
1 /*
2  * Copyright (c) 2014 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 "utc-image-loading-common.h"
19
20 void utc_image_loading_cancel_all_loads_startup(void)
21 {
22   utc_dali_loading_startup();
23 }
24
25 void utc_image_loading_cancel_all_loads_cleanup(void)
26 {
27   utc_dali_loading_cleanup();
28 }
29
30 /**
31  * @brief Test case for load cancellation.
32  *
33  * Load lots of images in batches, cancelling all in a batch after a small delay to
34  * allow the first of a batch to be launched before cancellation starts.
35  * Assert that all loads issued are either completed or cancelled.
36  *
37  * @note Many loads will succeed despite our cancellations due to the coarse
38  * granularity of the waits we introduce after loading each batch. That is
39  * expected.
40  */
41 int UtcDaliCancelAllLoads(void)
42 {
43   tet_printf( "Running load cancel-all test.\n" );
44
45   DALI_ASSERT_ALWAYS( gAbstraction != 0 );
46
47   // Start a bunch of loads that should work:
48
49   Dali::Integration::LoadResourcePriority priority = LoadPriorityNormal;
50   unsigned loadsLaunched = 0;
51   Dali::Internal::Platform::ResourceCollector resourceSink;
52
53   for( unsigned loadGroup = 0; loadGroup < NUM_CANCELLED_LOAD_GROUPS_TO_ISSUE; ++loadGroup )
54   {
55     // Issue load requests for a batch of images:
56     for( unsigned validImage = 0; validImage < NUM_VALID_IMAGES; ++validImage )
57     {
58       const BitmapResourceType bitmapResourceType( gCancelAttributes[ loadsLaunched % gCancelAttributes.size() ] );
59       const ResourceId resourceId = loadGroup * NUM_VALID_IMAGES + validImage + 1;
60       gAbstraction->LoadResource( ResourceRequest( resourceId, bitmapResourceType, VALID_IMAGES[validImage], priority ) );
61       loadsLaunched += 1;
62     }
63
64     // Poll for at least one completed load so we have a good chance of catching an
65     // in-flight load as we run through the cancellations further below:
66     PollForNotification( resourceSink, *gAbstraction, 100 );
67
68     // Cancel all the launched loads in the batch from oldest to newest:
69     for( unsigned validImage = 0; validImage < NUM_VALID_IMAGES; ++validImage )
70     {
71       const ResourceId resourceId = loadGroup * NUM_VALID_IMAGES + validImage + 1;
72       gAbstraction->CancelLoad( resourceId, ResourceBitmap );
73     }
74   }
75
76   // Drain the completed loads:
77
78   unsigned lastNotifications = -1;
79   for( unsigned i = 0; resourceSink.mGrandTotalCompletions < loadsLaunched && resourceSink.mGrandTotalNotifications != lastNotifications; )
80   {
81     lastNotifications = resourceSink.mGrandTotalNotifications;
82     gAbstraction->GetResources( resourceSink );
83
84     ++i;
85     if( i < MAX_NUM_RESOURCE_TRIES && resourceSink.mGrandTotalCompletions < loadsLaunched )
86     {
87       usleep( 1000 * 10 );
88     }
89     else
90     {
91       break;
92     }
93   }
94
95   // Check the loads completed as expected:
96
97   tet_printf( "Issued Loads: %u, Completed Loads: %u, Successful Loads: %u, Failed Loads: %u \n", loadsLaunched, resourceSink.mGrandTotalCompletions, unsigned(resourceSink.mSuccessCounts.size()), unsigned(resourceSink.mFailureCounts.size()) );
98   DALI_TEST_CHECK( loadsLaunched > resourceSink.mGrandTotalCompletions );
99   DALI_TEST_CHECK( loadsLaunched > resourceSink.mSuccessCounts.size() );
100   DALI_TEST_CHECK( 0 == resourceSink.mFailureCounts.size() );
101
102   // Check that each success was reported exactly once:
103   for( ResourceCounterMap::const_iterator it = resourceSink.mSuccessCounts.begin(), end = resourceSink.mSuccessCounts.end(); it != end; ++it )
104   {
105     DALI_TEST_CHECK( it->second == 1u );
106   }
107
108   END_TEST;
109 }