|
| 1 | +package com.codedifferently.lesson16.KimberleeObjectTest; |
| 2 | + |
| 3 | +import static org.assertj.core.api.Assertions.assertThatThrownBy; |
| 4 | +import static org.junit.jupiter.api.Assertions.*; |
| 5 | + |
| 6 | +import com.codedifferently.lesson16.KimberleeObject.ConnectionNotFoundException; |
| 7 | +import com.codedifferently.lesson16.KimberleeObject.HeadPhones; |
| 8 | +import com.codedifferently.lesson16.KimberleeObject.HeadPhones.BoostMode; |
| 9 | +import com.codedifferently.lesson16.KimberleeObject.HeadPhones.HeadPhoneColor; |
| 10 | +import org.junit.jupiter.api.BeforeEach; |
| 11 | +import org.junit.jupiter.api.Test; |
| 12 | + |
| 13 | +public class HeadPhonesTest { |
| 14 | + |
| 15 | + private HeadPhones headphones; |
| 16 | + |
| 17 | + @BeforeEach |
| 18 | + public void setUp() { |
| 19 | + headphones = new HeadPhones(); |
| 20 | + } |
| 21 | + |
| 22 | + @Test |
| 23 | + public void testDefaultState() { |
| 24 | + assertEquals(0, headphones.getVolume(), "Volume should be 0 by default."); |
| 25 | + assertEquals( |
| 26 | + HeadPhoneColor.BLACK, headphones.getHeadPhoneColor(), "Color should be black by default."); |
| 27 | + assertFalse(headphones.isPoweredOn(), "HeadPhones should be off by default."); |
| 28 | + assertTrue(headphones.isWireless(), "HeadPhones should be wireless by default."); |
| 29 | + assertArrayEquals(new String[] {"Beats"}, new String[] {"Beats"}); |
| 30 | + assertEquals( |
| 31 | + BoostMode.BASS_BOOST, |
| 32 | + headphones.getCurrentMode(), |
| 33 | + "BASS_BOOST should be the default setting."); |
| 34 | + } |
| 35 | + |
| 36 | + @Test |
| 37 | + public void testTurnOn() { |
| 38 | + // Act |
| 39 | + headphones.turnOn(); |
| 40 | + // Assert |
| 41 | + assertTrue(headphones.isPoweredOn(), "Headphones should turn on after calling turnOn"); |
| 42 | + } |
| 43 | + |
| 44 | + @Test |
| 45 | + public void testTurnOff() { |
| 46 | + // Arrange |
| 47 | + headphones.turnOn(); |
| 48 | + // Act |
| 49 | + headphones.turnOff(); |
| 50 | + // Assert |
| 51 | + assertFalse(headphones.isPoweredOn(), "Headphones should turn off after calling"); |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + public void testIncreaseVolume() { |
| 56 | + // Arrange |
| 57 | + headphones.turnOn(); |
| 58 | + headphones.increaseVolume(); |
| 59 | + assertEquals(1, headphones.getVolume(), "Volume should increase by 1"); |
| 60 | + // Act |
| 61 | + headphones.setVolume(99); |
| 62 | + headphones.increaseVolume(); |
| 63 | + // Assert |
| 64 | + assertEquals(100, headphones.getVolume(), "Volume should not exceed 100."); |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + public void testDecreaseVolume() { |
| 69 | + // Arrange |
| 70 | + headphones.turnOn(); |
| 71 | + headphones.setVolume(1); |
| 72 | + headphones.decreaseVolume(); |
| 73 | + assertEquals(0, headphones.getVolume(), "Volume should decrease to 0."); |
| 74 | + // Act |
| 75 | + headphones.decreaseVolume(); |
| 76 | + // Assert |
| 77 | + assertEquals(0, headphones.getVolume(), "Volume should not go lower than 0."); |
| 78 | + } |
| 79 | + |
| 80 | + @Test |
| 81 | + public void testSetColor() { |
| 82 | + // Act |
| 83 | + headphones.setColor(HeadPhoneColor.ROSEGOLD); |
| 84 | + // Assert |
| 85 | + assertEquals( |
| 86 | + HeadPhoneColor.ROSEGOLD, |
| 87 | + headphones.getHeadPhoneColor(), |
| 88 | + "Headphone color should be set to Rose Gold."); |
| 89 | + } |
| 90 | + |
| 91 | + @Test |
| 92 | + public void testPreferredBrand() { |
| 93 | + // Assert |
| 94 | + assertTrue( |
| 95 | + HeadPhones.BrandUtils.isPreferredBrand("Beats"), "Beats should be a preferred brand."); |
| 96 | + assertTrue(HeadPhones.BrandUtils.isPreferredBrand("Sony"), "Sony should be a preferred brand."); |
| 97 | + assertFalse( |
| 98 | + HeadPhones.BrandUtils.isPreferredBrand("Apple"), "Apple should not be a preferred brand."); |
| 99 | + assertTrue( |
| 100 | + HeadPhones.BrandUtils.isPreferredBrand("SkullCandy"), |
| 101 | + "SkullCandy should be a preferred brand."); |
| 102 | + assertTrue( |
| 103 | + HeadPhones.BrandUtils.isPreferredBrand("Juicy"), "Juicy should be a preferred brand."); |
| 104 | + } |
| 105 | + |
| 106 | + @Test |
| 107 | + public void testwirelessConnection() throws Exception { |
| 108 | + // Arrange |
| 109 | + headphones.turnOn(); |
| 110 | + // Act |
| 111 | + headphones.connectToBluetooth(); |
| 112 | + // Assert |
| 113 | + assertTrue( |
| 114 | + headphones.isConnectedToBluetooth(), |
| 115 | + "Headphones should connect to bluetooh when wireless and turned on."); |
| 116 | + } |
| 117 | + |
| 118 | + @Test |
| 119 | + public void testwirelessConnection_connectionNotFound() throws Exception { |
| 120 | + // Act |
| 121 | + assertThatThrownBy(() -> headphones.wirelessConnection()) |
| 122 | + .isInstanceOf(ConnectionNotFoundException.class) |
| 123 | + .hasMessage("Headphones Wireless Connection Not Found."); |
| 124 | + } |
| 125 | + |
| 126 | + @Test |
| 127 | + public void testBoostMode() { |
| 128 | + assertEquals( |
| 129 | + BoostMode.BASS_BOOST, |
| 130 | + headphones.getCurrentMode(), |
| 131 | + "BASS_BOOST should be the default setting."); |
| 132 | + |
| 133 | + headphones.nextBoostMode(); |
| 134 | + assertEquals( |
| 135 | + BoostMode.VOCAL_BOOST, |
| 136 | + headphones.getCurrentMode(), |
| 137 | + "The next Boost setting should be VOCAL."); |
| 138 | + |
| 139 | + headphones.nextBoostMode(); |
| 140 | + assertEquals( |
| 141 | + BoostMode.TREBLE_BOOST, |
| 142 | + headphones.getCurrentMode(), |
| 143 | + "The next Boost setting should be TREBLE."); |
| 144 | + |
| 145 | + headphones.nextBoostMode(); |
| 146 | + assertEquals( |
| 147 | + BoostMode.BASS_BOOST, |
| 148 | + headphones.getCurrentMode(), |
| 149 | + "BoostMode cycle should return to BASS_BOOST."); |
| 150 | + } |
| 151 | +} |
0 commit comments