blob: f24a021902cb59a518a9a897268216d60b63db52 [file] [log] [blame]
/*
* Copyright (C) 2017 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 com.android.compatibility.common.tradefed.testtype;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.junit.Assume.assumeTrue;
import com.android.compatibility.common.tradefed.build.CompatibilityBuildHelper;
import com.android.compatibility.common.util.BusinessLogic;
import com.android.compatibility.common.util.BusinessLogicExecutor;
import com.android.compatibility.common.util.BusinessLogicFactory;
import com.android.compatibility.common.util.BusinessLogicHostExecutor;
import com.android.tradefed.log.LogUtil.CLog;
import com.android.tradefed.testtype.junit4.BaseHostJUnit4Test;
import org.junit.Before;
import org.junit.Rule;
import org.junit.rules.TestName;
import java.io.File;
/**
* Host-side base class for tests leveraging the Business Logic service.
*/
public class BusinessLogicHostTestBase extends BaseHostJUnit4Test {
/* String marking the beginning of the parameter in a test name */
private static final String PARAM_START = "[";
/* Test name rule that tracks the current test method under execution */
@Rule public TestName mTestCase = new TestName();
protected BusinessLogic mBusinessLogic;
protected boolean mCanReadBusinessLogic = true;
@Before
public void handleBusinessLogic() {
loadBusinessLogic();
ensureAuthenticated();
executeBusinessLogic();
}
protected void executeBusinessLogic() {
String methodName = mTestCase.getMethodName();
assertTrue(String.format("Test \"%s\" is unable to execute as it depends on the missing "
+ "remote configuration.", methodName), mCanReadBusinessLogic);
if (methodName.contains(PARAM_START)) {
// Strip parameter suffix (e.g. "[0]") from method name
methodName = methodName.substring(0, methodName.lastIndexOf(PARAM_START));
}
String testName = String.format("%s#%s", this.getClass().getName(), methodName);
if (mBusinessLogic.hasLogicFor(testName)) {
CLog.i("Applying business logic for test case: ", testName);
BusinessLogicExecutor executor = new BusinessLogicHostExecutor(getDevice(),
getBuild(), this);
mBusinessLogic.applyLogicFor(testName, executor);
}
}
protected void loadBusinessLogic() {
File businessLogicFile = null;
CompatibilityBuildHelper helper = new CompatibilityBuildHelper(getBuild());
// Check if business logic file has been already collected by suite level business logic
// preparer.
if (helper.hasBusinessLogicHostFile()) {
businessLogicFile = helper.getBusinessLogicHostFile();
}
else {
String bitness = (getAbi() != null) ? getAbi().getBitness() : "";
String moduleName = getInvocationContext().getConfigurationDescriptor().
getModuleName();
businessLogicFile = helper.getBusinessLogicHostFile(bitness + moduleName);
}
if (businessLogicFile != null && businessLogicFile.canRead()) {
mBusinessLogic = BusinessLogicFactory.createFromFile(businessLogicFile);
} else {
mCanReadBusinessLogic = false; // failed to retrieve business logic
}
}
protected void ensureAuthenticated() {
if (!mCanReadBusinessLogic) {
// super class handles the condition that the service is unavailable.
return;
}
if (!mBusinessLogic.mConditionalTestsEnabled) {
skipTest("Execution of device specific tests is not enabled. "
+ "Enable with '--conditional-business-logic-tests-enabled'");
}
if (mBusinessLogic.isAuthorized()) {
// Run test as normal.
return;
}
String message = mBusinessLogic.getAuthenticationStatusMessage();
// Fail test since request was not authorized.
failTest(String.format("Unable to execute because %s.", message));
}
public static void skipTest(String message) {
assumeTrue(message, false);
}
public static void failTest(String message) {
fail(message);
}
}