Home | History | Annotate | Download | only in properties
      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.editors.layout.properties;
     17 
     18 import static com.android.SdkConstants.ANDROID_URI;
     19 
     20 import com.android.annotations.NonNull;
     21 import com.android.annotations.Nullable;
     22 import com.android.ide.common.api.IAttributeInfo;
     23 import com.android.ide.common.api.IAttributeInfo.Format;
     24 import com.android.ide.common.layout.TestAttributeInfo;
     25 import com.android.ide.eclipse.adt.internal.editors.common.CommonXmlEditor;
     26 import com.android.ide.eclipse.adt.internal.editors.descriptors.AttributeDescriptor;
     27 import com.android.ide.eclipse.adt.internal.editors.descriptors.TextAttributeDescriptor;
     28 
     29 import org.eclipse.jface.fieldassist.IContentProposal;
     30 
     31 import java.util.ArrayList;
     32 import java.util.Arrays;
     33 import java.util.EnumSet;
     34 import java.util.List;
     35 
     36 import junit.framework.TestCase;
     37 
     38 @SuppressWarnings("javadoc")
     39 public class ValueCompleterTest extends TestCase {
     40     private void checkCompletion(String text, int offset,
     41             String property, EnumSet<Format> formats, String[] values,
     42             List<String> expected) {
     43         assertTrue(text.length() >= offset);
     44 
     45         TestAttributeInfo info =
     46                 new TestAttributeInfo(property, formats, "unittest", values, values, null);
     47         TestValueCompleter completer = new TestValueCompleter(
     48                 new TestTextAttributeDescriptor(property, info));
     49         IContentProposal[] proposals = completer.getProposals(text, offset);
     50         List<String> actual = new ArrayList<String>();
     51         for (IContentProposal proposal : proposals) {
     52             String content = proposal.getContent();
     53             actual.add(content);
     54         }
     55         assertEquals(expected.toString(), actual.toString());
     56     }
     57 
     58     public void test() throws Exception {
     59         checkCompletion("@android:string", 3, "text",
     60                 EnumSet.of(Format.REFERENCE), null,
     61                 Arrays.asList(new String[] { }));
     62     }
     63 
     64     public void test1a() throws Exception {
     65         checkCompletion("matc", 4, "layout_width",
     66                 EnumSet.of(Format.DIMENSION, Format.ENUM),
     67                 new String[] { "fill_parent", "match_parent", "wrap_content" },
     68 
     69                 Arrays.asList(new String[] { "match_parent", "fill_parent", "wrap_content" }));
     70     }
     71 
     72     public void test1b() throws Exception {
     73         checkCompletion("fi", 2, "layout_width",
     74                 EnumSet.of(Format.DIMENSION, Format.ENUM),
     75                 new String[] { "fill_parent", "match_parent", "wrap_content" },
     76 
     77                 Arrays.asList(new String[] { "fill_parent", "match_parent", "wrap_content" }));
     78     }
     79 
     80     public void test2() throws Exception {
     81         checkCompletion("50", 2, "layout_width",
     82                 EnumSet.of(Format.DIMENSION, Format.ENUM),
     83                 new String[] { "fill_parent", "match_parent", "wrap_content" },
     84 
     85                 Arrays.asList(new String[] { "50dp", "fill_parent", "match_parent",
     86                         "wrap_content" }));
     87     }
     88 
     89     public void test3() throws Exception {
     90         checkCompletion("42", 2, "textSize",
     91                 EnumSet.of(Format.DIMENSION),
     92                 null,
     93 
     94                 Arrays.asList(new String[] { "42sp", "42dp" }));
     95     }
     96 
     97     public void test4() throws Exception {
     98         checkCompletion("", 0, "gravity",
     99                 EnumSet.of(Format.FLAG),
    100                 new String[] { "top", "bottom", "left", "right", "center" },
    101 
    102                 Arrays.asList(new String[] { "top", "bottom", "left", "right", "center" }));
    103     }
    104 
    105     public void test5() throws Exception {
    106         checkCompletion("left", 4, "gravity",
    107                 EnumSet.of(Format.FLAG),
    108                 new String[] { "top", "bottom", "left", "right", "center" },
    109 
    110                 Arrays.asList(new String[] {
    111                         "left", "left|top", "left|bottom", "left|right", "left|center" }));
    112     }
    113 
    114     public void test6() throws Exception {
    115         checkCompletion("left|top", 8, "gravity",
    116                 EnumSet.of(Format.FLAG),
    117                 new String[] { "top", "bottom", "left", "right", "center" },
    118 
    119                 Arrays.asList(new String[] {
    120                         "left|top", "left|top|bottom", "left|top|right", "left|top|center" }));
    121     }
    122 
    123     // TODO ?android
    124 
    125     private class TestTextAttributeDescriptor extends TextAttributeDescriptor {
    126         public TestTextAttributeDescriptor(String xmlLocalName, IAttributeInfo attrInfo) {
    127             super(xmlLocalName, ANDROID_URI, attrInfo);
    128         }
    129     }
    130 
    131     private class TestValueCompleter extends ValueCompleter {
    132         private final AttributeDescriptor mDescriptor;
    133 
    134         TestValueCompleter(AttributeDescriptor descriptor) {
    135             mDescriptor = descriptor;
    136             assert descriptor.getAttributeInfo() != null;
    137         }
    138 
    139         @Override
    140         @Nullable
    141         protected CommonXmlEditor getEditor() {
    142             return null;
    143         }
    144 
    145         @Override
    146         @NonNull
    147         protected AttributeDescriptor getDescriptor() {
    148             return mDescriptor;
    149         }
    150     }
    151 }
    152