diff options
Diffstat (limited to 'tests/common/src/com/android/tv')
6 files changed, 233 insertions, 4 deletions
diff --git a/tests/common/src/com/android/tv/MockTvApplication.java b/tests/common/src/com/android/tv/MockTvApplication.java new file mode 100644 index 00000000..7060e6b0 --- /dev/null +++ b/tests/common/src/com/android/tv/MockTvApplication.java @@ -0,0 +1,86 @@ +/* + * Copyright (C) 2015 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; + +import android.app.Application; +import android.test.mock.MockApplication; + +import com.android.tv.analytics.Analytics; +import com.android.tv.analytics.OptOutPreferenceHelper; +import com.android.tv.analytics.Tracker; +import com.android.tv.data.ChannelDataManager; +import com.android.tv.data.ProgramDataManager; +import com.android.tv.dvr.DvrDataManager; +import com.android.tv.dvr.DvrManager; +import com.android.tv.dvr.DvrSessionManager; +import com.android.tv.util.TvInputManagerHelper; + +/** + * A mock TV Application used for testing. + */ +public class MockTvApplication extends MockApplication implements ApplicationSingletons { + public final ApplicationSingletons mDelegate; + + public MockTvApplication(ApplicationSingletons delegate) { + mDelegate = delegate; + } + + @Override + public DvrManager getDvrManager() { + return mDelegate.getDvrManager(); + } + + @Override + public DvrSessionManager getDvrSessionManger() { + return mDelegate.getDvrSessionManger(); + } + + @Override + public Analytics getAnalytics() { + return mDelegate.getAnalytics(); + } + + @Override + public Tracker getTracker() { + return mDelegate.getTracker(); + } + + @Override + public OptOutPreferenceHelper getOptPreferenceHelper() { + return mDelegate.getOptPreferenceHelper(); + } + + @Override + public ChannelDataManager getChannelDataManager() { + return mDelegate.getChannelDataManager(); + } + + @Override + public ProgramDataManager getProgramDataManager() { + return mDelegate.getProgramDataManager(); + } + + @Override + public DvrDataManager getDvrDataManager() { + return mDelegate.getDvrDataManager(); + } + + @Override + public TvInputManagerHelper getTvInputManagerHelper() { + return mDelegate.getTvInputManagerHelper(); + } +} diff --git a/tests/common/src/com/android/tv/testing/ChannelUtils.java b/tests/common/src/com/android/tv/testing/ChannelUtils.java index 8d96cbed..00881c86 100644 --- a/tests/common/src/com/android/tv/testing/ChannelUtils.java +++ b/tests/common/src/com/android/tv/testing/ChannelUtils.java @@ -41,6 +41,7 @@ import java.util.Map; */ public class ChannelUtils { private static final String TAG = "ChannelUtils"; + private static final boolean DEBUG = false; /** * Query and return the map of (channel_id, ChannelInfo). @@ -115,10 +116,10 @@ public class ChannelUtils { Long rowId = existingChannelsMap.get(channel.originalNetworkId); Uri uri; if (rowId == null) { - Log.v(TAG, "Inserting "+ channel); + if (DEBUG) Log.d(TAG, "Inserting "+ channel); uri = resolver.insert(TvContract.Channels.CONTENT_URI, values); } else { - Log.v(TAG, "Updating "+ channel); + if (DEBUG) Log.d(TAG, "Updating "+ channel); uri = TvContract.buildChannelUri(rowId); resolver.update(uri, values, null, null); existingChannelsMap.remove(channel.originalNetworkId); diff --git a/tests/common/src/com/android/tv/testing/FakeClock.java b/tests/common/src/com/android/tv/testing/FakeClock.java new file mode 100644 index 00000000..372a7569 --- /dev/null +++ b/tests/common/src/com/android/tv/testing/FakeClock.java @@ -0,0 +1,106 @@ +/* + * Copyright (C) 2015 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.testing; + +import com.android.tv.util.Clock; + +import java.util.concurrent.TimeUnit; + +/** + * Fake implementation of Clock suitable for testing. + * + * <p>The current time only changes if {@link #setCurrentTimeMillis(long)}, {@link #increment} or + * {@link #sleep(long)} is called. + */ +public class FakeClock implements Clock { + /** + * Creates a fake clock with the time set to now and the boot time set to now - 100,000. + */ + public static FakeClock createWithCurrentTime() { + long now = System.currentTimeMillis(); + return new FakeClock(now, now - 100_000); + } + + /** + * Creates a fake clock with the time set to zero. + */ + public static FakeClock createWithTimeOne() { + return new FakeClock(1L, 0L); + } + + + private long mCurrentTimeMillis; + + private long mBootTimeMillis; + + private FakeClock(long currentTimeMillis, long bootTimeMillis) { + mCurrentTimeMillis = currentTimeMillis; + mBootTimeMillis = bootTimeMillis; + } + + public void setCurrentTimeMillis(long ms) { + if (ms < mBootTimeMillis) { + throw new IllegalStateException("current time can not be before boot time"); + } + mCurrentTimeMillis = ms; + } + + public void setBootTimeMillis(long ms) { + if (ms > mCurrentTimeMillis) { + throw new IllegalStateException("boot time can not be after current time"); + } + mBootTimeMillis = ms; + } + + /** + * Increment the current time by one unit of time. + * + * @param unit The time unit to increment by. + */ + public void increment(TimeUnit unit) { + increment(unit, 1); + } + + /** + * Increment the current time by {@code amount} unit of time. + * + * @param unit The time unit to increment by. + * @param amount The amount of time units to increment by. + */ + private void increment(TimeUnit unit, long amount) { + mCurrentTimeMillis += unit.toMillis(amount); + } + + @Override + public long currentTimeMillis() { + return mCurrentTimeMillis; + } + + @Override + public long elapsedRealtime() { + return mCurrentTimeMillis - mBootTimeMillis; + } + + /** + * Sleep does not block it just updates the current time. + */ + @Override + public void sleep(long ms) { + // TODO: implement blocking if needed. + mCurrentTimeMillis += ms; + } +} diff --git a/tests/common/src/com/android/tv/testing/ProgramUtils.java b/tests/common/src/com/android/tv/testing/ProgramUtils.java index 92fe9c82..493891b6 100644 --- a/tests/common/src/com/android/tv/testing/ProgramUtils.java +++ b/tests/common/src/com/android/tv/testing/ProgramUtils.java @@ -30,6 +30,7 @@ import java.util.concurrent.TimeUnit; public class ProgramUtils { private static final String TAG = "ProgramUtils"; + private static final boolean DEBUG = false; // Populate program data for a week. private static final long PROGRAM_INSERT_DURATION_MS = TimeUnit.DAYS.toMillis(7); @@ -77,7 +78,7 @@ public class ProgramUtils { || timeMs >= targetEndTimeMs) { context.getContentResolver().bulkInsert(Programs.CONTENT_URI, list.toArray(new ContentValues[list.size()])); - Log.v(TAG, "Inserted " + list.size() + " programs for " + channelUri); + if (DEBUG) Log.d(TAG, "Inserted " + list.size() + " programs for " + channelUri); list.clear(); } } diff --git a/tests/common/src/com/android/tv/testing/Utils.java b/tests/common/src/com/android/tv/testing/Utils.java index c1a7506f..9e1bd3e9 100644 --- a/tests/common/src/com/android/tv/testing/Utils.java +++ b/tests/common/src/com/android/tv/testing/Utils.java @@ -29,6 +29,7 @@ import android.os.Looper; import android.text.TextUtils; import android.util.Log; +import com.android.tv.common.TvCommonUtils; import com.android.tv.util.MainThreadExecutor; import java.io.IOException; @@ -46,7 +47,7 @@ import java.util.concurrent.Future; * * <p>This class is also used to check whether TV app is running in tests or not. * - * @see com.android.tv.util.Utils#isRunningInTest + * @see TvCommonUtils#isRunningInTest */ public final class Utils { private static final String TAG ="Utils"; diff --git a/tests/common/src/com/android/tv/testing/dvr/RecordingTestUtils.java b/tests/common/src/com/android/tv/testing/dvr/RecordingTestUtils.java new file mode 100644 index 00000000..30e21e2e --- /dev/null +++ b/tests/common/src/com/android/tv/testing/dvr/RecordingTestUtils.java @@ -0,0 +1,34 @@ +/* + * Copyright (C) 2015 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.testing.dvr; + +import com.android.tv.data.Channel; +import com.android.tv.dvr.Recording; + +import java.util.Collections; + +/** + * Static utils for using {@link Recording} in tests. + */ +public class RecordingTestUtils { + public static Recording createTestRecordingWithPeriod(long id, long startTime, long endTime) { + return Recording.builder(new Channel.Builder().build(), startTime, endTime) + .setId(id) + .setPrograms(Collections.EMPTY_LIST) + .build(); + } +} |