1 /*
2  * Copyright (C) 2015 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.content.Intent;
20 import android.database.Cursor;
21 import android.graphics.drawable.Drawable;
22 import android.media.tv.TvContract;
23 import android.text.TextUtils;
24 import android.view.View;
25 import android.widget.TextView;
26 import android.widget.Toast;
27 
28 import com.android.cts.verifier.R;
29 
30 /**
31  * Tests for verifying TV app behavior for TV app-link.
32  */
33 public class AppLinkTestActivity extends TvAppVerifierActivity implements View.OnClickListener {
34     private static final long TIMEOUT_MS = 5l * 60l * 1000l;  // 5 mins.
35 
36     private boolean mSelectAppLinkItemPassed;
37     private View mSelectAppLinkItem;
38     private View mVerifyAppLinkIntentItem;
39     private View mVerifyAppLinkCardItem;
40     private View mSupportThirdPartyInputYesItem;
41     private View mSupportThirdPartyInputNoItem;
42 
43     Runnable mSelectAppLinkFailCallback;
44 
45     @Override
onClick(View v)46     public void onClick(View v) {
47         final View postTarget = getPostTarget();
48 
49         if (containsButton(mSupportThirdPartyInputYesItem, v)) {
50             setPassState(mSupportThirdPartyInputYesItem, true);
51             setButtonEnabled(mSupportThirdPartyInputNoItem, false);
52             setButtonEnabled(mSelectAppLinkItem, true);
53             return;
54         } else if (containsButton(mSupportThirdPartyInputNoItem, v)){
55             setPassState(mSupportThirdPartyInputYesItem, true);
56             setButtonEnabled(mSupportThirdPartyInputNoItem, false);
57             getPassButton().setEnabled(true);
58             return;
59         } else if (containsButton(mSelectAppLinkItem, v)) {
60             Intent tvAppIntent = null;
61             String[] projection = { TvContract.Channels._ID };
62             try (Cursor cursor = getContentResolver().query(
63                     TvContract.buildChannelsUriForInput(MockTvInputService.getInputId(this)),
64                     projection, null, null, null)) {
65                 if (cursor != null && cursor.moveToNext()) {
66                     tvAppIntent = new Intent(Intent.ACTION_VIEW,
67                             TvContract.buildChannelUri(cursor.getLong(0)));
68                 }
69             }
70             if (tvAppIntent == null) {
71                 Toast.makeText(this, R.string.tv_channel_not_found, Toast.LENGTH_SHORT).show();
72                 return;
73             }
74 
75             mSelectAppLinkFailCallback = new Runnable() {
76                 @Override
77                 public void run() {
78                     mSelectAppLinkItemPassed = false;
79                     setPassState(mSelectAppLinkItem, false);
80                     setPassState(mVerifyAppLinkIntentItem, false);
81                 }
82             };
83             postTarget.postDelayed(mSelectAppLinkFailCallback, TIMEOUT_MS);
84             mSelectAppLinkItemPassed = true;
85             setPassState(mSelectAppLinkItem, true);
86 
87             startActivity(tvAppIntent);
88         } else if (containsButton(mVerifyAppLinkCardItem, v)) {
89             setPassState(mVerifyAppLinkCardItem, true);
90             getPassButton().setEnabled(true);
91         }
92     }
93 
94     @Override
onNewIntent(Intent intent)95     protected void onNewIntent(Intent intent) {
96         if (mSelectAppLinkItemPassed
97                 && TextUtils.equals(MockTvInputSetupActivity.APP_LINK_TEST_VALUE,
98                         intent.getStringExtra(MockTvInputSetupActivity.APP_LINK_TEST_KEY))) {
99             getPostTarget().removeCallbacks(mSelectAppLinkFailCallback);
100             setPassState(mVerifyAppLinkIntentItem, true);
101             setButtonEnabled(mVerifyAppLinkCardItem, true);
102         }
103     }
104 
105     @Override
createTestItems()106     protected void createTestItems() {
107         mSupportThirdPartyInputYesItem = createAndAttachUserItem(
108                 R.string.tv_input_discover_test_third_party_tif_input_support,
109                 R.string.tv_yes, this);
110         setButtonEnabled(mSupportThirdPartyInputYesItem, true);
111         mSupportThirdPartyInputNoItem = createAndAttachButtonItem(R.string.tv_no, this);
112         setButtonEnabled(mSupportThirdPartyInputNoItem, true);
113         mSelectAppLinkItem = createAndAttachUserItem(R.string.tv_app_link_test_select_app_link,
114                 R.string.tv_launch_tv_app, this);
115         mVerifyAppLinkIntentItem = createAndAttachAutoItem(
116                 R.string.tv_app_link_test_verify_link_clicked);
117         mVerifyAppLinkCardItem = createAndAttachUserItem(
118                 R.string.tv_input_link_test_verify_link_interface,
119                 android.R.string.yes, this);
120         TextView instructions = (TextView) mVerifyAppLinkCardItem.findViewById(R.id.instructions);
121         Drawable image = getDrawable(R.drawable.app_link_img);
122         image.setBounds(0, 0, 317, 241);
123         instructions.setCompoundDrawablePadding(10);
124         instructions.setCompoundDrawables(image, null, null, null);
125     }
126 
127     @Override
setInfoResources()128     protected void setInfoResources() {
129         setInfoResources(R.string.tv_app_link_test, R.string.tv_app_link_test_info, -1);
130     }
131 }
132