aboutsummaryrefslogtreecommitdiff
path: root/common/tests/robotests/src/com/android/tv/common/support/tis/BaseTvInputServiceTest.java
blob: 88b0215e7d7dda01356ac5593ee834147cbe219f (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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
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 org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.Robolectric;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.android.controller.ServiceController;
import org.robolectric.annotation.Config;

/** Tests for {@link BaseTvInputService}. */
@RunWith(RobolectricTestRunner.class)
@Config(minSdk = Build.VERSION_CODES.LOLLIPOP, maxSdk = Build.VERSION_CODES.P)
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);
    }
}