summaryrefslogtreecommitdiff
path: root/android/media/MockMediaLibraryService2.java
blob: 14cf25778cfa466207d6951432db3ee5e80ed7b7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/*
* Copyright 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 android.media;

import static junit.framework.Assert.fail;

import android.content.Context;
import android.media.MediaSession2.CommandGroup;
import android.media.MediaSession2.ControllerInfo;
import android.media.TestUtils.SyncHandler;
import android.os.Bundle;
import android.os.Process;

import javax.annotation.concurrent.GuardedBy;

/**
 * Mock implementation of {@link MediaLibraryService2} for testing.
 */
public class MockMediaLibraryService2 extends MediaLibraryService2 {
    // Keep in sync with the AndroidManifest.xml
    public static final String ID = "TestLibrary";

    public static final String ROOT_ID = "rootId";
    public static final Bundle EXTRA = new Bundle();
    static {
        EXTRA.putString(ROOT_ID, ROOT_ID);
    }
    @GuardedBy("MockMediaLibraryService2.class")
    private static SessionToken2 sToken;

    private MediaLibrarySession mSession;

    @Override
    public MediaLibrarySession onCreateSession(String sessionId) {
        final MockPlayer player = new MockPlayer(1);
        final SyncHandler handler = (SyncHandler) TestServiceRegistry.getInstance().getHandler();
        try {
            handler.postAndSync(() -> {
                TestLibrarySessionCallback callback = new TestLibrarySessionCallback();
                mSession = new MediaLibrarySessionBuilder(MockMediaLibraryService2.this,
                        player, (runnable) -> handler.post(runnable), callback)
                        .setId(sessionId).build();
            });
        } catch (InterruptedException e) {
            fail(e.toString());
        }
        return mSession;
    }

    @Override
    public void onDestroy() {
        TestServiceRegistry.getInstance().cleanUp();
        super.onDestroy();
    }

    public static SessionToken2 getToken(Context context) {
        synchronized (MockMediaLibraryService2.class) {
            if (sToken == null) {
                sToken = new SessionToken2(SessionToken2.TYPE_LIBRARY_SERVICE,
                        context.getPackageName(), ID,
                        MockMediaLibraryService2.class.getName(), null);
            }
            return sToken;
        }
    }

    private class TestLibrarySessionCallback extends MediaLibrarySessionCallback {
        @Override
        public CommandGroup onConnect(ControllerInfo controller) {
            if (Process.myUid() != controller.getUid()) {
                // It's system app wants to listen changes. Ignore.
                return super.onConnect(controller);
            }
            TestServiceRegistry.getInstance().setServiceInstance(
                    MockMediaLibraryService2.this, controller);
            return super.onConnect(controller);
        }

        @Override
        public BrowserRoot onGetRoot(ControllerInfo controller, Bundle rootHints) {
            return new BrowserRoot(ROOT_ID, EXTRA);
        }
    }
}