From 907d31e2af9a95b688b353fca691ac344b5a2440 Mon Sep 17 00:00:00 2001 From: Michael Comella Date: Wed, 25 Sep 2019 14:12:55 -0700 Subject: [PATCH] Issue #2805: Add WebBackForwardList.toList. This code is adapted from Severin's WIP: https://github.com/Baron-Severin/firefox-tv/commit/0f4f432891fde18ec355499b5a9123ff7416e924 --- .../tv/firefox/ext/WebBackForwardList.kt | 17 +++++++++++ .../firefox/ext/WebBackForwardListKtTest.kt | 30 +++++++++++++++++++ 2 files changed, 47 insertions(+) create mode 100644 app/src/main/java/org/mozilla/tv/firefox/ext/WebBackForwardList.kt create mode 100644 app/src/testSystem/java/org/mozilla/tv/firefox/ext/WebBackForwardListKtTest.kt 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) + } +}