/** Verify initializing attributes and event handlers during component injection. */
  public void testAttributesAndEvents() {
    String attributeMarkup =
        "<aura:attribute name='strAttr' type='String'/>"
            + "<aura:attribute name='booleanAttr' type='Boolean'/>";
    String eventsMarkup =
        "<aura:registerevent name='press' type='ui:press'/>"
            + "<aura:registerevent name='mouseout' type='ui:mouseout'/> ";

    DefDescriptor<ComponentDef> cmp =
        addSourceAutoCleanup(
            ComponentDef.class,
            String.format(baseComponentTag, "", attributeMarkup + eventsMarkup));
    Map<String, Object> attributes = Maps.newHashMap();
    attributes.put("strAttr", "");
    attributes.put("booleanAttr", false);
    attributes.put("press", "function(e){alert('press')}");
    attributes.put("mouseout", "function(e){alert('mouseout')}");

    Appendable out = new StringBuffer();
    Integration integration = service.createIntegration("", Mode.UTEST, true, null);
    try {
      integration.injectComponent(cmp.getDescriptorName(), attributes, "", "", out);
    } catch (Exception unexpected) {
      fail(
          "Exception occured when injecting component with attribute and event handlers. Exception:"
              + unexpected.getMessage());
    }
  }
 /** Verify injecting multiple components using a single Integration Object. */
 @UnAdaptableTest
 public void testInjectingMultipleComponents() throws Exception {
   DefDescriptor<ComponentDef> cmp1 =
       addSourceAutoCleanup(ComponentDef.class, String.format(baseComponentTag, "", ""));
   DefDescriptor<ComponentDef> cmp2 =
       addSourceAutoCleanup(ComponentDef.class, String.format(baseComponentTag, "", ""));
   Map<String, Object> attributes = Maps.newHashMap();
   Appendable out = new StringBuffer();
   Integration integration = service.createIntegration("", Mode.UTEST, true, null);
   try {
     integration.injectComponent(cmp1.getDescriptorName(), attributes, "", "", out);
     integration.injectComponent(cmp2.getDescriptorName(), attributes, "", "", out);
   } catch (Exception unexpected) {
     fail("Failed to inject multiple component. Exception:" + unexpected.getMessage());
   }
   // Verify that the boot strap was written only once
   assertNotNull(out);
   Pattern frameworkJS =
       Pattern.compile(
           "<script src=\"/auraFW/javascript/[^/]+/aura_.{4}.js\\?aura.fwuid=[-0-9A-Za-z_]*\" ></script>");
   Matcher m = frameworkJS.matcher(out.toString());
   int counter = 0;
   while (m.find()) {
     counter++;
   }
   assertEquals("Bootstrap template should be written out only once.", 1, counter);
 }
 /**
  * Verify that injecting non existing exceptions is flagged with an exception.
  *
  * @throws Exception
  */
 public void testInjectingNonExistingComponent() throws Exception {
   Map<String, Object> attributes = Maps.newHashMap();
   Appendable out = new StringBuffer();
   Integration integration = service.createIntegration("", Mode.UTEST, true, null);
   try {
     integration.injectComponent("foo:bared", attributes, "", "", out);
     fail(
         "Instantiating component through integration service should have failed because of missing component def.");
   } catch (DefinitionNotFoundException expected) {
     // Expected exception
     assertTrue(expected.getMessage().contains("No COMPONENT named markup://foo:bared found"));
   }
 }
 /** Verify that only component defs can be injected. */
 public void testInjectingApplications() throws Exception {
   String validApp = "test:laxSecurity";
   Map<String, Object> attributes = Maps.newHashMap();
   Appendable out = new StringBuffer();
   Integration integration = service.createIntegration("", Mode.UTEST, true, null);
   try {
     integration.injectComponent(validApp, attributes, "", "", out);
     fail("Injecting an application through integration service should have failed.");
   } catch (DefinitionNotFoundException expected) {
     // TODO: Maybe a better error message?
     assertTrue(
         expected.getMessage().contains("No COMPONENT named markup://test:laxSecurity found"));
   }
 }
 /**
  * Verify that specifying non existing attributes names for initializing will result in
  * AuraRunTime exception.
  */
 public void testNonExistingAttributeValues() throws Exception {
   Map<String, Object> attributes = Maps.newHashMap();
   attributes.put("fooBar", "");
   Appendable out = new StringBuffer();
   Integration integration = service.createIntegration("", Mode.UTEST, true, null);
   try {
     integration.injectComponent(simpleComponentTag, attributes, "", "", out);
     fail("Using nonexisting attribute names should have failed.");
   } catch (AuraRuntimeException expected) {
     // TODO rework after ccollab: Earlier error message was like
     // "Unknown attribute or event ui:button:fooBar"
     assertEquals("Unknown attribute or event ui:button - fooBar", expected.getMessage());
   }
 }
 private Appendable injectSimpleComponent(Integration obj) throws Exception {
   Map<String, Object> attributes = Maps.newHashMap();
   attributes.put("label", "Click Me");
   Appendable out = new StringBuffer();
   obj.injectComponent(simpleComponentTag, attributes, "", "", out);
   return out;
 }
 /**
  * AuraExecutionExceptions that occur during component instantiation should not stop the process
  * of component injection. The exception message should be conveyed to the user. There will be a
  * UI Test for this scenario.
  *
  * @throws Exception
  */
 public void testExceptionDuringComponentInstantiation() throws Exception {
   DefDescriptor<ComponentDef> cmp =
       addSourceAutoCleanup(
           ComponentDef.class,
           String.format(
               baseComponentTag,
               "",
               "<aura:attribute name='reqAttr' required='true' type='String'/>"));
   Map<String, Object> attributes = Maps.newHashMap();
   Appendable out = new StringBuffer();
   Integration integration = service.createIntegration("", Mode.UTEST, true, null);
   try {
     integration.injectComponent(cmp.getDescriptorName(), attributes, "", "", out);
   } catch (Exception unexpected) {
     fail("Exceptions during component instantiation should be funneled to the client.");
   }
 }
 @Ignore("W-1505382")
 public void testNonStringAttributeValuesForEvents() throws Exception {
   // Non String attribute for functions
   Map<String, Object> attributes = Maps.newHashMap();
   Appendable out = new StringBuffer();
   Integration integration = service.createIntegration("", Mode.UTEST, true, null);
   attributes.put("label", "Click Me");
   attributes.put("press", new Integer(10));
   try {
     integration.injectComponent(simpleComponentTag, attributes, "", "", out);
     fail("Should have failed to accept a non String value for event handler.");
   } catch (AuraRuntimeException expected) {
     // Expected
   } catch (Exception unexpected) {
     fail(
         "Failed to detect bad value provided for event handlers. Failed :"
             + unexpected.getMessage());
   }
 }
  /**
   * Null check for arguments sent to Integration.createIntegration()
   *
   * @throws Exception
   */
  @Ignore("W-1495981")
  public void testNullsForCreateIntegration() throws Exception {
    Integration integration = service.createIntegration("", Mode.UTEST, true, null);
    Map<String, Object> attributes = Maps.newHashMap();
    attributes.put("label", "Click Me");
    Appendable out = new StringBuffer();
    // No component tag
    assertException(integration, null, attributes, "", "", out);
    // No attributes. TODO: Should be okay?
    assertException(integration, simpleComponentTag, null, "", "", out);
    // No locatorDomId
    assertException(integration, simpleComponentTag, attributes, "", null, out);
    // No stream to write output to
    assertException(integration, simpleComponentTag, attributes, "", "", null);

    // No local ID should be fine
    try {
      integration.injectComponent(simpleComponentTag, attributes, "", "", out);
    } catch (Exception unexpected) {
      fail("Not specifying a localId to injected component should be tolerated.");
    }
  }
  /**
   * Verify injection a component with different attribute types.
   *
   * @throws Exception
   */
  public void testAttributeTypes() throws Exception {
    String attributeMarkup =
        "<aura:attribute name='strAttr' type='String'/>"
            + "<aura:attribute name='booleanAttr' type='Boolean'/>"
            + "<aura:attribute name='strList' type='List'/>"
            + "<aura:attribute name='booleanArray' type='Boolean[]'/>"
            + "<aura:attribute name='cmps' type='Aura.Component[]'/>"
            + "<aura:attribute name='obj' type='Object'/>";
    String attributeWithDefaultsMarkup =
        "<aura:attribute name='strAttrDefault' type='String' default='IS'/>"
            + "<aura:attribute name='booleanAttrDefault' type='Boolean' default='true'/>"
            + "<aura:attribute name='strListDefault' type='List' default='foo,bar'/>"
            + "<aura:attribute name='booleanArrayDefault' type='Boolean[]' default='[true,false,false]'/>"
            + "<aura:attribute name='objDefault' type='Object' default='fooBar'/>"
            + "<aura:attribute name='cmpsDefault' type='Aura.Component[]'>"
            + "<div/><span/>text<p/>"
            + "</aura:attribute>";

    DefDescriptor<ComponentDef> cmp =
        addSourceAutoCleanup(
            ComponentDef.class,
            String.format(baseComponentTag, "", attributeMarkup + attributeWithDefaultsMarkup));
    Map<String, Object> attributes = Maps.newHashMap();
    attributes.put("strAttr", "");
    attributes.put("booleanAttr", false);
    attributes.put("strList", Lists.newArrayList("food", "bared"));
    attributes.put("booleanArray", new Boolean[] {true, false});
    attributes.put("obj", "Object");

    Appendable out = new StringBuffer();
    Integration integration = service.createIntegration("", Mode.UTEST, true, null);
    try {
      integration.injectComponent(cmp.getDescriptorName(), attributes, "", "", out);
    } catch (Exception unexpected) {
      fail(
          "Exception occured when injecting component with attribute values. Exception:"
              + unexpected.getMessage());
    }
  }
 private void assertException(
     Integration obj,
     String tag,
     Map<String, Object> attributes,
     String localId,
     String locatorDomId,
     Appendable out)
     throws Exception {
   try {
     obj.injectComponent(tag, attributes, localId, locatorDomId, out);
     fail("Expected IntegrationService to throw an AuraRuntimeException.");
   } catch (NullPointerException e) {
     fail("IntegrationService threw a NullPointerException, expected AuraRuntimeException.");
   } catch (AuraRuntimeException expected) {
     // Expected
   }
 }