| 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.build.gradle.model; |
| 18 |
|
| 19 |
import static com.android.build.gradle.model.AndroidComponentModelPlugin.COMPONENT_NAME; |
| 20 |
import static com.android.builder.core.VariantType.ANDROID_TEST; |
| 21 |
import static com.android.builder.core.VariantType.UNIT_TEST; |
| 22 |
|
| 23 |
import com.android.build.gradle.internal.TaskManager; |
| 24 |
import com.android.build.gradle.internal.VariantManager; |
| 25 |
import com.android.build.gradle.internal.variant.BaseVariantData; |
| 26 |
import com.android.build.gradle.internal.variant.TestVariantData; |
| 27 |
import com.android.build.gradle.model.internal.AndroidBinaryInternal; |
| 28 |
import com.android.build.gradle.model.internal.AndroidComponentSpecInternal; |
| 29 |
import com.android.builder.core.BuilderConstants; |
| 30 |
import com.google.common.base.Preconditions; |
| 31 |
|
| 32 |
import org.gradle.api.Task; |
| 33 |
import org.gradle.model.ModelMap; |
| 34 |
import org.gradle.model.Mutate; |
| 35 |
import org.gradle.model.Path; |
| 36 |
import org.gradle.model.RuleSource; |
| 37 |
|
| 38 |
/** |
| 39 |
* Plugin for creating test tasks for AndroidBinary. |
| 40 |
*/ |
| 41 |
@SuppressWarnings("MethodMayBeStatic") |
| 42 |
public class AndroidComponentModelTestPlugin extends RuleSource { |
| 43 |
|
| 44 |
@Mutate |
| 45 |
public void createConnectedTestTasks( |
| 46 |
final ModelMap<Task> tasks, |
| 47 |
@Path("binaries") ModelMap<AndroidBinaryInternal> binaries, |
| 48 |
TaskManager taskManager, |
| 49 |
ModelMap<AndroidComponentSpecInternal> specs) { |
| 50 |
final VariantManager variantManager = specs.get(COMPONENT_NAME).getVariantManager(); |
| 51 |
for (AndroidBinaryInternal binary : binaries.values()) { |
| 52 |
// Create test tasks. |
| 53 |
BaseVariantData testedVariantData = binary.getVariantData(); |
| 54 |
|
| 55 |
TestVariantData unitTestVariantData = variantManager.createTestVariantData( |
| 56 |
testedVariantData, |
| 57 |
UNIT_TEST); |
| 58 |
variantManager.getVariantDataList().add(unitTestVariantData); |
| 59 |
variantManager.createTasksForVariantData( |
| 60 |
new TaskModelMapAdaptor(tasks), |
| 61 |
unitTestVariantData); |
| 62 |
|
| 63 |
// TODO: compare against testBuildType instead of BuilderConstants.DEBUG. |
| 64 |
if (!binary.getBuildType().getName().equals(BuilderConstants.DEBUG)) { |
| 65 |
continue; |
| 66 |
} |
| 67 |
|
| 68 |
Preconditions.checkState(testedVariantData != null, |
| 69 |
"Internal error: tested variant must be created before test variant."); |
| 70 |
|
| 71 |
TestVariantData testVariantData = |
| 72 |
variantManager.createTestVariantData(testedVariantData, ANDROID_TEST); |
| 73 |
variantManager.getVariantDataList().add(testVariantData); |
| 74 |
variantManager.createTasksForVariantData( |
| 75 |
new TaskModelMapAdaptor(tasks), |
| 76 |
testVariantData); |
| 77 |
} |
| 78 |
} |
| 79 |
} |