1 /* 2 * Copyright (C) 2013 The Android Open Source Project 3 * 4 * Licensed under the Eclipse Public License, Version 1.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.eclipse.org/org/documents/epl-v10.php 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 package com.android.ide.eclipse.adt.internal.wizards.exportgradle; 17 18 import static com.android.sdklib.internal.project.ProjectProperties.PROPERTY_LIBRARY; 19 20 import com.android.SdkConstants; 21 import com.android.ide.eclipse.adt.AdtPlugin; 22 import com.android.ide.eclipse.adt.AdtUtils; 23 import com.android.ide.eclipse.adt.internal.editors.layout.refactoring.AdtProjectTest; 24 import com.android.ide.eclipse.adt.internal.project.BaseProjectHelper; 25 import com.android.ide.eclipse.adt.internal.sdk.ProjectState; 26 import com.android.ide.eclipse.adt.internal.sdk.Sdk; 27 import com.android.ide.eclipse.adt.internal.wizards.newproject.NewProjectCreator; 28 import com.android.ide.eclipse.adt.internal.wizards.newproject.NewProjectWizardState; 29 import com.android.ide.eclipse.adt.internal.wizards.newproject.NewProjectWizardState.Mode; 30 import com.android.sdklib.internal.project.ProjectPropertiesWorkingCopy; 31 import com.google.common.base.Charsets; 32 import com.google.common.io.Files; 33 34 import org.eclipse.core.resources.IProject; 35 import org.eclipse.core.resources.IResource; 36 import org.eclipse.core.runtime.IProgressMonitor; 37 import org.eclipse.core.runtime.IStatus; 38 import org.eclipse.core.runtime.NullProgressMonitor; 39 import org.eclipse.core.runtime.QualifiedName; 40 import org.eclipse.core.runtime.jobs.Job; 41 import org.eclipse.jdt.core.IJavaProject; 42 import org.eclipse.jface.operation.IRunnableContext; 43 import org.eclipse.jface.operation.IRunnableWithProgress; 44 45 import java.io.File; 46 import java.lang.reflect.InvocationTargetException; 47 import java.util.Collections; 48 49 public class ExportGradleTest extends AdtProjectTest { 50 private QualifiedName ERROR_KEY = new QualifiedName(AdtPlugin.PLUGIN_ID, "JobErrorKey"); 51 private Throwable mLastThrown; 52 53 @Override 54 public void setUp() throws Exception { 55 super.setUp(); 56 mLastThrown = null; 57 } 58 59 @Override 60 protected boolean testCaseNeedsUniqueProject() { 61 return true; 62 } 63 64 public void testSimpleAndroidApp() throws Throwable { 65 IProject project = getProject("simple-app"); 66 final IJavaProject javaProject = BaseProjectHelper.getJavaProject(project); 67 68 final ProjectSetupBuilder builder = new ProjectSetupBuilder(); 69 builder.setProject(Collections.singletonList(javaProject)); 70 71 Job job = new Job("Validate project") { 72 @Override 73 protected IStatus run(IProgressMonitor monitor) { 74 try { 75 BuildFileCreator.createBuildFiles(builder, null, monitor); 76 File buildfile = new File(javaProject.getResource().getLocation().toString(), 77 BuildFileCreator.BUILD_FILE); 78 assertTrue(buildfile.exists()); 79 String contents = Files.toString(buildfile, Charsets.UTF_8); 80 String expectedContents = 81 "buildscript {\n" + 82 " repositories {\n" + 83 " " + BuildFileCreator.MAVEN_REPOSITORY + "\n" + 84 " }\n" + 85 " dependencies {\n" + 86 " " + BuildFileCreator.PLUGIN_CLASSPATH + "\n" + 87 " }\n" + 88 "}\n" + 89 "apply plugin: 'android'\n" + 90 "\n" + 91 "dependencies {\n" + 92 "}\n" + 93 "\n" + 94 "android {\n" + 95 " compileSdkVersion 16\n" + 96 " buildToolsVersion \"16\"\n" + 97 "\n" + 98 " defaultConfig {\n" + 99 " minSdkVersion 1\n" + 100 " targetSdkVersion 1\n" + 101 " }\n" + 102 " sourceSets {\n" + 103 " main {\n" + 104 " manifest.srcFile 'AndroidManifest.xml'\n" + 105 " java.srcDirs = ['src']\n" + 106 " resources.srcDirs = ['src']\n" + 107 " aidl.srcDirs = ['src']\n" + 108 " renderscript.srcDirs = ['src']\n" + 109 " res.srcDirs = ['res']\n" + 110 " assets.srcDirs = ['assets']\n" + 111 " }\n" + 112 " instrumentTest.setRoot('tests')\n" + 113 " }\n" + 114 "}"; 115 116 assertEqualsWhitespaceInsensitive(expectedContents, contents); 117 } catch (Throwable t) { 118 mLastThrown = t; 119 } 120 return null; 121 } 122 }; 123 job.schedule(1000); 124 job.join(); 125 Object property = job.getProperty(ERROR_KEY); 126 assertNull(property); 127 if (mLastThrown != null) { 128 throw mLastThrown; 129 } 130 } 131 132 public void testSimpleAndroidLib() throws Throwable { 133 final IProject project = getProject("simple-library"); 134 ProjectState projectState = Sdk.getProjectState(project.getProject()); 135 ProjectPropertiesWorkingCopy propertiesWorkingCopy = projectState.getProperties().makeWorkingCopy(); 136 propertiesWorkingCopy.setProperty(PROPERTY_LIBRARY, "true"); 137 propertiesWorkingCopy.save(); 138 IResource projectProp = project.findMember(SdkConstants.FN_PROJECT_PROPERTIES); 139 if (projectProp != null) { 140 projectProp.refreshLocal(IResource.DEPTH_ZERO, new NullProgressMonitor()); 141 } 142 143 Job job = new Job("Validate project") { 144 @Override 145 protected IStatus run(IProgressMonitor monitor) { 146 try { 147 IJavaProject javaProject = BaseProjectHelper.getJavaProject(project); 148 149 final ProjectSetupBuilder builder = new ProjectSetupBuilder(); 150 builder.setProject(Collections.singletonList(javaProject)); 151 152 BuildFileCreator.createBuildFiles(builder, null, monitor); 153 File buildfile = new File(javaProject.getResource().getLocation().toString(), 154 BuildFileCreator.BUILD_FILE); 155 assertTrue(buildfile.exists()); 156 String contents = Files.toString(buildfile, Charsets.UTF_8); 157 String expectedContents = 158 "buildscript {\n" + 159 " repositories {\n" + 160 " " + BuildFileCreator.MAVEN_REPOSITORY + "\n" + 161 " }\n" + 162 " dependencies {\n" + 163 " " + BuildFileCreator.PLUGIN_CLASSPATH + "\n" + 164 " }\n" + 165 "}\n" + 166 "apply plugin: 'android-library'\n" + 167 "\n" + 168 "dependencies {\n" + 169 "}\n" + 170 "\n" + 171 "android {\n" + 172 " compileSdkVersion 16\n" + 173 " buildToolsVersion \"16\"\n" + 174 "\n" + 175 " defaultConfig {\n" + 176 " minSdkVersion 1\n" + 177 " targetSdkVersion 1\n" + 178 " }\n" + 179 " sourceSets {\n" + 180 " main {\n" + 181 " manifest.srcFile 'AndroidManifest.xml'\n" + 182 " java.srcDirs = ['src']\n" + 183 " resources.srcDirs = ['src']\n" + 184 " aidl.srcDirs = ['src']\n" + 185 " renderscript.srcDirs = ['src']\n" + 186 " res.srcDirs = ['res']\n" + 187 " assets.srcDirs = ['assets']\n" + 188 " }\n" + 189 " instrumentTest.setRoot('tests')\n" + 190 " }\n" + 191 "}"; 192 193 assertEqualsWhitespaceInsensitive(expectedContents, contents); 194 } catch (Throwable t) { 195 mLastThrown = t; 196 } 197 return null; 198 } 199 }; 200 job.schedule(1000); 201 job.join(); 202 Object property = job.getProperty(ERROR_KEY); 203 assertNull(property); 204 if (mLastThrown != null) { 205 throw mLastThrown; 206 } 207 } 208 209 public void testPlainJavaProject() throws Throwable { 210 IProject project = getJavaProject("simple-java"); 211 final IJavaProject javaProject = BaseProjectHelper.getJavaProject(project); 212 213 final ProjectSetupBuilder builder = new ProjectSetupBuilder(); 214 builder.setProject(Collections.singletonList(javaProject)); 215 216 BuildFileCreator.createBuildFiles(builder, null, null); 217 Job job = new Job("Validate project") { 218 @Override 219 protected IStatus run(IProgressMonitor monitor) { 220 try { 221 File buildfile = new File(javaProject.getResource().getLocation().toString(), "build.gradle"); 222 assertTrue(buildfile.exists()); 223 String contents = Files.toString(buildfile, Charsets.UTF_8); 224 String expectedContents = 225 "apply plugin: 'java'\n" + 226 "sourceSets {\n" + 227 " main.java.srcDirs = ['src']\n" + 228 "}"; 229 230 assertEqualsWhitespaceInsensitive(expectedContents, contents); 231 } catch (Throwable t) { 232 mLastThrown = t; 233 } 234 return null; 235 } 236 }; 237 job.schedule(1000); 238 job.join(); 239 Object property = job.getProperty(ERROR_KEY); 240 assertNull(property); 241 if (mLastThrown != null) { 242 throw mLastThrown; 243 } 244 } 245 246 protected IProject getProject(String projectName) { 247 IProject project = createProject(projectName); 248 assertNotNull(project); 249 if (!testCaseNeedsUniqueProject() && !testNeedsUniqueProject()) { 250 addCleanupDir(AdtUtils.getAbsolutePath(project).toFile()); 251 } 252 addCleanupDir(project.getFullPath().toFile()); 253 return project; 254 } 255 256 protected IProject getJavaProject(String projectName) { 257 IProject project = createJavaProject(projectName); 258 assertNotNull(project); 259 if (!testCaseNeedsUniqueProject() && !testNeedsUniqueProject()) { 260 addCleanupDir(AdtUtils.getAbsolutePath(project).toFile()); 261 } 262 addCleanupDir(project.getFullPath().toFile()); 263 return project; 264 } 265 266 protected IProject createJavaProject(String name) { 267 IRunnableContext context = new IRunnableContext() { 268 @Override 269 public void run(boolean fork, boolean cancelable, IRunnableWithProgress runnable) 270 throws InvocationTargetException, InterruptedException { 271 runnable.run(new NullProgressMonitor()); 272 } 273 }; 274 NewProjectWizardState state = new NewProjectWizardState(Mode.ANY); 275 state.projectName = name; 276 state.packageName = TEST_PROJECT_PACKAGE; 277 state.activityName = name; 278 state.applicationName = name; 279 state.createActivity = false; 280 state.useDefaultLocation = true; 281 if (getMinSdk() != -1) { 282 state.minSdk = Integer.toString(getMinSdk()); 283 } 284 285 NewProjectCreator creator = new NewProjectCreator(state, context); 286 creator.createJavaProjects(); 287 return validateProjectExists(name); 288 } 289 290 /** 291 * Compares two strings, disregarding whitespace. This makes the test less brittle with respect 292 * to insignificant changes. 293 */ 294 protected void assertEqualsWhitespaceInsensitive(String a, String b) { 295 a = stripWhitespace(a); 296 b = stripWhitespace(b); 297 assertEquals("Expected:\n" + a + "\nbut was:\n" + b + "\n\n", a, b); 298 } 299 300 protected String stripWhitespace(String s) { 301 return s.replaceAll("\\s",""); 302 } 303 }