-
Notifications
You must be signed in to change notification settings - Fork 47
add Brush Designer ViewModel, preview, and app wiring #59
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,3 +15,4 @@ | |
| .externalNativeBuild | ||
| .cxx | ||
| local.properties | ||
| ink-proto/bin/ | ||
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
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
155 changes: 155 additions & 0 deletions
155
...t/java/com/example/cahier/developer/brushdesigner/viewmodel/BrushDesignerViewModelTest.kt
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,155 @@ | ||
| package com.example.cahier.developer.brushdesigner.viewmodel | ||
|
|
||
| import android.content.Context | ||
| import android.graphics.Bitmap | ||
| import androidx.test.core.app.ApplicationProvider | ||
| import com.example.cahier.developer.brushdesigner.data.BrushDesignerRepository | ||
| import com.example.cahier.developer.brushdesigner.data.CustomBrushDao | ||
| import dagger.hilt.android.testing.HiltAndroidRule | ||
| import dagger.hilt.android.testing.HiltAndroidTest | ||
| import kotlinx.coroutines.Dispatchers | ||
| import kotlinx.coroutines.ExperimentalCoroutinesApi | ||
| import kotlinx.coroutines.flow.first | ||
| import kotlinx.coroutines.test.UnconfinedTestDispatcher | ||
| import kotlinx.coroutines.test.resetMain | ||
| import kotlinx.coroutines.test.runTest | ||
| import kotlinx.coroutines.test.setMain | ||
| import org.junit.After | ||
| import org.junit.Assert.assertEquals | ||
| import org.junit.Assert.assertNotNull | ||
| import org.junit.Assert.assertNull | ||
| import org.junit.Assert.assertTrue | ||
| import org.junit.Before | ||
| import org.junit.Rule | ||
| import org.junit.Test | ||
| import javax.inject.Inject | ||
|
|
||
| @OptIn(ExperimentalCoroutinesApi::class) | ||
| @HiltAndroidTest | ||
| class BrushDesignerViewModelTest { | ||
|
|
||
| @get:Rule | ||
| var hiltRule = HiltAndroidRule(this) | ||
|
|
||
| private val testDispatcher = UnconfinedTestDispatcher() | ||
|
|
||
| @Inject | ||
| lateinit var customBrushDao: CustomBrushDao | ||
|
|
||
| @Inject | ||
| lateinit var repository: BrushDesignerRepository | ||
|
|
||
| private lateinit var viewModel: BrushDesignerViewModel | ||
|
|
||
| @Before | ||
| fun setup() { | ||
| hiltRule.inject() | ||
| Dispatchers.setMain(testDispatcher) | ||
|
|
||
| val context = ApplicationProvider.getApplicationContext<Context>() | ||
| viewModel = BrushDesignerViewModel(context, repository, customBrushDao) | ||
| } | ||
|
|
||
| @After | ||
| fun tearDown() { | ||
| Dispatchers.resetMain() | ||
| } | ||
|
|
||
| @Test | ||
| fun updateTip_modifies_correct_coat_index() = runTest { | ||
| assertEquals( | ||
| 1.0f, | ||
| viewModel.activeBrushProto.value.getCoats(0).tip.scaleX, | ||
| 0.01f | ||
| ) | ||
|
|
||
| viewModel.updateTip { it.setScaleX(2.5f) } | ||
|
|
||
| assertEquals( | ||
| 2.5f, | ||
| viewModel.activeBrushProto.value.getCoats(0).tip.scaleX, | ||
| 0.01f | ||
| ) | ||
| } | ||
|
|
||
| @Test | ||
| fun addBehavior_stacks_multiple_rules() = runTest { | ||
| viewModel.clearBehaviors() | ||
| assertEquals( | ||
| 0, | ||
| viewModel.activeBrushProto.value.getCoats(0).tip.behaviorsCount | ||
| ) | ||
|
|
||
| val node1 = ink.proto.BrushBehavior.Node.newBuilder().setConstantNode( | ||
| ink.proto.BrushBehavior.ConstantNode.newBuilder().setValue(1f) | ||
| ).build() | ||
| viewModel.addBehavior(listOf(node1)) | ||
|
|
||
| val node2 = ink.proto.BrushBehavior.Node.newBuilder().setConstantNode( | ||
| ink.proto.BrushBehavior.ConstantNode.newBuilder().setValue(2f) | ||
| ).build() | ||
| viewModel.addBehavior(listOf(node2)) | ||
|
|
||
| val behaviors = viewModel.activeBrushProto.value.getCoats(0).tip.behaviorsList | ||
| assertEquals(2, behaviors.size) | ||
| } | ||
|
|
||
| @Test | ||
| fun addNewCoat_increments_count_and_switches_selection() = runTest { | ||
| assertEquals(1, viewModel.activeBrushProto.value.coatsCount) | ||
| assertEquals(0, viewModel.selectedCoatIndex.value) | ||
|
|
||
| viewModel.addNewCoat() | ||
|
|
||
| assertEquals(2, viewModel.activeBrushProto.value.coatsCount) | ||
| assertEquals(1, viewModel.selectedCoatIndex.value) | ||
| } | ||
|
|
||
| @Test | ||
| fun saveToPalette_persists_to_dao() = runTest { | ||
| val brushName = "Test Persistence Brush" | ||
| viewModel.updateClientBrushFamilyId("test-id") | ||
|
|
||
| viewModel.saveToPalette(brushName).join() | ||
|
|
||
| val savedBrushes = customBrushDao.getAllCustomBrushes().first() | ||
| assertTrue(savedBrushes.any { it.name == brushName }) | ||
| } | ||
|
|
||
| @Test | ||
| fun previewBrushFamily_is_null_on_invalid_proto() = runTest { | ||
| val invalidRepo = BrushDesignerRepository() | ||
| invalidRepo.updateActiveBrushProto(ink.proto.BrushFamily.newBuilder().build()) | ||
|
|
||
| val vm = BrushDesignerViewModel( | ||
| ApplicationProvider.getApplicationContext(), | ||
| invalidRepo, | ||
| customBrushDao | ||
| ) | ||
|
|
||
| assertNull(vm.previewBrushFamily.value) | ||
| } | ||
|
|
||
| @Test | ||
| fun setTextureStore_immediately_syncs_proto_textures() = runTest { | ||
| val context = ApplicationProvider.getApplicationContext<Context>() | ||
| val testTextureId = "test-texture" | ||
| val testBitmap = Bitmap.createBitmap(2, 2, Bitmap.Config.ARGB_8888) | ||
|
|
||
| val baos = java.io.ByteArrayOutputStream() | ||
| testBitmap.compress(Bitmap.CompressFormat.PNG, 100, baos) | ||
| val textureBytes = com.google.protobuf.ByteString.copyFrom(baos.toByteArray()) | ||
|
|
||
| val protoWithTexture = viewModel.activeBrushProto.value.toBuilder() | ||
| .putTextureIdToBitmap(testTextureId, textureBytes) | ||
| .build() | ||
| repository.updateActiveBrushProto(protoWithTexture) | ||
|
|
||
| val store = com.example.cahier.core.ui.CahierTextureBitmapStore(context) | ||
| assertNull(store[testTextureId]) | ||
|
|
||
| viewModel.setTextureStore(store) | ||
|
|
||
| assertNotNull(store[testTextureId]) | ||
| } | ||
| } |
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
17 changes: 17 additions & 0 deletions
17
app/src/main/java/com/example/cahier/core/ui/CahierTextureBitmapStore.kt
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
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
Oops, something went wrong.
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.
(nit) Should the licenses be indented with 1, 2, or 3 stars? I'm noticing differing patterns across this file,
app/src/main/java/com/example/cahier/developer/brushdesigner/ui/BrushDesignerComponents.kt, andapp/src/main/java/com/example/cahier/core/navigation/CahierNavGraph.kt, for example.