Home | History | Annotate | Download | only in gre
      1 /*
      2  * Copyright (C) 2010 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 
     17 package com.android.ide.eclipse.adt.internal.editors.layout.gre;
     18 
     19 import com.android.ide.common.api.INode;
     20 import com.android.ide.common.api.Rect;
     21 import com.android.ide.common.rendering.api.ViewInfo;
     22 import com.android.ide.eclipse.adt.internal.editors.layout.descriptors.ViewElementDescriptor;
     23 import com.android.ide.eclipse.adt.internal.editors.layout.gle2.CanvasViewInfo;
     24 import com.android.ide.eclipse.adt.internal.editors.layout.uimodel.UiViewElementNode;
     25 
     26 import java.util.Arrays;
     27 
     28 import junit.framework.TestCase;
     29 
     30 public class NodeFactoryTest extends TestCase {
     31 
     32     private NodeFactory m;
     33 
     34     @Override
     35     protected void setUp() throws Exception {
     36         super.setUp();
     37         m = new NodeFactory(null);
     38 
     39     }
     40 
     41     @Override
     42     protected void tearDown() throws Exception {
     43         super.tearDown();
     44         m = null;
     45     }
     46 
     47     public final void testCreateCanvasViewInfo() {
     48         ViewElementDescriptor ved = new ViewElementDescriptor("xml", "com.example.MyJavaClass");
     49         UiViewElementNode uiv = new UiViewElementNode(ved);
     50         ViewInfo lvi = new ViewInfo("name", uiv, 10, 12, 110, 120);
     51         CanvasViewInfo cvi = CanvasViewInfo.create(lvi, true /* layoutlib5 */).getFirst();
     52 
     53         // Create a NodeProxy.
     54         NodeProxy proxy = m.create(cvi);
     55 
     56         // getNode() is our only internal implementation method.
     57         assertNotNull(proxy);
     58         assertSame(uiv, proxy.getNode());
     59 
     60         // Groovy scripts only see the INode interface so we want to primarily test that.
     61         INode inode = proxy;
     62         assertEquals(new Rect(10, 12, 110-10-1, 120-12-1), inode.getBounds());
     63         assertTrue(Arrays.equals(new INode[0], inode.getChildren()));
     64         assertEquals("com.example.MyJavaClass", inode.getFqcn());
     65         assertNull(inode.getParent());
     66         assertSame(inode, inode.getRoot());
     67 
     68     }
     69 
     70     public final void testCreateUiViewElementNode() {
     71         ViewElementDescriptor ved = new ViewElementDescriptor("xml", "com.example.MyJavaClass");
     72         UiViewElementNode uiv = new UiViewElementNode(ved);
     73 
     74         // Create a NodeProxy.
     75         NodeProxy proxy = m.create(uiv);
     76 
     77         // getNode() is our only internal implementation method.
     78         assertNotNull(proxy);
     79         assertSame(uiv, proxy.getNode());
     80 
     81         // Groovy scripts only see the INode interface so we want to primarily test that.
     82         INode inode = proxy;
     83         // Nodes constructed using this create() method do not have valid bounds.
     84         // There should be one invalid bound rectangle.
     85         assertNotNull(inode.getBounds());
     86         assertFalse(inode.getBounds().isValid());
     87         // All the other properties should be set correctly.
     88         assertTrue(Arrays.equals(new INode[0], inode.getChildren()));
     89         assertEquals("com.example.MyJavaClass", inode.getFqcn());
     90         assertNull(inode.getParent());
     91         assertSame(inode, inode.getRoot());
     92     }
     93 
     94     public final void testCreateDup() {
     95         ViewElementDescriptor ved = new ViewElementDescriptor("xml", "com.example.MyJavaClass");
     96         UiViewElementNode uiv = new UiViewElementNode(ved);
     97         ViewInfo lvi = new ViewInfo("name", uiv, 10, 12, 110, 120);
     98         CanvasViewInfo cvi = CanvasViewInfo.create(lvi, true /* layoutlib5 */).getFirst();
     99 
    100         // NodeProxies are cached. Creating the same one twice returns the same proxy.
    101         NodeProxy proxy1 = m.create(cvi);
    102         NodeProxy proxy2 = m.create(cvi);
    103         assertSame(proxy2, proxy1);
    104     }
    105 
    106     public final void testClear() {
    107         ViewElementDescriptor ved = new ViewElementDescriptor("xml", "com.example.MyJavaClass");
    108         UiViewElementNode uiv = new UiViewElementNode(ved);
    109         ViewInfo lvi = new ViewInfo("name", uiv, 10, 12, 110, 120);
    110         CanvasViewInfo cvi = CanvasViewInfo.create(lvi, true /* layoutlib5 */).getFirst();
    111 
    112         // NodeProxies are cached. Creating the same one twice returns the same proxy.
    113         NodeProxy proxy1 = m.create(cvi);
    114         NodeProxy proxy2 = m.create(cvi);
    115         assertSame(proxy2, proxy1);
    116 
    117         // Clearing the cache will force it create a new proxy.
    118         m.clear();
    119         NodeProxy proxy3 = m.create(cvi);
    120         assertNotSame(proxy1, proxy3);
    121     }
    122 
    123 }
    124