### Question:
Sh1106 implements Closeable { public void setContrast(int level) throws IOException, IllegalArgumentException { if (mI2cDevice == null) { throw new IllegalStateException("I2C Device not open"); } if (level < 0 || level > 255) { throw new IllegalArgumentException("Invalid contrast " + String.valueOf(level) + ", level must be between 0 and 255"); } mI2cDevice.writeRegByte(0, (byte) COMMAND_CONTRAST_LEVEL); mI2cDevice.writeRegByte(0, (byte) level); } Sh1106(String i2cName);  Sh1106(String i2cName, int width, int height);  Sh1106(String i2cName, int i2cAddress);  Sh1106(String i2cName, int i2cAddress, int width, int height);   Sh1106(I2cDevice device); @Override void close(); int getLcdWidth(); int getLcdHeight(); void clearPixels(); void setPixel(int x, int y, boolean on); void setContrast(int level); void setDisplayOn(boolean on); void setInvertDisplay(boolean invert); void show(); static final int I2C_ADDRESS; static final int I2C_ADDRESS_ALT; }### Answer:
@Test public void setContrastValidValues() throws IOException { mockStatic(BitmapHelper.class); mockStatic(Bitmap.class); Sh1106 sh1106 = new Sh1106(mI2c); int[] validValues = {0, 66, 255}; for (int validValue : validValues) { sh1106.setContrast(validValue); Mockito.verify(mI2c).writeRegByte(0x00, (byte) 0x81); Mockito.verify(mI2c).writeRegByte(0x00, (byte) validValue); Mockito.reset(mI2c); } }
@Test public void setContrastInValidValues() throws IOException { int[] invalidValues = {-777, -1, 256, 999}; mockStatic(BitmapHelper.class); mockStatic(Bitmap.class); Sh1106 sh1106 = new Sh1106(mI2c); for (int invalidValue : invalidValues) { mExpectedException.expect(IllegalArgumentException.class); mExpectedException.expectMessage("Invalid contrast " + String.valueOf(invalidValue) + ", level must be " + "between 0 and 255"); sh1106.setContrast(invalidValue); Mockito.verify(mI2c, Mockito.never()).writeRegByte(0x00, (byte) 0x81); Mockito.verify(mI2c, Mockito.never()).writeRegByte(0x00, (byte) 66); Mockito.reset(mExpectedException); } }
@Test public void nullmI2cDevice() throws IOException { Sh1106 sh1106 = mock(Sh1106.class, withSettings().defaultAnswer(CALLS_REAL_METHODS)); mExpectedException.expect(IllegalStateException.class); mExpectedException.expectMessage("I2C Device not open"); sh1106.setContrast(44); }