uidesigner/com.nokia.sdt.uimodel.tests/src/com/nokia/sdt/uimodel/tests/EventsTest.java
changeset 0 fb279309251b
equal deleted inserted replaced
-1:000000000000 0:fb279309251b
       
     1 /*
       
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of the License "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: 
       
    15 *
       
    16 */
       
    17 package com.nokia.sdt.uimodel.tests;
       
    18 
       
    19 import com.nokia.sdt.component.IComponent;
       
    20 import com.nokia.sdt.component.event.IEventDescriptor;
       
    21 import com.nokia.sdt.component.event.IEventDescriptorProvider;
       
    22 import com.nokia.sdt.datamodel.adapter.*;
       
    23 import com.nokia.sdt.datamodel.util.ModelUtils;
       
    24 import com.nokia.sdt.symbian.dm.DesignerDataModel;
       
    25 import com.nokia.sdt.testsupport.AdapterHelpers;
       
    26 import com.nokia.sdt.testsupport.TestDataModelHelpers;
       
    27 
       
    28 import org.eclipse.emf.common.command.Command;
       
    29 import org.eclipse.emf.ecore.EObject;
       
    30 import org.eclipse.ui.views.properties.IPropertySource;
       
    31 
       
    32 import java.util.Collection;
       
    33 
       
    34 import junit.framework.TestCase;
       
    35 
       
    36 public class EventsTest extends TestCase {
       
    37 
       
    38 	static final String CONTAINER_COMPONENT = "com.nokia.examples.container";
       
    39 	static final String SPECIAL_CONTAINER_COMPONENT = "com.nokia.examples.specialContainer";
       
    40 	static final String CHILD_COMPONENT = "com.nokia.examples.baseComponent";
       
    41 	static final String FILTERED_EVENTS_CHILD_COMPONENT = "com.nokia.examples.filteredEventsComponent";
       
    42 	
       
    43 	DesignerDataModel model;
       
    44 	EObject root;
       
    45 	EObject child;
       
    46 	
       
    47 	protected void setUp() throws Exception {
       
    48 		
       
    49 		model = (DesignerDataModel) TestDataModelHelpers.createTemporaryModel();
       
    50 		TestDataModelHelpers.initDefaultComponentSet(model);
       
    51 		IComponent component = model.getComponentSet().lookupComponent(CONTAINER_COMPONENT);
       
    52 		root = model.createNewComponentInstance(component);
       
    53 		model.getDesignerData().getRootContainers().add(root);
       
    54 		IPropertySource ps = AdapterHelpers.getPropertySource(root);
       
    55 		ps.setPropertyValue("name", "view1");
       
    56 		ps.setPropertyValue("className", "CMyContainer");
       
    57 
       
    58 		component = model.getComponentSet().lookupComponent(CHILD_COMPONENT);
       
    59 		child = model.createNewComponentInstance(component);
       
    60 		Command command = model.createAddNewComponentInstanceCommand(root, child, -1);
       
    61 		assertTrue(command.canExecute());
       
    62 		command.execute();
       
    63 	}
       
    64 
       
    65 	protected void tearDown() throws Exception {
       
    66 		super.tearDown();
       
    67 	}
       
    68 		
       
    69 	public void testAddDefaultBinding() {
       
    70 		IComponentInstance ci = ModelUtils.getComponentInstance(child);
       
    71 		IComponent component = ci.getComponent();
       
    72 		IEventDescriptorProvider edp = (IEventDescriptorProvider) component.getAdapter(IEventDescriptorProvider.class);
       
    73 		IEventDescriptor[] eventDescriptors = edp.getEventDescriptors();
       
    74 		Command command = model.createAddEventBindingCommand(child, eventDescriptors[0], null);
       
    75 		assertTrue(command.canExecute());
       
    76 		command.execute();
       
    77 		Collection result = command.getResult();
       
    78 		Object[] objects = result.toArray();
       
    79 		assertTrue(objects[0] instanceof IEventBinding);
       
    80 		IEventBinding binding = (IEventBinding) objects[0];
       
    81 		assertEquals(binding.getHandlerName(), "BaseComponent1_SomethingHappened");
       
    82 
       
    83 		IEventBinding[] eventBindings = ci.getEventBindings();
       
    84 		assertNotNull(eventBindings);
       
    85 		assertTrue(eventBindings.length == 1);
       
    86 		assertEquals(eventBindings[0].getEventDescriptor(), eventDescriptors[0]);
       
    87 	}
       
    88 	
       
    89 	public void testAddUserSpecifiedBinding() {
       
    90 		IComponentInstance ci = ModelUtils.getComponentInstance(child);
       
    91 		IComponent component = ci.getComponent();
       
    92 		IEventDescriptorProvider edp = (IEventDescriptorProvider) component.getAdapter(IEventDescriptorProvider.class);
       
    93 		IEventDescriptor[] eventDescriptors = edp.getEventDescriptors();
       
    94 		String userSpecifiedName = "MyEventHandler";
       
    95 		Command command = model.createAddEventBindingCommand(child, eventDescriptors[0], userSpecifiedName);
       
    96 		assertTrue(command.canExecute());
       
    97 		command.execute();
       
    98 		Collection result = command.getResult();
       
    99 		Object[] objects = result.toArray();
       
   100 		assertTrue(objects[0] instanceof IEventBinding);
       
   101 		IEventBinding binding = (IEventBinding) objects[0];
       
   102 		assertEquals(binding.getHandlerName(), userSpecifiedName);
       
   103 	}
       
   104 	
       
   105 	public void testRemoveBinding() {
       
   106 		IComponentInstance ci = ModelUtils.getComponentInstance(child);
       
   107 		IComponent component = ci.getComponent();
       
   108 		IEventDescriptorProvider edp = (IEventDescriptorProvider) component.getAdapter(IEventDescriptorProvider.class);
       
   109 		IEventDescriptor[] eventDescriptors = edp.getEventDescriptors();
       
   110 		Command command = model.createAddEventBindingCommand(child, eventDescriptors[0], null);
       
   111 		assertTrue(command.canExecute());
       
   112 		command.execute();
       
   113 		Collection result = command.getResult();
       
   114 		Object[] objects = result.toArray();
       
   115 		assertTrue(objects[0] instanceof IEventBinding);
       
   116 		IEventBinding binding = (IEventBinding) objects[0];
       
   117 		assertEquals(binding.getHandlerName(), "BaseComponent1_SomethingHappened");
       
   118 
       
   119 		IEventBinding[] eventBindings = ci.getEventBindings();
       
   120 		assertNotNull(eventBindings);
       
   121 		assertTrue(eventBindings.length == 1);
       
   122 
       
   123 		command = model.createRemoveEventBindingCommand(binding);
       
   124 		assertTrue(command.canExecute());
       
   125 		command.execute();
       
   126 		
       
   127 		eventBindings = ci.getEventBindings();
       
   128 		assertNull(eventBindings);
       
   129 	}
       
   130 
       
   131 	/**
       
   132 	 * This case has no event info override so all the events are exposed
       
   133 	 *
       
   134 	 */
       
   135 	public void testUnfilteredEvents() {
       
   136 		// add to filter child to root, a container
       
   137 		IComponent component = model.getComponentSet().lookupComponent(FILTERED_EVENTS_CHILD_COMPONENT);
       
   138 		EObject filteredChild = model.createNewComponentInstance(component);
       
   139 		Command command = model.createAddNewComponentInstanceCommand(root, filteredChild, -1);
       
   140 		assertTrue(command.canExecute());
       
   141 		command.execute();
       
   142 
       
   143 		// everything visible on component
       
   144 		IEventDescriptorProvider edp = (IEventDescriptorProvider) component.getAdapter(IEventDescriptorProvider.class);
       
   145 		IEventDescriptor[] eventDescriptors = edp.getEventDescriptors();
       
   146 		assertEquals(2, eventDescriptors.length);
       
   147 		int defaultIndex = edp.getDefaultEventIndex();
       
   148 		assertEquals(1, defaultIndex);
       
   149 		
       
   150 		// some may be filtered on child, but not in this case
       
   151 		IComponentEventDescriptorProvider cedp = ModelUtils.getComponentEventDescriptorProvider(filteredChild);
       
   152 		IEventDescriptor[] filteredEventDescriptors = cedp.getEventDescriptors();
       
   153 		assertEquals(eventDescriptors.length, filteredEventDescriptors.length);
       
   154 		
       
   155 		int filteredDefaultIndex = cedp.getDefaultEventIndex();
       
   156 		assertEquals(defaultIndex, filteredDefaultIndex);
       
   157 	}
       
   158 
       
   159 	/**
       
   160 	 * This case has no event info override so all the events are exposed
       
   161 	 *
       
   162 	 */
       
   163 	public void testFilteredEvents() {
       
   164 		// add to a special container 
       
   165 		IComponent specialContainerComponent = model.getComponentSet().lookupComponent(SPECIAL_CONTAINER_COMPONENT);
       
   166 		EObject container = model.createNewComponentInstance(specialContainerComponent);
       
   167 		Command command = model.createAddNewComponentInstanceCommand(root, container, -1);
       
   168 		assertTrue(command.canExecute());
       
   169 		command.execute();
       
   170 
       
   171 		IComponent component = model.getComponentSet().lookupComponent(FILTERED_EVENTS_CHILD_COMPONENT);
       
   172 		EObject filteredChild = model.createNewComponentInstance(component);
       
   173 		command = model.createAddNewComponentInstanceCommand(container, filteredChild, -1);
       
   174 		assertTrue(command.canExecute());
       
   175 		command.execute();
       
   176 
       
   177 		// filtered child is different here
       
   178 		IEventDescriptorProvider edp = (IEventDescriptorProvider) component.getAdapter(IEventDescriptorProvider.class);
       
   179 		IEventDescriptor[] eventDescriptors = edp.getEventDescriptors();
       
   180 		assertEquals(2, eventDescriptors.length);
       
   181 		int defaultIndex = edp.getDefaultEventIndex();
       
   182 		assertEquals(1, defaultIndex);
       
   183 		
       
   184 		IComponentEventDescriptorProvider cedp = ModelUtils.getComponentEventDescriptorProvider(filteredChild);
       
   185 		IEventDescriptor[] filteredEventDescriptors = cedp.getEventDescriptors();
       
   186 		assertEquals(1, filteredEventDescriptors.length);
       
   187 		
       
   188 		int filteredDefaultIndex = cedp.getDefaultEventIndex();
       
   189 		assertEquals(0, filteredDefaultIndex);
       
   190 	}
       
   191 
       
   192 }