/**
  * Null check for arguments sent to Integration() constructor invoked through
  * IntegrationService.createIntegration()
  *
  * @throws Exception
  */
 @Ignore("W-1495981")
 public void testNullsForIntegrationService() throws Exception {
   Integration integration = null;
   assertNotNull("Failed to locate integration service implementation.", service);
   // All Nulls
   integration = service.createIntegration(null, null, true, null);
   assertException(integration);
   // No Context Path
   integration = service.createIntegration(null, Mode.UTEST, true, null);
   assertException(integration);
   // No mode specified
   integration = service.createIntegration("", null, true, null);
   assertException(integration);
 }
  /** 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);
 }
 /**
  * Integration Service throws exception when used with an unsupported client.
  *
  * @throws Exception
  */
 public void testThrowsOnUnsupportedBrowsers() throws Exception {
   String ie6UserAgent =
       "Mozilla/4.0 (compatible; MSIE 6.1; Windows XP; .NET CLR 1.1.4322; .NET CLR 2.0.50727)";
   Integration integration = service.createIntegration("", Mode.DEV, true, ie6UserAgent);
   try {
     injectSimpleComponent(integration);
     fail("Integration service should throw exception when used with unsupported browsers.");
   } catch (UnsupportedUserAgentException e) {
     // expected
   }
 }
 /**
  * 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());
   }
 }
 /**
  * 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());
    }
  }
  /**
   * Sanity check for IntegrationService.
   *
   * @throws Exception
   */
  @UnAdaptableTest
  public void testSanityCheck() throws Exception {
    assertNotNull("Failed to locate implementation of IntegrationService.", service);

    Mode[] testModes = new Mode[] {Mode.UTEST, Mode.PROD};
    for (Mode m : testModes) {
      Integration integration = service.createIntegration("", m, true, null);
      assertNotNull(
          String.format(
              "Failed to create an integration object using IntegrationService in %s mode. Returned null.",
              m),
          integration);
      try {
        injectSimpleComponent(integration);
      } catch (Exception unexpected) {
        fail(
            String.format(
                    "Failed to use IntegrationService to inject a component in %s mode with the following exception:",
                    m)
                + unexpected.getMessage());
      }
    }
  }