Skip to content

Commit

Permalink
Convert StateUpdatesTest to Kotlin
Browse files Browse the repository at this point in the history
Summary: As per title

Reviewed By: fabiocarballo

Differential Revision: D44952337

fbshipit-source-id: 4a1948c181e77c13b6e66fbd1577ca6dbfbb4cac
  • Loading branch information
zielinskimz authored and facebook-github-bot committed Apr 27, 2023
1 parent 887d3cf commit e99d12a
Show file tree
Hide file tree
Showing 5 changed files with 451 additions and 516 deletions.
3 changes: 1 addition & 2 deletions litho-it/src/test/java/com/facebook/litho/BUCK
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,9 @@ litho_robolectric4_test(
litho_android_library(
name = "testutil",
srcs = [
"StateUpdateTestComponent.java",
"StateUpdateTestComponent.kt",
],
autoglob = False,
language = "JAVA",
source = "8",
target = "8",
visibility = [
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.facebook.litho

import java.util.concurrent.atomic.AtomicInteger

class StateUpdateTestComponent : SpecGeneratedComponent("StateUpdateTest") {

private val createInitialStateCount = AtomicInteger(0)

override fun isEquivalentProps(other: Component?, shouldCompareCommonProps: Boolean): Boolean =
this === other

override fun hasState(): Boolean = true

override fun createInitialState(c: ComponentContext, stateContainer: StateContainer) {
val testStateContainer = stateContainer as TestStateContainer
testStateContainer.mCount = INITIAL_COUNT_STATE_VALUE
createInitialStateCount.incrementAndGet()
finalCounterValue.set(INITIAL_COUNT_STATE_VALUE)
}

fun getCount(c: ComponentContext?): Int = finalCounterValue.get()

val componentForStateUpdate: StateUpdateTestComponent
get() = this

override fun createStateContainer(): StateContainer = TestStateContainer()

protected fun getStateContainerImpl(c: ComponentContext): TestStateContainer? =
c.scopedComponentInfo.stateContainer as TestStateContainer?

class TestStateContainer : StateContainer() {
@JvmField var mCount = 0
override fun applyStateUpdate(stateUpdate: StateUpdate) {
when (stateUpdate.type) {
STATE_UPDATE_TYPE_NOOP -> {}
STATE_UPDATE_TYPE_INCREMENT -> mCount += 1
STATE_UPDATE_TYPE_MULTIPLY -> mCount *= 2
}
finalCounterValue.set(mCount)
}
}

companion object {
private const val STATE_UPDATE_TYPE_NOOP = 0
private const val STATE_UPDATE_TYPE_INCREMENT = 1
private const val STATE_UPDATE_TYPE_MULTIPLY = 2
const val INITIAL_COUNT_STATE_VALUE = 4
@JvmStatic
fun createNoopStateUpdate(): StateContainer.StateUpdate =
StateContainer.StateUpdate(STATE_UPDATE_TYPE_NOOP)

@JvmStatic
fun createIncrementStateUpdate(): StateContainer.StateUpdate =
StateContainer.StateUpdate(STATE_UPDATE_TYPE_INCREMENT)

@JvmStatic
fun createMultiplyStateUpdate(): StateContainer.StateUpdate =
StateContainer.StateUpdate(STATE_UPDATE_TYPE_MULTIPLY)

private val idGenerator = AtomicInteger(0)
private val finalCounterValue = AtomicInteger(0)
}
}
Loading

0 comments on commit e99d12a

Please sign in to comment.