Home | History | Annotate | Download | only in core
      1 /*
      2  * Copyright (C) 2012 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.refactorings.core;
     17 
     18 import com.android.annotations.NonNull;
     19 import com.android.ide.eclipse.adt.AdtPlugin;
     20 import com.android.ide.eclipse.adt.internal.project.BaseProjectHelper;
     21 
     22 import org.eclipse.core.resources.IFolder;
     23 import org.eclipse.core.resources.IProject;
     24 import org.eclipse.core.resources.IResource;
     25 import org.eclipse.jdt.core.IJavaElement;
     26 import org.eclipse.jdt.core.IJavaProject;
     27 import org.eclipse.jdt.core.IType;
     28 import org.eclipse.jdt.internal.corext.refactoring.reorg.IReorgPolicy.IMovePolicy;
     29 import org.eclipse.jdt.internal.corext.refactoring.reorg.JavaMoveProcessor;
     30 import org.eclipse.jdt.internal.corext.refactoring.reorg.ReorgDestinationFactory;
     31 import org.eclipse.jdt.internal.corext.refactoring.reorg.ReorgPolicyFactory;
     32 import org.eclipse.jdt.internal.ui.refactoring.reorg.CreateTargetQueries;
     33 import org.eclipse.jdt.internal.ui.refactoring.reorg.ReorgQueries;
     34 import org.eclipse.ltk.core.refactoring.participants.MoveRefactoring;
     35 import org.eclipse.swt.widgets.Shell;
     36 
     37 
     38 @SuppressWarnings({"javadoc", "restriction"})
     39 public class AndroidTypeMoveParticipantTest extends RefactoringTestBase {
     40     public void testRefactor1() throws Exception {
     41         moveType(
     42                 TEST_PROJECT2,
     43                 "com.example.refactoringtest.CustomView1",
     44                 "src/com/example/refactoringtest/subpackage",
     45                 true /*updateReferences*/,
     46 
     47                 "CHANGES:\n" +
     48                 "-------\n" +
     49                 "[x] Move resource 'testRefactor1/src/com/example/refactoringtest/CustomView1.java' to 'subpackage'\n" +
     50                 "\n" +
     51                 "[x] Move resource 'testRefactor1/src/com/example/refactoringtest/CustomView1.java' to 'subpackage'\n" +
     52                 "\n" +
     53                 "[x] customviews.xml - /testRefactor1/res/layout/customviews.xml\n" +
     54                 "  @@ -9 +9\n" +
     55                 "  -     <com.example.refactoringtest.CustomView1\n" +
     56                 "  +     <com.example.refactoringtest.subpackage.CustomView1\n" +
     57                 "\n" +
     58                 "\n" +
     59                 "[x] customviews.xml - /testRefactor1/res/layout-land/customviews.xml\n" +
     60                 "  @@ -9 +9\n" +
     61                 "  -     <com.example.refactoringtest.CustomView1\n" +
     62                 "  +     <com.example.refactoringtest.subpackage.CustomView1");
     63     }
     64 
     65     public void testRefactorFragment() throws Exception {
     66         moveType(
     67                 TEST_PROJECT2,
     68                 "com.example.refactoringtest.MyFragment",
     69                 "src/com/example/refactoringtest/subpackage",
     70                 true /*updateReferences*/,
     71 
     72                 "CHANGES:\n" +
     73                 "-------\n" +
     74                 "[x] Move resource 'testRefactorFragment/src/com/example/refactoringtest/MyFragment.java' to 'subpackage'\n" +
     75                 "\n" +
     76                 "[x] Move resource 'testRefactorFragment/src/com/example/refactoringtest/MyFragment.java' to 'subpackage'\n" +
     77                 "\n" +
     78                 "[x] activity_main.xml - /testRefactorFragment/res/layout/activity_main.xml\n" +
     79                 "  @@ -33 +33\n" +
     80                 "  -     <fragment android:name=\"com.example.refactoringtest.MyFragment\"/>\n" +
     81                 "  +     <fragment android:name=\"com.example.refactoringtest.subpackage.MyFragment\"/>");
     82     }
     83 
     84     public void testRefactor1_norefs() throws Exception {
     85         moveType(
     86                 TEST_PROJECT2,
     87                 "com.example.refactoringtest.CustomView1",
     88                 "src/com/example/refactoringtest/subpackage",
     89                 false /*updateReferences*/,
     90 
     91                 "CHANGES:\n" +
     92                 "-------\n" +
     93                 "[x] Move resource 'testRefactor1_norefs/src/com/example/refactoringtest/CustomView1.java' to 'subpackage'\n" +
     94                 "\n" +
     95                 "[x] Move resource 'testRefactor1_norefs/src/com/example/refactoringtest/CustomView1.java' to 'subpackage'");
     96     }
     97 
     98     // ---- Test infrastructure ----
     99 
    100     protected void moveType(
    101             @NonNull Object[] testData,
    102             @NonNull String typeFqcn,
    103             @NonNull String destination,
    104             boolean updateReferences,
    105             @NonNull String expected) throws Exception {
    106         IProject project = createProject(testData);
    107 
    108         IFolder destinationFolder = project.getFolder(destination);
    109 
    110         IJavaProject javaProject = BaseProjectHelper.getJavaProject(project);
    111         assertNotNull(javaProject);
    112         IType type = javaProject.findType(typeFqcn);
    113         assertNotNull(typeFqcn, type);
    114         assertTrue(typeFqcn, type.exists());
    115         IResource resource = type.getResource();
    116         assertNotNull(typeFqcn, resource);
    117         assertTrue(typeFqcn, resource.exists());
    118 
    119         IResource[] resources = new IResource[] { resource };
    120         IJavaElement[] elements = new IJavaElement[] { type };
    121         IMovePolicy policy = ReorgPolicyFactory.createMovePolicy(resources, elements);
    122         JavaMoveProcessor processor = new JavaMoveProcessor(policy);
    123         processor.setUpdateReferences(updateReferences);
    124         processor.setUpdateQualifiedNames(true);
    125         assertTrue(policy.canEnable());
    126         processor.setDestination(ReorgDestinationFactory.createDestination(destinationFolder));
    127         Shell parent = AdtPlugin.getShell();
    128         assertNotNull(parent);
    129         processor.setCreateTargetQueries(new CreateTargetQueries(parent));
    130         processor.setReorgQueries(new ReorgQueries(parent));
    131 
    132         MoveRefactoring refactoring = new MoveRefactoring(processor);
    133         checkRefactoring(refactoring, expected);
    134     }
    135 }
    136