TestServiceRegistry.java revision fdc8233f70bd5c4f2d26e4990cacc2ed5913959a
1/*
2 * Copyright 2018 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
17package androidx.media.test.client;
18
19import static org.junit.Assert.fail;
20
21import android.os.Handler;
22
23import androidx.annotation.GuardedBy;
24import androidx.media.MediaLibraryService2.MediaLibrarySession.MediaLibrarySessionCallback;
25import androidx.media.MediaSessionService2;
26import androidx.media.test.client.TestUtils.SyncHandler;
27
28/**
29 * Keeps the instance of currently running {@link MockMediaSessionService2}. And also provides
30 * a way to control them in one place.
31 * <p>
32 * It only support only one service at a time.
33 */
34public class TestServiceRegistry {
35    @GuardedBy("TestServiceRegistry.class")
36    private static TestServiceRegistry sInstance;
37    @GuardedBy("TestServiceRegistry.class")
38    private MediaSessionService2 mService;
39    @GuardedBy("TestServiceRegistry.class")
40    private SyncHandler mHandler;
41    @GuardedBy("TestServiceRegistry.class")
42    private MediaLibrarySessionCallback mSessionCallback;
43    @GuardedBy("TestServiceRegistry.class")
44    private SessionServiceCallback mSessionServiceCallback;
45
46    /**
47     * Callback for session service's lifecyle (onCreate() / onDestroy())
48     */
49    public interface SessionServiceCallback {
50        void onCreated();
51        void onDestroyed();
52    }
53
54    public static TestServiceRegistry getInstance() {
55        synchronized (TestServiceRegistry.class) {
56            if (sInstance == null) {
57                sInstance = new TestServiceRegistry();
58            }
59            return sInstance;
60        }
61    }
62
63    public void setHandler(Handler handler) {
64        synchronized (TestServiceRegistry.class) {
65            mHandler = new SyncHandler(handler.getLooper());
66        }
67    }
68
69    public Handler getHandler() {
70        synchronized (TestServiceRegistry.class) {
71            return mHandler;
72        }
73    }
74
75    public void setSessionServiceCallback(SessionServiceCallback sessionServiceCallback) {
76        synchronized (TestServiceRegistry.class) {
77            mSessionServiceCallback = sessionServiceCallback;
78        }
79    }
80
81    public void setSessionCallback(MediaLibrarySessionCallback sessionCallback) {
82        synchronized (TestServiceRegistry.class) {
83            mSessionCallback = sessionCallback;
84        }
85    }
86
87    public MediaLibrarySessionCallback getSessionCallback() {
88        synchronized (TestServiceRegistry.class) {
89            return mSessionCallback;
90        }
91    }
92
93    public void setServiceInstance(MediaSessionService2 service) {
94        synchronized (TestServiceRegistry.class) {
95            if (mService != null) {
96                fail("Previous service instance is still running. Clean up manually to ensure"
97                        + " previoulsy running service doesn't break current test");
98            }
99            mService = service;
100            if (mSessionServiceCallback != null) {
101                mSessionServiceCallback.onCreated();
102            }
103        }
104    }
105
106    public MediaSessionService2 getServiceInstance() {
107        synchronized (TestServiceRegistry.class) {
108            return mService;
109        }
110    }
111
112    public void cleanUp() {
113        synchronized (TestServiceRegistry.class) {
114            if (mService != null) {
115                // TODO(jaewan): Remove this, and override SessionService#onDestroy() to do this
116                mService.getSession().close();
117                // stopSelf() would not kill service while the binder connection established by
118                // bindService() exists, and close() above will do the job instead.
119                // So stopSelf() isn't really needed, but just for sure.
120                mService.stopSelf();
121                mService = null;
122            }
123            if (mHandler != null) {
124                mHandler.removeCallbacksAndMessages(null);
125            }
126            mSessionCallback = null;
127            if (mSessionServiceCallback != null) {
128                mSessionServiceCallback.onDestroyed();
129                mSessionServiceCallback = null;
130            }
131        }
132    }
133}
134