Files
hpos/app/src/test/java/com/example/hpostesting/DeviceFragmentTest.kt
Pritimay Sarkar 7cf1d838ad add unit tests
2024-01-19 15:12:26 +05:30

83 lines
2.9 KiB
Kotlin

package com.example.hpostesting
import android.content.Context
import android.content.SharedPreferences
import androidx.fragment.app.testing.FragmentScenario
import androidx.fragment.app.testing.launchFragmentInContainer
import androidx.lifecycle.MutableLiveData
import androidx.test.core.app.ApplicationProvider
import com.example.hpostesting.data.constant.Constants
import com.example.hpostesting.data.constant.HemoCubeCommands
import com.example.hpostesting.presentation.deviceinfo.DeviceActivity
import com.example.hpostesting.presentation.UsbServiceListener
import com.example.hpostesting.presentation.hemocube.HemoCubeViewModel
import com.google.firebase.crashlytics.ktx.crashlytics
import com.google.firebase.ktx.Firebase
import `in`.sminnovations.hpostesting.databinding.FragmentDeviceBinding
import org.junit.After
import org.junit.Before
import org.junit.Test
import org.mockito.Mock
import org.mockito.Mockito.*
import org.mockito.MockitoAnnotations
class DeviceFragmentTest {
//
// @Mock
// private lateinit var sharedPreferences: SharedPreferences
//
// @Mock
// private lateinit var hemoCubeViewModel: HemoCubeViewModel
//
// private lateinit var scenario: FragmentScenario<DeviceFragment>
//
// @Before
// fun setup() {
// MockitoAnnotations.openMocks(this)
// // Mock the ViewModel and SharedPreferences
// `when`(hemoCubeViewModel.progressBar).thenReturn(MutableLiveData())
// `when`(hemoCubeViewModel.messages).thenReturn(MutableLiveData())
//
// `when`(sharedPreferences.getString(Constants.DEVICE_ID, "")).thenReturn("mockedDeviceId")
//
// // Launch the fragment in a test container
// scenario = launchFragmentInContainer(themeResId = R.style.Theme_AppCompat) {
// DeviceFragment().apply {
// deviceViewModel = hemoCubeViewModel
// }
// }
// scenario.onFragment { fragment ->
// fragment.sharedPreferences = sharedPreferences
// }
// }
//
// @After
// fun tearDown() {
// scenario.moveToState(Lifecycle.State.DESTROYED)
// }
//
// @Test
// fun testGetDeviceId() {
// // Mock UsbService and its behavior
// val usbService = (scenario.activity as DeviceActivity).mService
// `when`(usbService.sendAndListenToHemoCube(
// eq(HemoCubeCommands.DEVICE_CONFIGURATION_COMMAND),
// any(UsbServiceListener::class.java)
// )).thenAnswer {
// val listener = it.arguments[1] as UsbServiceListener
// listener.onUsbRead("MockedDeviceId".toByteArray())
// }
//
// // Trigger the method to be tested
// scenario.onFragment {
// it.getDeviceId()
// }
//
// // Verify that the shared preferences were updated
// verify(sharedPreferences.edit()).putString(Constants.DEVICE_ID, "MockedDeviceId")
// verify(sharedPreferences.edit()).apply()
// }
// Similar tests can be written for other methods
}