Skip to content

Commit

Permalink
Merge branch 'oppia:develop' into content-handler
Browse files Browse the repository at this point in the history
  • Loading branch information
manas-yu authored Dec 22, 2024
2 parents 7d39cec + bc0483d commit c53a082
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public static void setLayoutMarginStart(@NonNull View view, float marginStart) {
if (view.getLayoutParams() instanceof MarginLayoutParams) {
MarginLayoutParams params = (MarginLayoutParams) view.getLayoutParams();
MarginLayoutParamsCompat.setMarginStart(params, (int) marginStart);
view.requestLayout();
view.setLayoutParams(params);
}
}

Expand All @@ -25,7 +25,7 @@ public static void setLayoutMarginEnd(@NonNull View view, float marginEnd) {
if (view.getLayoutParams() instanceof MarginLayoutParams) {
MarginLayoutParams params = (MarginLayoutParams) view.getLayoutParams();
MarginLayoutParamsCompat.setMarginEnd(params, (int) marginEnd);
view.requestLayout();
view.setLayoutParams(params);
}
}

Expand All @@ -36,7 +36,6 @@ public static void setLayoutMarginTop(@NonNull View view, float marginTop) {
MarginLayoutParams params = (MarginLayoutParams) view.getLayoutParams();
params.topMargin = (int) marginTop;
view.setLayoutParams(params);
view.requestLayout();
}
}

Expand All @@ -47,22 +46,6 @@ public static void setLayoutMarginBottom(@NonNull View view, float marginBottom)
MarginLayoutParams params = (MarginLayoutParams) view.getLayoutParams();
params.bottomMargin = (int) marginBottom;
view.setLayoutParams(params);
view.requestLayout();
}
}

/** Used to set a margin for views. */
@BindingAdapter("layoutMargin")
public static void setLayoutMargin(@NonNull View view, float margin) {
if (view.getLayoutParams() instanceof MarginLayoutParams) {
MarginLayoutParams params = (MarginLayoutParams) view.getLayoutParams();
params.setMargins(
(int) margin,
(int) margin,
(int) margin,
(int) margin
);
view.requestLayout();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -219,8 +219,12 @@ class DragAndDropSortInteractionViewModel private constructor(
dragDropInteractionContentViewModel.itemIndex = index
dragDropInteractionContentViewModel.listSize = _choiceItems.size
}
// to update the content of grouped item

// To update the list
(adapter as BindableAdapter<*>).setDataUnchecked(_choiceItems)

// Trigger pending answer check to re-enable submit button
checkPendingAnswerError(AnswerErrorCategory.REAL_TIME)
}

fun unlinkElement(itemIndex: Int, adapter: RecyclerView.Adapter<RecyclerView.ViewHolder>) {
Expand All @@ -246,15 +250,24 @@ class DragAndDropSortInteractionViewModel private constructor(
dragDropInteractionContentViewModel.itemIndex = index
dragDropInteractionContentViewModel.listSize = _choiceItems.size
}
// to update the list

// Update the list
(adapter as BindableAdapter<*>).setDataUnchecked(_choiceItems)

// Trigger pending answer check* to re-enable submit button
checkPendingAnswerError(AnswerErrorCategory.REAL_TIME)
}

private fun getSubmitTimeError(): DragAndDropSortInteractionError {
return if (_originalChoiceItems == _choiceItems) {
val haveItemsChanged = _originalChoiceItems.size != _choiceItems.size ||
_originalChoiceItems.zip(_choiceItems).any { (originalItem, currentItem) ->
originalItem.htmlContent != currentItem.htmlContent
}
return if (!haveItemsChanged) {
DragAndDropSortInteractionError.EMPTY_INPUT
} else
} else {
DragAndDropSortInteractionError.VALID
}
}

/** Implementation of [StateItemViewModel.InteractionItemFactory] for this view model. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import android.app.Application
import android.content.Context
import android.content.Intent
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
Expand Down Expand Up @@ -112,8 +113,11 @@ class MarginBindingAdaptersTest {
@get:Rule
val initializeDefaultLocaleRule = InitializeDefaultLocaleRule()

@Inject lateinit var context: Context
@Inject lateinit var testCoroutineDispatchers: TestCoroutineDispatchers
@Inject
lateinit var context: Context

@Inject
lateinit var testCoroutineDispatchers: TestCoroutineDispatchers

@get:Rule
val oppiaTestRule = OppiaTestRule()
Expand Down Expand Up @@ -292,6 +296,75 @@ class MarginBindingAdaptersTest {
assertThat(textView.marginEnd.toFloat()).isWithin(TOLERANCE).of(40f)
}

@Config(qualifiers = "port")
@Test
fun testMarginBindableAdapters_setLayoutParams_preservesMargins() {
val textView = activityRule.scenario.runWithActivity {
val textView: TextView = it.findViewById(R.id.test_margin_text_view)

// Set initial margins
setLayoutMarginStart(textView, /* marginStart= */ 24f)
setLayoutMarginEnd(textView, /* marginEnd= */ 40f)
setLayoutMarginTop(textView, /* marginTop= */ 16f)
setLayoutMarginBottom(textView, /* marginBottom= */ 32f)

return@runWithActivity textView
}

assertThat(textView.marginStart.toFloat()).isWithin(TOLERANCE).of(24f)
assertThat(textView.marginEnd.toFloat()).isWithin(TOLERANCE).of(40f)

val layoutParams = textView.layoutParams as ViewGroup.MarginLayoutParams
assertThat(layoutParams.topMargin.toFloat()).isWithin(TOLERANCE).of(16f)
assertThat(layoutParams.bottomMargin.toFloat()).isWithin(TOLERANCE).of(32f)
}

@Config(qualifiers = "land")
@Test
fun testMarginBindableAdapters_landscapeMode_setLayoutParams_preservesMargins() {
val textView = activityRule.scenario.runWithActivity {
val textView: TextView = it.findViewById(R.id.test_margin_text_view)

// Set initial margins
setLayoutMarginStart(textView, /* marginStart= */ 24f)
setLayoutMarginEnd(textView, /* marginEnd= */ 40f)
setLayoutMarginTop(textView, /* marginTop= */ 16f)
setLayoutMarginBottom(textView, /* marginBottom= */ 32f)

return@runWithActivity textView
}

assertThat(textView.marginStart.toFloat()).isWithin(TOLERANCE).of(24f)
assertThat(textView.marginEnd.toFloat()).isWithin(TOLERANCE).of(40f)

val layoutParams = textView.layoutParams as ViewGroup.MarginLayoutParams
assertThat(layoutParams.topMargin.toFloat()).isWithin(TOLERANCE).of(16f)
assertThat(layoutParams.bottomMargin.toFloat()).isWithin(TOLERANCE).of(32f)
}

@Config(qualifiers = "sw600dp-port")
@Test
fun testMarginBindableAdapters_tabletMode_setLayoutParams_preservesMargins() {
val textView = activityRule.scenario.runWithActivity {
val textView: TextView = it.findViewById(R.id.test_margin_text_view)

// Set initial margins
setLayoutMarginStart(textView, /* marginStart= */ 24f)
setLayoutMarginEnd(textView, /* marginEnd= */ 40f)
setLayoutMarginTop(textView, /* marginTop= */ 16f)
setLayoutMarginBottom(textView, /* marginBottom= */ 32f)

return@runWithActivity textView
}

assertThat(textView.marginStart.toFloat()).isWithin(TOLERANCE).of(24f)
assertThat(textView.marginEnd.toFloat()).isWithin(TOLERANCE).of(40f)

val layoutParams = textView.layoutParams as ViewGroup.MarginLayoutParams
assertThat(layoutParams.topMargin.toFloat()).isWithin(TOLERANCE).of(16f)
assertThat(layoutParams.bottomMargin.toFloat()).isWithin(TOLERANCE).of(32f)
}

private fun testMarginBindableAdapters_topAndBottomIsCorrect() {
activityRule.scenario.runWithActivity {
val textView: TextView = it.findViewById(R.id.test_margin_text_view)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1117,6 +1117,18 @@ class StateFragmentTest {
}
}

@Test
fun testStateFragment_loadDragDropExp_groupingItemsEnablesSubmitButton() {
setUpTestWithLanguageSwitchingFeatureOff()
launchForExploration(TEST_EXPLORATION_ID_4, shouldSavePartialProgress = false).use {
startPlayingExploration()
clickSubmitAnswerButton()
verifySubmitAnswerButtonIsDisabled()
mergeDragAndDropItems(position = 0)
verifySubmitAnswerButtonIsEnabled()
}
}

@Test
@RunOn(TestPlatform.ESPRESSO) // TODO(#1612): Enable for Robolectric.
fun testStateFragment_loadDragDropExp_retainStateOnConfigurationChange() {
Expand Down

0 comments on commit c53a082

Please sign in to comment.