Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: improve general code coverage #310

Merged
merged 8 commits into from
Dec 19, 2024
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package com.github.lookupgroup27.lookup.ui.overview

import androidx.compose.ui.test.*
import androidx.compose.ui.test.junit4.createComposeRule
import com.github.lookupgroup27.lookup.ui.navigation.NavigationActions
import org.junit.Rule
import org.junit.Test
import org.mockito.kotlin.mock

class SampleScreenTest {

@get:Rule val composeTestRule = createComposeRule()

private val mockNavigationActions: NavigationActions = mock()

@Test
fun sampleScreen_displaysCorrectText() {
// Set up test input
val testText = "Sample Screen Text"
val screenTag = "sampleScreenTag"
val backButtonTag = "backButtonTag"

// Render the composable
composeTestRule.setContent {
SampleScreen(
screenText = testText,
navigationActions = mockNavigationActions,
screenTag = screenTag,
backButtonTag = backButtonTag)
}

// Verify the screen text is displayed
composeTestRule.onNodeWithText(testText).assertIsDisplayed()
}

@Test
fun sampleScreen_hasCorrectTestTags() {
// Set up test input
val screenTag = "sampleScreenTag"
val backButtonTag = "backButtonTag"

// Render the composable
composeTestRule.setContent {
SampleScreen(
screenText = "Sample Screen Text",
navigationActions = mockNavigationActions,
screenTag = screenTag,
backButtonTag = backButtonTag)
}

// Verify the test tags are present
composeTestRule.onNodeWithTag(screenTag).assertIsDisplayed()
composeTestRule.onNodeWithTag(backButtonTag).assertIsDisplayed()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package com.github.lookupgroup27.lookup.ui.profile

import androidx.compose.ui.test.assertIsDisplayed
import androidx.compose.ui.test.junit4.createComposeRule
import androidx.compose.ui.test.onNodeWithText
import androidx.compose.ui.test.performClick
import com.github.lookupgroup27.lookup.R
import com.github.lookupgroup27.lookup.ui.profile.components.ChangeAvatarButton
import org.junit.Rule
import org.junit.Test

class ChangeAvatarButtonTest {

@get:Rule val composeTestRule = createComposeRule()

@Test
fun changeAvatarButton_isDisplayed_whenAvatarIsNotDefaultOrNull() {
// Set up the test
composeTestRule.setContent {
ChangeAvatarButton(
selectedAvatar = R.drawable.avatar1, isAvatarDefaultOrNull = false, onButtonClick = {})
}

// Verify the button is displayed
composeTestRule.onNodeWithText("Change Avatar").assertIsDisplayed()
}

@Test
fun changeAvatarButton_isNotDisplayed_whenAvatarIsDefaultOrNull() {
// Set up the test
composeTestRule.setContent {
ChangeAvatarButton(
selectedAvatar = R.drawable.default_profile_icon,
isAvatarDefaultOrNull = true,
onButtonClick = {})
}

// Verify the button does not exist
composeTestRule.onNodeWithText("Change Avatar").assertDoesNotExist()
}

@Test
fun changeAvatarButton_triggersOnClick_whenClicked() {
var isClicked = false

// Set up the test
composeTestRule.setContent {
ChangeAvatarButton(
selectedAvatar = R.drawable.avatar1,
isAvatarDefaultOrNull = false,
onButtonClick = { isClicked = true })
}

// Perform a click on the button
composeTestRule.onNodeWithText("Change Avatar").performClick()

// Verify the callback was invoked
assert(isClicked)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import android.hardware.SensorEvent
import android.hardware.SensorEventListener
import android.hardware.SensorManager
import android.opengl.Matrix
import android.util.Log

/**
* Represents a camera for handling movement and projection in our OpenGL World.
Expand Down Expand Up @@ -75,21 +74,5 @@ class Camera(private var fov: Float) : SensorEventListener {
}
}

override fun onAccuracyChanged(sensor: Sensor?, accuracy: Int) {
when (sensor?.type) {
Sensor.TYPE_ROTATION_VECTOR -> {
when (accuracy) {
SensorManager.SENSOR_STATUS_UNRELIABLE -> {
Log.w("SensorAccuracy", "Rotation vector sensor is unreliable")
}
SensorManager.SENSOR_STATUS_ACCURACY_LOW -> {
Log.i("SensorAccuracy", "Rotation vector sensor accuracy is low")
}
SensorManager.SENSOR_STATUS_ACCURACY_HIGH -> {
Log.d("SensorAccuracy", "Rotation vector sensor accuracy is high")
}
}
}
}
}
override fun onAccuracyChanged(sensor: Sensor?, accuracy: Int) = Unit
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.github.lookupgroup27.lookup.model.map

import android.hardware.Sensor
import android.hardware.SensorManager
import org.junit.Test
import org.mockito.Mockito.mock
import org.mockito.Mockito.times
import org.mockito.Mockito.verify

class CameraTest {

@Test
fun testOnAccuracyChanged() {
val sensor = mock(Sensor::class.java)
val camera = Camera(90f)

camera.onAccuracyChanged(sensor, SensorManager.SENSOR_STATUS_ACCURACY_HIGH)

// Since the method does nothing, we just verify that it can be called without exceptions
verify(sensor, times(0)).type // This is just to use the sensor mock
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.github.lookupgroup27.lookup.model.map.renderables.utils

import com.github.lookupgroup27.lookup.util.opengl.BufferUtils.toBuffer
import java.nio.ShortBuffer
import org.junit.Assert.assertArrayEquals
import org.junit.Test

class BufferUtilsTest {

@Test
fun testShortArrayToBuffer() {
// Input short array
val inputArray = shortArrayOf(1, 2, 3, 4, 5)

// Convert the array to a ShortBuffer
val buffer: ShortBuffer = inputArray.toBuffer()

// Verify the buffer's capacity
assert(buffer.capacity() == inputArray.size)

// Create an output array and read the buffer's contents into it
val outputArray = ShortArray(inputArray.size)
buffer.get(outputArray)

// Verify the output array matches the input array
assertArrayEquals(inputArray, outputArray)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package com.github.lookupgroup27.lookup.model.map.renderables.utils

import org.junit.Assert.assertArrayEquals
import org.junit.Assert.assertEquals
import org.junit.Test

class GeometryUtilsTest {

@Test
fun testGenerateCircularGeometry_vertices() {
val numSegments = 4
val geometryData = GeometryUtils.generateCircularGeometry(numSegments)

// Expected vertices for a 4-segment circle
val expectedVertices =
floatArrayOf(
0f,
0f,
0f, // Center vertex
1f,
0f,
0f, // First segment
0f,
1f,
0f, // Second segment
-1f,
0f,
0f, // Third segment
0f,
-1f,
0f, // Fourth segment
1f,
0f,
0f // Wrap around to the first segment
)

// Assert that the vertices match
assertArrayEquals(expectedVertices, geometryData.vertices, 0.001f)
}

@Test
fun testGenerateCircularGeometry_indices() {
val numSegments = 4
val geometryData = GeometryUtils.generateCircularGeometry(numSegments)

// Expected indices for a 4-segment circle
val expectedIndices =
shortArrayOf(
0,
1,
2, // Triangle 1
0,
2,
3, // Triangle 2
0,
3,
4, // Triangle 3
0,
4,
1 // Triangle 4 (wrap around)
)

// Assert that the indices match
assertArrayEquals(expectedIndices, geometryData.indices)
}

@Test
fun testGenerateCircularGeometry_numberOfVerticesAndIndices() {
val numSegments = 32
val geometryData = GeometryUtils.generateCircularGeometry(numSegments)

// The number of vertices should be numSegments + 2 (center + one for wraparound)
assertEquals(numSegments + 2, geometryData.vertices.size / 3)

// The number of indices should be numSegments * 3 (3 indices per triangle)
assertEquals(numSegments * 3, geometryData.indices.size)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.github.lookupgroup27.lookup.model.map.stars

import android.content.Context
import org.junit.Assert.assertEquals
import org.junit.Before
import org.junit.Test
import org.mockito.Mockito.*

class StarsLoaderTest {

private lateinit var mockContext: Context
private lateinit var mockRepository: StarDataRepository
private lateinit var starsLoader: StarsLoader

@Before
fun setUp() {
mockContext = mock(Context::class.java)
mockRepository = mock(StarDataRepository::class.java)
starsLoader = StarsLoader(mockContext, mockRepository)
}

@Test
fun testLoadStars_callsUpdateStarPositions() {
// Arrange: Set up the mock repository
`when`(mockRepository.getUpdatedStars()).thenReturn(emptyList())

// Act: Call loadStars
starsLoader.loadStars()

// Assert: Verify that updateStarPositions was called
verify(mockRepository).updateStarPositions()
}

@Test
fun testLoadStars_handlesEmptyStarDataList() {
// Arrange: Return an empty star data list
`when`(mockRepository.getUpdatedStars()).thenReturn(emptyList())

// Act: Call loadStars
val stars = starsLoader.loadStars()

// Assert: Verify the result is an empty list
assertEquals(0, stars.size)
}
}
Loading