1#!/bin/bash
2#
3# Copyright (C) 2017 The Android Open Source Project
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9#      http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16#
17# usage: usbtuner-test.sh <test_case> [channel]
18#
19# To test repeated channel change, run:
20#
21# ./usbtuner-test.sh <1 or 3>
22#
23# To test watching a fixed channel, run:
24#
25# ./usbtuner-test.sh 2
26#
27# Case 2 uses the last-viewed channel by TV app. Give a channel number
28# as a 2nd parameter if you want to use the channel for testing, like below:
29#
30# ./usbtuner-test.sh 2 6-1
31#
32# The script assumes that:
33#   1) Browsing by keydown event circulates among the USB input channels only
34#   2) When started, TV app should tune to one of the channels provided by the USB input
35#
36# The test result is logged in the doc: https://goo.gl/MsPBf7
37
38function start_tv {
39  disable_analytics_report
40  adb shell am force-stop com.android.tv
41  adb shell am start -n com.android.tv/.MainActivity > /dev/null
42  sleep 5
43}
44
45function log_begin {
46  adb shell dumpsys meminfo -d --package com.android.tv.tuner > meminfo-begin.txt
47}
48
49function tune {
50  adb shell input text $1
51  adb shell input keyevent KEYCODE_DPAD_CENTER
52  sleep 5  # Wait enough for tuning
53}
54
55function browse {
56  for i in {1..50}; do
57    adb shell input keyevent DPAD_DOWN
58    sleep 10  # Tune and watch the channel for a while
59  done;
60}
61
62function browse_heavily {
63  for i in {1..60}; do
64    echo "$(date '+%x %X') ======== Test #$i of 60 ========"
65    clear_logcat
66    for j in {1..60}; do
67      adb shell input keyevent DPAD_DOWN
68      sleep $(( $RANDOM % 3 ))  # Sleep for 0 - 2 seconds
69    done;
70    measure_tuning_time
71  done;
72}
73
74function clear_logcat {
75  adb logcat -c
76}
77
78function measure_tuning_time {
79  timeout 1 adb logcat -s TvInputSessionImpl | awk -f $(dirname $0)/measure-tuning-time.awk
80}
81
82function log_end {
83  adb shell dumpsys meminfo -d --package com.android.tv.tuner > meminfo-end.txt
84}
85
86function stop_tv {
87  # Stop TV by running other app (Settings)
88  adb shell am start com.android.tv.settings/com.android.tv.settings.MainSettings
89  restore_analytics_setting
90}
91
92function output {
93  echo "Cut and paste this"
94  sed -n 33,46p meminfo-begin.txt | cut -f 2 -d ":" -s | awk '{print $1}'
95  sed -n 33,46p meminfo-end.txt | cut -f 2 -d ":" -s | awk '{print $1}'
96}
97
98function disable_analytics_report {
99  tracker=$(adb shell getprop tv_use_tracker | tr -d '[[:space:]]')
100  adb shell setprop tv_use_tracker false
101}
102
103function restore_analytics_setting {
104  if [ "${tracker}" == "" ]; then
105    adb shell setprop tv_use_tracker ""
106  else
107    adb shell setprop tv_use_tracker ${tracker}
108  fi
109}
110
111function control_c {
112  restore_analytics_setting
113  echo "Exiting..."
114  exit 1
115}
116
117# Entry point
118
119trap control_c SIGINT
120
121case "$1" in
122  1)
123     echo "Runing test 1"
124     start_tv
125     log_begin
126     clear_logcat
127     browse  # Repeat channel change for about 10 minutes
128     measure_tuning_time
129     log_end
130     stop_tv
131     output
132     ;;
133  2)
134     echo "Runing test 2"
135     start_tv
136     log_begin
137     if [ "$2" != "" ]; then
138       tune $2
139     fi
140     sleep 600  # 10 minutes
141     log_end
142     stop_tv
143     output
144     ;;
145  3)
146     echo "Runing test 3"
147     start_tv
148     log_begin
149     browse_heavily  # Repeat channel change for about 3 hours
150     log_end
151     stop_tv
152     output
153     ;;
154  *)
155     echo "usage: usbtuner-test.sh <1|2|3> [channel]"
156     exit 1
157     ;;
158esac
159
160