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.DOT_XML; 19 20 import com.android.ide.eclipse.adt.AdtPlugin; 21 import com.android.ide.eclipse.adt.internal.preferences.AdtPrefs; 22 23 import org.eclipse.core.resources.IFile; 24 import org.eclipse.core.runtime.IPath; 25 import org.eclipse.core.runtime.NullProgressMonitor; 26 import org.eclipse.jface.preference.IPreferenceStore; 27 import org.eclipse.ltk.core.refactoring.Change; 28 import org.eclipse.ltk.core.refactoring.TextFileChange; 29 import org.w3c.dom.Element; 30 31 import java.util.HashMap; 32 import java.util.List; 33 import java.util.Map; 34 35 public class ExtractIncludeRefactoringTest extends RefactoringTest { 36 37 @Override 38 protected boolean autoFormat() { 39 return false; 40 } 41 42 @Override 43 protected boolean testCaseNeedsUniqueProject() { 44 // Because some of these tests look at ALL layouts in the project 45 // to identify matches 46 return true; 47 } 48 49 public void testExtract1() throws Exception { 50 // Basic: Extract a single button 51 checkRefactoring("sample3.xml", "newlayout1", false, null, 2, false /* diffs */, 52 "@+id/button2"); 53 } 54 55 public void testExtract2() throws Exception { 56 // Extract a couple of elements 57 checkRefactoring("sample3.xml", "newlayout2", false, null, 2, false /* diffs */, 58 "@+id/button2", "@+id/android_logo"); 59 } 60 61 public void testExtract3() throws Exception { 62 // Test to make sure layout attributes are updated 63 checkRefactoring("sample2.xml", "newlayout3", false, null, 2, false /* diffs */, 64 "@+id/button3"); 65 } 66 67 public void testExtract4() throws Exception { 68 // Tests extracting from -multiple- files (as well as with custom android namespace 69 // prefix) 70 71 // Make sure the variation-files exist 72 Map<IPath, String> extraFiles = new HashMap<IPath, String>(); 73 extraFiles.put(getTestDataFile(getProject(), "sample3-variation1.xml", 74 "res/layout-land/sample3.xml").getProjectRelativePath(), 75 "sample3-variation1.xml"); 76 extraFiles.put(getTestDataFile(getProject(), "sample3-variation2.xml", 77 "res/layout-xlarge-land/sample3.xml").getProjectRelativePath(), 78 "sample3-variation2.xml"); 79 80 checkRefactoring("sample3.xml", "newlayout3", true, extraFiles, 4, false /* diffs */, 81 "@+id/android_logo"); 82 } 83 84 public void testExtract5() throws Exception { 85 // Tests extracting from multiple files with -contiguous regions-. 86 87 // Make sure the variation-files exist 88 Map<IPath, String> extraFiles = new HashMap<IPath, String>(); 89 extraFiles.put(getTestDataFile(getProject(), "sample3-variation1.xml", 90 "res/layout-land/sample3.xml").getProjectRelativePath(), 91 "sample3-variation1.xml"); 92 extraFiles.put(getTestDataFile(getProject(), "sample3-variation2.xml", 93 "res/layout-xlarge-land/sample3.xml").getProjectRelativePath(), 94 "sample3-variation2.xml"); 95 96 checkRefactoring("sample3.xml", "newlayout3", true, extraFiles, 4, false /* diffs */, 97 "@+id/android_logo", "@+id/button1"); 98 } 99 100 public void testExtract6() throws Exception { 101 // Tests extracting from multiple files where the layouts are completely 102 // different/unrelated files 103 104 // Create the duplicate files 105 Map<IPath, String> extraFiles = new HashMap<IPath, String>(); 106 extraFiles.put(getTestDataFile(getProject(), "sample1a.xml", 107 "res/layout/sample1a.xml").getProjectRelativePath(), 108 "sample1a.xml"); 109 extraFiles.put(getTestDataFile(getProject(), "sample7.xml", "res/layout/sample7.xml") 110 .getProjectRelativePath(), "sample7.xml"); 111 extraFiles.put(getTestDataFile(getProject(), "sample8.xml", "res/layout/sample8.xml") 112 .getProjectRelativePath(), "sample8.xml"); 113 114 checkRefactoring("sample7.xml", "newlayout6", true, extraFiles, 4, true /* diffs */, 115 "@+id/linearLayout4"); 116 } 117 118 public void testExtract7() throws Exception { 119 // Just like testExtract6, except we turn on auto-formatting 120 IPreferenceStore store = AdtPlugin.getDefault().getPreferenceStore(); 121 AdtPrefs.init(store); 122 AdtPrefs prefs = AdtPrefs.getPrefs(); 123 prefs.initializeStoreWithDefaults(store); 124 store.setValue(AdtPrefs.PREFS_FORMAT_GUI_XML, true); 125 prefs.loadValues(null); 126 127 assertTrue(AdtPrefs.getPrefs().getFormatGuiXml()); 128 129 testExtract6(); 130 } 131 132 133 private void checkRefactoring(String basename, String layoutName, 134 boolean replaceOccurrences, Map<IPath,String> extraFiles, 135 int expectedModifiedFileCount, boolean createDiffs, String... ids) throws Exception { 136 assertTrue(ids.length > 0); 137 138 IFile file = getLayoutFile(getProject(), basename); 139 TestContext info = setupTestContext(file, basename); 140 TestLayoutEditorDelegate layoutEditor = info.mLayoutEditorDelegate; 141 List<Element> selectedElements = getElements(info.mElement, ids); 142 143 ExtractIncludeRefactoring refactoring = new ExtractIncludeRefactoring(selectedElements, 144 layoutEditor); 145 refactoring.setLayoutName(layoutName); 146 refactoring.setReplaceOccurrences(replaceOccurrences); 147 List<Change> changes = refactoring.computeChanges(new NullProgressMonitor()); 148 149 assertTrue(changes.size() >= 3); 150 151 Map<IPath,String> fileToGolden = new HashMap<IPath,String>(); 152 IPath sourcePath = file.getProjectRelativePath(); 153 fileToGolden.put(sourcePath, basename); 154 IPath newPath = sourcePath.removeLastSegments(1).append(layoutName + DOT_XML); 155 fileToGolden.put(newPath, layoutName + DOT_XML); 156 if (extraFiles != null) { 157 fileToGolden.putAll(extraFiles); 158 } 159 160 checkEdits(changes, fileToGolden, createDiffs); 161 162 int modifiedFileCount = 0; 163 for (Change change : changes) { 164 if (change instanceof TextFileChange) { 165 modifiedFileCount++; 166 } 167 } 168 assertEquals(expectedModifiedFileCount, modifiedFileCount); 169 } 170 } 171