|
| 1 | +package @PACKAGE@; |
| 2 | + |
| 3 | +import static org.junit.Assert.assertNotNull; |
| 4 | +import static org.junit.Assert.assertTrue; |
| 5 | + |
| 6 | +import android.graphics.Bitmap; |
| 7 | +import android.graphics.Canvas; |
| 8 | +import android.util.DisplayMetrics; |
| 9 | +import android.view.View; |
| 10 | + |
| 11 | +import com.codename1.ui.Display; |
| 12 | +import com.codename1.ui.Form; |
| 13 | + |
| 14 | +import java.io.File; |
| 15 | +import java.io.FileOutputStream; |
| 16 | +import java.io.IOException; |
| 17 | +import java.util.concurrent.CountDownLatch; |
| 18 | +import java.util.concurrent.TimeUnit; |
| 19 | +import java.util.concurrent.atomic.AtomicReference; |
| 20 | + |
| 21 | +import org.junit.Test; |
| 22 | +import org.junit.runner.RunWith; |
| 23 | +import org.robolectric.Robolectric; |
| 24 | +import org.robolectric.RobolectricTestRunner; |
| 25 | +import org.robolectric.android.controller.ActivityController; |
| 26 | +import org.robolectric.annotation.Config; |
| 27 | + |
| 28 | +@RunWith(RobolectricTestRunner.class) |
| 29 | +@Config(sdk = 30) |
| 30 | +public class @MAIN_NAME@UiTest { |
| 31 | + |
| 32 | + @Test |
| 33 | + public void mainFormScreenshotContainsRenderedContent() throws Exception { |
| 34 | + ActivityController<@MAIN_NAME@Stub> controller = Robolectric.buildActivity(@ [email protected]).setup(); |
| 35 | + try { |
| 36 | + Form displayed = waitForDisplayedForm(5000L); |
| 37 | + assertNotNull("Codename One form should be displayed", displayed); |
| 38 | + |
| 39 | + @MAIN_NAME@Stub activity = controller.get(); |
| 40 | + Bitmap screenshot = captureScreenshot(activity); |
| 41 | + assertNotNull("Screenshot capture should succeed", screenshot); |
| 42 | + assertTrue("Screenshot width should be positive", screenshot.getWidth() > 0); |
| 43 | + assertTrue("Screenshot height should be positive", screenshot.getHeight() > 0); |
| 44 | + assertTrue("Screenshot should contain rendered content beyond the background", hasRenderableContent(screenshot)); |
| 45 | + |
| 46 | + File screenshotFile = saveScreenshot(screenshot); |
| 47 | + assertTrue("Screenshot file should exist", screenshotFile.isFile()); |
| 48 | + assertTrue("Screenshot file should not be empty", screenshotFile.length() > 0L); |
| 49 | + } finally { |
| 50 | + controller.pause().stop().destroy(); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + private static Form waitForDisplayedForm(long timeoutMillis) throws InterruptedException { |
| 55 | + long deadline = System.currentTimeMillis() + timeoutMillis; |
| 56 | + final Form[] current = new Form[1]; |
| 57 | + while (System.currentTimeMillis() < deadline) { |
| 58 | + if (Display.isInitialized()) { |
| 59 | + Display.getInstance().callSeriallyAndWait(() -> current[0] = Display.getInstance().getCurrent()); |
| 60 | + if (current[0] != null) { |
| 61 | + return current[0]; |
| 62 | + } |
| 63 | + } |
| 64 | + Thread.sleep(50L); |
| 65 | + } |
| 66 | + throw new AssertionError("Timed out waiting for Codename One form to be displayed"); |
| 67 | + } |
| 68 | + |
| 69 | + private static Bitmap captureScreenshot(@MAIN_NAME@Stub activity) throws InterruptedException { |
| 70 | + View decorView = waitForDecorViewWithDimensions(activity, 5000L); |
| 71 | + Bitmap bitmap = Bitmap.createBitmap(decorView.getWidth(), decorView.getHeight(), Bitmap.Config.ARGB_8888); |
| 72 | + runOnUiThreadAndWait(activity, () -> { |
| 73 | + Canvas canvas = new Canvas(bitmap); |
| 74 | + decorView.draw(canvas); |
| 75 | + }); |
| 76 | + return bitmap; |
| 77 | + } |
| 78 | + |
| 79 | + private static boolean hasRenderableContent(Bitmap screenshot) { |
| 80 | + int width = screenshot.getWidth(); |
| 81 | + int height = screenshot.getHeight(); |
| 82 | + if (width <= 0 || height <= 0) { |
| 83 | + return false; |
| 84 | + } |
| 85 | + int[] pixels = new int[width * height]; |
| 86 | + screenshot.getPixels(pixels, 0, width, 0, 0, width, height); |
| 87 | + if (pixels.length == 0) { |
| 88 | + return false; |
| 89 | + } |
| 90 | + int background = pixels[0]; |
| 91 | + int contentPixels = 0; |
| 92 | + for (int argb : pixels) { |
| 93 | + int alpha = (argb >>> 24) & 0xFF; |
| 94 | + if (alpha == 0) { |
| 95 | + continue; |
| 96 | + } |
| 97 | + if (argb != background) { |
| 98 | + contentPixels++; |
| 99 | + if (contentPixels > width) { |
| 100 | + return true; |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | + return false; |
| 105 | + } |
| 106 | + |
| 107 | + private static File saveScreenshot(Bitmap screenshot) throws IOException { |
| 108 | + String directory = System.getenv("CN1_TEST_SCREENSHOT_DIR"); |
| 109 | + File outputDir = (directory != null && !directory.isEmpty()) ? new File(directory) : new File("build/ui-test-screenshots"); |
| 110 | + if (!outputDir.exists() && !outputDir.mkdirs()) { |
| 111 | + throw new IOException("Failed to create screenshot directory " + outputDir.getAbsolutePath()); |
| 112 | + } |
| 113 | + File screenshotFile = new File(outputDir, "@ [email protected]"); |
| 114 | + try (FileOutputStream out = new FileOutputStream(screenshotFile)) { |
| 115 | + if (!screenshot.compress(Bitmap.CompressFormat.PNG, 100, out)) { |
| 116 | + throw new IOException("Failed to encode screenshot as PNG"); |
| 117 | + } |
| 118 | + } |
| 119 | + return screenshotFile; |
| 120 | + } |
| 121 | + |
| 122 | + private static View waitForDecorViewWithDimensions(@MAIN_NAME@Stub activity, long timeoutMillis) throws InterruptedException { |
| 123 | + long deadline = System.currentTimeMillis() + timeoutMillis; |
| 124 | + while (System.currentTimeMillis() < deadline) { |
| 125 | + View decorView = activity.getWindow().getDecorView(); |
| 126 | + if (decorView != null) { |
| 127 | + ensureViewHasLayout(activity, decorView); |
| 128 | + if (decorView.getWidth() > 0 && decorView.getHeight() > 0) { |
| 129 | + return decorView; |
| 130 | + } |
| 131 | + } |
| 132 | + Thread.sleep(50L); |
| 133 | + } |
| 134 | + throw new AssertionError("Timed out waiting for decor view layout"); |
| 135 | + } |
| 136 | + |
| 137 | + private static void ensureViewHasLayout(@MAIN_NAME@Stub activity, View view) throws InterruptedException { |
| 138 | + if (view.getWidth() > 0 && view.getHeight() > 0) { |
| 139 | + return; |
| 140 | + } |
| 141 | + runOnUiThreadAndWait(activity, () -> { |
| 142 | + DisplayMetrics metrics = activity.getResources().getDisplayMetrics(); |
| 143 | + int widthSpec = View.MeasureSpec.makeMeasureSpec(metrics.widthPixels, View.MeasureSpec.EXACTLY); |
| 144 | + int heightSpec = View.MeasureSpec.makeMeasureSpec(metrics.heightPixels, View.MeasureSpec.EXACTLY); |
| 145 | + view.measure(widthSpec, heightSpec); |
| 146 | + view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight()); |
| 147 | + }); |
| 148 | + } |
| 149 | + |
| 150 | + private static void runOnUiThreadAndWait(@MAIN_NAME@Stub activity, Runnable runnable) throws InterruptedException { |
| 151 | + CountDownLatch latch = new CountDownLatch(1); |
| 152 | + AtomicReference<Throwable> error = new AtomicReference<>(); |
| 153 | + activity.runOnUiThread(() -> { |
| 154 | + try { |
| 155 | + runnable.run(); |
| 156 | + } catch (Throwable t) { |
| 157 | + error.set(t); |
| 158 | + } finally { |
| 159 | + latch.countDown(); |
| 160 | + } |
| 161 | + }); |
| 162 | + if (!latch.await(5, TimeUnit.SECONDS)) { |
| 163 | + throw new AssertionError("Timed out waiting for UI thread task"); |
| 164 | + } |
| 165 | + if (error.get() != null) { |
| 166 | + Throwable thrown = error.get(); |
| 167 | + if (thrown instanceof RuntimeException) { |
| 168 | + throw (RuntimeException) thrown; |
| 169 | + } |
| 170 | + if (thrown instanceof Error) { |
| 171 | + throw (Error) thrown; |
| 172 | + } |
| 173 | + throw new RuntimeException(thrown); |
| 174 | + } |
| 175 | + } |
| 176 | +} |
0 commit comments