aboutsummaryrefslogtreecommitdiff
path: root/gradle/src
diff options
context:
space:
mode:
authorXavier Ducrohet <xav@android.com>2012-10-22 18:18:03 -0700
committerXavier Ducrohet <xav@android.com>2012-10-22 18:21:22 -0700
commit9f8277f8557e6a9b5019deb3fbca076c8ccc82a3 (patch)
treefecc6af9d4a8bb962acab00c29dbdfad332388d1 /gradle/src
parentef2ea65856b2db1b4f1691931967880f16c39828 (diff)
downloadbuild-9f8277f8557e6a9b5019deb3fbca076c8ccc82a3.tar.gz
Add some basic test of the plugin.
Change-Id: Ib4b00ac416a43ac0ea37b86dedabd7c012becf22
Diffstat (limited to 'gradle/src')
-rw-r--r--gradle/src/main/groovy/com/android/build/gradle/AppPlugin.groovy13
-rw-r--r--gradle/src/main/groovy/com/android/build/gradle/BasePlugin.groovy7
-rw-r--r--gradle/src/main/groovy/com/android/build/gradle/internal/PluginHolder.groovy28
-rw-r--r--gradle/src/test/groovy/com/android/build/gradle/BasicConfigTest.groovy201
4 files changed, 246 insertions, 3 deletions
diff --git a/gradle/src/main/groovy/com/android/build/gradle/AppPlugin.groovy b/gradle/src/main/groovy/com/android/build/gradle/AppPlugin.groovy
index 13e3b9f..7cd6f04 100644
--- a/gradle/src/main/groovy/com/android/build/gradle/AppPlugin.groovy
+++ b/gradle/src/main/groovy/com/android/build/gradle/AppPlugin.groovy
@@ -20,6 +20,7 @@ import com.android.build.gradle.internal.BuildTypeFactory
import com.android.build.gradle.internal.ConfigurationDependencies
import com.android.build.gradle.internal.GroupableProductFlavor
import com.android.build.gradle.internal.GroupableProductFlavorFactory
+import com.android.build.gradle.internal.PluginHolder
import com.android.build.gradle.internal.ProductFlavorData
import com.android.build.gradle.internal.ProductionAppVariant
import com.android.build.gradle.internal.TestAppVariant
@@ -41,8 +42,9 @@ import javax.inject.Inject
* Gradle plugin class for 'application' projects.
*/
class AppPlugin extends com.android.build.gradle.BasePlugin implements org.gradle.api.Plugin<Project> {
- private final Map<String, BuildTypeData> buildTypes = [:]
- private final Map<String, ProductFlavorData<GroupableProductFlavor>> productFlavors = [:]
+ static PluginHolder pluginHolder;
+ final Map<String, BuildTypeData> buildTypes = [:]
+ final Map<String, ProductFlavorData<GroupableProductFlavor>> productFlavors = [:]
AppExtension extension
@@ -55,6 +57,11 @@ class AppPlugin extends com.android.build.gradle.BasePlugin implements org.gradl
void apply(Project project) {
super.apply(project)
+ // This is for testing.
+ if (pluginHolder != null) {
+ pluginHolder.plugin = this;
+ }
+
def buildTypeContainer = project.container(BuildType, new BuildTypeFactory(instantiator))
def productFlavorContainer = project.container(GroupableProductFlavor,
new GroupableProductFlavorFactory(instantiator))
@@ -122,7 +129,7 @@ class AppPlugin extends com.android.build.gradle.BasePlugin implements org.gradl
productFlavors[productFlavor.name] = productFlavorData
}
- private void createAndroidTasks() {
+ protected void createAndroidTasks() {
// resolve dependencies for all config
List<ConfigurationDependencies> dependencies = []
dependencies.addAll(buildTypes.values())
diff --git a/gradle/src/main/groovy/com/android/build/gradle/BasePlugin.groovy b/gradle/src/main/groovy/com/android/build/gradle/BasePlugin.groovy
index 3da8822..9131571 100644
--- a/gradle/src/main/groovy/com/android/build/gradle/BasePlugin.groovy
+++ b/gradle/src/main/groovy/com/android/build/gradle/BasePlugin.groovy
@@ -84,6 +84,7 @@ import org.gradle.util.GUtil
public abstract class BasePlugin {
public final static String INSTALL_GROUP = "Install"
+ protected static File TEST_SDK_DIR;
protected Instantiator instantiator
@@ -171,6 +172,12 @@ public abstract class BasePlugin {
}
private void findSdk(Project project) {
+ // if already set through tests.
+ if (TEST_SDK_DIR != null) {
+ sdkDir = TEST_SDK_DIR
+ return
+ }
+
def rootDir = project.rootDir
def localProperties = new File(rootDir, SdkConstants.FN_LOCAL_PROPERTIES)
if (localProperties.exists()) {
diff --git a/gradle/src/main/groovy/com/android/build/gradle/internal/PluginHolder.groovy b/gradle/src/main/groovy/com/android/build/gradle/internal/PluginHolder.groovy
new file mode 100644
index 0000000..89741de
--- /dev/null
+++ b/gradle/src/main/groovy/com/android/build/gradle/internal/PluginHolder.groovy
@@ -0,0 +1,28 @@
+/*
+ * Copyright (C) 2012 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.build.gradle.internal
+
+import com.android.build.gradle.AppPlugin
+
+/**
+ * Class used to hold a reference to an AppPlugin.
+ * This is used only to test the plugin.
+ */
+class PluginHolder {
+
+ AppPlugin plugin
+}
diff --git a/gradle/src/test/groovy/com/android/build/gradle/BasicConfigTest.groovy b/gradle/src/test/groovy/com/android/build/gradle/BasicConfigTest.groovy
new file mode 100644
index 0000000..417a240
--- /dev/null
+++ b/gradle/src/test/groovy/com/android/build/gradle/BasicConfigTest.groovy
@@ -0,0 +1,201 @@
+/*
+ * Copyright (C) 2012 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.build.gradle
+
+import com.android.build.gradle.internal.PluginHolder
+import com.android.builder.BuildType
+import junit.framework.TestCase
+import org.gradle.api.Project
+import org.gradle.testfixtures.ProjectBuilder
+
+import java.security.CodeSource
+
+public class BasicConfigTest extends TestCase {
+
+ @Override
+ protected void setUp() throws Exception {
+ BasePlugin.TEST_SDK_DIR = new File("foo")
+ AppPlugin.pluginHolder = new PluginHolder();
+ }
+
+ public void testBasic() {
+ Project project = ProjectBuilder.builder().withProjectDir(
+ new File(testDir, "basic")).build()
+
+ project.apply plugin: 'android'
+
+ project.android {
+ target "android-15"
+ }
+
+ AppPlugin plugin = AppPlugin.pluginHolder.plugin
+ plugin.createAndroidTasks()
+
+ assertEquals(2, plugin.buildTypes.size())
+ assertNotNull(plugin.buildTypes.get(BuildType.DEBUG))
+ assertNotNull(plugin.buildTypes.get(BuildType.RELEASE))
+ assertEquals(0, plugin.productFlavors.size())
+ assertEquals(3, plugin.variants.size()) // includes the test variant(s)
+ }
+
+ public void testDefaultConfig() {
+ Project project = ProjectBuilder.builder().withProjectDir(
+ new File(testDir, "basic")).build()
+
+ project.apply plugin: 'android'
+
+ project.android {
+ target "android-15"
+
+ defaultConfig {
+ versionCode 1
+ versionName "2.0"
+ minSdkVersion 2
+ targetSdkVersion 3
+
+ signingStoreLocation "aa"
+ signingStorePassword "bb"
+ signingKeyAlias "cc"
+ signingKeyPassword "dd"
+ }
+ }
+
+ AppPlugin plugin = AppPlugin.pluginHolder.plugin
+ plugin.createAndroidTasks()
+
+ assertEquals(1, plugin.extension.defaultConfig.versionCode)
+ assertEquals(2, plugin.extension.defaultConfig.minSdkVersion)
+ assertEquals(3, plugin.extension.defaultConfig.targetSdkVersion)
+ assertEquals("2.0", plugin.extension.defaultConfig.versionName)
+
+ assertEquals("aa", plugin.extension.defaultConfig.signingStoreLocation)
+ assertEquals("bb", plugin.extension.defaultConfig.signingStorePassword)
+ assertEquals("cc", plugin.extension.defaultConfig.signingKeyAlias)
+ assertEquals("dd", plugin.extension.defaultConfig.signingKeyPassword)
+ }
+
+ public void testBuildTypes() {
+ Project project = ProjectBuilder.builder().withProjectDir(
+ new File(testDir, "basic")).build()
+
+ project.apply plugin: 'android'
+
+ project.android {
+ target "android-15"
+
+ buildTypes {
+ staging {
+
+ }
+ }
+ }
+
+ AppPlugin plugin = AppPlugin.pluginHolder.plugin
+ plugin.createAndroidTasks()
+
+ assertEquals(3, plugin.buildTypes.size())
+ assertEquals(4, plugin.variants.size()) // includes the test variant(s)
+ }
+
+ public void testFlavors() {
+ Project project = ProjectBuilder.builder().withProjectDir(
+ new File(testDir, "basic")).build()
+
+ project.apply plugin: 'android'
+
+ project.android {
+ target "android-15"
+
+ productFlavors {
+ flavor1 {
+
+ }
+ flavor2 {
+
+ }
+ }
+ }
+
+ AppPlugin plugin = AppPlugin.pluginHolder.plugin
+ plugin.createAndroidTasks()
+
+ assertEquals(2, plugin.productFlavors.size())
+ assertEquals(6, plugin.variants.size()) // includes the test variant(s)
+ }
+
+ public void testMultiFlavors() {
+ Project project = ProjectBuilder.builder().withProjectDir(
+ new File(testDir, "basic")).build()
+
+ project.apply plugin: 'android'
+
+ project.android {
+ target "android-15"
+
+ flavorGroups "group1", "group2"
+
+ productFlavors {
+ f1 {
+ flavorGroup "group1"
+ }
+ f2 {
+ flavorGroup "group1"
+ }
+
+ fa {
+ flavorGroup "group2"
+ }
+ fb {
+ flavorGroup "group2"
+ }
+ fc {
+ flavorGroup "group2"
+ }
+ }
+ }
+
+ AppPlugin plugin = AppPlugin.pluginHolder.plugin
+ plugin.createAndroidTasks()
+
+ assertEquals(5, plugin.productFlavors.size())
+ assertEquals(18, plugin.variants.size()) // includes the test variant(s)
+ }
+
+
+ /**
+ * Returns the Android source tree root dir.
+ * @return the root dir or null if it couldn't be computed.
+ */
+ private File getTestDir() {
+ CodeSource source = getClass().getProtectionDomain().getCodeSource()
+ if (source != null) {
+ URL location = source.getLocation();
+ try {
+ File dir = new File(location.toURI())
+ assertTrue(dir.getPath(), dir.exists())
+ System.out.println(dir.absolutePath)
+
+ File rootDir = dir.getParentFile().getParentFile().getParentFile().getParentFile()
+
+ return new File(rootDir, "tests")
+ } catch (URISyntaxException e) {
+ fail(e.getLocalizedMessage())
+ }
+ }
+
+ fail("Fail to get tests folder")
+ }
+} \ No newline at end of file