private final int HEIGHT = 32;
public void testIconManager()
throws NoSuchFieldException, IllegalAccessException, InterruptedException, BrokenBarrierException, ClassNotFoundException {
// Stop iconsLoader of iconManager
IconManager iconManager = IconManager.getInstance();
iconManager.clear();
// Replace icon manager by an executor that controls the start of a task with a barrier
final CyclicBarrier iconLoadingStartBarrier = new CyclicBarrier(2);
final ThreadPoolExecutor replacingIconsLoader =
new ThreadPoolExecutor(1, 1, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>()) {
@Override
protected void beforeExecute(Thread t, Runnable r) {
super.beforeExecute(t, r);
awaitBarrier(iconLoadingStartBarrier);
}
};
// Redirect rejected tasks on iconsLoader to the replacing executor
TestUtilities.setField(iconManager, "iconsLoader", replacingIconsLoader);
// Test icon loading on a good image
testIconLoading(getClass().getResource("resources/test.png"), true, iconLoadingStartBarrier);
// Test icon loading on a content that doesn't an image
testIconLoading(getClass().getResource("IconManagerTest.class"), false, iconLoadingStartBarrier);
Class iconProxyClass = Class.forName(iconManager.getClass().getName() + "$IconProxy");
URLContent waitIconContent =
(URLContent)TestUtilities.getField(iconManager, "waitIconContent");
URLContent errorIconContent =
(URLContent)TestUtilities.getField(iconManager, "errorIconContent");
// Check waitIcon is loaded directly without proxy
Icon waitIcon = iconManager.getIcon(waitIconContent, HEIGHT, null);
assertNotSame("Wait icon loaded with IconProxy", waitIcon.getClass(), iconProxyClass);
// Check errorIcon is loaded directly without proxy
Icon errorIcon = iconManager.getIcon(errorIconContent, HEIGHT, null);
assertNotSame("Error icon loaded with IconProxy", errorIcon.getClass(), iconProxyClass);
// For other tests, replace again iconLoader by an executor that let icon loading complete normally
iconManager.clear();
}