Improve handling of CEC versions in CTS tests

A device may only support CEC 1.4. Thus even if CTS tests attempt to set
the CEC version to 2.0 the device will continue to report 1.4.
Tests that require CEC 2.0 will be skipped on 1.4-only devices.

Test: atest CtsHdmiCecHostsideTests
Bug: 169121290
Change-Id: Ib001ff7e4aa61ceed3c1f206c62cbb7890bb5fe1
diff --git a/hostsidetests/hdmicec/src/android/hdmicec/cts/BaseHdmiCecCtsTest.java b/hostsidetests/hdmicec/src/android/hdmicec/cts/BaseHdmiCecCtsTest.java
index ff2c256..7c966dd0 100644
--- a/hostsidetests/hdmicec/src/android/hdmicec/cts/BaseHdmiCecCtsTest.java
+++ b/hostsidetests/hdmicec/src/android/hdmicec/cts/BaseHdmiCecCtsTest.java
@@ -16,6 +16,8 @@
 
 package android.hdmicec.cts;
 
+import static org.junit.Assume.assumeTrue;
+
 import com.android.tradefed.config.Option;
 import com.android.tradefed.config.OptionClass;
 import com.android.tradefed.device.ITestDevice;
@@ -26,6 +28,7 @@
 
 import java.io.BufferedReader;
 import java.io.StringReader;
+import java.util.concurrent.TimeUnit;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
@@ -65,8 +68,7 @@
 
     @Before
     public void setUp() throws Exception {
-        ITestDevice device = getDevice();
-        CecVersionHelper.setCec14(device);
+        setCec14();
 
         if (mDutLogicalAddress == LogicalAddress.UNKNOWN) {
             mDutLogicalAddress = LogicalAddress.getLogicalAddress(getDumpsysLogicalAddress());
@@ -184,4 +186,31 @@
         }
         throw new Exception("Could not parse active source from dumpsys.");
     }
+
+    private static void setCecVersion(ITestDevice device, int cecVersion) throws Exception {
+        device.executeShellCommand("settings put global hdmi_cec_version " + cecVersion);
+
+        TimeUnit.SECONDS.sleep(HdmiCecConstants.TIMEOUT_CEC_REINIT_SECONDS);
+    }
+
+    /**
+     * Configures the device to use CEC 2.0. Skips the test if the device does not support CEC 2.0.
+     * @throws Exception
+     */
+    public void setCec20() throws Exception {
+        setCecVersion(getDevice(), HdmiCecConstants.CEC_VERSION_2_0);
+        hdmiCecClient.sendCecMessage(hdmiCecClient.getSelfDevice(), mDutLogicalAddress,
+                CecOperand.GET_CEC_VERSION);
+        String reportCecVersion = hdmiCecClient.checkExpectedOutput(hdmiCecClient.getSelfDevice(),
+                CecOperand.CEC_VERSION);
+        boolean supportsCec2 = CecMessage.getParams(reportCecVersion)
+                >= HdmiCecConstants.CEC_VERSION_2_0;
+
+        // Device still reports a CEC version < 2.0.
+        assumeTrue(supportsCec2);
+    }
+
+    public void setCec14() throws Exception {
+        setCecVersion(getDevice(), HdmiCecConstants.CEC_VERSION_1_4);
+    }
 }
diff --git a/hostsidetests/hdmicec/src/android/hdmicec/cts/CecVersionHelper.java b/hostsidetests/hdmicec/src/android/hdmicec/cts/CecVersionHelper.java
deleted file mode 100644
index 484b10e..0000000
--- a/hostsidetests/hdmicec/src/android/hdmicec/cts/CecVersionHelper.java
+++ /dev/null
@@ -1,49 +0,0 @@
-/*
- * Copyright (C) 2019 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.hdmicec.cts;
-
-import static com.google.common.truth.Truth.assertThat;
-
-import static org.junit.Assume.assumeTrue;
-
-import com.android.tradefed.device.ITestDevice;
-
-import com.google.common.collect.Lists;
-
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.List;
-import java.util.Scanner;
-import java.util.concurrent.TimeUnit;
-
-/** Helper class to set the CEC version. */
-public final class CecVersionHelper {
-
-    public static void setCecVersion(ITestDevice device, int cecVersion) throws Exception {
-        device.executeShellCommand("settings put global hdmi_cec_version " + cecVersion);
-
-        TimeUnit.SECONDS.sleep(HdmiCecConstants.TIMEOUT_CEC_REINIT_SECONDS);
-    }
-
-    public static void setCec20(ITestDevice device) throws Exception {
-        setCecVersion(device, HdmiCecConstants.CEC_VERSION_2_0);
-    }
-
-    public static void setCec14(ITestDevice device) throws Exception {
-        setCecVersion(device, HdmiCecConstants.CEC_VERSION_1_4);
-    }
-}
diff --git a/hostsidetests/hdmicec/src/android/hdmicec/cts/common/HdmiCecFeatureAbortTest.java b/hostsidetests/hdmicec/src/android/hdmicec/cts/common/HdmiCecFeatureAbortTest.java
index 6ecd5e3..d9b1b35 100644
--- a/hostsidetests/hdmicec/src/android/hdmicec/cts/common/HdmiCecFeatureAbortTest.java
+++ b/hostsidetests/hdmicec/src/android/hdmicec/cts/common/HdmiCecFeatureAbortTest.java
@@ -19,11 +19,9 @@
 import android.hdmicec.cts.BaseHdmiCecCtsTest;
 import android.hdmicec.cts.CecMessage;
 import android.hdmicec.cts.CecOperand;
-import android.hdmicec.cts.CecVersionHelper;
 import android.hdmicec.cts.HdmiCecConstants;
 import android.hdmicec.cts.LogicalAddress;
 
-import com.android.tradefed.device.ITestDevice;
 import com.android.tradefed.testtype.DeviceJUnit4ClassRunner;
 
 import org.junit.Rule;
@@ -53,8 +51,7 @@
      */
     @Test
     public void cect_hf4_2_11_featureAbortBehavior() throws Exception {
-        ITestDevice device = getDevice();
-        CecVersionHelper.setCec20(device);
+        setCec20();
 
         List<Integer> abortReasons = Arrays.asList(
                 HdmiCecConstants.ABORT_UNRECOGNIZED_MODE,
diff --git a/hostsidetests/hdmicec/src/android/hdmicec/cts/common/HdmiCecPollingTest.java b/hostsidetests/hdmicec/src/android/hdmicec/cts/common/HdmiCecPollingTest.java
index 1d4a389..85c4c5c 100644
--- a/hostsidetests/hdmicec/src/android/hdmicec/cts/common/HdmiCecPollingTest.java
+++ b/hostsidetests/hdmicec/src/android/hdmicec/cts/common/HdmiCecPollingTest.java
@@ -18,7 +18,6 @@
 
 import android.hdmicec.cts.BaseHdmiCecCtsTest;
 import android.hdmicec.cts.CecClientMessage;
-import android.hdmicec.cts.CecVersionHelper;
 
 import com.android.tradefed.device.ITestDevice;
 import com.android.tradefed.testtype.DeviceJUnit4ClassRunner;
@@ -61,9 +60,9 @@
      */
     @Test
     public void cect_hf4_2_10_Ack() throws Exception {
-        ITestDevice device = getDevice();
-        CecVersionHelper.setCec20(device);
+        setCec20();
 
+        ITestDevice device = getDevice();
         device.executeShellCommand("input keyevent KEYCODE_SLEEP");
 
         String command = CecClientMessage.POLL + " " + mDutLogicalAddress;
diff --git a/hostsidetests/hdmicec/src/android/hdmicec/cts/common/HdmiCecPowerStatusTest.java b/hostsidetests/hdmicec/src/android/hdmicec/cts/common/HdmiCecPowerStatusTest.java
index 1e8808d..8a2e14b 100644
--- a/hostsidetests/hdmicec/src/android/hdmicec/cts/common/HdmiCecPowerStatusTest.java
+++ b/hostsidetests/hdmicec/src/android/hdmicec/cts/common/HdmiCecPowerStatusTest.java
@@ -22,7 +22,6 @@
 import android.hdmicec.cts.BaseHdmiCecCtsTest;
 import android.hdmicec.cts.CecMessage;
 import android.hdmicec.cts.CecOperand;
-import android.hdmicec.cts.CecVersionHelper;
 import android.hdmicec.cts.HdmiCecConstants;
 import android.hdmicec.cts.LogicalAddress;
 
@@ -69,7 +68,7 @@
     @Test
     public void cect_hf4_6_20_broadcastsWhenTurningOn() throws Exception {
         ITestDevice device = getDevice();
-        CecVersionHelper.setCec20(device);
+        setCec20();
 
         // Move device to standby
         device.executeShellCommand("input keyevent KEYCODE_SLEEP");
@@ -101,7 +100,7 @@
     @Test
     public void cect_hf4_6_21_broadcastsWhenGoingToStandby() throws Exception {
         ITestDevice device = getDevice();
-        CecVersionHelper.setCec20(device);
+        setCec20();
 
         // Turn device on
         device.executeShellCommand("input keyevent KEYCODE_WAKEUP");
diff --git a/hostsidetests/hdmicec/src/android/hdmicec/cts/common/HdmiCecStartupTest.java b/hostsidetests/hdmicec/src/android/hdmicec/cts/common/HdmiCecStartupTest.java
index 0272796..0644d64 100644
--- a/hostsidetests/hdmicec/src/android/hdmicec/cts/common/HdmiCecStartupTest.java
+++ b/hostsidetests/hdmicec/src/android/hdmicec/cts/common/HdmiCecStartupTest.java
@@ -22,16 +22,13 @@
 
 import android.hdmicec.cts.BaseHdmiCecCtsTest;
 import android.hdmicec.cts.CecOperand;
-import android.hdmicec.cts.CecVersionHelper;
 import android.hdmicec.cts.HdmiCecConstants;
-import android.hdmicec.cts.LogicalAddress;
 
 import com.android.tradefed.device.ITestDevice;
 import com.android.tradefed.testtype.DeviceJUnit4ClassRunner;
 
 import com.google.common.collect.ImmutableList;
 
-import org.junit.Ignore;
 import org.junit.Rule;
 import org.junit.Test;
 import org.junit.rules.RuleChain;
@@ -68,7 +65,7 @@
     @Test
     public void cectVerifyStartupMessages() throws Exception {
         ITestDevice device = getDevice();
-        CecVersionHelper.setCec20(device);
+        setCec20();
 
         device.executeShellCommand("reboot");
         device.waitForBootComplete(HdmiCecConstants.REBOOT_TIMEOUT);
diff --git a/hostsidetests/hdmicec/src/android/hdmicec/cts/common/HdmiCecSystemInformationTest.java b/hostsidetests/hdmicec/src/android/hdmicec/cts/common/HdmiCecSystemInformationTest.java
index 1aaa934..c520030 100644
--- a/hostsidetests/hdmicec/src/android/hdmicec/cts/common/HdmiCecSystemInformationTest.java
+++ b/hostsidetests/hdmicec/src/android/hdmicec/cts/common/HdmiCecSystemInformationTest.java
@@ -23,7 +23,6 @@
 import android.hdmicec.cts.BaseHdmiCecCtsTest;
 import android.hdmicec.cts.CecMessage;
 import android.hdmicec.cts.CecOperand;
-import android.hdmicec.cts.CecVersionHelper;
 import android.hdmicec.cts.HdmiCecConstants;
 import android.hdmicec.cts.LogicalAddress;
 
@@ -86,7 +85,7 @@
      */
     @Test
     public void cect_reportFeatures_deviceTypeContainedInAllDeviceTypes() throws Exception {
-        CecVersionHelper.setCec20(getDevice());
+        setCec20();
         List<LogicalAddress> testDevices =
                 Arrays.asList(
                         LogicalAddress.TV,
@@ -137,7 +136,6 @@
     @Test
     public void cect_11_2_6_6_GiveCecVersion() throws Exception {
         int cecVersion = HdmiCecConstants.CEC_VERSION_1_4;
-        CecVersionHelper.setCecVersion(getDevice(), cecVersion);
 
         hdmiCecClient.sendCecMessage(hdmiCecClient.getSelfDevice(), CecOperand.GET_CEC_VERSION);
         String message =
@@ -157,7 +155,7 @@
     @Test
     public void cect_hf4_2_12_GiveCecVersion() throws Exception {
         int cecVersion = HdmiCecConstants.CEC_VERSION_2_0;
-        CecVersionHelper.setCecVersion(getDevice(), cecVersion);
+        setCec20();
 
         hdmiCecClient.sendCecMessage(hdmiCecClient.getSelfDevice(), CecOperand.GET_CEC_VERSION);
         String reportCecVersion = hdmiCecClient.checkExpectedOutput(hdmiCecClient.getSelfDevice(),