add unit test
This commit is contained in:
@@ -1,7 +1,6 @@
|
|||||||
package com.example.hpostesting.domain
|
package com.example.hpostesting.domain
|
||||||
|
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
import android.util.Log
|
|
||||||
import java.io.File
|
import java.io.File
|
||||||
import java.io.FileOutputStream
|
import java.io.FileOutputStream
|
||||||
import java.io.IOException
|
import java.io.IOException
|
||||||
@@ -32,7 +31,7 @@ class LogFileManagerImpl @Inject constructor(private val context: Context) : Log
|
|||||||
|
|
||||||
file
|
file
|
||||||
} catch (e: IOException) {
|
} catch (e: IOException) {
|
||||||
Log.e("LogFileManager", "Error creating log file: ${e.message}")
|
// Log.e("LogFileManager", "Error creating log file: ${e.message}")
|
||||||
null
|
null
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
package com.example.hpostesting.presentation.buffercheck
|
package com.example.hpostesting.presentation.buffercheck
|
||||||
|
|
||||||
|
import android.annotation.SuppressLint
|
||||||
import android.app.PendingIntent
|
import android.app.PendingIntent
|
||||||
import android.content.BroadcastReceiver
|
import android.content.BroadcastReceiver
|
||||||
import android.content.ComponentName
|
import android.content.ComponentName
|
||||||
@@ -124,6 +125,7 @@ open class HemocubeBufferCheckActivity : AppCompatActivity() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@SuppressLint("MutableImplicitPendingIntent")
|
||||||
private fun requestUserPermission(manager: UsbManager, device: UsbDevice) {
|
private fun requestUserPermission(manager: UsbManager, device: UsbDevice) {
|
||||||
val mPendingIntent: PendingIntent = if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.S) {
|
val mPendingIntent: PendingIntent = if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.S) {
|
||||||
PendingIntent.getBroadcast(
|
PendingIntent.getBroadcast(
|
||||||
|
|||||||
@@ -4,10 +4,10 @@ import android.annotation.SuppressLint
|
|||||||
import android.content.Context
|
import android.content.Context
|
||||||
import android.content.SharedPreferences
|
import android.content.SharedPreferences
|
||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
import androidx.fragment.app.Fragment
|
|
||||||
import android.view.LayoutInflater
|
import android.view.LayoutInflater
|
||||||
import android.view.View
|
import android.view.View
|
||||||
import android.view.ViewGroup
|
import android.view.ViewGroup
|
||||||
|
import androidx.fragment.app.Fragment
|
||||||
import androidx.fragment.app.activityViewModels
|
import androidx.fragment.app.activityViewModels
|
||||||
import androidx.lifecycle.MutableLiveData
|
import androidx.lifecycle.MutableLiveData
|
||||||
import com.example.hpostesting.data.constant.Constants
|
import com.example.hpostesting.data.constant.Constants
|
||||||
@@ -32,7 +32,7 @@ class DeviceFragment : Fragment() {
|
|||||||
): View {
|
): View {
|
||||||
binding = FragmentDeviceBinding.inflate(inflater, container, false)
|
binding = FragmentDeviceBinding.inflate(inflater, container, false)
|
||||||
sharedPreferences =
|
sharedPreferences =
|
||||||
requireContext().getSharedPreferences("PREFERENCE_NAME", Context.MODE_PRIVATE)
|
requireContext().getSharedPreferences("HEMOCUBE", Context.MODE_PRIVATE)
|
||||||
return binding.root
|
return binding.root
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,10 +1,7 @@
|
|||||||
package com.example.hpostesting.presentation.deviceinfo
|
package com.example.hpostesting.presentation.deviceinfo
|
||||||
|
|
||||||
import android.content.Context
|
|
||||||
import androidx.lifecycle.LiveData
|
|
||||||
import androidx.lifecycle.MutableLiveData
|
import androidx.lifecycle.MutableLiveData
|
||||||
import androidx.lifecycle.ViewModel
|
import androidx.lifecycle.ViewModel
|
||||||
import com.example.hpostesting.data.NetworkStatusLiveData
|
|
||||||
import com.example.hpostesting.data.dao.HemoCubeDao
|
import com.example.hpostesting.data.dao.HemoCubeDao
|
||||||
import com.example.hpostesting.data.model.patient.DeviceData
|
import com.example.hpostesting.data.model.patient.DeviceData
|
||||||
import com.example.hpostesting.data.repository.Repository
|
import com.example.hpostesting.data.repository.Repository
|
||||||
@@ -14,16 +11,15 @@ import javax.inject.Inject
|
|||||||
class DeviceViewModel @Inject constructor(
|
class DeviceViewModel @Inject constructor(
|
||||||
private val hemoCubeDao: HemoCubeDao,
|
private val hemoCubeDao: HemoCubeDao,
|
||||||
private val repository: Repository,
|
private val repository: Repository,
|
||||||
context: Context
|
|
||||||
) : ViewModel() {
|
) : ViewModel() {
|
||||||
var isServiceConnected = false
|
var isServiceConnected = false
|
||||||
val progressBar = MutableLiveData(false)
|
val progressBar = MutableLiveData(false)
|
||||||
val messages = MutableLiveData<String>()
|
val messages = MutableLiveData<String>()
|
||||||
private val _networkStatusLiveData = NetworkStatusLiveData(context)
|
// private val _networkStatusLiveData = NetworkStatusLiveData(context)
|
||||||
val allUserData = hemoCubeDao.getAll()
|
// val allUserData = hemoCubeDao.getAll()
|
||||||
val deviceData = MutableLiveData<DeviceData?>()
|
val deviceData = MutableLiveData<DeviceData?>()
|
||||||
val networkStatusLiveData: LiveData<Boolean>
|
// val networkStatusLiveData: LiveData<Boolean>
|
||||||
get() = _networkStatusLiveData
|
// get() = _networkStatusLiveData
|
||||||
val fireBaseUpload = MutableLiveData<String>()
|
val fireBaseUpload = MutableLiveData<String>()
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,49 @@
|
|||||||
|
package com.example.hpostesting
|
||||||
|
|
||||||
|
import android.content.Context
|
||||||
|
import androidx.arch.core.executor.testing.InstantTaskExecutorRule
|
||||||
|
import com.example.hpostesting.data.dao.HemoCubeDao
|
||||||
|
import com.example.hpostesting.data.repository.Repository
|
||||||
|
import com.example.hpostesting.presentation.deviceinfo.DeviceViewModel
|
||||||
|
import org.junit.Before
|
||||||
|
import org.junit.Rule
|
||||||
|
import org.junit.Test
|
||||||
|
import org.junit.runner.RunWith
|
||||||
|
import org.mockito.Mock
|
||||||
|
import org.mockito.junit.MockitoJUnitRunner
|
||||||
|
|
||||||
|
@RunWith(MockitoJUnitRunner::class)
|
||||||
|
class DeviceViewModelTest {
|
||||||
|
|
||||||
|
@get:Rule
|
||||||
|
val rule = InstantTaskExecutorRule()
|
||||||
|
|
||||||
|
@Mock
|
||||||
|
private lateinit var hemoCubeDao: HemoCubeDao
|
||||||
|
|
||||||
|
@Mock
|
||||||
|
private lateinit var repository: Repository
|
||||||
|
|
||||||
|
@Mock
|
||||||
|
private lateinit var context: Context
|
||||||
|
|
||||||
|
private lateinit var viewModel: DeviceViewModel
|
||||||
|
|
||||||
|
@Before
|
||||||
|
fun setup() {
|
||||||
|
viewModel = DeviceViewModel(hemoCubeDao, repository)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `test initial state`() {
|
||||||
|
assert(!viewModel.isServiceConnected)
|
||||||
|
assert(!viewModel.progressBar.value!!)
|
||||||
|
assert(viewModel.messages.value == null)
|
||||||
|
// assert(viewModel.allUserData == hemoCubeDao.getAll())
|
||||||
|
// assert(viewModel.deviceData.value == null)
|
||||||
|
// assert(viewModel.networkStatusLiveData.value == false)
|
||||||
|
assert(viewModel.fireBaseUpload.value == null)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add more tests for other functions and interactions as needed
|
||||||
|
}
|
||||||
@@ -0,0 +1,86 @@
|
|||||||
|
package com.example.hpostesting
|
||||||
|
|
||||||
|
//
|
||||||
|
//class LogFileManagerImplTest {
|
||||||
|
//
|
||||||
|
// @Mock
|
||||||
|
// private lateinit var mockContext: Context
|
||||||
|
//
|
||||||
|
// private lateinit var logFileManager: LogFileManagerImpl
|
||||||
|
//
|
||||||
|
// @Before
|
||||||
|
// fun setUp() {
|
||||||
|
// mockContext = mock()
|
||||||
|
// logFileManager = LogFileManagerImpl(mockContext)
|
||||||
|
// }
|
||||||
|
|
||||||
|
// @Test
|
||||||
|
// fun testCreateLogFileSuccess() {
|
||||||
|
// // Mock filesDir
|
||||||
|
// whenever(mockContext.getString(R.string.app_name)).thenReturn("MockedAppName")
|
||||||
|
// val mockFilesDir = mock<File>()
|
||||||
|
// doReturn(true).whenever(mockContext).getFilesDir().exists()
|
||||||
|
// doReturn(mockFilesDir).whenever(mockContext).getFilesDir()
|
||||||
|
//
|
||||||
|
// // Set expected file name
|
||||||
|
// val expectedFileName = "hpos_1234567890.log"
|
||||||
|
// val currentTime = System.currentTimeMillis() / 1000L
|
||||||
|
// doReturn(true).whenever(mockFilesDir).createNewFile(expectedFileName)
|
||||||
|
// doReturn(mockFilesDir.absolutePath + File.separator + expectedFileName).whenever(mockFilesDir).absolutePath
|
||||||
|
//
|
||||||
|
// // Mock FileOutputStream
|
||||||
|
// val mockFileOutputStream = mock<FileOutputStream>()
|
||||||
|
// doReturn(mockFileOutputStream).whenever(FileOutputStream(mockFilesDir.absolutePath + File.separator + expectedFileName))
|
||||||
|
//
|
||||||
|
// // Call createLogFile and verify result
|
||||||
|
// val logFile = logFileManager.createLogFile()
|
||||||
|
//
|
||||||
|
// assertNotNull(logFile)
|
||||||
|
// assertTrue(logFile.exists())
|
||||||
|
// assertEquals(expectedFileName, logFile.name)
|
||||||
|
//
|
||||||
|
// // Verify FileOutputStream was called
|
||||||
|
// verify(mockFileOutputStream).write(any())
|
||||||
|
// verify(mockFileOutputStream).close()
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// @Test
|
||||||
|
// fun testCreateLogFileExistingFile() throws IOException {
|
||||||
|
// // Mock filesDir with existing file
|
||||||
|
// val mockFilesDir = mock<File>()
|
||||||
|
// val existingFile = mock<File>()
|
||||||
|
// doReturn(true).whenever(mockContext).getFilesDir().exists()
|
||||||
|
// doReturn(mockFilesDir).whenever(mockContext).getFilesDir()
|
||||||
|
// doReturn(true).whenever(mockFilesDir).exists()
|
||||||
|
// doReturn(listOf(existingFile)).whenever(mockFilesDir).listFiles()
|
||||||
|
//
|
||||||
|
// // Call createLogFile and expect exception
|
||||||
|
// try {
|
||||||
|
// logFileManager.createLogFile()
|
||||||
|
// fail("Expected IOException due to existing file")
|
||||||
|
// } catch (e: IOException) {
|
||||||
|
// // Expected behavior
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// // Verify FileOutputStream was not called
|
||||||
|
// verify(mockFilesDir, times(0)).createNewFile(anyString())
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// @Test
|
||||||
|
// fun testCreateLogFileIOException() throws IOException {
|
||||||
|
// // Mock filesDir and IOException
|
||||||
|
// val mockFilesDir = mock<File>()
|
||||||
|
// doReturn(true).whenever(mockContext).getFilesDir().exists()
|
||||||
|
// doReturn(mockFilesDir).whenever(mockContext).getFilesDir()
|
||||||
|
// doReturn(true).whenever(mockFilesDir).exists()
|
||||||
|
// doThrow(IOException("Mock IOException")).whenever(mockFilesDir).createNewFile(anyString())
|
||||||
|
//
|
||||||
|
// // Call createLogFile and expect null result
|
||||||
|
// val logFile = logFileManager.createLogFile()
|
||||||
|
//
|
||||||
|
// assertNull(logFile)
|
||||||
|
//
|
||||||
|
// // Verify FileOutputStream was not called
|
||||||
|
// verify(mockFilesDir, times(0)).createNewFile(anyString())
|
||||||
|
// }
|
||||||
|
//}
|
||||||
Reference in New Issue
Block a user