Commits

radim  committed 583a1d6

Step 1 to support test projects - they are recognized now.

  • Participants
  • Parent commits 4553360

Comments (0)

Files changed (7)

File project/src/org/netbeans/modules/android/project/AndroidGeneralData.java

 
 package org.netbeans.modules.android.project;
 
+import com.google.common.base.Objects;
 import org.netbeans.api.project.ProjectInformation;
 import org.netbeans.modules.android.core.sdk.DalvikPlatform;
 
     data.setProjectName(project.getLookup().lookup(ProjectInformation.class).getName());
     data.setProjectDirPath(project.getProjectDirectoryFile().getAbsolutePath());
     data.setPlatform(AndroidProjectUtil.projectPlatform(project));
+    data.setMainProjectDirPath(project.evaluator().getProperty(PropertyName.TEST_PROJECT_DIR.getName()));
     return data;
   }
 
   private String projectName;
   private String projectDirPath;
   private DalvikPlatform platform;
+  protected String mainProjectDirPath;
 
   public AndroidGeneralData() {
   }
     this.platform = platform;
   }
 
+  /**
+   * Path to a main project if this is a test project or {@code null}
+   */
+  public String getMainProjectDirPath() {
+    return mainProjectDirPath;
+  }
+
+  /**
+   * Set path to a main project if this is a test project or {@code null}
+   */
+  public void setMainProjectDirPath(String mainProjectDirPath) {
+    this.mainProjectDirPath = mainProjectDirPath;
+  }
+
   @Override
   public boolean equals(Object obj) {
     if (obj == null) {
         (this.platform == null || !this.platform.equals(other.platform))) {
       return false;
     }
+    if (!Objects.equal(this.mainProjectDirPath, other.mainProjectDirPath)) {
+      return false;
+    }
     return true;
   }
 
     hash = 67 * hash + (this.projectName != null ? this.projectName.hashCode() : 0);
     hash = 67 * hash + (this.projectDirPath != null ? this.projectDirPath.hashCode() : 0);
     hash = 67 * hash + (this.platform != null ? this.platform.hashCode() : 0);
+    hash = 67 * hash + (this.mainProjectDirPath != null ? this.mainProjectDirPath.hashCode() : 0);
     return hash;
   }
 }

File project/src/org/netbeans/modules/android/project/AndroidInfoImpl.java

 
 package org.netbeans.modules.android.project;
 
+import com.google.common.base.Strings;
 import java.beans.PropertyChangeListener;
 import java.beans.PropertyChangeSupport;
 import java.util.logging.Level;
   public void removePropertyChangeListener(PropertyChangeListener lsnr) {
     pcs.removePropertyChangeListener(lsnr);
   }
+
+  @Override
+  public boolean isTest() {
+    return !Strings.isNullOrEmpty(
+        project.evaluator().getProperty(PropertyName.TEST_PROJECT_DIR.getName()));
+  }
 }

File project/src/org/netbeans/modules/android/project/AndroidProjectImpl.java

     ProjectCreator prjCreator = new ProjectCreator(
         sdkManager, sdkManager.getLocation(),
         ProjectCreator.OutputLevel.NORMAL, SdkLogProvider.createSdkLogger(true));
-    prjCreator.updateProject(data.getProjectDirPath(),
-        data.getPlatform().getAndroidTarget(),
-        data.getProjectName(),
-        /*library*/null);
+    if (data.getMainProjectDirPath() == null) {
+      // regular project
+      prjCreator.updateProject(data.getProjectDirPath(),
+          data.getPlatform().getAndroidTarget(),
+          data.getProjectName(),
+          /*library*/null);
+    } else {
+      prjCreator.updateTestProject(data.getProjectDirPath(), data.getMainProjectDirPath(), sdkManager);
+    }
     getProjectDirectory().refresh();
   }
 

File project/src/org/netbeans/modules/android/project/AndroidProjectInfo.java

 
   boolean isLibrary();
   
+  /** Answers if the project looks like test project.
+   * To be determined from tested.project.dir property in build.properties.
+   * If this file is missing then AndroidManifest.xml is scanned for instrumentation element.
+   * @return 
+   */
+  boolean isTest();
+  
   // TODO - maybe move update methods here from AndroidProject?
   
   /** Getter to find whether project needs to be updated using SDK tools. */

File project/src/org/netbeans/modules/android/project/AndroidProjectUtil.java

         activityEntry,
         data.getPlatform().getAndroidTarget(),
         /*library*/false,
-        /*pathToMainProject*/null);
+        data.getMainProjectDirPath());
   }
 
   public static ManifestData parseProjectManifest(AndroidProject project) {
         
         @Override
         public void warning(SAXParseException ex) throws SAXException {
-          LOG.log(Level.WARNING, null, ex);
+          LOG.log(Level.FINE, null, ex);
         }
 
         @Override
         public void error(SAXParseException ex) throws SAXException {
-          LOG.log(Level.WARNING, null, ex);
+          LOG.log(Level.INFO, null, ex);
           ok = false;
         }
 
         @Override
         public void fatalError(SAXParseException ex) throws SAXException {
-          LOG.log(Level.WARNING, null, ex);
+          LOG.log(Level.INFO, null, ex);
           ok = false;
         }
         

File project/src/org/netbeans/modules/android/project/PropertyName.java

 public enum PropertyName {
 
   SDK_DIR("sdk.dir"),
-  TARGET("target");
+  TARGET("target"),
+  TEST_PROJECT_DIR("tested.project.dir");
   
   private final String name;
 

File project/test/unit/src/org/netbeans/modules/android/project/AndroidInfoImplTest.java

+/*
+ * 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 org.netbeans.modules.android.project;
+
+import java.io.File;
+import org.junit.AfterClass;
+import org.junit.BeforeClass;
+import org.junit.Test;
+import org.netbeans.api.project.Project;
+import org.netbeans.api.project.ProjectManager;
+import org.netbeans.junit.MockServices;
+import org.netbeans.modules.android.core.sdk.DalvikPlatformManager;
+import org.openide.filesystems.FileObject;
+import org.openide.filesystems.FileUtil;
+import static org.junit.Assert.*;
+
+/**
+ * Tests for AndroidActionProvider
+ */
+public class AndroidInfoImplTest {
+    
+  private static final String SDK_DIR = System.getProperty("test.all.android.sdks.home");
+
+  private static File tempFolder;
+  private static FileObject projdir;
+  private static FileObject testprojdir;
+
+  private static Project srcPrj;
+  private static Project testPrj;
+  private static FileObject someSource1;
+  private static FileObject someTest1;
+    
+  @BeforeClass
+  public static void setUpClass() throws Exception {
+    MockServices.setServices();
+    DalvikPlatformManager.getDefault().setSdkLocation(SDK_DIR);
+    tempFolder = File.createTempFile("junit", "");
+    tempFolder.delete();
+    tempFolder.mkdir();
+    
+    FileObject scratch = FileUtil.toFileObject(tempFolder);
+    FileObject sdkDirFo = FileUtil.toFileObject(new File(SDK_DIR));
+
+    projdir = scratch.createFolder("Spinner");
+    FileUtilities.recursiveCopy(sdkDirFo.getFileObject("samples/android-8/Spinner"), projdir);
+    testprojdir = scratch.createFolder("SpinnerTest");
+    FileUtilities.recursiveCopy(sdkDirFo.getFileObject("samples/android-8/SpinnerTest"), testprojdir);
+
+    srcPrj = ProjectManager.getDefault().findProject(projdir);
+    someSource1 = projdir.getFileObject("src/com/android/example/spinner/SpinnerActivity.java");
+    testPrj = ProjectManager.getDefault().findProject(testprojdir);
+    someTest1 = testprojdir.getFileObject("src/com/android/example/spinner/test/SpinnerActivityTest.java");
+    }
+
+
+  
+  @AfterClass
+  public static void delete() {
+    FileUtilities.recursiveDelete(tempFolder);
+  }
+  
+  @Test
+  public void isTest() throws Exception {
+    AndroidProject proj = (AndroidProject) ProjectManager.getDefault().findProject(projdir);
+    assertNotNull(someSource1);
+    AndroidGeneralData data = AndroidGeneralData.fromProject(proj);
+    data.setPlatform(DalvikPlatformManager.getDefault().findPlatformForTarget("android-8"));
+    proj.update(data);
+    AndroidProjectInfo aInfo = proj.getLookup().lookup(AndroidProjectInfo.class);
+    assertFalse(aInfo.isTest());
+    
+    // This project is not updated yet so it hard to tell if it is test.
+    // update it first and check the result
+    AndroidProject tProj = (AndroidProject) ProjectManager.getDefault().findProject(testprojdir);
+    data = AndroidGeneralData.fromProject(tProj);
+    data.setPlatform(DalvikPlatformManager.getDefault().findPlatformForTarget("android-8"));
+    data.setMainProjectDirPath("../Spinner");
+    tProj.update(data);
+    aInfo = tProj.getLookup().lookup(AndroidProjectInfo.class);
+    assertTrue(tProj + " is test project", aInfo.isTest());
+  }
+
+  @Test
+  public void newTestProject() throws Exception {
+    AndroidGeneralData data = new AndroidGeneralData();
+    File prjDir = new File(tempFolder, "project");
+    prjDir.mkdir();
+    data.setProjectDirPath(prjDir.getAbsolutePath());
+    data.setProjectName("project");
+    data.setPlatform(DalvikPlatformManager.getDefault().findPlatformForTarget("android-7"));
+    String pkgName = "com.foo.bar";
+    String activity = "MainActivity";
+
+    AndroidProjectUtil.create(data, pkgName, activity);
+    Project prj = ProjectManager.getDefault().findProject(FileUtil.toFileObject(prjDir));
+    assertNotNull(prj);
+    AndroidProjectInfo aPrj = prj.getLookup().lookup(AndroidProjectInfo.class);
+    assertNotNull(aPrj);
+    assertFalse(aPrj.isTest());
+    
+    File testPrjDir = new File(tempFolder, "projectTest");
+    testPrjDir.mkdir();
+    data.setProjectDirPath(testPrjDir.getAbsolutePath());
+    data.setMainProjectDirPath("../project");
+    AndroidProjectUtil.create(data, pkgName, activity);
+    
+    Project tPrj = ProjectManager.getDefault().findProject(FileUtil.toFileObject(testPrjDir));
+    assertNotNull(tPrj);
+    aPrj = tPrj.getLookup().lookup(AndroidProjectInfo.class);
+    assertNotNull(aPrj);
+    assertTrue(aPrj.isTest());
+  }
+}