Skip to content

Commit

Permalink
Added Tests For ConsoleLogger
Browse files Browse the repository at this point in the history
  • Loading branch information
manas-yu committed Oct 9, 2024
1 parent 02a769c commit a637527
Showing 1 changed file with 36 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import org.oppia.android.util.data.DataProvidersInjectorProvider
import org.oppia.android.util.locale.testing.LocaleTestModule
import org.robolectric.annotation.Config
import org.robolectric.annotation.LooperMode
import java.io.File
import javax.inject.Inject
import javax.inject.Singleton

Expand All @@ -54,9 +55,13 @@ class ConsoleLoggerTest {
@field:[Inject BackgroundTestDispatcher]
lateinit var backgroundTestDispatcher: TestCoroutineDispatcher

private lateinit var logFile: File

@Before
fun setUp() {
setUpTestApplicationComponent()
logFile = File(context.filesDir, "oppia_app.log")
logFile.delete() // Ensure we start with a clean log file
}

@Test
Expand All @@ -76,6 +81,35 @@ class ConsoleLoggerTest {
assertThat(firstErrorContext.fullErrorLog).isEqualTo(testMessage)
}

@Test
fun testConsoleLogger_multipleLogCalls_appendsToFile() {
consoleLogger.e(testTag, "Error 1")
testCoroutineDispatchers.advanceUntilIdle()
consoleLogger.e(testTag, "Error 2")
testCoroutineDispatchers.advanceUntilIdle()

val logContent = logFile.readText()
assertThat(logContent).contains("Error 1")
assertThat(logContent).contains("Error 2")
assertThat(logContent.indexOf("Error 1")).isLessThan(logContent.indexOf("Error 2"))
}

@Test
fun testConsoleLogger_closeAndReopen_continuesToAppend() {
consoleLogger.e(testTag, "Error before close")
testCoroutineDispatchers.advanceUntilIdle()
consoleLogger.closeLogFile()
testCoroutineDispatchers.advanceUntilIdle()

consoleLogger.e(testTag, "Error after reopen")
testCoroutineDispatchers.advanceUntilIdle()

val logContent = logFile.readText()
assertThat(logContent).contains("Error before close")
assertThat(logContent).contains("Error after reopen")
assertThat(logContent.indexOf("Error before close")).isLessThan(logContent.indexOf("Error after reopen"))
}

private fun setUpTestApplicationComponent() {
ApplicationProvider.getApplicationContext<TestApplication>().inject(this)
}
Expand All @@ -96,7 +130,7 @@ class ConsoleLoggerTest {
@Provides
@Singleton
@EnableFileLog
fun provideEnableFileLog(): Boolean = false
fun provideEnableFileLog(): Boolean = true

@Provides
@Singleton
Expand All @@ -113,7 +147,6 @@ class ConsoleLoggerTest {
FakeOppiaClockModule::class,
]
)

interface TestApplicationComponent : DataProvidersInjector {
@Component.Builder
interface Builder {
Expand Down Expand Up @@ -143,4 +176,4 @@ class ConsoleLoggerTest {

override fun getDataProvidersInjector(): DataProvidersInjector = component
}
}
}

0 comments on commit a637527

Please sign in to comment.