|
| 1 | +package com.kienhoang.dualsubreplay.ui |
| 2 | + |
| 3 | +import android.app.Application |
| 4 | +import android.content.res.Configuration |
| 5 | +import android.webkit.WebResourceResponse |
| 6 | +import android.webkit.WebView |
| 7 | +import android.widget.FrameLayout |
| 8 | +import androidx.compose.runtime.CompositionLocalProvider |
| 9 | +import androidx.compose.runtime.mutableStateOf |
| 10 | +import androidx.compose.ui.platform.LocalConfiguration |
| 11 | +import androidx.compose.ui.test.junit4.createComposeRule |
| 12 | +import androidx.lifecycle.viewModelScope |
| 13 | +import androidx.test.platform.app.InstrumentationRegistry |
| 14 | +import com.kienhoang.dualsubreplay.data.CaptionProvider |
| 15 | +import com.kienhoang.dualsubreplay.data.CaptionTrackResult |
| 16 | +import com.kienhoang.dualsubreplay.data.RawCaptionCue |
| 17 | +import com.kienhoang.dualsubreplay.data.SubtitleWord |
| 18 | +import kotlinx.coroutines.cancel |
| 19 | +import org.junit.Assert.assertEquals |
| 20 | +import org.junit.Assert.assertFalse |
| 21 | +import org.junit.Assert.assertTrue |
| 22 | +import org.junit.Rule |
| 23 | +import org.junit.Test |
| 24 | +import java.io.ByteArrayInputStream |
| 25 | +import java.util.concurrent.CountDownLatch |
| 26 | +import java.util.concurrent.TimeUnit |
| 27 | +import java.util.concurrent.atomic.AtomicInteger |
| 28 | +import java.util.concurrent.atomic.AtomicReference |
| 29 | + |
| 30 | +class LandscapeLiveCaptureTest { |
| 31 | + @get:Rule val compose = createComposeRule() |
| 32 | + |
| 33 | + @Test |
| 34 | + fun realPlayerKeepsLiveWordsWhenLandscapeAndFullscreenHideThePanel() { |
| 35 | + val instrumentation = InstrumentationRegistry.getInstrumentation() |
| 36 | + val application = instrumentation.targetContext.applicationContext as Application |
| 37 | + val preferences = application.getSharedPreferences("dual_sub_preferences", 0) |
| 38 | + val saved = preferences.all |
| 39 | + preferences |
| 40 | + .edit() |
| 41 | + .putBoolean("onboarding_completed", true) |
| 42 | + .putBoolean(GUIDE_COMPLETED_PREFERENCE, true) |
| 43 | + .putBoolean(PRELOAD_MODELS_ENABLED_PREFERENCE, false) |
| 44 | + .putBoolean(WORD_HIGHLIGHT_ENABLED_PREFERENCE, true) |
| 45 | + .putBoolean(AUTO_OVERLAY_LANDSCAPE_PREFERENCE, true) |
| 46 | + .putBoolean(AUTO_OVERLAY_FULLSCREEN_PREFERENCE, true) |
| 47 | + .putString(PLAYER_EXPERIENCE_MODE_PREFERENCE, PlayerExperienceMode.TRANSCRIPT_PANEL.storageValue) |
| 48 | + .putString("last_browser_url", "https://m.youtube.com/watch?v=abcdefghijk") |
| 49 | + .putString("preferred_caption_language", "en") |
| 50 | + .putString("target_language", "en") |
| 51 | + .putString(CAPTION_FORMAT_PREFERENCE, CaptionFormat.WHOLE_SENTENCE.storageValue) |
| 52 | + .putString(ORIGINAL_VISIBILITY, CaptionVisibility.ALWAYS.name) |
| 53 | + .commit() |
| 54 | + lateinit var model: AppViewModel |
| 55 | + instrumentation.runOnMainSync { model = AppViewModel(application, DelayedTranscript()) } |
| 56 | + val configuration = mutableStateOf(Configuration(application.resources.configuration)) |
| 57 | + configuration.value = Configuration(configuration.value).apply { orientation = Configuration.ORIENTATION_PORTRAIT } |
| 58 | + val page = AtomicReference<WebView>() |
| 59 | + val loads = AtomicInteger() |
| 60 | + try { |
| 61 | + compose.setContent { |
| 62 | + CompositionLocalProvider( |
| 63 | + LocalConfiguration provides configuration.value, |
| 64 | + LocalYouTubeRequestInterceptor provides { view, request -> |
| 65 | + page.compareAndSet(null, view) |
| 66 | + val html = if (request.isForMainFrame) OFFLINE_PAGE.also { loads.incrementAndGet() } else "" |
| 67 | + WebResourceResponse("text/html", "UTF-8", ByteArrayInputStream(html.toByteArray())) |
| 68 | + }, |
| 69 | + ) { |
| 70 | + LearningPlayerRoot(model) |
| 71 | + } |
| 72 | + } |
| 73 | + compose.waitUntil(15_000) { model.state.value.generatedCaptions && model.state.value.activeWordIndex == 0 } |
| 74 | + assertEquals("true", javascript(page.get(), "!!window['$LIVE_CAPTION_CAPTURE_STATE_KEY']?.enabled")) |
| 75 | + // The transcript starts at 2 seconds. Earlier highlights can only |
| 76 | + // come from the actual WebView live-caption path. |
| 77 | + advanceCaption(page.get(), model, "One two", 0.2, 1) |
| 78 | + compose.runOnIdle { |
| 79 | + configuration.value = Configuration(configuration.value).apply { orientation = Configuration.ORIENTATION_LANDSCAPE } |
| 80 | + } |
| 81 | + compose.waitUntil(5000) { !model.state.value.subtitlePanelVisible } |
| 82 | + assertEquals("true", javascript(page.get(), "!!window['$LIVE_CAPTION_CAPTURE_STATE_KEY']?.enabled")) |
| 83 | + advanceCaption(page.get(), model, "One two three", 0.3, 2) |
| 84 | + compose.runOnIdle { |
| 85 | + page.get().webChromeClient!!.onShowCustomView(FrameLayout(page.get().context)) {} |
| 86 | + } |
| 87 | + compose.waitUntil(5000) { youtubeFullscreenActive.value } |
| 88 | + advanceCaption(page.get(), model, "One two three four", 0.4, 3) |
| 89 | + compose.runOnIdle { page.get().webChromeClient!!.onHideCustomView() } |
| 90 | + compose.waitUntil(5000) { !youtubeFullscreenActive.value } |
| 91 | + assertFalse(model.state.value.subtitlePanelVisible) |
| 92 | + compose.runOnIdle { |
| 93 | + configuration.value = Configuration(configuration.value).apply { orientation = Configuration.ORIENTATION_PORTRAIT } |
| 94 | + } |
| 95 | + compose.waitUntil(5000) { model.state.value.subtitlePanelVisible } |
| 96 | + advanceCaption(page.get(), model, "One two three four five", 0.5, 4) |
| 97 | + assertEquals("The same native page must survive every presentation switch", 1, loads.get()) |
| 98 | + } finally { |
| 99 | + instrumentation.runOnMainSync { |
| 100 | + page.get()?.webChromeClient?.onHideCustomView() |
| 101 | + model.viewModelScope.cancel() |
| 102 | + youtubeFullscreenActive.value = false |
| 103 | + } |
| 104 | + val editor = preferences.edit().clear() |
| 105 | + saved.forEach { (key, value) -> |
| 106 | + when (value) { |
| 107 | + is String -> editor.putString(key, value) |
| 108 | + is Boolean -> editor.putBoolean(key, value) |
| 109 | + is Float -> editor.putFloat(key, value) |
| 110 | + is Int -> editor.putInt(key, value) |
| 111 | + is Long -> editor.putLong(key, value) |
| 112 | + } |
| 113 | + } |
| 114 | + editor.commit() |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + private fun advanceCaption( |
| 119 | + page: WebView, |
| 120 | + model: AppViewModel, |
| 121 | + text: String, |
| 122 | + second: Double, |
| 123 | + word: Int, |
| 124 | + ) { |
| 125 | + javascript(page, "video._time = $second; document.querySelector('.ytp-caption-segment').textContent = '$text';") |
| 126 | + compose.waitUntil(5000) { model.state.value.activeWordIndex == word } |
| 127 | + assertTrue("A late transcript must not explain this update", second < 1.0) |
| 128 | + } |
| 129 | + |
| 130 | + private fun javascript( |
| 131 | + page: WebView, |
| 132 | + script: String, |
| 133 | + ): String { |
| 134 | + val done = CountDownLatch(1) |
| 135 | + val result = AtomicReference<String>() |
| 136 | + InstrumentationRegistry.getInstrumentation().runOnMainSync { |
| 137 | + page.evaluateJavascript(script) { |
| 138 | + result.set(it) |
| 139 | + done.countDown() |
| 140 | + } |
| 141 | + } |
| 142 | + assertTrue("WebView script did not complete", done.await(5, TimeUnit.SECONDS)) |
| 143 | + return result.get() |
| 144 | + } |
| 145 | + |
| 146 | + private class DelayedTranscript : CaptionProvider { |
| 147 | + override suspend fun fetch( |
| 148 | + videoId: String, |
| 149 | + preferredLanguages: List<String>, |
| 150 | + ): CaptionTrackResult = |
| 151 | + CaptionTrackResult( |
| 152 | + "en", |
| 153 | + true, |
| 154 | + listOf( |
| 155 | + RawCaptionCue( |
| 156 | + 0, |
| 157 | + 12_000, |
| 158 | + "One two three four five", |
| 159 | + listOf("One", "two", "three", "four", "five").mapIndexed { index, word -> |
| 160 | + SubtitleWord(word, (index + 1) * 2000L, (index + 2) * 2000L) |
| 161 | + }, |
| 162 | + ), |
| 163 | + ), |
| 164 | + ) |
| 165 | + } |
| 166 | + |
| 167 | + companion object { |
| 168 | + private val OFFLINE_PAGE = |
| 169 | + """ |
| 170 | + <html><body> |
| 171 | + <div id="movie_player" class="html5-video-player ytp-autohide"> |
| 172 | + <video></video><span class="ytp-caption-segment">One</span> |
| 173 | + </div> |
| 174 | + <script> |
| 175 | + const video = document.querySelector('video'); |
| 176 | + video._time = 0.1; |
| 177 | + Object.defineProperties(video, { |
| 178 | + currentTime: {get: () => video._time}, readyState: {get: () => 4}, |
| 179 | + paused: {get: () => false}, seeking: {get: () => false} |
| 180 | + }); |
| 181 | + video.requestVideoFrameCallback = function() {}; |
| 182 | + const caption = document.querySelector('.ytp-caption-segment'); |
| 183 | + const bootstrap = setInterval(function() { |
| 184 | + if (caption.textContent === 'One') caption.textContent = 'One.'; |
| 185 | + else if (caption.textContent === 'One.') caption.textContent = 'One'; |
| 186 | + else clearInterval(bootstrap); |
| 187 | + }, 250); |
| 188 | + const player = document.getElementById('movie_player'); |
| 189 | + player.getOption = function() { return {languageCode: 'en'}; }; |
| 190 | + player.setOption = function() {}; |
| 191 | + player.getPlayerResponse = function() { return {videoDetails: {videoId: 'abcdefghijk'}}; }; |
| 192 | + </script></body></html> |
| 193 | + """.trimIndent() |
| 194 | + } |
| 195 | +} |
0 commit comments