From 9306f5d5aab5fb3ccaa22096f67bf60a4feec846 Mon Sep 17 00:00:00 2001 From: Konstantin Ivanov <54908981+konstantiniiv@users.noreply.github.com> Date: Tue, 15 Nov 2022 17:26:53 +0100 Subject: [PATCH] DROID-612 Editor | Link to object block, default as text style (#2704) * DROID-612 test setup fix * DROID-612 fix model * DROID-612 code style fix * DROID-612 add test Co-authored-by: konstantiniiv --- .../features/editor/base/EditorTestSetup.kt | 9 +- .../anytype/ui/editor/EditorFragment.kt | 5 +- .../anytypeio/anytype/core_models/Block.kt | 11 +- .../domain/page/AddBackLinkToObject.kt | 2 +- .../presentation/editor/EditorViewModel.kt | 6 +- .../editor/editor/EditorCreateBlockTest.kt | 101 ++++++++++++++++++ 6 files changed, 118 insertions(+), 16 deletions(-) create mode 100644 presentation/src/test/java/com/anytypeio/anytype/presentation/editor/editor/EditorCreateBlockTest.kt diff --git a/app/src/androidTest/java/com/anytypeio/anytype/features/editor/base/EditorTestSetup.kt b/app/src/androidTest/java/com/anytypeio/anytype/features/editor/base/EditorTestSetup.kt index 3f807aa80e..a5fbe54d8c 100644 --- a/app/src/androidTest/java/com/anytypeio/anytype/features/editor/base/EditorTestSetup.kt +++ b/app/src/androidTest/java/com/anytypeio/anytype/features/editor/base/EditorTestSetup.kt @@ -5,6 +5,7 @@ import androidx.fragment.app.testing.FragmentScenario import androidx.fragment.app.testing.launchFragmentInContainer import com.anytypeio.anytype.R import com.anytypeio.anytype.analytics.base.Analytics +import com.anytypeio.anytype.app.DefaultFeatureToggles import com.anytypeio.anytype.core_models.Block import com.anytypeio.anytype.core_models.Command import com.anytypeio.anytype.core_models.DocumentInfo @@ -13,6 +14,7 @@ import com.anytypeio.anytype.core_models.Id import com.anytypeio.anytype.core_models.ObjectType import com.anytypeio.anytype.core_models.Payload import com.anytypeio.anytype.core_models.Relation +import com.anytypeio.anytype.core_utils.tools.FeatureToggles import com.anytypeio.anytype.domain.`object`.ConvertObjectToSet import com.anytypeio.anytype.domain.`object`.ObjectTypesProvider import com.anytypeio.anytype.domain.`object`.UpdateDetail @@ -201,6 +203,8 @@ open class EditorTestSetup { lateinit var downloadUnsplashImage: DownloadUnsplashImage + lateinit var featureToggles: FeatureToggles + @Mock lateinit var updateDivider: UpdateDivider @@ -333,6 +337,8 @@ open class EditorTestSetup { applyTemplate = applyTemplate ) + featureToggles = DefaultFeatureToggles() + TestEditorFragment.testViewModelFactory = EditorViewModelFactory( openPage = openPage, closeObject = closePage, @@ -413,7 +419,8 @@ open class EditorTestSetup { setDocImageIcon = setDocImageIcon, editorTemplateDelegate = editorTemplateDelegate, createNewObject = createNewObject, - objectToSet = objectToSet + objectToSet = objectToSet, + featureToggles = featureToggles ) } diff --git a/app/src/main/java/com/anytypeio/anytype/ui/editor/EditorFragment.kt b/app/src/main/java/com/anytypeio/anytype/ui/editor/EditorFragment.kt index 2d565bc6f9..38d0f42f5f 100644 --- a/app/src/main/java/com/anytypeio/anytype/ui/editor/EditorFragment.kt +++ b/app/src/main/java/com/anytypeio/anytype/ui/editor/EditorFragment.kt @@ -2071,10 +2071,7 @@ open class EditorFragment : NavigationFragment(R.layout.f vm.proceedWithLinkToAction( link = link, target = target, - text = text, - icon = icon, - isBookmark = isBookmark, - isSet = isSet + isBookmark = isBookmark ) } diff --git a/core-models/src/main/java/com/anytypeio/anytype/core_models/Block.kt b/core-models/src/main/java/com/anytypeio/anytype/core_models/Block.kt index 4a9f50bb51..1eabcdf723 100644 --- a/core-models/src/main/java/com/anytypeio/anytype/core_models/Block.kt +++ b/core-models/src/main/java/com/anytypeio/anytype/core_models/Block.kt @@ -386,12 +386,11 @@ data class Block( ) : Prototype() data class Link( - val target: Id - ) : Prototype() { - val cardStyle: Content.Link.CardStyle = Content.Link.CardStyle.CARD - val iconSize: Content.Link.IconSize = Content.Link.IconSize.MEDIUM - val description: Content.Link.Description = Content.Link.Description.CONTENT - } + val target: Id, + val cardStyle: Content.Link.CardStyle = Content.Link.CardStyle.TEXT, + val iconSize: Content.Link.IconSize = Content.Link.IconSize.SMALL, + val description: Content.Link.Description = Content.Link.Description.NONE + ) : Prototype() object DividerLine : Prototype() object DividerDots : Prototype() diff --git a/domain/src/main/java/com/anytypeio/anytype/domain/page/AddBackLinkToObject.kt b/domain/src/main/java/com/anytypeio/anytype/domain/page/AddBackLinkToObject.kt index e5d69555c2..f94cda77ca 100644 --- a/domain/src/main/java/com/anytypeio/anytype/domain/page/AddBackLinkToObject.kt +++ b/domain/src/main/java/com/anytypeio/anytype/domain/page/AddBackLinkToObject.kt @@ -43,7 +43,7 @@ class AddBackLinkToObject( context = params.objectToPlaceLink, target = targetBlock, position = Position.BOTTOM, - prototype = Block.Prototype.Link(params.objectToLink) + prototype = Block.Prototype.Link(target = params.objectToLink) ) ) diff --git a/presentation/src/main/java/com/anytypeio/anytype/presentation/editor/EditorViewModel.kt b/presentation/src/main/java/com/anytypeio/anytype/presentation/editor/EditorViewModel.kt index 58f38c2a3b..1bdbe333ca 100644 --- a/presentation/src/main/java/com/anytypeio/anytype/presentation/editor/EditorViewModel.kt +++ b/presentation/src/main/java/com/anytypeio/anytype/presentation/editor/EditorViewModel.kt @@ -5067,11 +5067,9 @@ class EditorViewModel( fun proceedWithLinkToAction( link: Id, target: Id, - isBookmark: Boolean, - text: String, - icon: ObjectIcon, - isSet: Boolean + isBookmark: Boolean ) { + Timber.d("proceedWithLinkToAction, link:[$link], target:[$target], isBookmark:[$isBookmark]") val targetBlock = blocks.firstOrNull { it.id == target } if (targetBlock != null) { val targetContent = targetBlock.content diff --git a/presentation/src/test/java/com/anytypeio/anytype/presentation/editor/editor/EditorCreateBlockTest.kt b/presentation/src/test/java/com/anytypeio/anytype/presentation/editor/editor/EditorCreateBlockTest.kt new file mode 100644 index 0000000000..fe40e53acb --- /dev/null +++ b/presentation/src/test/java/com/anytypeio/anytype/presentation/editor/editor/EditorCreateBlockTest.kt @@ -0,0 +1,101 @@ +package com.anytypeio.anytype.presentation.editor.editor + +import androidx.arch.core.executor.testing.InstantTaskExecutorRule +import com.anytypeio.anytype.core_models.Block +import com.anytypeio.anytype.core_models.Position +import com.anytypeio.anytype.core_models.StubHeader +import com.anytypeio.anytype.core_models.StubParagraph +import com.anytypeio.anytype.core_models.StubTitle +import com.anytypeio.anytype.domain.block.interactor.CreateBlock +import com.anytypeio.anytype.presentation.editor.editor.model.BlockView +import com.anytypeio.anytype.presentation.editor.editor.slash.SlashItem +import com.anytypeio.anytype.presentation.util.CoroutinesTestRule +import com.anytypeio.anytype.test_utils.MockDataFactory +import kotlinx.coroutines.test.runTest +import org.junit.Before +import org.junit.Rule +import org.junit.Test +import org.mockito.MockitoAnnotations +import org.mockito.kotlin.eq +import org.mockito.kotlin.times +import org.mockito.kotlin.verify + +class EditorCreateBlockTest : EditorPresentationTestSetup() { + + @get:Rule + val rule = InstantTaskExecutorRule() + + @get:Rule + val coroutineTestRule = CoroutinesTestRule() + + @Before + fun setup() { + MockitoAnnotations.openMocks(this) + } + + val title = StubTitle() + val header = StubHeader(children = listOf(title.id)) + val a = StubParagraph(text = "") + + @Test + fun `should create default link_to_object block with text style, small icon and no description`() = + runTest { + + // SETUP + val page = Block( + id = root, + fields = Block.Fields(emptyMap()), + content = Block.Content.Smart(), + children = listOf(header.id, a.id) + ) + + val document = listOf(page, header, title, a) + + stubOpenDocument(document = document) + stubInterceptEvents() + stubInterceptThreadStatus() + stubUpdateText() + stubCreateBlock(root) + + val vm = buildViewModel() + + // TESTING + + vm.onStart(root) + vm.onBlockFocusChanged(id = a.id, hasFocus = true) + vm.onSelectionChanged( + id = a.id, + selection = IntRange(1, 1) + ) + val blockView = BlockView.Text.Paragraph(id = a.id, text = "/") + vm.onTextBlockTextChanged(view = blockView) + vm.onSlashItemClicked(item = SlashItem.Actions.LinkTo) + + val linkToObject = MockDataFactory.randomUuid() + + vm.proceedWithLinkToAction( + link = linkToObject, + target = a.id, + isBookmark = false + ) + + //VERIFY + verify(createBlock, times(1)).execute( + params = eq( + CreateBlock.Params( + context = root, + target = a.id, + position = Position.BOTTOM, + prototype = Block.Prototype.Link( + target = linkToObject, + cardStyle = Block.Content.Link.CardStyle.TEXT, + iconSize = Block.Content.Link.IconSize.SMALL, + description = Block.Content.Link.Description.NONE + ) + ) + ) + ) + + coroutineTestRule.advanceTime(300L) + } +} \ No newline at end of file