2 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
22 #include "utc-image-loading-common.h"
24 void utc_image_loading_cancel_some_loads_startup(void)
26 utc_dali_loading_startup();
29 void utc_image_loading_cancel_some_loads_cleanup(void)
31 utc_dali_loading_cleanup();
35 * @brief Test case for load cancellation.
37 * Load lots, cancel a subset and be sure the wrong loads are never cancelled
38 * and that all loads issued are either completed or cancelled.
40 int UtcDaliCancelSomeLoads(void)
42 tet_printf( "Running load cancel load subset test.\n" );
44 DALI_ASSERT_ALWAYS( gAbstraction != 0 );
46 // Start a bunch of loads that should work:
48 Dali::Integration::LoadResourcePriority priority = LoadPriorityNormal;
49 unsigned loadsLaunched = 0;
51 std::set<Integration::ResourceId> cancelledLoadSet;
52 Dali::Internal::Platform::ResourceCollector resourceSink;
54 for( unsigned loadGroup = 0; loadGroup < NUM_LOAD_GROUPS_TO_ISSUE; ++loadGroup )
56 const unsigned preIterationCompletions = resourceSink.mGrandTotalCompletions;
58 // Issue load requests for a batch of images:
59 for( unsigned validImage = 0; validImage < NUM_VALID_IMAGES; ++validImage )
61 const ImageParameters & loadParams = gCancelAttributes[ loadsLaunched % gCancelAttributes.size() ];
62 const BitmapResourceType bitmapResourceType( loadParams.first, loadParams.second.first, loadParams.second.second.first, loadParams.second.second.second );
63 const ResourceId resourceId = loadGroup * NUM_VALID_IMAGES + validImage + 1;
64 gAbstraction->LoadResource( ResourceRequest( resourceId, bitmapResourceType, VALID_IMAGES[validImage], priority ) );
68 // Let the first image in the batch start to load so we can try to cancel it in-flight:
69 usleep( 1 * 1000 ); //< 1 ms is enough to let an image start to load.
70 ///@Note: The log should show cancellations of many in-flight loads in desktop builds with info-level logging enabled (e.g., "INFO: DALI: : CheckForCancellation: Cancelled in-flight resource (21)."). If it doesn't, the above delay may need to be adjusted.
72 // Cancel just two loads (hopefully one in-flight and one queued):
74 // Cancel first load, hopefully while it is in-flight:
75 const ResourceId cancelledInFlight = loadGroup * NUM_VALID_IMAGES + 1;
76 gAbstraction->CancelLoad( cancelledInFlight, ResourceBitmap );
77 cancelledLoadSet.insert( cancelledInFlight );
79 // Cancel second load, that is still queued:
80 const ResourceId cancelledFromQueue = loadGroup * NUM_VALID_IMAGES + NUM_VALID_IMAGES;
81 gAbstraction->CancelLoad( cancelledFromQueue, ResourceBitmap );
82 cancelledLoadSet.insert( cancelledFromQueue );
84 // Drain a group worth of images so the cancellations hit in-flight loads on the next iteration:
85 for( unsigned i = 0; i < NUM_VALID_IMAGES * 1000 * 1000 * 10 / (5 * 1000) && resourceSink.mGrandTotalCompletions < preIterationCompletions + NUM_VALID_IMAGES - 2; ++i )
87 gAbstraction->GetResources( resourceSink );
92 // Drain any spare completed loads until no new loads complete on an iteration:
93 unsigned lastNotifications = -1;
94 for( unsigned i = 0; i < MAX_NUM_RESOURCE_TRIES && resourceSink.mGrandTotalCompletions < loadsLaunched && resourceSink.mGrandTotalNotifications != lastNotifications; ++i )
96 lastNotifications = resourceSink.mGrandTotalNotifications;
97 gAbstraction->GetResources( resourceSink );
98 usleep( 70 * 1000 ); //< 70 ms should allow at least one medium image to load. You might to increase this to run on a slow device.
99 gAbstraction->GetResources( resourceSink );
101 gAbstraction->GetResources( resourceSink );
103 gAbstraction->GetResources( resourceSink );
106 // Check the loads completed as expected:
108 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()) );
109 DALI_TEST_CHECK( loadsLaunched >= resourceSink.mGrandTotalCompletions );
110 DALI_TEST_CHECK( loadsLaunched >= resourceSink.mSuccessCounts.size() );
111 DALI_TEST_CHECK( 0 == resourceSink.mFailureCounts.size() );
113 // Check that if an image was not loaded, it is one of the ones that was cancelled:
114 // This is the main point of this test case.
115 std::vector<Integration::ResourceId> missingLoads;
116 for( unsigned resourceId = 1; resourceId <= NUM_LOAD_GROUPS_TO_ISSUE * NUM_VALID_IMAGES; ++resourceId )
118 // Was the load (not) completed?
119 if( resourceSink.mCompletionStatuses.find( resourceId ) == resourceSink.mCompletionStatuses.end() )
121 // Was the load (not) cancelled?
122 if( cancelledLoadSet.find( resourceId ) == cancelledLoadSet.end() )
124 // Whoa, the load was not completed and not cancelled either... so where did it go then?
125 missingLoads.push_back( resourceId );
126 tet_printf( "Missing load. ResourceId %u was not completed but was also not cancelled.\n", resourceId );
127 ///@note If this fires, you are probably not waiting long enough in the draining loop above (usleep( 70 * 1000 );).
131 DALI_TEST_CHECK( missingLoads.size() == 0U );
133 // Check that each success was reported exactly once:
134 for(ResourceCounterMap::const_iterator it = resourceSink.mSuccessCounts.begin(), end = resourceSink.mSuccessCounts.end(); it != end; ++it )
136 DALI_TEST_CHECK( it->second == 1u );