-
Notifications
You must be signed in to change notification settings - Fork 0
🧪 [Add tests for UpdateChecker error handling] #193
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
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
162 changes: 162 additions & 0 deletions
162
android/app/src/test/java/com/clhs/score/data/UpdateCheckerTest.kt.orig
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,162 @@ | ||
| package com.clhs.score.data | ||
|
|
||
| import kotlinx.coroutines.test.runTest | ||
| import okhttp3.OkHttpClient | ||
| import okhttp3.mockwebserver.MockResponse | ||
|
Comment on lines
+1
to
+5
|
||
| import okhttp3.mockwebserver.MockWebServer | ||
| import org.junit.After | ||
| import org.junit.Assert.assertEquals | ||
| import org.junit.Assert.assertNull | ||
| import org.junit.Assert.assertTrue | ||
| import org.junit.Before | ||
| import org.junit.Test | ||
|
|
||
| class UpdateCheckerTest { | ||
| private lateinit var server: MockWebServer | ||
|
|
||
| @Before | ||
| fun setUp() { | ||
| server = MockWebServer() | ||
| server.start() | ||
| } | ||
|
|
||
| @After | ||
| fun tearDown() { | ||
| server.shutdown() | ||
| } | ||
|
|
||
| @Test | ||
| fun newerReleaseReturnsValidatedDownloadUrl() = runTest { | ||
| server.enqueue( | ||
| jsonResponse( | ||
| """ | ||
| { | ||
| "tag_name": "v1.2.0", | ||
| "html_url": "https://github.com/alvin000009238/clhs_score/releases/tag/v1.2.0", | ||
| "body": "Bug fixes", | ||
| "assets": [ | ||
| { | ||
| "name": "clhs-score.apk", | ||
| "browser_download_url": "https://github.com/alvin000009238/clhs_score/releases/download/v1.2.0/app.apk" | ||
| } | ||
| ] | ||
| } | ||
| """.trimIndent(), | ||
| ), | ||
| ) | ||
|
|
||
| val result = checker().check("1.1.9") | ||
|
|
||
| result as UpdateResult.NewVersion | ||
| assertEquals("1.2.0", result.versionName) | ||
| assertEquals("Bug fixes", result.releaseNotes) | ||
| assertEquals( | ||
| "https://github.com/alvin000009238/clhs_score/releases/download/v1.2.0/app.apk", | ||
| result.apkDownloadUrl, | ||
| ) | ||
| } | ||
|
|
||
| @Test | ||
| fun sameVersionReturnsUpToDate() = runTest { | ||
| server.enqueue( | ||
| jsonResponse( | ||
| """ | ||
| { | ||
| "tag_name": "v1.2.0", | ||
| "html_url": "https://github.com/alvin000009238/clhs_score/releases/tag/v1.2.0", | ||
| "body": "", | ||
| "assets": [] | ||
| } | ||
| """.trimIndent(), | ||
| ), | ||
| ) | ||
|
|
||
| assertEquals(UpdateResult.UpToDate, checker().check("1.2.0")) | ||
| } | ||
|
|
||
| @Test | ||
| fun invalidReleaseLinksReturnErrorInsteadOfActionableUpdate() = runTest { | ||
| server.enqueue( | ||
| jsonResponse( | ||
| """ | ||
| { | ||
| "tag_name": "v1.2.0", | ||
| "html_url": "javascript:alert(1)", | ||
| "body": "", | ||
| "assets": [ | ||
| { | ||
| "name": "clhs-score.apk", | ||
| "browser_download_url": "file:///tmp/app.apk" | ||
| } | ||
| ] | ||
| } | ||
| """.trimIndent(), | ||
| ), | ||
| ) | ||
|
|
||
| val result = checker().check("1.1.9") | ||
|
|
||
| result as UpdateResult.Error | ||
| assertEquals("更新連結格式不正確", result.message) | ||
| } | ||
|
|
||
| @Test | ||
| fun invalidApkUrlFallsBackToValidHtmlUrl() = runTest { | ||
| server.enqueue( | ||
| jsonResponse( | ||
| """ | ||
| { | ||
| "tag_name": "v1.2.0", | ||
| "html_url": "https://github.com/alvin000009238/clhs_score/releases/tag/v1.2.0", | ||
| "body": "", | ||
| "assets": [ | ||
| { | ||
| "name": "clhs-score.apk", | ||
| "browser_download_url": "intent://download" | ||
| } | ||
| ] | ||
| } | ||
| """.trimIndent(), | ||
| ), | ||
| ) | ||
|
|
||
| val result = checker().check("1.1.9") | ||
|
|
||
| result as UpdateResult.NewVersion | ||
| assertNull(result.apkDownloadUrl) | ||
| assertTrue(result.htmlUrl.startsWith("https://github.com/")) | ||
| } | ||
|
|
||
| @Test | ||
| fun networkExceptionReturnsError() = runTest { | ||
| val exceptionClient = OkHttpClient.Builder() | ||
| .addInterceptor { chain -> throw java.io.IOException("Network failed") } | ||
| .build() | ||
|
|
||
| val checker = UpdateChecker( | ||
| client = exceptionClient, | ||
| latestReleaseUrl = server.url("/release/latest").toString(), | ||
| ) | ||
|
|
||
| assertEquals(UpdateResult.Error("Network failed"), checker.check("1.1.9")) | ||
| } | ||
|
|
||
| @Test | ||
| fun nonSuccessfulHttpResponseReturnsError() = runTest { | ||
| server.enqueue(MockResponse().setResponseCode(404)) | ||
|
|
||
| assertEquals(UpdateResult.Error("HTTP 404"), checker().check("1.1.9")) | ||
| } | ||
|
|
||
| private fun checker(): UpdateChecker = | ||
| UpdateChecker( | ||
| client = OkHttpClient(), | ||
| latestReleaseUrl = server.url("/release/latest").toString(), | ||
| ) | ||
|
|
||
| private fun jsonResponse(body: String): MockResponse = | ||
| MockResponse() | ||
| .setResponseCode(200) | ||
| .setHeader("Content-Type", "application/json") | ||
| .setBody(body) | ||
| } | ||
28 changes: 28 additions & 0 deletions
28
android/app/src/test/java/com/clhs/score/data/UpdateCheckerTest.kt.rej
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| --- UpdateCheckerTest.kt | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| +++ UpdateCheckerTest.kt | ||
| @@ -137,14 +137,14 @@ | ||
| latestReleaseUrl = server.url("/release/latest").toString(), | ||
| ) | ||
|
|
||
|
Comment on lines
+1
to
+6
|
||
| - assertEquals(UpdateResult.Error("Network failed"), checker.check("1.1.9")) | ||
| + assertNull(checker.check("1.1.9")) | ||
| } | ||
|
|
||
| @Test | ||
| fun nonSuccessfulHttpResponseReturnsError() = runTest { | ||
| server.enqueue(MockResponse().setResponseCode(404)) | ||
|
|
||
| - assertEquals(UpdateResult.Error("HTTP 404"), checker().check("1.1.9")) | ||
| + assertNull(checker().check("1.1.9")) | ||
| } | ||
|
|
||
| @Test | ||
| @@ -158,7 +158,7 @@ | ||
| latestReleaseUrl = server.url("/release/latest").toString(), | ||
| ) | ||
|
|
||
| - assertEquals(UpdateResult.Error("未知錯誤"), checker.check("1.1.9")) | ||
| + assertNull(checker.check("1.1.9")) | ||
| } | ||
|
|
||
| private fun checker(): UpdateChecker = | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| --- android/app/src/test/java/com/clhs/score/data/UpdateCheckerTest.kt | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| +++ android/app/src/test/java/com/clhs/score/data/UpdateCheckerTest.kt | ||
| @@ -128,6 +128,20 @@ | ||
| assertEquals(UpdateResult.Error("HTTP 404"), checker().check("1.1.9")) | ||
| } | ||
|
Comment on lines
+1
to
+5
|
||
|
|
||
| + @Test | ||
| + fun networkExceptionWithoutMessageReturnsDefaultError() = runTest { | ||
| + val exceptionClient = OkHttpClient.Builder() | ||
| + .addInterceptor { chain -> throw java.io.IOException() } | ||
| + .build() | ||
| + | ||
| + val checker = UpdateChecker( | ||
| + client = exceptionClient, | ||
| + latestReleaseUrl = server.url("/release/latest").toString(), | ||
| + ) | ||
| + | ||
| + assertEquals(UpdateResult.Error("未知錯誤"), checker.check("1.1.9")) | ||
| + } | ||
| + | ||
| private fun checker(): UpdateChecker = | ||
| UpdateChecker( | ||
| client = OkHttpClient(), | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This file (UpdateCheckerTest.kt.orig) is a backup file created during conflict resolution or patching. It should be removed from the repository.