Created
October 30, 2019 09:22
-
-
Save dsvoronin/e6cd6c37de548393f7099c5597a72478 to your computer and use it in GitHub Desktop.
This file contains 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
package com.avito.android.rule | |
import android.app.Activity | |
import android.app.Application | |
import android.graphics.Color | |
import android.graphics.drawable.ColorDrawable | |
import android.os.Bundle | |
import androidx.test.platform.app.InstrumentationRegistry | |
import android.view.View | |
import android.view.ViewGroup | |
import android.view.ViewTreeObserver | |
import android.widget.ProgressBar | |
import com.avito.android.app.SimpleActivityLifecycleCallbacks | |
import com.avito.android.util.children | |
import com.avito.android.util.getFieldValue | |
import com.avito.android.util.removeGlobalLayoutListener | |
import com.avito.android.util.whenAlive | |
import java.util.IdentityHashMap | |
import java.util.LinkedList | |
import java.util.Queue | |
/** | |
* Rule supressing infinite animation is intstrumentation tests; i.e. indeterminate ProgessBar | |
* @param siblingWindowToo wierd flag. Suppress animation not only on current activity but in all windows. For example: in AlertDialogs. | |
*/ | |
class SuppressAnimationRule(private val siblingWindowToo: Boolean = false) : SimpleRule() { | |
val app = InstrumentationRegistry.getInstrumentation().targetContext.applicationContext as Application | |
private val activityCallbacks = LifecycleCallbacks() | |
override fun before() { | |
super.before() | |
app.registerActivityLifecycleCallbacks(activityCallbacks) | |
} | |
override fun after() { | |
super.after() | |
app.unregisterActivityLifecycleCallbacks(activityCallbacks) | |
} | |
private inner class LifecycleCallbacks : SimpleActivityLifecycleCallbacks() { | |
val listeners = IdentityHashMap<Activity, AnimationSuppressor>() | |
override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle?) { | |
super.onActivityCreated(activity, savedInstanceState) | |
val rootView = activity.window.decorView | |
val listener = AnimationSuppressor(rootView, siblingWindowToo) | |
rootView.viewTreeObserver.whenAlive { observer -> | |
observer.addOnGlobalLayoutListener(listener) | |
listeners[activity] = listener | |
} | |
} | |
override fun onActivityDestroyed(activity: Activity) { | |
super.onActivityDestroyed(activity) | |
val listener = listeners[activity] ?: return | |
activity.window.decorView.viewTreeObserver.whenAlive { observer -> | |
observer.removeGlobalLayoutListener(listener) | |
} | |
} | |
} | |
private class AnimationSuppressor( | |
private val rootView: View, | |
private val suppressSiblings: Boolean | |
) : ViewTreeObserver.OnGlobalLayoutListener { | |
private val drawableStub = ColorDrawable(Color.parseColor("#FF00FF00")) | |
override fun onGlobalLayout() { | |
val queue: Queue<View> = LinkedList() | |
val roots = if (suppressSiblings) { | |
collectAllSiblingRoots(rootView) | |
} else { | |
listOf(rootView) | |
} | |
queue.addAll(roots) | |
while (queue.isNotEmpty()) { | |
val current = queue.remove() | |
if (current is ViewGroup) { | |
queue.addAll(current.children.asSequence()) | |
} else { | |
suppressAnimation(current) | |
} | |
} | |
} | |
private fun suppressAnimation(view: View) { | |
when (view) { | |
is ProgressBar -> view.indeterminateDrawable = drawableStub | |
// Add hacks for another view types here | |
} | |
} | |
private fun collectAllSiblingRoots(rootView: View): List<View> { | |
try { | |
val windowManager = (rootView.context as Activity).windowManager | |
val global: Any = windowManager.getFieldValue("mGlobal") | |
val roots: List<Any> = global.getFieldValue("mRoots") | |
return roots.map { it.getFieldValue<View>("mView") } | |
} catch (ex: Throwable) { | |
throw RuntimeException("Cannot collect sibling root view", ex) | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment