diff --git a/app/src/main/java/org/mozilla/tv/firefox/ext/WebBackForwardList.kt b/app/src/main/java/org/mozilla/tv/firefox/ext/WebBackForwardList.kt new file mode 100644 index 0000000000..f4f70a47bb --- /dev/null +++ b/app/src/main/java/org/mozilla/tv/firefox/ext/WebBackForwardList.kt @@ -0,0 +1,17 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +package org.mozilla.tv.firefox.ext + +import android.webkit.WebBackForwardList +import android.webkit.WebHistoryItem + +fun WebBackForwardList.toList(): List { + val size = this.size + val outputList = mutableListOf() + for (i in 0 until size) { + outputList.add(getItemAtIndex(i)) + } + return outputList +} diff --git a/app/src/testSystem/java/org/mozilla/tv/firefox/ext/WebBackForwardListKtTest.kt b/app/src/testSystem/java/org/mozilla/tv/firefox/ext/WebBackForwardListKtTest.kt new file mode 100644 index 0000000000..188cbb0f8a --- /dev/null +++ b/app/src/testSystem/java/org/mozilla/tv/firefox/ext/WebBackForwardListKtTest.kt @@ -0,0 +1,30 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +package org.mozilla.tv.firefox.ext + +import org.junit.Assert.assertEquals +import org.junit.Test +import org.mozilla.tv.firefox.helpers.MockWebHistoryItem +import org.mozilla.tv.firefox.helpers.toMockWebBackForwardList + +class WebBackForwardListKtTest { + + @Test + fun `WHEN a WebBackForwardList is converted to a List THEN the original URLs are all in order and unmodified`() { + val expected = listOf( + "https://google.com", + "https://mozilla.org", + "https://facebook.com" + ) + + // This test relies on our MockWeb* impls having no bugs but we can't do any better. + val webBackForwardList = expected.map { MockWebHistoryItem(mockOriginalUrl = it) } + .toMockWebBackForwardList() + + val actual = webBackForwardList.toList() + .map { it.originalUrl } + assertEquals(expected, actual) + } +}