1 /*
2  * Copyright (C) 2014 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.cts.verifier.tv;
18 
19 import android.annotation.SuppressLint;
20 import android.content.Intent;
21 import android.database.Cursor;
22 import android.media.tv.TvContentRating;
23 import android.media.tv.TvContract;
24 import android.media.tv.TvInputManager;
25 import android.view.View;
26 import android.widget.Toast;
27 
28 import com.android.cts.verifier.R;
29 
30 /**
31  * Tests for verifying TV app behavior on parental control.
32  */
33 @SuppressLint("NewApi")
34 public class ParentalControlTestActivity extends TvAppVerifierActivity
35         implements View.OnClickListener {
36     private static final String TAG = "ParentalControlTestActivity";
37 
38     private static final long TIMEOUT_MS = 5l * 60l * 1000l;  // 5 mins.
39 
40     private View mTurnOnParentalControlItem;
41     private View mVerifyReceiveBroadcast1Item;
42     private View mBlockTvMaItem;
43     private View mVerifyReceiveBroadcast2Item;
44     private View mBlockUnblockItem;
45     private View mParentalControlsSwitchYesItem;
46     private View mParentalControlsSwitchNoItem;
47     private View mSupportThirdPartyInputYesItem;
48     private View mSupportThirdPartyInputNoItem;
49 
50     private Intent mTvAppIntent = null;
51 
52     @Override
onClick(View v)53     public void onClick(View v) {
54         final View postTarget = getPostTarget();
55 
56         if (containsButton(mSupportThirdPartyInputYesItem, v)) {
57             setPassState(mSupportThirdPartyInputYesItem, true);
58             setButtonEnabled(mSupportThirdPartyInputNoItem, false);
59             setButtonEnabled(mParentalControlsSwitchYesItem, true);
60             setButtonEnabled(mParentalControlsSwitchNoItem, true);
61             return;
62         } else if (containsButton(mSupportThirdPartyInputNoItem, v)){
63             setPassState(mSupportThirdPartyInputYesItem, true);
64             setButtonEnabled(mSupportThirdPartyInputNoItem, false);
65             getPassButton().setEnabled(true);
66             return;
67         } else if (containsButton(mParentalControlsSwitchYesItem, v)) {
68             setPassState(mParentalControlsSwitchYesItem, true);
69             setButtonEnabled(mParentalControlsSwitchNoItem, false);
70             mTurnOnParentalControlItem.setVisibility(View.VISIBLE);
71             mVerifyReceiveBroadcast1Item.setVisibility(View.VISIBLE);
72             mBlockTvMaItem.setVisibility(View.VISIBLE);
73             mVerifyReceiveBroadcast2Item.setVisibility(View.VISIBLE);
74             mBlockUnblockItem.setVisibility(View.VISIBLE);
75             setButtonEnabled(mTurnOnParentalControlItem, true);
76             return;
77         } else if (containsButton(mParentalControlsSwitchNoItem, v)){
78             setPassState(mParentalControlsSwitchYesItem, true);
79             setButtonEnabled(mParentalControlsSwitchNoItem, false);
80             mBlockTvMaItem.setVisibility(View.VISIBLE);
81             mVerifyReceiveBroadcast2Item.setVisibility(View.VISIBLE);
82             mBlockUnblockItem.setVisibility(View.VISIBLE);
83             setButtonEnabled(mBlockTvMaItem, true);
84             return;
85         } else if (containsButton(mTurnOnParentalControlItem, v)) {
86             final Runnable failCallback = new Runnable() {
87                 @Override
88                 public void run() {
89                     setPassState(mVerifyReceiveBroadcast1Item, false);
90                 }
91             };
92             postTarget.postDelayed(failCallback, TIMEOUT_MS);
93             MockTvInputService.expectBroadcast(postTarget,
94                     TvInputManager.ACTION_PARENTAL_CONTROLS_ENABLED_CHANGED, new Runnable() {
95                 @Override
96                 public void run() {
97                     postTarget.removeCallbacks(failCallback);
98                     setPassState(mTurnOnParentalControlItem, true);
99                     setPassState(mVerifyReceiveBroadcast1Item, true);
100                     setButtonEnabled(mBlockTvMaItem, true);
101                 }
102             });
103         } else if (containsButton(mBlockTvMaItem, v)) {
104             final Runnable failCallback = new Runnable() {
105                 @Override
106                 public void run() {
107                     setPassState(mVerifyReceiveBroadcast2Item, false);
108                 }
109             };
110             postTarget.postDelayed(failCallback, TIMEOUT_MS);
111             MockTvInputService.expectBroadcast(postTarget,
112                     TvInputManager.ACTION_BLOCKED_RATINGS_CHANGED, new Runnable() {
113                 @Override
114                 public void run() {
115                     postTarget.removeCallbacks(failCallback);
116                     setPassState(mBlockTvMaItem, true);
117                     setPassState(mVerifyReceiveBroadcast2Item, true);
118                     setButtonEnabled(mBlockUnblockItem, true);
119                 }
120             });
121         } else if (containsButton(mBlockUnblockItem, v)) {
122             final Runnable failCallback = new Runnable() {
123                 @Override
124                 public void run() {
125                     setPassState(mBlockUnblockItem, false);
126                 }
127             };
128             postTarget.postDelayed(failCallback, TIMEOUT_MS);
129             MockTvInputService.setBlockRating(TvContentRating.createRating(
130                     "com.android.cts.verifier", "CTS_VERIFIER", "FAKE"));
131             MockTvInputService.expectUnblockContent(postTarget, new Runnable() {
132                 @Override
133                 public void run() {
134                     postTarget.removeCallbacks(failCallback);
135                     setPassState(mBlockUnblockItem, true);
136                     getPassButton().setEnabled(true);
137                 }
138             });
139         }
140         if (mTvAppIntent == null) {
141             String[] projection = { TvContract.Channels._ID };
142             try (Cursor cursor = getContentResolver().query(
143                     TvContract.buildChannelsUriForInput(MockTvInputService.getInputId(this)),
144                     projection, null, null, null)) {
145                 if (cursor != null && cursor.moveToNext()) {
146                     mTvAppIntent = new Intent(Intent.ACTION_VIEW,
147                             TvContract.buildChannelUri(cursor.getLong(0)));
148                 }
149             }
150             if (mTvAppIntent == null) {
151                 Toast.makeText(this, R.string.tv_channel_not_found, Toast.LENGTH_SHORT).show();
152                 return;
153             }
154         }
155         startActivity(mTvAppIntent);
156     }
157 
158     @Override
createTestItems()159     protected void createTestItems() {
160         mSupportThirdPartyInputYesItem = createUserItem(
161                 R.string.tv_input_discover_test_third_party_tif_input_support,
162                 R.string.tv_yes, this);
163         setButtonEnabled(mSupportThirdPartyInputYesItem, true);
164         mSupportThirdPartyInputNoItem = createButtonItem(R.string.tv_no, this);
165         setButtonEnabled(mSupportThirdPartyInputNoItem, true);
166         mParentalControlsSwitchYesItem = createUserItem(
167                 R.string.tv_parental_control_test_check_parental_controls_switch,
168                 R.string.tv_yes, this);
169         mParentalControlsSwitchNoItem = createButtonItem(R.string.tv_no, this);
170         mTurnOnParentalControlItem = createUserItem(
171                 R.string.tv_parental_control_test_turn_on_parental_control,
172                 R.string.tv_launch_tv_app, this);
173         mVerifyReceiveBroadcast1Item = createAutoItem(
174                 R.string.tv_parental_control_test_verify_receive_broadcast1);
175         mBlockTvMaItem = createUserItem(R.string.tv_parental_control_test_block_tv_ma,
176                 R.string.tv_launch_tv_app, this);
177         mVerifyReceiveBroadcast2Item = createAutoItem(
178                 R.string.tv_parental_control_test_verify_receive_broadcast2);
179         mBlockUnblockItem = createUserItem(R.string.tv_parental_control_test_block_unblock,
180                 R.string.tv_launch_tv_app, this);
181         mTurnOnParentalControlItem.setVisibility(View.GONE);
182         mVerifyReceiveBroadcast1Item.setVisibility(View.GONE);
183         mBlockTvMaItem.setVisibility(View.GONE);
184         mVerifyReceiveBroadcast2Item.setVisibility(View.GONE);
185         mBlockUnblockItem.setVisibility(View.GONE);
186     }
187 
188     @Override
setInfoResources()189     protected void setInfoResources() {
190         setInfoResources(R.string.tv_parental_control_test,
191                 R.string.tv_parental_control_test_info, -1);
192     }
193 }
194