Home | History | Annotate | Download | only in refactoring
      1 /*
      2  * Copyright (C) 2011 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.editors.layout.refactoring;
     17 
     18 import static com.android.SdkConstants.FQCN_GRID_LAYOUT;
     19 import static com.android.SdkConstants.FQCN_LINEAR_LAYOUT;
     20 import static com.android.SdkConstants.FQCN_RELATIVE_LAYOUT;
     21 
     22 import com.android.ide.eclipse.adt.internal.editors.layout.gle2.CanvasViewInfo;
     23 
     24 import org.eclipse.core.resources.IFile;
     25 import org.eclipse.core.runtime.NullProgressMonitor;
     26 import org.eclipse.ltk.core.refactoring.Change;
     27 import org.w3c.dom.Element;
     28 
     29 import java.util.Collections;
     30 import java.util.List;
     31 
     32 @SuppressWarnings("javadoc")
     33 public class ChangeLayoutRefactoringTest extends RefactoringTest {
     34 
     35     public void testChangeLayout1a() throws Exception {
     36         // Test a basic layout which performs some nesting -- tests basic grid layout conversion
     37         checkRefactoring("sample1a.xml", true);
     38     }
     39 
     40     public void testChangeLayout1b() throws Exception {
     41         // Same as 1a, but with different formatting to look for edit handling to for example
     42         // remove a line that is made empty when its only attribute is removed
     43         checkRefactoring("sample1b.xml", true);
     44     }
     45 
     46     public void testChangeLayout2() throws Exception {
     47         // Test code which analyzes an embedded RelativeLayout
     48         checkRefactoring("sample2.xml", true);
     49     }
     50 
     51     public void testChangeLayout3() throws Exception {
     52         // Test handling of LinearLayout "weight" attributes on its children: the child with
     53         // weight > 0 should fill and subsequent children attach on the bottom/right
     54         checkRefactoring("sample3.xml", true);
     55     }
     56 
     57     public void testChangeLayout4() throws Exception {
     58         checkRefactoring("sample4.xml", true);
     59     }
     60 
     61     public void testChangeLayout5() throws Exception {
     62         // Test handling of LinearLayout "gravity" attributes on its children
     63         checkRefactoring("sample5.xml", true);
     64     }
     65 
     66     public void testChangeLayout6() throws Exception {
     67         // Check handling of the LinearLayout "baseline" attribute
     68         checkRefactoring("sample6.xml", true);
     69     }
     70 
     71     public void testGridLayout1() throws Exception {
     72         checkRefactoring(FQCN_GRID_LAYOUT, "sample1a.xml", true);
     73     }
     74 
     75     public void testGridLayout2() throws Exception {
     76         // Test code which analyzes an embedded RelativeLayout
     77         checkRefactoring(FQCN_GRID_LAYOUT, "sample2.xml", true);
     78     }
     79 
     80     public void testGridLayout5() throws Exception {
     81         // Test handling of LinearLayout "gravity" attributes on its children
     82         checkRefactoring(FQCN_GRID_LAYOUT, "sample5.xml", true);
     83     }
     84 
     85     public void testConvertToGrid() throws Exception {
     86         checkRefactoring(FQCN_GRID_LAYOUT, "sample9.xml", true);
     87     }
     88 
     89     public void testConvertFromGrid() throws Exception {
     90         checkRefactoring(FQCN_LINEAR_LAYOUT, "sample10.xml", true);
     91     }
     92 
     93     private void checkRefactoring(String basename, boolean flatten) throws Exception {
     94         checkRefactoring(FQCN_RELATIVE_LAYOUT, basename, flatten);
     95     }
     96 
     97     public void testInitialAttributes() throws Exception {
     98         checkRefactoring(FQCN_LINEAR_LAYOUT, "sample10.xml", true, "android:orientation=vertical");
     99     }
    100 
    101     public void testInsertSpacer() throws Exception {
    102         checkRefactoring(FQCN_GRID_LAYOUT, "sample11.xml", true);
    103     }
    104 
    105     private void checkRefactoring(String newLayoutType, String basename,
    106             boolean flatten) throws Exception {
    107         checkRefactoring(newLayoutType, basename, flatten, null);
    108     }
    109 
    110     @Override
    111     protected int getMinSdk() {
    112         return 14;
    113     }
    114 
    115     private void checkRefactoring(String newLayoutType, String basename,
    116             boolean flatten, String initialAttributes) throws Exception {
    117         IFile file = getLayoutFile(getProject(), basename);
    118         TestContext info = setupTestContext(file, basename);
    119         TestLayoutEditorDelegate layoutEditor = info.mLayoutEditorDelegate;
    120         CanvasViewInfo rootView = info.mRootView;
    121         Element element = info.mElement;
    122 
    123         List<Element> selectedElements = Collections.singletonList(element);
    124         ChangeLayoutRefactoring refactoring = new ChangeLayoutRefactoring(selectedElements,
    125                 layoutEditor);
    126         refactoring.setFlatten(flatten);
    127         refactoring.setType(newLayoutType);
    128         if (initialAttributes != null) {
    129             refactoring.setInitializedAttributes(initialAttributes);
    130         }
    131         refactoring.setRootView(rootView);
    132 
    133         List<Change> changes = refactoring.computeChanges(new NullProgressMonitor());
    134         checkEdits(basename, changes);
    135     }
    136 }
    137