Skip to content

Commit

Permalink
#17 in progress - fix for failing pipeline
Browse files Browse the repository at this point in the history
  • Loading branch information
mjureczko committed Aug 7, 2024
1 parent 8204976 commit 2928e1f
Show file tree
Hide file tree
Showing 12 changed files with 90 additions and 52 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ android {
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'com.google.android.gms:play-services-ads:20.6.0'
implementation 'com.google.android.gms:play-services-ads:22.6.0'
implementation 'androidx.appcompat:appcompat:1.4.1'
implementation 'androidx.core:core-ktx:1.7.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.3'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,36 @@ import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.graphics.Typeface
import androidx.core.content.res.ResourcesCompat
import androidx.lifecycle.SavedStateHandle
import androidx.test.platform.app.InstrumentationRegistry
import kotlinx.coroutines.test.StandardTestDispatcher
import org.junit.Before
import pl.marianjureczko.poszukiwacz.R
import pl.marianjureczko.poszukiwacz.activity.facebook.n.FacebookViewModel
import pl.marianjureczko.poszukiwacz.activity.facebook.n.ROUTE_NAME
import pl.marianjureczko.poszukiwacz.model.Route
import pl.marianjureczko.poszukiwacz.model.TreasureDescription
import pl.marianjureczko.poszukiwacz.model.TreasuresProgress
import pl.marianjureczko.poszukiwacz.shared.StorageHelper

abstract class ReportAbstractTest {
val context: Context = InstrumentationRegistry.getInstrumentation().targetContext
val model: pl.marianjureczko.poszukiwacz.activity.facebook.FacebookViewModel =
pl.marianjureczko.poszukiwacz.activity.facebook.FacebookViewModel(SavedStateHandle(mapOf()))
val storageHelper: StorageHelper = StorageHelper(context)

val font: Typeface = ResourcesCompat.getFont(context, R.font.akaya_telivigala)!!
val treasuresProgress: TreasuresProgress = TreasuresProgress("123456789", TreasureDescription.nullObject())
val treasuresProgress: TreasuresProgress = TreasuresProgress(ROUTE_NAME, TreasureDescription.nullObject())

fun createFacebookViewModel() = FacebookViewModel(storageHelper, context.resources, StandardTestDispatcher())

fun saveEmptyProgress() = storageHelper.save(treasuresProgress)

fun expected(fileName: String): Bitmap {
val inputStream = InstrumentationRegistry.getInstrumentation().context.resources.assets.open(fileName)
return BitmapFactory.decodeStream(inputStream)
}

@Before
fun setup() {
val route = Route(ROUTE_NAME)
storageHelper.save(route)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import junit.framework.TestCase.assertTrue
import org.junit.After
import org.junit.Test
import org.junit.runner.RunWith
import pl.marianjureczko.poszukiwacz.activity.facebook.n.ReportCommemorativePhotos
import pl.marianjureczko.poszukiwacz.activity.facebook.n.ReportCommons
import pl.marianjureczko.poszukiwacz.model.Route
import pl.marianjureczko.poszukiwacz.shared.StorageHelper
import java.io.File
Expand All @@ -30,15 +32,15 @@ internal class ReportCommemorativePhotosTest : ReportAbstractTest() {
@Test
fun shouldDrawCommemorativePhotosInSingleLine_when2() {
//given
val bitmap = Bitmap.createBitmap(pl.marianjureczko.poszukiwacz.activity.facebook.ReportCommons.REPORT_WIDTH, 800, Bitmap.Config.ARGB_8888)
val bitmap = Bitmap.createBitmap(ReportCommons.REPORT_WIDTH, 800, Bitmap.Config.ARGB_8888)
val canvas = Canvas(bitmap)
canvas.drawColor(Color.WHITE)
treasuresProgress.commemorativePhotosByTreasuresDescriptionIds.put(1, tempPhoto(800, 400))
treasuresProgress.commemorativePhotosByTreasuresDescriptionIds.put(2, tempPhoto(600, 600))

storageHelper.save(treasuresProgress)
StorageHelper(context).save(Route(treasuresProgress.routeName))
model.initialize(context, null, treasuresProgress)
val reportPhotos = pl.marianjureczko.poszukiwacz.activity.facebook.ReportCommemorativePhotos(model, font, seed)
// model.initialize(context, null, treasuresProgress)
val reportPhotos = ReportCommemorativePhotos(createFacebookViewModel().state.value, font, seed)

//when
reportPhotos.draw(context, canvas, 0f)
Expand All @@ -52,15 +54,16 @@ internal class ReportCommemorativePhotosTest : ReportAbstractTest() {
@Test
fun shouldDrawCommemorativePhotosIn2Lines_when4() {
//given
val bitmap = Bitmap.createBitmap(pl.marianjureczko.poszukiwacz.activity.facebook.ReportCommons.REPORT_WIDTH, 800, Bitmap.Config.ARGB_8888)
val bitmap = Bitmap.createBitmap(ReportCommons.REPORT_WIDTH, 800, Bitmap.Config.ARGB_8888)
val canvas = Canvas(bitmap)
canvas.drawColor(Color.WHITE)
treasuresProgress.commemorativePhotosByTreasuresDescriptionIds.put(1, tempPhoto(800, 400))
treasuresProgress.commemorativePhotosByTreasuresDescriptionIds.put(2, tempPhoto(600, 600))
treasuresProgress.commemorativePhotosByTreasuresDescriptionIds.put(3, tempPhoto(400, 800))
treasuresProgress.commemorativePhotosByTreasuresDescriptionIds.put(4, tempPhoto(600, 500))
model.initialize(context, null, treasuresProgress)
val reportPhotos = pl.marianjureczko.poszukiwacz.activity.facebook.ReportCommemorativePhotos(model, font, seed)
storageHelper.save(treasuresProgress)
// model.initialize(context, null, treasuresProgress)
val reportPhotos = ReportCommemorativePhotos(createFacebookViewModel().state.value, font, seed)

//when
reportPhotos.draw(context, canvas, 0f)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import androidx.test.ext.junit.runners.AndroidJUnit4
import junit.framework.TestCase
import org.junit.Test
import org.junit.runner.RunWith
import pl.marianjureczko.poszukiwacz.activity.facebook.n.ReportCommons
import pl.marianjureczko.poszukiwacz.activity.facebook.n.ReportFooter
import java.io.File

// Requires phone with API 34
Expand All @@ -16,8 +18,8 @@ class ReportFooterTest : ReportAbstractTest() {
@Test
fun should_drawFooter() {
//given
val footer = pl.marianjureczko.poszukiwacz.activity.facebook.ReportFooter()
val bitmap = Bitmap.createBitmap(pl.marianjureczko.poszukiwacz.activity.facebook.ReportCommons.REPORT_WIDTH, 200, Bitmap.Config.ARGB_8888)
val footer = ReportFooter()
val bitmap = Bitmap.createBitmap(ReportCommons.REPORT_WIDTH, 200, Bitmap.Config.ARGB_8888)
val canvas = Canvas(bitmap)
canvas.drawColor(Color.WHITE)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,22 @@ package pl.marianjureczko.poszukiwacz.activity.facebook
import android.content.Context
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import androidx.lifecycle.SavedStateHandle
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.platform.app.InstrumentationRegistry
import kotlinx.coroutines.test.StandardTestDispatcher
import org.junit.Test
import org.junit.runner.RunWith
import pl.marianjureczko.poszukiwacz.activity.treasureselector.Coordinates
import pl.marianjureczko.poszukiwacz.activity.facebook.n.FacebookViewModel
import pl.marianjureczko.poszukiwacz.activity.facebook.n.ROUTE_NAME
import pl.marianjureczko.poszukiwacz.activity.facebook.n.ReportGenerator
import pl.marianjureczko.poszukiwacz.activity.facebook.n.Type
import pl.marianjureczko.poszukiwacz.model.HunterPath
import pl.marianjureczko.poszukiwacz.model.Route
import pl.marianjureczko.poszukiwacz.model.Treasure
import pl.marianjureczko.poszukiwacz.model.TreasureDescription
import pl.marianjureczko.poszukiwacz.model.TreasureType
import pl.marianjureczko.poszukiwacz.model.TreasuresProgress
import pl.marianjureczko.poszukiwacz.shared.Coordinates
import pl.marianjureczko.poszukiwacz.shared.StorageHelper
import java.io.File
import java.util.Date
Expand All @@ -24,33 +28,38 @@ class ReportGeneratorTest {
@Test
fun shouldCreateImage() {
//given
val report = pl.marianjureczko.poszukiwacz.activity.facebook.ReportGenerator()
val report = ReportGenerator()
val context = InstrumentationRegistry.getInstrumentation().targetContext
val storageHelper: StorageHelper = StorageHelper(context)
val photos = arrangePhotos(context)
val treasuresProgress = TreasuresProgress("123456789 123456789 123456789 123456789 12345", TreasureDescription.nullObject())
val treasuresProgress = TreasuresProgress(ROUTE_NAME, TreasureDescription.nullObject())
val treasure = Treasure("1", 7, TreasureType.DIAMOND)
treasuresProgress.collect(treasure)
treasuresProgress.commemorativePhotosByTreasuresDescriptionIds.put(1, photos[0])
treasuresProgress.commemorativePhotosByTreasuresDescriptionIds.put(2, photos[1])
treasuresProgress.commemorativePhotosByTreasuresDescriptionIds.put(3, photos[2])
treasuresProgress.commemorativePhotosByTreasuresDescriptionIds.put(13, photos[4])
treasuresProgress.commemorativePhotosByTreasuresDescriptionIds.put(0, photos[5])
val hunterPath = HunterPath()
hunterPath.addLocation(pl.marianjureczko.poszukiwacz.activity.treasureselector.Coordinates(10.0, 10.0), Date(1))
hunterPath.addLocation(pl.marianjureczko.poszukiwacz.activity.treasureselector.Coordinates(10.0, 11.0), Date(1_000_000))
hunterPath.addLocation(pl.marianjureczko.poszukiwacz.activity.treasureselector.Coordinates(10.0, 11.0), Date(2_000_000))
storageHelper.save(treasuresProgress)
val hunterPath = HunterPath(ROUTE_NAME)
hunterPath.addLocation(Coordinates(10.0, 10.0), Date(1))
hunterPath.addLocation(Coordinates(10.0, 11.0), Date(1_000_000))
hunterPath.addLocation(Coordinates(10.0, 11.0), Date(2_000_000))
storageHelper.save(hunterPath)
StorageHelper(context).save(Route(treasuresProgress.routeName))

//when
val model = pl.marianjureczko.poszukiwacz.activity.facebook.FacebookViewModel(SavedStateHandle(mapOf()))
model.initialize(context, hunterPath, treasuresProgress)
//MapBox doesn't work in tests
val mapIdx = model.elements.indices.find { model.elements[it].type == pl.marianjureczko.poszukiwacz.activity.facebook.Type.MAP }!!
val elements = model.elements.toMutableList()
elements[mapIdx] = model.elements[mapIdx].copy(isSelected = false)
model.elements = elements
val model = FacebookViewModel(StorageHelper(context), context.resources, StandardTestDispatcher())
// //SavedStateHandle(mapOf()))
// model.initialize(context, hunterPath, treasuresProgress)
// //MapBox doesn't work in tests
var state = model.state.value
val mapIdx = state.elements.indices.find { state.elements[it].type == Type.MAP }!!
val elements = state.elements.toMutableList()
elements[mapIdx] = state.elements[mapIdx].copy(isSelected = false)
state = state.copy(elements = elements)

val actual = report.create(context, model) {
val actual = report.create(context, state) {
// do nothing
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import androidx.test.ext.junit.runners.AndroidJUnit4
import junit.framework.TestCase
import org.junit.Test
import org.junit.runner.RunWith
import pl.marianjureczko.poszukiwacz.activity.facebook.n.ReportCommons
import pl.marianjureczko.poszukiwacz.activity.facebook.n.ReportMapHeader
import java.io.File

// Requires phone with API 34
Expand All @@ -15,11 +17,11 @@ class ReportMapHeaderTest : ReportAbstractTest() {
@Test
fun shouldDrawSummary() {
//given
val reportMapHeader = pl.marianjureczko.poszukiwacz.activity.facebook.ReportMapHeader(model, font)
val bitmap = Bitmap.createBitmap(pl.marianjureczko.poszukiwacz.activity.facebook.ReportCommons.REPORT_WIDTH, 200, Bitmap.Config.ARGB_8888)
val bitmap = Bitmap.createBitmap(ReportCommons.REPORT_WIDTH, 200, Bitmap.Config.ARGB_8888)
val canvas = Canvas(bitmap)
canvas.drawColor(Color.WHITE)
model.initialize(context, null, treasuresProgress)
saveEmptyProgress()
val reportMapHeader = ReportMapHeader(createFacebookViewModel().state.value, font)

//when
reportMapHeader.draw(context, canvas, 0f)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@ import androidx.test.ext.junit.runners.AndroidJUnit4
import junit.framework.TestCase
import org.junit.Test
import org.junit.runner.RunWith
import pl.marianjureczko.poszukiwacz.activity.treasureselector.Coordinates
import pl.marianjureczko.poszukiwacz.activity.facebook.n.ROUTE_NAME
import pl.marianjureczko.poszukiwacz.activity.facebook.n.ReportCommons
import pl.marianjureczko.poszukiwacz.activity.facebook.n.ReportMapSummary
import pl.marianjureczko.poszukiwacz.model.HunterPath
import pl.marianjureczko.poszukiwacz.shared.Coordinates
import java.io.File
import java.util.Date

Expand All @@ -19,15 +22,17 @@ class ReportMapSummaryTest : ReportAbstractTest() {
@Test
fun should_drawSummaryWithLengthAndTimestamps() {
//given
val reportMapHeader = pl.marianjureczko.poszukiwacz.activity.facebook.ReportMapSummary(model, font)
val bitmap = Bitmap.createBitmap(pl.marianjureczko.poszukiwacz.activity.facebook.ReportCommons.REPORT_WIDTH, 200, Bitmap.Config.ARGB_8888)
val bitmap = Bitmap.createBitmap(ReportCommons.REPORT_WIDTH, 200, Bitmap.Config.ARGB_8888)
val canvas = Canvas(bitmap)
canvas.drawColor(Color.WHITE)
val hunterPath = HunterPath()
hunterPath.addLocation(pl.marianjureczko.poszukiwacz.activity.treasureselector.Coordinates(10.0, 10.0), Date(1))
hunterPath.addLocation(pl.marianjureczko.poszukiwacz.activity.treasureselector.Coordinates(10.0, 11.0), Date(1_000_000))
hunterPath.addLocation(pl.marianjureczko.poszukiwacz.activity.treasureselector.Coordinates(10.0, 11.0), Date(2_000_000))
model.initialize(context, hunterPath, treasuresProgress)
val hunterPath = HunterPath(ROUTE_NAME)
hunterPath.addLocation(Coordinates(10.0, 10.0), Date(1))
hunterPath.addLocation(Coordinates(10.0, 11.0), Date(1_000_000))
hunterPath.addLocation(Coordinates(10.0, 11.0), Date(2_000_000))
// model.initialize(context, hunterPath, treasuresProgress)
saveEmptyProgress()
storageHelper.save(hunterPath)
val reportMapHeader = ReportMapSummary(createFacebookViewModel().state.value, font)

//when
reportMapHeader.draw(context, canvas, 0f)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import androidx.test.ext.junit.runners.AndroidJUnit4
import junit.framework.TestCase.assertTrue
import org.junit.Test
import org.junit.runner.RunWith
import pl.marianjureczko.poszukiwacz.activity.facebook.n.ReportCommons
import pl.marianjureczko.poszukiwacz.activity.facebook.n.ReportSummary
import pl.marianjureczko.poszukiwacz.model.Treasure
import pl.marianjureczko.poszukiwacz.model.TreasureType
import java.io.File
Expand All @@ -18,14 +20,14 @@ internal class ReportSummaryTest : ReportAbstractTest() {
@Test
fun shouldDrawSummary() {
//given
val reportSummary = pl.marianjureczko.poszukiwacz.activity.facebook.ReportSummary(model, font)
val bitmap = Bitmap.createBitmap(pl.marianjureczko.poszukiwacz.activity.facebook.ReportCommons.REPORT_WIDTH, 200, Bitmap.Config.ARGB_8888)
val bitmap = Bitmap.createBitmap(ReportCommons.REPORT_WIDTH, 200, Bitmap.Config.ARGB_8888)
val canvas = Canvas(bitmap)
canvas.drawColor(Color.WHITE)

val treasure = Treasure("1", 17, TreasureType.GOLD)
treasuresProgress.collect(treasure)
model.initialize(context, null, treasuresProgress)
storageHelper.save(treasuresProgress)
val reportSummary = ReportSummary(createFacebookViewModel().state.value, font)

//when
reportSummary.draw(context, canvas, 0f)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import androidx.test.ext.junit.runners.AndroidJUnit4
import org.junit.Assert
import org.junit.Test
import org.junit.runner.RunWith
import pl.marianjureczko.poszukiwacz.activity.treasureselector.Coordinates
import pl.marianjureczko.poszukiwacz.shared.Coordinates
import java.util.Date

@RunWith(AndroidJUnit4::class)
Expand All @@ -14,10 +14,10 @@ class HunterPathTest {
fun shouldCalculateDistance() {
//given
val path = HunterPath()
path.addLocation(pl.marianjureczko.poszukiwacz.activity.treasureselector.Coordinates(51.1428, 16.5254), Date(0))
path.addLocation(pl.marianjureczko.poszukiwacz.activity.treasureselector.Coordinates(51.1534, 16.54076), Date(100_000))
path.addLocation(pl.marianjureczko.poszukiwacz.activity.treasureselector.Coordinates(51.14499, 16.55419), Date(200_000))
path.addLocation(pl.marianjureczko.poszukiwacz.activity.treasureselector.Coordinates(51.14499, 16.55419), Date(300_000))
path.addLocation(Coordinates(51.1428, 16.5254), Date(0))
path.addLocation(Coordinates(51.1534, 16.54076), Date(100_000))
path.addLocation(Coordinates(51.14499, 16.55419), Date(200_000))
path.addLocation(Coordinates(51.14499, 16.55419), Date(300_000))

//when
val actual = path.pathLengthInKm()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ data class FacebookState(
override val route: Route,
val elements: List<ElementDescription>,
val recompose: Int = Random().nextInt()
) : pl.marianjureczko.poszukiwacz.activity.facebook.n.FacebookReportModel {
) : FacebookReportModel {
override fun getSummaryElement(): ElementDescription {
return elements[0]
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import pl.marianjureczko.poszukiwacz.shared.StorageHelper
import javax.inject.Inject

//TODO: should be configurable for the sake of classic version
private const val ROUTE_NAME = "custom"
const val ROUTE_NAME = "custom"

@HiltViewModel
class FacebookViewModel @Inject constructor(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ fun AdvertBanner(){
modifier = Modifier.fillMaxWidth(),
factory = { context ->
AdView(App.getAppContext()).apply {
adSize = AdSize.BANNER
setAdSize(AdSize.BANNER)
adUnitId = App.getResources().getString(R.string.main_ad)
loadAd(AdRequest.Builder().build())
}
Expand Down

0 comments on commit 2928e1f

Please sign in to comment.