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.ide.eclipse.adt.internal.editors.layout.refactoring.UseCompoundDrawableRefactoring.combine;
19 
20 import com.android.ide.eclipse.adt.AdtUtils;
21 
22 import org.eclipse.core.resources.IFile;
23 import org.eclipse.core.runtime.NullProgressMonitor;
24 import org.eclipse.ltk.core.refactoring.Change;
25 import org.eclipse.ltk.core.refactoring.CompositeChange;
26 import org.w3c.dom.Element;
27 
28 import java.util.List;
29 
30 @SuppressWarnings("javadoc")
31 public class UseCompoundDrawableRefactoringTest extends RefactoringTest {
testCombine()32     public void testCombine() throws Exception {
33         assertNull(combine(null, null));
34         assertNull(combine("@dimen/foo", "@dimen/bar"));
35         assertNull(combine("@dimen/foo", "@dimen/bar"));
36         assertNull(combine("1sp", "@dimen/bar"));
37         assertNull(combine("1sp", "2dp"));
38         assertNull(combine(null, ""));
39         assertNull(combine("", null));
40 
41         assertEquals("@dimen/foo", combine(null, "@dimen/foo"));
42         assertEquals("@dimen/foo", combine("@dimen/foo", null));
43         assertEquals("5sp", combine("5sp", null));
44 
45         assertEquals("10sp", combine("8sp", "2sp"));
46         assertEquals("50dp", combine("30dp", "20dp"));
47     }
48 
test1()49     public void test1() throws Exception {
50         // Test converting an image above a text view
51         checkRefactoring("refactoring/usecompound/compound1.xml", "@+id/layout1");
52     }
53 
test2()54     public void test2() throws Exception {
55         // Test converting an image below a text view
56         checkRefactoring("refactoring/usecompound/compound2.xml", "@+id/layout2");
57     }
58 
test3()59     public void test3() throws Exception {
60         // Test converting an image to the left of a text view
61         checkRefactoring("refactoring/usecompound/compound3.xml", "@+id/layout3");
62     }
63 
test4()64     public void test4() throws Exception {
65         // Test converting an image to the right of a text view
66         checkRefactoring("refactoring/usecompound/compound4.xml", "@+id/layout4");
67     }
68 
test5()69     public void test5() throws Exception {
70         // Test converting an image where the LinearLayout is referenced (in a relative layout)
71         // and the text view has an id
72         checkRefactoring("refactoring/usecompound/compound_all.xml", "@+id/layout2");
73     }
74 
test6()75     public void test6() throws Exception {
76         // Test converting an image where the LinearLayout is referenced (in a relative layout)
77         // and the text view does not have an id
78         checkRefactoring("refactoring/usecompound/compound_all.xml", "@+id/layout3");
79     }
80 
test7()81     public void test7() throws Exception {
82         // Test converting where a namespace needs to be migrated
83         checkRefactoring("refactoring/usecompound/compound5.xml", "@+id/layout");
84     }
85 
test8()86     public void test8() throws Exception {
87         // Test padding handling
88         checkRefactoring("refactoring/usecompound/compound6.xml", "@+id/layout1");
89     }
90 
test9()91     public void test9() throws Exception {
92         // Test margin combination
93         checkRefactoring("refactoring/usecompound/compound7.xml", "@+id/layout1");
94     }
95 
checkRefactoring(String basename, String id)96     private void checkRefactoring(String basename, String id)
97             throws Exception {
98         IFile file = getLayoutFile(getProject(), basename);
99         TestContext info = setupTestContext(file, basename);
100         TestLayoutEditorDelegate layoutEditor = info.mLayoutEditorDelegate;
101         List<Element> selectedElements = getElements(info.mElement, new String[] { id });
102 
103         UseCompoundDrawableRefactoring refactoring = new UseCompoundDrawableRefactoring(
104                 selectedElements, layoutEditor);
105         List<Change> changes = refactoring.computeChanges(new NullProgressMonitor());
106 
107         CompositeChange cc = new CompositeChange("Combined from unit test",
108                 changes.toArray(new Change[changes.size()]));
109         cc.markAsSynthetic();
110         addCleanupDir(AdtUtils.getAbsolutePath(getProject()).toFile());
111 
112         checkEdits(basename, changes);
113     }
114 }
115