diff options
author | Nick Chalko <nchalko@google.com> | 2019-07-26 14:25:54 -0700 |
---|---|---|
committer | Copybara-Service <copybara-worker@google.com> | 2019-08-13 10:00:22 -0700 |
commit | 3fb42d0b81a16a976a9ce08cce9bc5d78ef79c7a (patch) | |
tree | 896b9d30f6b3891f88956ed45f0089272c44711b /common | |
parent | 3cb42398eefd6e5ea274672f7eb5c9951d33913b (diff) | |
download | TV-3fb42d0b81a16a976a9ce08cce9bc5d78ef79c7a.tar.gz |
Include robotest in aosp.
PiperOrigin-RevId: 260213424
Change-Id: I5fc75f204a0c24612ce2f6cd4436fe3d4574b6c9
Diffstat (limited to 'common')
15 files changed, 1635 insertions, 0 deletions
diff --git a/common/tests/robotests/src/com/android/tv/common/TvContentRatingCacheTest.java b/common/tests/robotests/src/com/android/tv/common/TvContentRatingCacheTest.java new file mode 100644 index 00000000..01bfdc4d --- /dev/null +++ b/common/tests/robotests/src/com/android/tv/common/TvContentRatingCacheTest.java @@ -0,0 +1,219 @@ +/* + * Copyright (C) 2017 The Android Open Source Project + * + * 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.android.tv.common; + +import static com.google.common.truth.Truth.assertThat; +import static com.google.common.truth.Truth.assertWithMessage; + +import android.content.ComponentCallbacks2; +import android.media.tv.TvContentRating; +import com.android.tv.testing.constants.ConfigConstants; +import com.android.tv.testing.constants.TvContentRatingConstants; +import com.google.common.collect.ImmutableList; +import com.google.thirdparty.robolectric.GoogleRobolectricTestRunner; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.robolectric.annotation.Config; + +/** Test for {@link TvContentRatingCache}. */ +@RunWith(GoogleRobolectricTestRunner.class) +@Config(sdk = ConfigConstants.SDK) +public class TvContentRatingCacheTest { + /** US_TV_MA and US_TV_Y7 in order */ + public static final String MA_AND_Y7 = + TvContentRatingConstants.STRING_US_TV_MA + + "," + + TvContentRatingConstants.STRING_US_TV_Y7_US_TV_FV; + + /** US_TV_MA and US_TV_Y7 not in order */ + public static final String Y7_AND_MA = + TvContentRatingConstants.STRING_US_TV_Y7_US_TV_FV + + "," + + TvContentRatingConstants.STRING_US_TV_MA; + + final TvContentRatingCache mCache = TvContentRatingCache.getInstance(); + + @Before + public void setUp() { + mCache.performTrimMemory(ComponentCallbacks2.TRIM_MEMORY_COMPLETE); + } + + @After + public void tearDown() { + mCache.performTrimMemory(ComponentCallbacks2.TRIM_MEMORY_COMPLETE); + } + + @Test + public void testGetRatings_US_TV_MA() { + ImmutableList<TvContentRating> result = + mCache.getRatings(TvContentRatingConstants.STRING_US_TV_MA); + assertThat(result).contains(TvContentRatingConstants.CONTENT_RATING_US_TV_MA); + } + + @Test + public void testGetRatings_US_TV_MA_same() { + ImmutableList<TvContentRating> first = + mCache.getRatings(TvContentRatingConstants.STRING_US_TV_MA); + ImmutableList<TvContentRating> second = + mCache.getRatings(TvContentRatingConstants.STRING_US_TV_MA); + assertThat(first).isSameInstanceAs(second); + } + + @Test + public void testGetRatings_US_TV_MA_diffAfterClear() { + ImmutableList<TvContentRating> first = + mCache.getRatings(TvContentRatingConstants.STRING_US_TV_MA); + mCache.performTrimMemory(ComponentCallbacks2.TRIM_MEMORY_COMPLETE); + ImmutableList<TvContentRating> second = + mCache.getRatings(TvContentRatingConstants.STRING_US_TV_MA); + assertThat(first).isNotSameInstanceAs(second); + } + + @Test + public void testGetRatings_TWO_orderDoesNotMatter() { + ImmutableList<TvContentRating> first = mCache.getRatings(MA_AND_Y7); + ImmutableList<TvContentRating> second = mCache.getRatings(Y7_AND_MA); + assertThat(first).isSameInstanceAs(second); + } + + @Test + public void testContentRatingsToString_null() { + String result = TvContentRatingCache.contentRatingsToString(null); + assertWithMessage("ratings string").that(result).isNull(); + } + + @Test + public void testContentRatingsToString_none() { + String result = TvContentRatingCache.contentRatingsToString(ImmutableList.of()); + assertWithMessage("ratings string").that(result).isEmpty(); + } + + @Test + public void testContentRatingsToString_one() { + String result = + TvContentRatingCache.contentRatingsToString( + ImmutableList.of(TvContentRatingConstants.CONTENT_RATING_US_TV_MA)); + assertWithMessage("ratings string") + .that(result) + .isEqualTo(TvContentRatingConstants.STRING_US_TV_MA); + } + + @Test + public void testContentRatingsToString_twoInOrder() { + String result = + TvContentRatingCache.contentRatingsToString( + ImmutableList.of( + TvContentRatingConstants.CONTENT_RATING_US_TV_MA, + TvContentRatingConstants.CONTENT_RATING_US_TV_Y7_US_TV_FV)); + assertWithMessage("ratings string").that(result).isEqualTo(MA_AND_Y7); + } + + @Test + public void testContentRatingsToString_twoNotInOrder() { + String result = + TvContentRatingCache.contentRatingsToString( + ImmutableList.of( + TvContentRatingConstants.CONTENT_RATING_US_TV_Y7_US_TV_FV, + TvContentRatingConstants.CONTENT_RATING_US_TV_MA)); + assertWithMessage("ratings string").that(result).isEqualTo(MA_AND_Y7); + } + + @Test + public void testContentRatingsToString_double() { + String result = + TvContentRatingCache.contentRatingsToString( + ImmutableList.of( + TvContentRatingConstants.CONTENT_RATING_US_TV_MA, + TvContentRatingConstants.CONTENT_RATING_US_TV_MA)); + assertWithMessage("ratings string") + .that(result) + .isEqualTo(TvContentRatingConstants.STRING_US_TV_MA); + } + + @Test + public void testStringToContentRatings_null() { + assertThat(TvContentRatingCache.stringToContentRatings(null)).isEmpty(); + } + + @Test + public void testStringToContentRatings_none() { + assertThat(TvContentRatingCache.stringToContentRatings("")).isEmpty(); + } + + @Test + public void testStringToContentRatings_bad() { + assertThat(TvContentRatingCache.stringToContentRatings("bad")).isEmpty(); + } + + @Test + public void testStringToContentRatings_oneGoodOneBad() { + ImmutableList<TvContentRating> results = + TvContentRatingCache.stringToContentRatings( + TvContentRatingConstants.STRING_US_TV_Y7_US_TV_FV + ",bad"); + assertWithMessage("ratings") + .that(results) + .containsExactly(TvContentRatingConstants.CONTENT_RATING_US_TV_Y7_US_TV_FV); + } + + @Test + public void testStringToContentRatings_one() { + ImmutableList<TvContentRating> results = + TvContentRatingCache.stringToContentRatings( + TvContentRatingConstants.STRING_US_TV_Y7_US_TV_FV); + assertWithMessage("ratings") + .that(results) + .containsExactly(TvContentRatingConstants.CONTENT_RATING_US_TV_Y7_US_TV_FV); + } + + @Test + public void testStringToContentRatings_twoNotInOrder() { + ImmutableList<TvContentRating> results = + TvContentRatingCache.stringToContentRatings(Y7_AND_MA); + assertWithMessage("ratings") + .that(results) + .containsExactly( + TvContentRatingConstants.CONTENT_RATING_US_TV_MA, + TvContentRatingConstants.CONTENT_RATING_US_TV_Y7_US_TV_FV); + } + + @Test + public void testStringToContentRatings_twoInOrder() { + ImmutableList<TvContentRating> results = + TvContentRatingCache.stringToContentRatings(MA_AND_Y7); + assertWithMessage("ratings") + .that(results) + .containsExactly( + TvContentRatingConstants.CONTENT_RATING_US_TV_MA, + TvContentRatingConstants.CONTENT_RATING_US_TV_Y7_US_TV_FV); + } + + @Test + public void testStringToContentRatings_double() { + ImmutableList<TvContentRating> results = + TvContentRatingCache.stringToContentRatings( + TvContentRatingConstants.STRING_US_TV_MA + + "," + + TvContentRatingConstants.STRING_US_TV_MA); + assertWithMessage("ratings") + .that(results) + .containsExactly((TvContentRatingConstants.CONTENT_RATING_US_TV_MA)); + + assertThat(results).containsExactly(TvContentRatingConstants.CONTENT_RATING_US_TV_MA); + } +} diff --git a/common/tests/robotests/src/com/android/tv/common/actions/InputSetupActionUtilsTest.java b/common/tests/robotests/src/com/android/tv/common/actions/InputSetupActionUtilsTest.java new file mode 100644 index 00000000..17d400ac --- /dev/null +++ b/common/tests/robotests/src/com/android/tv/common/actions/InputSetupActionUtilsTest.java @@ -0,0 +1,148 @@ +/* + * Copyright (C) 2018 The Android Open Source Project + * + * 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.android.tv.common.actions; + +import static com.google.android.libraries.testing.truth.BundleSubject.assertThat; +import static com.google.common.truth.Truth.assertThat; + +import android.content.Intent; +import android.os.Bundle; +import com.android.tv.testing.constants.ConfigConstants; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.robolectric.RobolectricTestRunner; +import org.robolectric.annotation.Config; + +/** Tests for {@link InputSetupActionUtils} */ +@RunWith(RobolectricTestRunner.class) +@Config(sdk = ConfigConstants.SDK) +public class InputSetupActionUtilsTest { + + @Test + public void hasInputSetupAction_launchInputSetup() { + Intent intent = new Intent("com.android.tv.action.LAUNCH_INPUT_SETUP"); + assertThat(InputSetupActionUtils.hasInputSetupAction(intent)).isTrue(); + } + + @Test + public void hasInputSetupAction_googleLaunchInputSetup() { + Intent intent = new Intent("com.google.android.tv.action.LAUNCH_INPUT_SETUP"); + assertThat(InputSetupActionUtils.hasInputSetupAction(intent)).isTrue(); + } + + @Test + public void hasInputSetupAction_bad() { + Intent intent = new Intent("com.example.action.LAUNCH_INPUT_SETUP"); + assertThat(InputSetupActionUtils.hasInputSetupAction(intent)).isFalse(); + } + + @Test + public void getExtraActivityAfter_null() { + Intent intent = new Intent(); + assertThat(InputSetupActionUtils.getExtraActivityAfter(intent)).isNull(); + } + + @Test + public void getExtraActivityAfter_activityAfter() { + Intent intent = new Intent(); + Intent after = new Intent("after"); + intent.putExtra("com.android.tv.intent.extra.ACTIVITY_AFTER_COMPLETION", after); + assertThat(InputSetupActionUtils.getExtraActivityAfter(intent)).isEqualTo(after); + } + + @Test + public void getExtraActivityAfter_googleActivityAfter() { + Intent intent = new Intent(); + Intent after = new Intent("google_setup"); + intent.putExtra("com.google.android.tv.intent.extra.ACTIVITY_AFTER_COMPLETION", after); + assertThat(InputSetupActionUtils.getExtraActivityAfter(intent)).isEqualTo(after); + } + + @Test + public void getExtraSetupIntent_null() { + Intent intent = new Intent(); + assertThat(InputSetupActionUtils.getExtraSetupIntent(intent)).isNull(); + } + + @Test + public void getExtraSetupIntent_setupIntent() { + Intent intent = new Intent(); + Intent setup = new Intent("setup"); + intent.putExtra("com.android.tv.extra.SETUP_INTENT", setup); + assertThat(InputSetupActionUtils.getExtraSetupIntent(intent)).isEqualTo(setup); + } + + @Test + public void getExtraSetupIntent_googleSetupIntent() { + Intent intent = new Intent(); + Intent setup = new Intent("google_setup"); + intent.putExtra("com.google.android.tv.extra.SETUP_INTENT", setup); + assertThat(InputSetupActionUtils.getExtraSetupIntent(intent)).isEqualTo(setup); + } + + @Test + public void removeSetupIntent_empty() { + Bundle extras = new Bundle(); + InputSetupActionUtils.removeSetupIntent(extras); + assertThat(extras).exactlyMatches(new Bundle()); + } + + @Test + public void removeSetupIntent_other() { + Bundle extras = createTestBundle(); + Bundle expected = createTestBundle(); + InputSetupActionUtils.removeSetupIntent(extras); + assertThat(extras).exactlyMatches(expected); + } + + @Test + public void removeSetupIntent_setup() { + Bundle extras = createTestBundle(); + Bundle expected = createTestBundle(); + Intent setup = new Intent("setup"); + extras.putParcelable("com.android.tv.extra.SETUP_INTENT", setup); + InputSetupActionUtils.removeSetupIntent(extras); + assertThat(extras).exactlyMatches(expected); + } + + @Test + public void removeSetupIntent_googleSetup() { + Bundle extras = createTestBundle(); + Bundle expected = createTestBundle(); + Intent googleSetup = new Intent("googleSetup"); + extras.putParcelable("com.google.android.tv.extra.SETUP_INTENT", googleSetup); + InputSetupActionUtils.removeSetupIntent(extras); + assertThat(extras).exactlyMatches(expected); + } + + @Test + public void removeSetupIntent_bothSetups() { + Bundle extras = createTestBundle(); + Bundle expected = createTestBundle(); + Intent setup = new Intent("setup"); + extras.putParcelable("com.android.tv.extra.SETUP_INTENT", setup); + Intent googleSetup = new Intent("googleSetup"); + extras.putParcelable("com.google.android.tv.extra.SETUP_INTENT", googleSetup); + InputSetupActionUtils.removeSetupIntent(extras); + assertThat(extras).exactlyMatches(expected); + } + + private static Bundle createTestBundle() { + Bundle extras = new Bundle(); + extras.putInt("other", 1); + return extras; + } +} diff --git a/common/tests/robotests/src/com/android/tv/common/compat/TvInputInfoCompatTest.java b/common/tests/robotests/src/com/android/tv/common/compat/TvInputInfoCompatTest.java new file mode 100644 index 00000000..93c04b95 --- /dev/null +++ b/common/tests/robotests/src/com/android/tv/common/compat/TvInputInfoCompatTest.java @@ -0,0 +1,107 @@ +/* + * Copyright (C) 2018 The Android Open Source Project + * + * 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.android.tv.common.compat; + +import static com.google.common.truth.Truth.assertThat; +import static junit.framework.Assert.fail; + +import android.content.pm.ResolveInfo; +import android.content.res.XmlResourceParser; +import android.media.tv.TvInputInfo; +import androidx.test.InstrumentationRegistry; + +import com.android.tv.testing.constants.ConfigConstants; +import com.android.tv.testing.utils.TestUtils; + +import java.io.StringReader; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.robolectric.RobolectricTestRunner; +import org.robolectric.annotation.Config; +import org.xmlpull.v1.XmlPullParser; +import org.xmlpull.v1.XmlPullParserException; +import org.xmlpull.v1.XmlPullParserFactory; + +/** + * Tests for {@link TvInputInfoCompat}. + */ +@RunWith(RobolectricTestRunner.class) +@Config(sdk = ConfigConstants.SDK) +public class TvInputInfoCompatTest { + private TvInputInfoCompat mTvInputInfoCompat; + private String mInputXml; + @Before + public void setUp() throws Exception { + ResolveInfo resolveInfo = TestUtils.createResolveInfo("test", "test"); + TvInputInfo info = + TestUtils.createTvInputInfo( + resolveInfo, "test_input", "test1", TvInputInfo.TYPE_OTHER, false); + mTvInputInfoCompat = + new TvInputInfoCompat( + InstrumentationRegistry.getTargetContext(), info) { + @Override + XmlPullParser getXmlResourceParser() { + XmlPullParser xpp = null; + try { + xpp = XmlPullParserFactory.newInstance().newPullParser(); + xpp.setInput(new StringReader(mInputXml)); + xpp.setFeature( + XmlResourceParser.FEATURE_PROCESS_NAMESPACES, true); + } catch (XmlPullParserException e) { + fail("failed in setUp() " + e.getMessage()); + } + return xpp; + } + }; + } + + @Test + public void testGetAttributeValue_notTvInputTag() { + mInputXml = + "<not-tv-input xmlns:android=\"http://schemas.android.com/apk/res/android\"\n" + + " android:setupActivity=\"\"\n" + + " android:settingsActivity=\"\"/>\n"; + assertThat(mTvInputInfoCompat.getExtras()).isEmpty(); + } + + @Test + public void testGetAttributeValue_noExtra() { + mInputXml = + "<not-tv-input xmlns:android=\"http://schemas.android.com/apk/res/android\"\n" + + " android:setupActivity=\"\"\n" + + " android:settingsActivity=\"\"/>\n"; + assertThat(mTvInputInfoCompat.getExtras()).isEmpty(); + } + + @Test + public void testGetAttributeValue() { + mInputXml = + "<tv-input xmlns:android=\"http://schemas.android.com/apk/res/android\"\n" + + " android:setupActivity=\"\"\n" + + " android:settingsActivity=\"\">\n" + + " <extra android:name=\"otherAttr1\" android:value=\"false\" />\n" + + " <extra android:name=\"otherAttr2\" android:value=\"false\" />\n" + + " <extra android:name=" + + " \"com.android.tv.common.compat.tvinputinfocompat.audioOnly\"" + + " android:value=\"true\" />\n" + + "</tv-input>"; + assertThat(mTvInputInfoCompat.getExtras()) + .containsExactly("otherAttr1", "false", "otherAttr2", "false", + "com.android.tv.common.compat.tvinputinfocompat.audioOnly", "true"); + } +} diff --git a/common/tests/robotests/src/com/android/tv/common/compat/internal/PrivateCommandTest.java b/common/tests/robotests/src/com/android/tv/common/compat/internal/PrivateCommandTest.java new file mode 100644 index 00000000..02705a4b --- /dev/null +++ b/common/tests/robotests/src/com/android/tv/common/compat/internal/PrivateCommandTest.java @@ -0,0 +1,59 @@ +/* + * Copyright (C) 2018 The Android Open Source Project + * + * 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.android.tv.common.compat.internal; + +import static org.mockito.Mockito.only; +import static org.mockito.Mockito.verify; + +import com.android.tv.common.compat.api.SessionCompatCommands; +import com.android.tv.testing.constants.ConfigConstants; +import com.google.protobuf.InvalidProtocolBufferException; +import com.google.testing.mockito.Mocks; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.robolectric.RobolectricTestRunner; +import org.robolectric.annotation.Config; + +/** + * Tests sending {@link Commands.PrivateCommand}s to a {@link SessionCompatCommands} from {@link + * TvViewCompatProcessor} via {@link TifSessionCompatProcessor}. + */ +@RunWith(RobolectricTestRunner.class) +@Config(sdk = ConfigConstants.SDK) +public class PrivateCommandTest { + @Rule public final Mocks mocks = new Mocks(this); + + @Mock SessionCompatCommands mCallback; + + private TvViewCompatProcessor mTvViewCompatProcessor; + + @Before + public void setUp() { + TifSessionCompatProcessor sessionCompatProcessor = + new TifSessionCompatProcessor(null, mCallback); + mTvViewCompatProcessor = + new TvViewCompatProcessor(sessionCompatProcessor::handleAppPrivateCommand); + } + + @Test + public void notifyDevToast() throws InvalidProtocolBufferException { + mTvViewCompatProcessor.devMessage("Hello Developers"); + verify(mCallback, only()).onDevMessage("Hello Developers"); + } +} diff --git a/common/tests/robotests/src/com/android/tv/common/compat/internal/PrivateRecordingCommandTest.java b/common/tests/robotests/src/com/android/tv/common/compat/internal/PrivateRecordingCommandTest.java new file mode 100644 index 00000000..6d9949ff --- /dev/null +++ b/common/tests/robotests/src/com/android/tv/common/compat/internal/PrivateRecordingCommandTest.java @@ -0,0 +1,61 @@ +/* + * Copyright (C) 2018 The Android Open Source Project + * + * 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.android.tv.common.compat.internal; + +import static org.mockito.Mockito.only; +import static org.mockito.Mockito.verify; + +import com.android.tv.common.compat.api.RecordingSessionCompatCommands; +import com.android.tv.testing.constants.ConfigConstants; +import com.google.protobuf.InvalidProtocolBufferException; +import com.google.testing.mockito.Mocks; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.robolectric.RobolectricTestRunner; +import org.robolectric.annotation.Config; + +/** + * Tests sending {@link RecordingCommands.PrivateRecordingCommand}s to a {@link + * RecordingSessionCompatCommands} from {@link RecordingClientCompatProcessor} via {@link + * RecordingSessionCompatProcessor}. + */ +@RunWith(RobolectricTestRunner.class) +@Config(sdk = ConfigConstants.SDK) +public class PrivateRecordingCommandTest { + @Rule public final Mocks mocks = new Mocks(this); + + @Mock private RecordingSessionCompatCommands mCallback; + + private RecordingClientCompatProcessor mCompatProcessor; + + @Before + public void setUp() { + RecordingSessionCompatProcessor sessionCompatProcessor = + new RecordingSessionCompatProcessor(null, mCallback); + mCompatProcessor = + new RecordingClientCompatProcessor( + sessionCompatProcessor::handleAppPrivateCommand, null); + } + + @Test + public void notifyDevToast() throws InvalidProtocolBufferException { + mCompatProcessor.devMessage("Hello Recorders"); + verify(mCallback, only()).onDevMessage("Hello Recorders"); + } +} diff --git a/common/tests/robotests/src/com/android/tv/common/compat/internal/RecordingSessionEventTest.java b/common/tests/robotests/src/com/android/tv/common/compat/internal/RecordingSessionEventTest.java new file mode 100644 index 00000000..b5bd0a01 --- /dev/null +++ b/common/tests/robotests/src/com/android/tv/common/compat/internal/RecordingSessionEventTest.java @@ -0,0 +1,68 @@ +/* + * Copyright (C) 2018 The Android Open Source Project + * + * 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.android.tv.common.compat.internal; + +import static org.mockito.Mockito.only; +import static org.mockito.Mockito.verify; + +import com.android.tv.common.compat.api.RecordingClientCallbackCompatEvents; +import com.android.tv.testing.constants.ConfigConstants; +import com.google.protobuf.InvalidProtocolBufferException; +import com.google.testing.mockito.Mocks; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.robolectric.RobolectricTestRunner; +import org.robolectric.annotation.Config; + +/** + * Tests sending {@link RecordingEvents.RecordingSessionEvent}s to a {@link + * RecordingClientCallbackCompatEvents} from {@link RecordingSessionCompatProcessor} via {@link + * RecordingClientCompatProcessor}. + */ +@RunWith(RobolectricTestRunner.class) +@Config(sdk = ConfigConstants.SDK) +public class RecordingSessionEventTest { + @Rule public final Mocks mocks = new Mocks(this); + + @Mock RecordingClientCallbackCompatEvents mCallback; + + private RecordingSessionCompatProcessor mCompatProcess; + + @Before + public void setUp() { + RecordingClientCompatProcessor compatProcessor = + new RecordingClientCompatProcessor(null, mCallback); + mCompatProcess = + new RecordingSessionCompatProcessor( + (event, data) -> compatProcessor.handleEvent("testinput", event, data), + null); + } + + @Test + public void notifyDevToast() throws InvalidProtocolBufferException { + mCompatProcess.notifyDevToast("Recording"); + verify(mCallback, only()).onDevToast("testinput", "Recording"); + } + + @Test + public void notifyRecordingStarted() throws InvalidProtocolBufferException { + mCompatProcess.notifyRecordingStarted("file:example"); + verify(mCallback, only()).onRecordingStarted("testinput", "file:example"); + } +} diff --git a/common/tests/robotests/src/com/android/tv/common/compat/internal/SessionEventTest.java b/common/tests/robotests/src/com/android/tv/common/compat/internal/SessionEventTest.java new file mode 100644 index 00000000..0cc300bb --- /dev/null +++ b/common/tests/robotests/src/com/android/tv/common/compat/internal/SessionEventTest.java @@ -0,0 +1,68 @@ +/* + * Copyright (C) 2018 The Android Open Source Project + * + * 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.android.tv.common.compat.internal; + +import static org.mockito.Mockito.only; +import static org.mockito.Mockito.verify; + +import com.android.tv.common.compat.api.TvInputCallbackCompatEvents; +import com.android.tv.testing.constants.ConfigConstants; +import com.google.protobuf.InvalidProtocolBufferException; +import com.google.testing.mockito.Mocks; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.robolectric.RobolectricTestRunner; +import org.robolectric.annotation.Config; + +/** + * Tests sending {@link Events.SessionEvent}s to a {@link TvInputCallbackCompatEvents} from {@link + * TifSessionCompatProcessor} via {@link TvViewCompatProcessor}. + */ +@RunWith(RobolectricTestRunner.class) +@Config(sdk = ConfigConstants.SDK) +public class SessionEventTest { + @Rule public final Mocks mocks = new Mocks(this); + + @Mock TvInputCallbackCompatEvents mCallback; + + private TifSessionCompatProcessor mCompatProcess; + + @Before + public void setUp() { + TvViewCompatProcessor tvViewCompatProcessor = new TvViewCompatProcessor(null); + tvViewCompatProcessor.setCallback(mCallback); + mCompatProcess = + new TifSessionCompatProcessor( + (event, data) -> + tvViewCompatProcessor.handleEvent("testinput", event, data), + null); + } + + @Test + public void notifyDevToast() throws InvalidProtocolBufferException { + mCompatProcess.notifyDevToast("testing"); + verify(mCallback, only()).onDevToast("testinput", "testing"); + } + + @Test + public void notifySignalStrength() throws InvalidProtocolBufferException { + mCompatProcess.notifySignalStrength(3); + verify(mCallback, only()).onSignalStrength("testinput", 3); + } +} diff --git a/common/tests/robotests/src/com/android/tv/common/dev/DeveloperPreferenceTest.java b/common/tests/robotests/src/com/android/tv/common/dev/DeveloperPreferenceTest.java new file mode 100644 index 00000000..a9c15ada --- /dev/null +++ b/common/tests/robotests/src/com/android/tv/common/dev/DeveloperPreferenceTest.java @@ -0,0 +1,54 @@ +/* + * Copyright (C) 2019 The Android Open Source Project + * + * 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.android.tv.common.dev; + +import static com.google.common.truth.Truth.assertThat; + +import com.android.tv.testing.constants.ConfigConstants; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.robolectric.RobolectricTestRunner; +import org.robolectric.RuntimeEnvironment; +import org.robolectric.annotation.Config; + +/** Test for {@link DeveloperPreference}. */ +@RunWith(RobolectricTestRunner.class) +@Config(sdk = ConfigConstants.SDK) +public class DeveloperPreferenceTest { + + @Test + public void createBoolean_default_true() { + DeveloperPreference<Boolean> devPref = DeveloperPreference.create("test", true); + assertThat(devPref.get(RuntimeEnvironment.systemContext)).isTrue(); + DeveloperPreference.getPreferences(RuntimeEnvironment.systemContext) + .edit() + .putBoolean("test", false) + .apply(); + assertThat(devPref.get(RuntimeEnvironment.systemContext)).isFalse(); + } + + @Test + public void create_integer_default_one() { + DeveloperPreference<Integer> devPref = DeveloperPreference.create("test", 1); + assertThat(devPref.get(RuntimeEnvironment.systemContext)).isEqualTo(1); + DeveloperPreference.getPreferences(RuntimeEnvironment.systemContext) + .edit() + .putInt("test", 2) + .apply(); + assertThat(devPref.get(RuntimeEnvironment.systemContext)).isEqualTo(2); + } +} diff --git a/common/tests/robotests/src/com/android/tv/common/support/tis/BaseTvInputServiceTest.java b/common/tests/robotests/src/com/android/tv/common/support/tis/BaseTvInputServiceTest.java new file mode 100644 index 00000000..bcd911f9 --- /dev/null +++ b/common/tests/robotests/src/com/android/tv/common/support/tis/BaseTvInputServiceTest.java @@ -0,0 +1,145 @@ +package com.android.tv.common.support.tis; + +import static com.google.common.truth.Truth.assertThat; + +import android.content.Intent; +import android.media.tv.TvContentRating; +import android.media.tv.TvInputManager; +import android.net.Uri; +import android.os.Build; +import android.support.annotation.Nullable; +import android.view.Surface; +import com.android.tv.common.support.tis.TifSession.TifSessionCallbacks; +import com.android.tv.common.support.tis.TifSession.TifSessionFactory; +import com.google.thirdparty.robolectric.GoogleRobolectricTestRunner; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.robolectric.Robolectric; +import org.robolectric.RuntimeEnvironment; +import org.robolectric.android.controller.ServiceController; +import org.robolectric.annotation.Config; + +/** Tests for {@link BaseTvInputService}. */ +@RunWith(GoogleRobolectricTestRunner.class) +@Config(minSdk = Build.VERSION_CODES.LOLLIPOP) +public class BaseTvInputServiceTest { + + private static class TestTvInputService extends BaseTvInputService { + + private final SessionManager sessionManager = new SimpleSessionManager(1); + + private int parentalControlsChangedCount = 0; + private final TifSessionFactory sessionFactory; + + private TestTvInputService() { + super(); + this.sessionFactory = + new TifSessionFactory() { + @Override + public TifSession create(TifSessionCallbacks callbacks, String inputId) { + return new TifSession(callbacks) { + @Override + public boolean onSetSurface(@Nullable Surface surface) { + return false; + } + + @Override + public void onSurfaceChanged(int format, int width, int height) {} + + @Override + public void onSetStreamVolume(float volume) {} + + @Override + public boolean onTune(Uri channelUri) { + return false; + } + + @Override + public void onSetCaptionEnabled(boolean enabled) {} + + @Override + public void onUnblockContent(TvContentRating unblockedRating) {} + + @Override + public void onParentalControlsChanged() { + parentalControlsChangedCount++; + } + }; + } + }; + } + + @Override + protected TifSessionFactory getTifSessionFactory() { + return sessionFactory; + } + + @Override + protected SessionManager getSessionManager() { + return sessionManager; + } + + private int getParentalControlsChangedCount() { + return parentalControlsChangedCount; + } + } + + TestTvInputService tvInputService; + ServiceController<TestTvInputService> controller; + + @Before + public void setUp() { + controller = Robolectric.buildService(TestTvInputService.class); + tvInputService = controller.create().get(); + } + + @Test + public void createSession_once() { + assertThat(tvInputService.onCreateSession("test")).isNotNull(); + } + + @Test + public void createSession_twice() { + WrappedSession first = tvInputService.onCreateSession("test"); + assertThat(first).isNotNull(); + WrappedSession second = tvInputService.onCreateSession("test"); + assertThat(second).isNull(); + } + + @Test + public void createSession_release() { + WrappedSession first = tvInputService.onCreateSession("test"); + assertThat(first).isNotNull(); + first.onRelease(); + WrappedSession second = tvInputService.onCreateSession("test"); + assertThat(second).isNotNull(); + assertThat(second).isNotSameInstanceAs(first); + } + + @Test + public void testReceiver_actionEnabledChanged() { + tvInputService.getSessionManager().addSession(tvInputService.onCreateSession("test")); + tvInputService.broadcastReceiver.onReceive( + RuntimeEnvironment.application, + new Intent(TvInputManager.ACTION_PARENTAL_CONTROLS_ENABLED_CHANGED)); + assertThat(tvInputService.getParentalControlsChangedCount()).isEqualTo(1); + } + + @Test + public void testReceiver_actionBlockedChanged() { + tvInputService.getSessionManager().addSession(tvInputService.onCreateSession("test")); + tvInputService.broadcastReceiver.onReceive( + RuntimeEnvironment.application, + new Intent(TvInputManager.ACTION_BLOCKED_RATINGS_CHANGED)); + assertThat(tvInputService.getParentalControlsChangedCount()).isEqualTo(1); + } + + @Test + public void testReceiver_invalidAction() { + tvInputService.getSessionManager().addSession(tvInputService.onCreateSession("test")); + tvInputService.broadcastReceiver.onReceive( + RuntimeEnvironment.application, new Intent("test")); + assertThat(tvInputService.getParentalControlsChangedCount()).isEqualTo(0); + } +} diff --git a/common/tests/robotests/src/com/android/tv/common/support/tis/SimpleSessionManagerTest.java b/common/tests/robotests/src/com/android/tv/common/support/tis/SimpleSessionManagerTest.java new file mode 100644 index 00000000..5119f55b --- /dev/null +++ b/common/tests/robotests/src/com/android/tv/common/support/tis/SimpleSessionManagerTest.java @@ -0,0 +1,122 @@ +package com.android.tv.common.support.tis; + +import static com.google.common.truth.Truth.assertThat; + +import android.media.tv.TvContentRating; +import android.net.Uri; +import android.os.Build; +import android.support.annotation.FloatRange; +import android.support.annotation.Nullable; +import android.view.Surface; +import com.android.tv.common.support.tis.TifSession.TifSessionCallbacks; +import com.android.tv.common.support.tis.TifSession.TifSessionFactory; +import com.google.thirdparty.robolectric.GoogleRobolectricTestRunner; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.robolectric.RuntimeEnvironment; +import org.robolectric.annotation.Config; + +/** Tests for {@link SimpleSessionManager}. */ +@RunWith(GoogleRobolectricTestRunner.class) +@Config(minSdk = Build.VERSION_CODES.LOLLIPOP) +public class SimpleSessionManagerTest { + + private SimpleSessionManager sessionManager; + + @Before + public void setup() { + sessionManager = new SimpleSessionManager(1); + } + + @Test + public void canCreateSession_none() { + assertThat(sessionManager.canCreateNewSession()).isTrue(); + } + + @Test + public void canCreateSession_one() { + sessionManager.addSession(createTestSession()); + assertThat(sessionManager.canCreateNewSession()).isFalse(); + } + + @Test + public void addSession() { + assertThat(sessionManager.getSessionCount()).isEqualTo(0); + sessionManager.addSession(createTestSession()); + assertThat(sessionManager.getSessionCount()).isEqualTo(1); + } + + @Test + public void onRelease() { + WrappedSession testSession = createTestSession(); + sessionManager.addSession(testSession); + assertThat(sessionManager.getSessionCount()).isEqualTo(1); + testSession.onRelease(); + assertThat(sessionManager.getSessionCount()).isEqualTo(0); + } + + @Test + public void onRelease_withUnRegisteredSession() { + WrappedSession testSession = createTestSession(); + sessionManager.addSession(createTestSession()); + assertThat(sessionManager.getSessionCount()).isEqualTo(1); + testSession.onRelease(); + assertThat(sessionManager.getSessionCount()).isEqualTo(1); + } + + @Test + public void getSessions() { + WrappedSession testSession = createTestSession(); + sessionManager.addSession(testSession); + assertThat(sessionManager.getSessions()).containsExactly(testSession); + } + + private WrappedSession createTestSession() { + return new WrappedSession( + RuntimeEnvironment.application, + sessionManager, + new TestTifSessionFactory(), + "testInputId"); + } + + private static final class TestTifSessionFactory implements TifSessionFactory { + + @Override + public TifSession create(TifSessionCallbacks callbacks, String inputId) { + return new TestTifSession(callbacks); + } + } + + private static final class TestTifSession extends TifSession { + + private TestTifSession(TifSessionCallbacks callbacks) { + super(callbacks); + } + + @Override + public void onRelease() {} + + @Override + public boolean onSetSurface(@Nullable Surface surface) { + return false; + } + + @Override + public void onSurfaceChanged(int format, int width, int height) {} + + @Override + public void onSetStreamVolume(@FloatRange(from = 0.0, to = 1.0) float volume) {} + + @Override + public boolean onTune(Uri channelUri) { + return false; + } + + @Override + public void onSetCaptionEnabled(boolean enabled) {} + + @Override + public void onUnblockContent(TvContentRating unblockedRating) {} + } +} diff --git a/common/tests/robotests/src/com/android/tv/common/support/tis/TisSessionTest.java b/common/tests/robotests/src/com/android/tv/common/support/tis/TisSessionTest.java new file mode 100644 index 00000000..f6bb67e0 --- /dev/null +++ b/common/tests/robotests/src/com/android/tv/common/support/tis/TisSessionTest.java @@ -0,0 +1,205 @@ +package com.android.tv.common.support.tis; + +import static com.google.common.truth.Truth.assertThat; + +import android.media.tv.TvContentRating; +import android.media.tv.TvInputManager; +import android.media.tv.TvTrackInfo; +import android.net.Uri; +import android.os.Build; +import android.support.annotation.FloatRange; +import android.support.annotation.NonNull; +import android.support.annotation.Nullable; +import android.util.Pair; +import android.view.Surface; +import android.view.View; +import com.android.tv.common.support.tis.TifSession.TifSessionCallbacks; +import com.google.common.collect.ImmutableList; +import com.google.thirdparty.robolectric.GoogleRobolectricTestRunner; +import java.util.List; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.robolectric.annotation.Config; + +/** Tests for {@link TifSession}. */ +@RunWith(GoogleRobolectricTestRunner.class) +@Config(minSdk = Build.VERSION_CODES.LOLLIPOP) +public class TisSessionTest { + + private TestSession testSession; + private TestCallback testCallback; + + @Before + public void setup() { + testCallback = new TestCallback(); + testSession = new TestSession(testCallback); + } + + @Test + public void notifyChannelReturned() { + Uri uri = Uri.parse("http://example.com"); + testSession.notifyChannelRetuned(uri); + assertThat(testCallback.channelUri).isEqualTo(uri); + } + + @Test + public void notifyTracksChanged() { + List<TvTrackInfo> tracks = + ImmutableList.of(new TvTrackInfo.Builder(TvTrackInfo.TYPE_VIDEO, "test").build()); + testSession.notifyTracksChanged(tracks); + assertThat(testCallback.tracks).isEqualTo(tracks); + } + + @Test + public void notifyTrackSelected() { + testSession.notifyTrackSelected(TvTrackInfo.TYPE_AUDIO, "audio_test"); + assertThat(testCallback.trackSelected) + .isEqualTo(Pair.create(TvTrackInfo.TYPE_AUDIO, "audio_test")); + } + + @Test + public void notifyVideoAvailable() { + testSession.notifyVideoAvailable(); + assertThat(testCallback.videoAvailable).isTrue(); + } + + @Test + public void notifyVideoUnavailable() { + testSession.notifyVideoUnavailable(TvInputManager.VIDEO_UNAVAILABLE_REASON_UNKNOWN); + assertThat(testCallback.notifyVideoUnavailableReason) + .isEqualTo(TvInputManager.VIDEO_UNAVAILABLE_REASON_UNKNOWN); + } + + @Test + public void notifyContentAllowed() { + testSession.notifyContentAllowed(); + assertThat(testCallback.contentAllowed).isTrue(); + } + + @Test + public void notifyContentBlocked() { + TvContentRating rating = TvContentRating.createRating("1", "2", "3"); + testSession.notifyContentBlocked(rating); + assertThat(testCallback.blockedContentRating).isEqualTo(rating); + } + + @Test + public void notifyTimeShiftStatusChanged() { + testSession.notifyTimeShiftStatusChanged(TvInputManager.TIME_SHIFT_STATUS_AVAILABLE); + assertThat(testCallback.timeShiftStatus) + .isEqualTo(TvInputManager.TIME_SHIFT_STATUS_AVAILABLE); + } + + @Test + public void testSetOverlayViewEnabled() { + testSession.helpTestSetOverlayViewEnabled(true); + assertThat(testCallback.overlayViewEnabled).isTrue(); + + testSession.helpTestSetOverlayViewEnabled(false); + assertThat(testCallback.overlayViewEnabled).isFalse(); + } + + @Test + public void testOnCreateOverlayView() { + View actualView = testSession.onCreateOverlayView(); + assertThat(actualView).isNull(); // Default implementation returns a null. + } + + @Test + public void testOnOverlayViewSizeChanged() { + testSession.onOverlayViewSizeChanged(5 /* width */, 7 /* height */); + // Just verifing that the call completes. + } + + private static final class TestCallback implements TifSessionCallbacks { + + private Uri channelUri; + private List<TvTrackInfo> tracks; + private Pair<Integer, String> trackSelected; + private boolean videoAvailable; + private int notifyVideoUnavailableReason; + private boolean contentAllowed; + private TvContentRating blockedContentRating; + private int timeShiftStatus; + private boolean overlayViewEnabled; + + @Override + public void notifyChannelRetuned(Uri channelUri) { + this.channelUri = channelUri; + } + + @Override + public void notifyTracksChanged(List<TvTrackInfo> tracks) { + this.tracks = tracks; + } + + @Override + public void notifyTrackSelected(int type, String trackId) { + this.trackSelected = Pair.create(type, trackId); + } + + @Override + public void notifyVideoAvailable() { + this.videoAvailable = true; + } + + @Override + public void notifyVideoUnavailable(int reason) { + this.notifyVideoUnavailableReason = reason; + } + + @Override + public void notifyContentAllowed() { + this.contentAllowed = true; + } + + @Override + public void notifyContentBlocked(@NonNull TvContentRating rating) { + this.blockedContentRating = rating; + } + + @Override + public void notifyTimeShiftStatusChanged(int status) { + this.timeShiftStatus = status; + } + + @Override + public void setOverlayViewEnabled(boolean enabled) { + this.overlayViewEnabled = enabled; + } + } + + private static final class TestSession extends TifSession { + + private TestSession(TifSessionCallbacks callbacks) { + super(callbacks); + } + + @Override + public boolean onSetSurface(@Nullable Surface surface) { + return false; + } + + @Override + public void onSurfaceChanged(int format, int width, int height) {} + + @Override + public void onSetStreamVolume(@FloatRange(from = 0.0, to = 1.0) float volume) {} + + @Override + public boolean onTune(Uri channelUri) { + return false; + } + + @Override + public void onSetCaptionEnabled(boolean enabled) {} + + @Override + public void onUnblockContent(TvContentRating unblockedRating) {} + + public void helpTestSetOverlayViewEnabled(boolean enabled) { + super.setOverlayViewEnabled(enabled); + } + } +} diff --git a/common/tests/robotests/src/com/android/tv/common/support/tis/WrappedSessionTest.java b/common/tests/robotests/src/com/android/tv/common/support/tis/WrappedSessionTest.java new file mode 100644 index 00000000..4760e0bf --- /dev/null +++ b/common/tests/robotests/src/com/android/tv/common/support/tis/WrappedSessionTest.java @@ -0,0 +1,188 @@ +package com.android.tv.common.support.tis; + +import static com.google.common.truth.Truth.assertThat; + +import android.media.PlaybackParams; +import android.media.tv.TvContentRating; +import android.net.Uri; +import android.os.Build; +import android.view.View; +import com.android.tv.common.support.tis.TifSession.TifSessionCallbacks; +import com.android.tv.common.support.tis.TifSession.TifSessionFactory; +import com.google.thirdparty.robolectric.GoogleRobolectricTestRunner; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.MockitoAnnotations; +import org.robolectric.RuntimeEnvironment; +import org.robolectric.annotation.Config; + +/** Tests for {@link WrappedSession}. */ +@RunWith(GoogleRobolectricTestRunner.class) +@Config(minSdk = Build.VERSION_CODES.LOLLIPOP) +public class WrappedSessionTest { + + @Mock TifSession mockDelegate; + private TifSessionFactory sessionFactory = + new TifSessionFactory() { + @Override + public TifSession create(TifSessionCallbacks callbacks, String inputId) { + return mockDelegate; + } + }; + private WrappedSession wrappedSession; + private SimpleSessionManager sessionManager = new SimpleSessionManager(1); + + @Before + public void setup() { + MockitoAnnotations.initMocks(this); + wrappedSession = + new WrappedSession( + RuntimeEnvironment.application, + sessionManager, + sessionFactory, + "testInputId"); + } + + @Test + public void onRelease() { + sessionManager.addSession(wrappedSession); + assertThat(sessionManager.getSessionCount()).isEqualTo(1); + wrappedSession.onRelease(); + assertThat(sessionManager.getSessionCount()).isEqualTo(0); + Mockito.verify(mockDelegate).onRelease(); + Mockito.verifyNoMoreInteractions(mockDelegate); + } + + @Test + public void onSetSurface() { + wrappedSession.onSetSurface(null); + Mockito.verify(mockDelegate).onSetSurface(null); + Mockito.verifyNoMoreInteractions(mockDelegate); + } + + @Test + public void onSurfaceChanged() { + wrappedSession.onSurfaceChanged(1, 2, 3); + Mockito.verify(mockDelegate).onSurfaceChanged(1, 2, 3); + Mockito.verifyNoMoreInteractions(mockDelegate); + } + + @Test + public void onSetStreamVolume() { + wrappedSession.onSetStreamVolume(.8f); + Mockito.verify(mockDelegate).onSetStreamVolume(.8f); + Mockito.verifyNoMoreInteractions(mockDelegate); + } + + @Test + public void onTune() { + Uri uri = Uri.EMPTY; + wrappedSession.onTune(uri); + Mockito.verify(mockDelegate).onTune(uri); + Mockito.verifyNoMoreInteractions(mockDelegate); + } + + @Test + public void onSetCaptionEnabled() { + wrappedSession.onSetCaptionEnabled(true); + Mockito.verify(mockDelegate).onSetCaptionEnabled(true); + Mockito.verifyNoMoreInteractions(mockDelegate); + } + + @Test + @Config(minSdk = Build.VERSION_CODES.M) + public void onTimeShiftGetCurrentPosition() { + Mockito.when(mockDelegate.onTimeShiftGetCurrentPosition()).thenReturn(7L); + assertThat(wrappedSession.onTimeShiftGetCurrentPosition()).isEqualTo(7L); + Mockito.verify(mockDelegate).onTimeShiftGetCurrentPosition(); + Mockito.verifyNoMoreInteractions(mockDelegate); + } + + @Test + @Config(minSdk = Build.VERSION_CODES.M) + public void onTimeShiftGetStartPosition() { + Mockito.when(mockDelegate.onTimeShiftGetStartPosition()).thenReturn(8L); + assertThat(wrappedSession.onTimeShiftGetStartPosition()).isEqualTo(8L); + Mockito.verify(mockDelegate).onTimeShiftGetStartPosition(); + Mockito.verifyNoMoreInteractions(mockDelegate); + } + + @Test + @Config(minSdk = Build.VERSION_CODES.M) + public void onTimeShiftPause() { + wrappedSession.onTimeShiftPause(); + Mockito.verify(mockDelegate).onTimeShiftPause(); + Mockito.verifyNoMoreInteractions(mockDelegate); + } + + @Test + @Config(minSdk = Build.VERSION_CODES.M) + public void onTimeShiftResume() { + wrappedSession.onTimeShiftResume(); + Mockito.verify(mockDelegate).onTimeShiftResume(); + Mockito.verifyNoMoreInteractions(mockDelegate); + } + + @Test + @Config(minSdk = Build.VERSION_CODES.M) + public void onTimeShiftSeekTo() { + wrappedSession.onTimeShiftSeekTo(9L); + Mockito.verify(mockDelegate).onTimeShiftSeekTo(9L); + Mockito.verifyNoMoreInteractions(mockDelegate); + } + + @Test + @Config(minSdk = Build.VERSION_CODES.M) + public void onTimeShiftSetPlaybackParams() { + PlaybackParams paras = new PlaybackParams(); + wrappedSession.onTimeShiftSetPlaybackParams(paras); + Mockito.verify(mockDelegate).onTimeShiftSetPlaybackParams(paras); + Mockito.verifyNoMoreInteractions(mockDelegate); + } + + @Test + @Config(minSdk = Build.VERSION_CODES.M) + public void onUnblockContent() { + TvContentRating rating = + TvContentRating.createRating( + "domain", "rating_system", "rating", "subrating1", "subrating2"); + wrappedSession.onUnblockContent(rating); + Mockito.verify(mockDelegate).onUnblockContent(rating); + Mockito.verifyNoMoreInteractions(mockDelegate); + } + + @Test + public void onParentalControlsChanged() { + wrappedSession.onParentalControlsChanged(); + Mockito.verify(mockDelegate).onParentalControlsChanged(); + Mockito.verifyNoMoreInteractions(mockDelegate); + } + + @Test + public void testSetOverlayViewEnabled() { + wrappedSession.setOverlayViewEnabled(true); + // Just verifying that the call completes. + } + + @Test + public void testOnCreateOverlayView() { + View v = new View(RuntimeEnvironment.application); + Mockito.when(mockDelegate.onCreateOverlayView()).thenReturn(v); + + View actualView = wrappedSession.onCreateOverlayView(); + + assertThat(actualView).isEqualTo(v); + Mockito.verify(mockDelegate).onCreateOverlayView(); + Mockito.verifyNoMoreInteractions(mockDelegate); + } + + @Test + public void testOnOverlayViewSizeChanged() { + wrappedSession.onOverlayViewSizeChanged(5 /* width */, 13 /* height */); + Mockito.verify(mockDelegate).onOverlayViewSizeChanged(5, 13); + Mockito.verifyNoMoreInteractions(mockDelegate); + } +} diff --git a/common/tests/robotests/src/com/android/tv/common/support/tvprovider/TvContractCompatXTest.java b/common/tests/robotests/src/com/android/tv/common/support/tvprovider/TvContractCompatXTest.java new file mode 100644 index 00000000..db32be68 --- /dev/null +++ b/common/tests/robotests/src/com/android/tv/common/support/tvprovider/TvContractCompatXTest.java @@ -0,0 +1,69 @@ +/* + * Copyright (C) 2019 The Android Open Source Project + * + * 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.android.tv.common.support.tvprovider; + +import static com.google.common.truth.Truth.assertThat; + +import android.net.Uri; +import com.android.tv.testing.constants.ConfigConstants; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.robolectric.RobolectricTestRunner; +import org.robolectric.annotation.Config; + +/** Test for {@link TvContractCompatX}. */ +@RunWith(RobolectricTestRunner.class) +@Config(sdk = ConfigConstants.SDK) +public class TvContractCompatXTest { + + @Test + public void buildChannelUri() { + assertThat(TvContractCompatX.buildChannelUri("com.example", "foo")) + .isEqualTo( + Uri.parse( + "content://android.media.tv/channel?" + + "package=com.example&internal_provider_id=foo")); + } + + @Test + public void buildProgramsUriForChannel() { + assertThat(TvContractCompatX.buildProgramsUriForChannel("com.example", "foo")) + .isEqualTo( + Uri.parse( + "content://android.media.tv/program?" + + "package=com.example&internal_provider_id=foo")); + } + + @Test + public void buildProgramsUriForChannel_period() { + assertThat(TvContractCompatX.buildProgramsUriForChannel("com.example", "foo", 1234L, 5467L)) + .isEqualTo( + Uri.parse( + "content://android.media.tv/program?" + + "package=com.example&internal_provider_id=foo" + + "&start_time=1234&end_time=5467")); + } + + @Test + public void buildProgramsUri_period() { + assertThat(TvContractCompatX.buildProgramsUri(1234L, 5467L)) + .isEqualTo( + Uri.parse( + "content://android.media.tv/program?" + + "start_time=1234&end_time=5467")); + } +} diff --git a/common/tests/robotests/src/com/android/tv/common/util/CommonUtilsTest.java b/common/tests/robotests/src/com/android/tv/common/util/CommonUtilsTest.java new file mode 100644 index 00000000..0b793dbc --- /dev/null +++ b/common/tests/robotests/src/com/android/tv/common/util/CommonUtilsTest.java @@ -0,0 +1,76 @@ +/* + * Copyright (C) 2018 The Android Open Source Project + * + * 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.android.tv.common.util; + +import static com.google.common.truth.Truth.assertThat; + +import com.android.tv.testing.constants.ConfigConstants; +import com.google.thirdparty.robolectric.GoogleRobolectricTestRunner; +import java.io.File; +import java.io.IOException; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.robolectric.RuntimeEnvironment; +import org.robolectric.annotation.Config; + +/** Tests for {@link CommonUtils}. */ +@RunWith(GoogleRobolectricTestRunner.class) +@Config(sdk = ConfigConstants.SDK) +public class CommonUtilsTest { + + @Test + public void deleteDirOrFile_file() throws IOException { + File file = new File(RuntimeEnvironment.application.getExternalFilesDir(null), "file"); + assertThat(file.createNewFile()).isTrue(); + assertThat(CommonUtils.deleteDirOrFile(file)).isTrue(); + assertThat(file.exists()).isFalse(); + } + + @Test + public void deleteDirOrFile_Dir() throws IOException { + File dir = new File(RuntimeEnvironment.application.getExternalFilesDir(null), "dir"); + assertThat(dir.mkdirs()).isTrue(); + assertThat(new File(dir, "file").createNewFile()).isTrue(); + assertThat(CommonUtils.deleteDirOrFile(dir)).isTrue(); + assertThat(dir.exists()).isFalse(); + } + + @Test + public void deleteDirOrFile_unreadableDir() throws IOException { + File dir = new File(RuntimeEnvironment.application.getExternalFilesDir(null), "dir"); + assertThat(dir.mkdirs()).isTrue(); + assertThat(new File(dir, "file").createNewFile()).isTrue(); + dir.setReadable(false); + // Since dir is unreadable dir.listFiles() returns null and file is not deleted thus + // dir is not actually deleted. + assertThat(CommonUtils.deleteDirOrFile(dir)).isFalse(); + assertThat(dir.exists()).isTrue(); + } + + @Test + public void deleteDirOrFile_unreadableSubDir() throws IOException { + File dir = new File(RuntimeEnvironment.application.getExternalFilesDir(null), "dir"); + File subDir = new File(dir, "sub"); + assertThat(subDir.mkdirs()).isTrue(); + File file = new File(subDir, "file"); + assertThat(file.createNewFile()).isTrue(); + subDir.setReadable(false); + // Since subDir is unreadable subDir.listFiles() returns null and file is not deleted thus + // dir is not actually deleted. + assertThat(CommonUtils.deleteDirOrFile(dir)).isFalse(); + assertThat(dir.exists()).isTrue(); + } +} diff --git a/common/tests/robotests/src/com/android/tv/common/util/ContentUriUtilsTest.java b/common/tests/robotests/src/com/android/tv/common/util/ContentUriUtilsTest.java new file mode 100644 index 00000000..be705a91 --- /dev/null +++ b/common/tests/robotests/src/com/android/tv/common/util/ContentUriUtilsTest.java @@ -0,0 +1,46 @@ +/* + * Copyright (C) 2018 The Android Open Source Project + * + * 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.android.tv.common.util; + +import static com.google.common.truth.Truth.assertThat; + +import android.net.Uri; +import com.android.tv.testing.constants.ConfigConstants; +import com.google.thirdparty.robolectric.GoogleRobolectricTestRunner; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.robolectric.annotation.Config; + +/** Tests for {@link ContentUriUtils}. */ +@RunWith(GoogleRobolectricTestRunner.class) +@Config(sdk = ConfigConstants.SDK) +public class ContentUriUtilsTest { + + @Test + public void safeParseId_empty() { + assertThat(ContentUriUtils.safeParseId(Uri.EMPTY)).isEqualTo(-1); + } + + @Test + public void safeParseId_bad() { + assertThat(ContentUriUtils.safeParseId(Uri.parse("foo/bad"))).isEqualTo(-1); + } + + @Test + public void safeParseId_123() { + assertThat(ContentUriUtils.safeParseId(Uri.parse("foo/123"))).isEqualTo(123); + } +} |