|
| 1 | +package com.callstack.agentdevice.snapshothelper; |
| 2 | + |
| 3 | +public final class DisplaySizeReaderTest { |
| 4 | + private DisplaySizeReaderTest() {} |
| 5 | + |
| 6 | + static void run() { |
| 7 | + assertReportsAUsableDisplayExtent(); |
| 8 | + assertRefusesADisplayThatAnsweredWithNoExtent(); |
| 9 | + } |
| 10 | + |
| 11 | + private static void assertReportsAUsableDisplayExtent() { |
| 12 | + DisplaySizeReader.Size portrait = DisplaySizeReader.resolve(1080, 2400); |
| 13 | + assertSize(portrait, 1080, 2400, "portrait phone"); |
| 14 | + |
| 15 | + // The rotation matters: the caller hands in whichever extent the metrics currently carry, and a |
| 16 | + // landscape capture publishes the box its bounds are actually measured in. |
| 17 | + DisplaySizeReader.Size landscape = DisplaySizeReader.resolve(2400, 1080); |
| 18 | + assertSize(landscape, 2400, 1080, "landscape phone"); |
| 19 | + |
| 20 | + // One pixel wide is still a box a rect can be measured in; the gate is zero, not plausibility. |
| 21 | + DisplaySizeReader.Size minimal = DisplaySizeReader.resolve(1, 1); |
| 22 | + assertSize(minimal, 1, 1, "smallest usable display"); |
| 23 | + } |
| 24 | + |
| 25 | + private static void assertRefusesADisplayThatAnsweredWithNoExtent() { |
| 26 | + // A refused read is what the host reads as "unknown". Answering 0 instead would tell a consumer |
| 27 | + // this screen has no width, which is a claim no producer ever measured. |
| 28 | + assertNull(DisplaySizeReader.resolve(0, 2400), "zero width"); |
| 29 | + assertNull(DisplaySizeReader.resolve(1080, 0), "zero height"); |
| 30 | + assertNull(DisplaySizeReader.resolve(0, 0), "both dimensions zero"); |
| 31 | + assertNull(DisplaySizeReader.resolve(-1, 2400), "negative width"); |
| 32 | + assertNull(DisplaySizeReader.resolve(1080, -1), "negative height"); |
| 33 | + } |
| 34 | + |
| 35 | + private static void assertSize(DisplaySizeReader.Size size, int width, int height, String label) { |
| 36 | + if (size == null) { |
| 37 | + throw new AssertionError("expected a size for " + label); |
| 38 | + } |
| 39 | + if (size.width != width || size.height != height) { |
| 40 | + throw new AssertionError( |
| 41 | + "wrong size for " + label + ": got " + size.width + "x" + size.height); |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + private static void assertNull(DisplaySizeReader.Size size, String label) { |
| 46 | + if (size != null) { |
| 47 | + throw new AssertionError("expected no size for " + label + " but got " + size.width + "x" + size.height); |
| 48 | + } |
| 49 | + } |
| 50 | +} |
0 commit comments