1 /*
2  * Copyright (C) 2017 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License
15  */
16 
17 package com.android.tv.testing;
18 
19 import android.app.Application;
20 import android.media.tv.TvInputManager;
21 import android.os.AsyncTask;
22 
23 import com.android.tv.InputSessionManager;
24 import com.android.tv.MainActivityWrapper;
25 import com.android.tv.TvSingletons;
26 import com.android.tv.analytics.Analytics;
27 import com.android.tv.analytics.Tracker;
28 import com.android.tv.common.BaseApplication;
29 import com.android.tv.common.flags.impl.DefaultBackendKnobsFlags;
30 import com.android.tv.common.flags.impl.DefaultCloudEpgFlags;
31 import com.android.tv.common.flags.impl.DefaultUiFlags;
32 import com.android.tv.common.flags.impl.SettableFlagsModule;
33 import com.android.tv.common.recording.RecordingStorageStatusManager;
34 import com.android.tv.common.singletons.HasSingletons;
35 import com.android.tv.common.util.Clock;
36 import com.android.tv.data.ChannelDataManager;
37 import com.android.tv.data.PreviewDataManager;
38 import com.android.tv.data.ProgramDataManager;
39 import com.android.tv.data.epg.EpgReader;
40 import com.android.tv.dvr.DvrDataManager;
41 import com.android.tv.dvr.DvrManager;
42 import com.android.tv.dvr.DvrScheduleManager;
43 import com.android.tv.dvr.DvrWatchedPositionManager;
44 import com.android.tv.dvr.recorder.RecordingScheduler;
45 import com.android.tv.perf.PerformanceMonitor;
46 import com.android.tv.perf.stub.StubPerformanceMonitor;
47 import com.android.tv.testing.dvr.DvrDataManagerInMemoryImpl;
48 import com.android.tv.testing.fakes.FakeClock;
49 import com.android.tv.testing.testdata.TestData;
50 import com.android.tv.tuner.singletons.TunerSingletons;
51 import com.android.tv.tunerinputcontroller.BuiltInTunerManager;
52 import com.android.tv.util.AsyncDbTask.DbExecutor;
53 import com.android.tv.util.SetupUtils;
54 import com.android.tv.util.TvInputManagerHelper;
55 
56 import com.google.common.base.Optional;
57 
58 import dagger.Lazy;
59 
60 import java.util.concurrent.Executor;
61 
62 /** Test application for TV app. */
63 public class TestSingletonApp extends Application
64         implements TvSingletons, TunerSingletons, HasSingletons<TvSingletons> {
65     public final FakeClock fakeClock = FakeClock.createWithCurrentTime();
66     public final FakeEpgReader epgReader = new FakeEpgReader(fakeClock);
67     public final FakeEpgFetcher epgFetcher = new FakeEpgFetcher();
68     public final SettableFlagsModule flagsModule = new SettableFlagsModule();
69 
70     public FakeTvInputManagerHelper tvInputManagerHelper;
71     public SetupUtils setupUtils;
72     public DvrManager dvrManager;
73     public DvrDataManager mDvrDataManager;
74     @DbExecutor public Executor dbExecutor = AsyncTask.SERIAL_EXECUTOR;
75 
76     private final Lazy<EpgReader> mEpgReaderProvider = () -> epgReader;
77     private final Optional<BuiltInTunerManager> mBuiltInTunerManagerOptional = Optional.absent();
78 
79     private final PerformanceMonitor mPerformanceMonitor = new StubPerformanceMonitor();
80     private ChannelDataManager mChannelDataManager;
81 
82     @Override
onCreate()83     public void onCreate() {
84         super.onCreate();
85         tvInputManagerHelper = new FakeTvInputManagerHelper(this);
86         setupUtils = new SetupUtils(this, mBuiltInTunerManagerOptional);
87         tvInputManagerHelper.start();
88         mChannelDataManager =
89                 new ChannelDataManager(
90                         this, tvInputManagerHelper, dbExecutor, getContentResolver());
91         mChannelDataManager.start();
92         mDvrDataManager = new DvrDataManagerInMemoryImpl(this, fakeClock);
93         // HACK reset the singleton for tests
94         BaseApplication.sSingletons = this;
95     }
96 
loadTestData(TestData testData, long durationMs)97     public void loadTestData(TestData testData, long durationMs) {
98         tvInputManagerHelper
99                 .getFakeTvInputManager()
100                 .add(testData.getTvInputInfo(), TvInputManager.INPUT_STATE_CONNECTED);
101         testData.init(this, fakeClock, durationMs);
102     }
103 
104     @Override
getAnalytics()105     public Analytics getAnalytics() {
106         return null;
107     }
108 
109     @Override
handleInputCountChanged()110     public void handleInputCountChanged() {}
111 
112     @Override
getChannelDataManager()113     public ChannelDataManager getChannelDataManager() {
114         return mChannelDataManager;
115     }
116 
117     @Override
getProgramDataManager()118     public ProgramDataManager getProgramDataManager() {
119         return null;
120     }
121 
122     @Override
getPreviewDataManager()123     public PreviewDataManager getPreviewDataManager() {
124         return null;
125     }
126 
127     @Override
getDvrDataManager()128     public DvrDataManager getDvrDataManager() {
129         return mDvrDataManager;
130     }
131 
132     @Override
getDvrScheduleManager()133     public DvrScheduleManager getDvrScheduleManager() {
134         return null;
135     }
136 
137     @Override
getDvrManager()138     public DvrManager getDvrManager() {
139         return dvrManager;
140     }
141 
142     @Override
getRecordingScheduler()143     public RecordingScheduler getRecordingScheduler() {
144         return null;
145     }
146 
147     @Override
getDvrWatchedPositionManager()148     public DvrWatchedPositionManager getDvrWatchedPositionManager() {
149         return null;
150     }
151 
152     @Override
getInputSessionManager()153     public InputSessionManager getInputSessionManager() {
154         return new InputSessionManager(this);
155     }
156 
157     @Override
getTracker()158     public Tracker getTracker() {
159         return null;
160     }
161 
162     @Override
getTvInputManagerHelper()163     public TvInputManagerHelper getTvInputManagerHelper() {
164         return tvInputManagerHelper;
165     }
166 
167     @Override
providesEpgReader()168     public Lazy<EpgReader> providesEpgReader() {
169         return mEpgReaderProvider;
170     }
171 
172     @Override
getSetupUtils()173     public SetupUtils getSetupUtils() {
174         return setupUtils;
175     }
176 
177     @Override
getBuiltInTunerManager()178     public Optional<BuiltInTunerManager> getBuiltInTunerManager() {
179         return mBuiltInTunerManagerOptional;
180     }
181 
182     @Override
getMainActivityWrapper()183     public MainActivityWrapper getMainActivityWrapper() {
184         return null;
185     }
186 
187     @Override
getClock()188     public Clock getClock() {
189         return fakeClock;
190     }
191 
192     @Override
getRecordingStorageStatusManager()193     public RecordingStorageStatusManager getRecordingStorageStatusManager() {
194         return null;
195     }
196 
197     @Override
isRunningInMainProcess()198     public boolean isRunningInMainProcess() {
199         return false;
200     }
201 
202     @Override
getPerformanceMonitor()203     public PerformanceMonitor getPerformanceMonitor() {
204         return mPerformanceMonitor;
205     }
206 
207     @Override
getEmbeddedTunerInputId()208     public String getEmbeddedTunerInputId() {
209         return "com.android.tv/.tuner.tvinput.TunerTvInputService";
210     }
211 
212     @Override
getDbExecutor()213     public Executor getDbExecutor() {
214         return dbExecutor;
215     }
216 
217     @Override
getBackendKnobs()218     public DefaultBackendKnobsFlags getBackendKnobs() {
219         return flagsModule.backendKnobsFlags;
220     }
221 
222     @Override
getCloudEpgFlags()223     public DefaultCloudEpgFlags getCloudEpgFlags() {
224         return flagsModule.cloudEpgFlags;
225     }
226 
227     @Override
getUiFlags()228     public DefaultUiFlags getUiFlags() {
229         return flagsModule.uiFlags;
230     }
231 
232     @Override
getBuildType()233     public BuildType getBuildType() {
234         return BuildType.ENG;
235     }
236 
237     @Override
singletons()238     public TvSingletons singletons() {
239         return this;
240     }
241 }
242