<commit_before>/*
 * Copyright (C) 2013 salesforce.com, inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.auraframework.impl;

import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.util.List;
import java.util.Map;

import org.auraframework.Aura;
import org.auraframework.def.ComponentDef;
import org.auraframework.def.ControllerDef;
import org.auraframework.def.DefDescriptor;
import org.auraframework.test.WebDriverTestCase;
import org.auraframework.test.WebDriverTestCase.ExcludeBrowsers;
import org.auraframework.test.WebDriverUtil.BrowserType;
import org.auraframework.util.AuraTextUtil;
import org.auraframework.util.json.Json;
import org.junit.Ignore;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

/**
 * 
 * UI test for usage of Integration Service.
 * 
 */
@ExcludeBrowsers({ BrowserType.IPAD, BrowserType.IPHONE })
public class IntegrationServiceImplUITest extends WebDriverTestCase {
    public IntegrationServiceImplUITest(String name) {
        super(name);
    }

    DefDescriptor<ComponentDef> defaultStubCmp;
    String defaultPlaceholderID = "placeholder";
    String defaultLocalId = "injectedComponent";

    @Override
    public void setUp() throws Exception {
        super.setUp();
        defaultStubCmp = addSourceAutoCleanup(
                ComponentDef.class,
                getIntegrationStubMarkup(
                        "java://org.auraframework.impl.renderer.sampleJavaRenderers.RendererForTestingIntegrationService",
                        true, true, true));
    }

    /**
     * Verify using IntegrationService to inject a simple component with a Java model, Javascript Controller and Java
     * Controller.
     */
    public void testSimpleComponentWithModelAndController() throws Exception {
        DefDescriptor<ComponentDef> cmpToInject = setupSimpleComponentWithModelAndController();
        Map<String, Object> attributes = Maps.newHashMap();
        attributes.put("strAttribute", "Oranges");
        openIntegrationStub(cmpToInject, attributes);

        String selectorForPlaceholder = String.format("div#%s", defaultPlaceholderID);
        assertTrue("Injected component not found inside placeholder",
                isElementPresent(By.cssSelector(selectorForPlaceholder + ">" + "div.wrapper")));
        WebElement attrValue = findDomElement(By.cssSelector("div.dataFromAttribute"));
        assertEquals("Failed to see data from model of injected component", "Oranges", attrValue.getText());
        WebElement modelValue = findDomElement(By.cssSelector("div.dataFromModel"));
        assertEquals("Failed to initilize attribute value of injected component", "firstThingDefault",
                modelValue.getText());

        WebElement button = findDomElement(By.cssSelector("button.uiButton"));
        button.click();
        auraUITestingUtil.waitForElementText(By.cssSelector("div.dataFromController"), "TestController", true);

        // Access injected component through ClientSide API
        assertTrue(auraUITestingUtil.getBooleanEval(String.format(
                "return window.$A.getRoot().find('%s')!== undefined ;", defaultLocalId)));
        assertEquals(cmpToInject.toString(),
                (String) auraUITestingUtil.getEval(String.format(
                        "return window.$A.getRoot().find('%s').getDef().getDescriptor().toString()", defaultLocalId)));
    }

    private DefDescriptor<ComponentDef> setupSimpleComponentWithModelAndController() {
        String systemAttributes = "model='java://org.auraframework.impl.java.model.TestModel' "
                + "controller='java://org.auraframework.impl.java.controller.TestController'";
        String bodyMarkup = "<aura:attribute name='strAttribute' type='String' default='Apple'/> "
                + "<ui:button label='clickMe' press='{!c.handleClick}'/>"
                + "<div class='wrapper'>"
                + "<div class='dataFromAttribute' aura:id='dataFromAttribute'>{!v.strAttribute}</div> "
                + "<div class='dataFromModel' aura:id='dataFromModel'>{!m.firstThing}</div> "
                + "<div class='dataFromController' aura:id='dataFromController'></div>" + "</div>";
        DefDescriptor<ComponentDef> cmpDesc = addSourceAutoCleanup(ComponentDef.class,
                String.format(AuraImplTestCase.baseComponentTag, systemAttributes, bodyMarkup));
        DefDescriptor<ControllerDef> jsControllerdesc = Aura.getDefinitionService()
                .getDefDescriptor(
                        String.format("%s://%s.%s", DefDescriptor.JAVASCRIPT_PREFIX, cmpDesc.getNamespace(),
                                cmpDesc.getName()), ControllerDef.class);
        addSourceAutoCleanup(jsControllerdesc, "{" + "handleClick:function(cmp){"
                + "var a = cmp.get('c.getString');"
                + "a.setCallback(cmp,function(a){ "
                + "$A.componentService.newComponentAsync("
                + "this, function(newCmp){cmp.find('dataFromController').getValue('v.body').push(newCmp);},"
                + "{componentDef: 'markup://aura:text', attributes:{ values:{ value: a.getReturnValue() }}}"
                + ")}); $A.enqueueAction(a);}}");
        return cmpDesc;
    }

    /**
     * Verify use of integration service to inject a component and initialize various types of attributes.
     */
    public void testAttributesInitialization() 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='stringArray' type='String[]'/>"
                + "<aura:attribute name='obj' type='Object'/>";
        String attributeWithDefaultsMarkup = "<aura:attribute name='strAttrDefault' type='String' default='Apple'/>"
                + "<aura:attribute name='booleanAttrDefault' type='Boolean' default='true'/>"
                + "<aura:attribute name='strListDefault' type='List' default='Apple,Orange'/>"
                + "<aura:attribute name='stringArrayDefault' type='String[]' default='Melon,Berry,Grapes'/>"
                + "<aura:attribute name='objDefault' type='Object' default='Banana'/>";
        DefDescriptor<ComponentDef> cmpToInject = addSourceAutoCleanup(ComponentDef.class,
                String.format(AuraImplTestCase.baseComponentTag, "", attributeMarkup + attributeWithDefaultsMarkup));

        Map<String, Object> attributes = Maps.newHashMap();
        attributes.put("strAttr", "Oranges");
        attributes.put("booleanAttr", false);
        List<String> strList = Lists.newArrayList("Pear", "Melon");
        attributes.put("strList", strList);
        List<String> stringList = Lists.newArrayList("Persimon", "Kiwi");
        attributes.put("stringArray", stringList.toArray());
        attributes.put("obj", "Object");

        openIntegrationStub(cmpToInject, attributes);
        // Access injected component through ClientSide API
        assertTrue(auraUITestingUtil.getBooleanEval(String.format(
                "return window.$A.getRoot().find('%s')!== undefined ;", defaultLocalId)));

        String attributeEvalScript = "return window.$A.getRoot().find('%s').get('v.%s');";
        // Provided attributes
        assertEquals("Oranges",
                auraUITestingUtil.getEval(String.format(attributeEvalScript, defaultLocalId, "strAttr")));
        assertFalse(auraUITestingUtil.getBooleanEval(String.format(attributeEvalScript, defaultLocalId, "booleanAttr")));
        assertEquals("Object", auraUITestingUtil.getEval(String.format(attributeEvalScript, defaultLocalId, "obj")));
        assertEquals(strList, auraUITestingUtil.getEval(String.format(attributeEvalScript, defaultLocalId, "strList")));
        assertEquals(stringList,
                auraUITestingUtil.getEval(String.format(attributeEvalScript, defaultLocalId, "stringArray")));

        // Attributes with Default values
        assertEquals("Apple",
                auraUITestingUtil.getEval(String.format(attributeEvalScript, defaultLocalId, "strAttrDefault")));
        assertTrue(auraUITestingUtil.getBooleanEval(String.format(attributeEvalScript, defaultLocalId,
                "booleanAttrDefault")));
        assertEquals("Banana",
                auraUITestingUtil.getEval(String.format(attributeEvalScript, defaultLocalId, "objDefault")));
        assertEquals(Lists.newArrayList("Apple", "Orange"),
                auraUITestingUtil.getEval(String.format(attributeEvalScript, defaultLocalId, "strListDefault")));
        assertEquals(Lists.newArrayList("Melon", "Berry", "Grapes"),
                auraUITestingUtil.getEval(String.format(attributeEvalScript, defaultLocalId, "stringArrayDefault")));
    }

    /**
     * Verify use of integration service to inject a component and initialize events with javascript function handlers. 
     */
    public void testComponentWithRegisteredEvents()throws Exception{
        String bodyMarkup = "<aura:attribute name='attr' type='String' default='Oranges'/> "
                +"<aura:registerevent name='press' type='ui:press'/>" +
                "<aura:registerevent name='change' type='ui:change'/>" +
                "<div class='dataFromAttribute' aura:id='dataFromAttribute'>{!v.attr}</div>" + 
                "<div class='click_t' onclick='{!c.clickHndlr}'>Click Me</div>" +
                "<input class='change_t' onchange='{!c.changeHndlr}' type='text'/>";
        DefDescriptor<ComponentDef> cmpToInject = addSourceAutoCleanup(ComponentDef.class,
                String.format(AuraImplTestCase.baseComponentTag, "", bodyMarkup));
        DefDescriptor<ControllerDef> jsControllerdesc = Aura.getDefinitionService()
                .getDefDescriptor(
                        String.format("%s://%s.%s", DefDescriptor.JAVASCRIPT_PREFIX, cmpToInject.getNamespace(),
                                cmpToInject.getName()), ControllerDef.class);
        addSourceAutoCleanup(jsControllerdesc, 
                "{" + 
                        "clickHndlr:function(cmp, evt){var e = cmp.getEvent('press');e.setParams({'domEvent': event});e.fire();}," +
                        "changeHndlr:function(cmp, evt){var e = cmp.getEvent('change');e.fire();}"+
        		"}");
        
        DefDescriptor<ComponentDef> customStub = addSourceAutoCleanup(
                ComponentDef.class,
                getIntegrationStubMarkup(<commit_msg>Fix IntegrationServiceImplTest. The test was using an undefined variable and caused failure in Firefox. @bug W-1778032@ @rev gtorres@<commit_after>/*
 * Copyright (C) 2013 salesforce.com, inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.auraframework.impl;

import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.util.List;
import java.util.Map;

import org.auraframework.Aura;
import org.auraframework.def.ComponentDef;
import org.auraframework.def.ControllerDef;
import org.auraframework.def.DefDescriptor;
import org.auraframework.test.WebDriverTestCase;
import org.auraframework.test.WebDriverTestCase.ExcludeBrowsers;
import org.auraframework.test.WebDriverUtil.BrowserType;
import org.auraframework.util.AuraTextUtil;
import org.auraframework.util.json.Json;
import org.junit.Ignore;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

/**
 * 
 * UI test for usage of Integration Service.
 * 
 */
@ExcludeBrowsers({ BrowserType.IPAD, BrowserType.IPHONE })
public class IntegrationServiceImplUITest extends WebDriverTestCase {
    public IntegrationServiceImplUITest(String name) {
        super(name);
    }

    DefDescriptor<ComponentDef> defaultStubCmp;
    String defaultPlaceholderID = "placeholder";
    String defaultLocalId = "injectedComponent";

    @Override
    public void setUp() throws Exception {
        super.setUp();
        defaultStubCmp = addSourceAutoCleanup(
                ComponentDef.class,
                getIntegrationStubMarkup(
                        "java://org.auraframework.impl.renderer.sampleJavaRenderers.RendererForTestingIntegrationService",
                        true, true, true));
    }

    /**
     * Verify using IntegrationService to inject a simple component with a Java model, Javascript Controller and Java
     * Controller.
     */
    public void testSimpleComponentWithModelAndController() throws Exception {
        DefDescriptor<ComponentDef> cmpToInject = setupSimpleComponentWithModelAndController();
        Map<String, Object> attributes = Maps.newHashMap();
        attributes.put("strAttribute", "Oranges");
        openIntegrationStub(cmpToInject, attributes);

        String selectorForPlaceholder = String.format("div#%s", defaultPlaceholderID);
        assertTrue("Injected component not found inside placeholder",
                isElementPresent(By.cssSelector(selectorForPlaceholder + ">" + "div.wrapper")));
        WebElement attrValue = findDomElement(By.cssSelector("div.dataFromAttribute"));
        assertEquals("Failed to see data from model of injected component", "Oranges", attrValue.getText());
        WebElement modelValue = findDomElement(By.cssSelector("div.dataFromModel"));
        assertEquals("Failed to initilize attribute value of injected component", "firstThingDefault",
                modelValue.getText());

        WebElement button = findDomElement(By.cssSelector("button.uiButton"));
        button.click();
        auraUITestingUtil.waitForElementText(By.cssSelector("div.dataFromController"), "TestController", true);

        // Access injected component through ClientSide API
        assertTrue(auraUITestingUtil.getBooleanEval(String.format(
                "return window.$A.getRoot().find('%s')!== undefined ;", defaultLocalId)));
        assertEquals(cmpToInject.toString(),
                (String) auraUITestingUtil.getEval(String.format(
                        "return window.$A.getRoot().find('%s').getDef().getDescriptor().toString()", defaultLocalId)));
    }

    private DefDescriptor<ComponentDef> setupSimpleComponentWithModelAndController() {
        String systemAttributes = "model='java://org.auraframework.impl.java.model.TestModel' "
                + "controller='java://org.auraframework.impl.java.controller.TestController'";
        String bodyMarkup = "<aura:attribute name='strAttribute' type='String' default='Apple'/> "
                + "<ui:button label='clickMe' press='{!c.handleClick}'/>"
                + "<div class='wrapper'>"
                + "<div class='dataFromAttribute' aura:id='dataFromAttribute'>{!v.strAttribute}</div> "
                + "<div class='dataFromModel' aura:id='dataFromModel'>{!m.firstThing}</div> "
                + "<div class='dataFromController' aura:id='dataFromController'></div>" + "</div>";
        DefDescriptor<ComponentDef> cmpDesc = addSourceAutoCleanup(ComponentDef.class,
                String.format(AuraImplTestCase.baseComponentTag, systemAttributes, bodyMarkup));
        DefDescriptor<ControllerDef> jsControllerdesc = Aura.getDefinitionService()
                .getDefDescriptor(
                        String.format("%s://%s.%s", DefDescriptor.JAVASCRIPT_PREFIX, cmpDesc.getNamespace(),
                                cmpDesc.getName()), ControllerDef.class);
        addSourceAutoCleanup(jsControllerdesc, "{" + "handleClick:function(cmp){"
                + "var a = cmp.get('c.getString');"
                + "a.setCallback(cmp,function(a){ "
                + "$A.componentService.newComponentAsync("
                + "this, function(newCmp){cmp.find('dataFromController').getValue('v.body').push(newCmp);},"
                + "{componentDef: 'markup://aura:text', attributes:{ values:{ value: a.getReturnValue() }}}"
                + ")}); $A.enqueueAction(a);}}");
        return cmpDesc;
    }

    /**
     * Verify use of integration service to inject a component and initialize various types of attributes.
     */
    public void testAttributesInitialization() 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='stringArray' type='String[]'/>"
                + "<aura:attribute name='obj' type='Object'/>";
        String attributeWithDefaultsMarkup = "<aura:attribute name='strAttrDefault' type='String' default='Apple'/>"
                + "<aura:attribute name='booleanAttrDefault' type='Boolean' default='true'/>"
                + "<aura:attribute name='strListDefault' type='List' default='Apple,Orange'/>"
                + "<aura:attribute name='stringArrayDefault' type='String[]' default='Melon,Berry,Grapes'/>"
                + "<aura:attribute name='objDefault' type='Object' default='Banana'/>";
        DefDescriptor<ComponentDef> cmpToInject = addSourceAutoCleanup(ComponentDef.class,
                String.format(AuraImplTestCase.baseComponentTag, "", attributeMarkup + attributeWithDefaultsMarkup));

        Map<String, Object> attributes = Maps.newHashMap();
        attributes.put("strAttr", "Oranges");
        attributes.put("booleanAttr", false);
        List<String> strList = Lists.newArrayList("Pear", "Melon");
        attributes.put("strList", strList);
        List<String> stringList = Lists.newArrayList("Persimon", "Kiwi");
        attributes.put("stringArray", stringList.toArray());
        attributes.put("obj", "Object");

        openIntegrationStub(cmpToInject, attributes);
        // Access injected component through ClientSide API
        assertTrue(auraUITestingUtil.getBooleanEval(String.format(
                "return window.$A.getRoot().find('%s')!== undefined ;", defaultLocalId)));

        String attributeEvalScript = "return window.$A.getRoot().find('%s').get('v.%s');";
        // Provided attributes
        assertEquals("Oranges",
                auraUITestingUtil.getEval(String.format(attributeEvalScript, defaultLocalId, "strAttr")));
        assertFalse(auraUITestingUtil.getBooleanEval(String.format(attributeEvalScript, defaultLocalId, "booleanAttr")));
        assertEquals("Object", auraUITestingUtil.getEval(String.format(attributeEvalScript, defaultLocalId, "obj")));
        assertEquals(strList, auraUITestingUtil.getEval(String.format(attributeEvalScript, defaultLocalId, "strList")));
        assertEquals(stringList,
                auraUITestingUtil.getEval(String.format(attributeEvalScript, defaultLocalId, "stringArray")));

        // Attributes with Default values
        assertEquals("Apple",
                auraUITestingUtil.getEval(String.format(attributeEvalScript, defaultLocalId, "strAttrDefault")));
        assertTrue(auraUITestingUtil.getBooleanEval(String.format(attributeEvalScript, defaultLocalId,
                "booleanAttrDefault")));
        assertEquals("Banana",
                auraUITestingUtil.getEval(String.format(attributeEvalScript, defaultLocalId, "objDefault")));
        assertEquals(Lists.newArrayList("Apple", "Orange"),
                auraUITestingUtil.getEval(String.format(attributeEvalScript, defaultLocalId, "strListDefault")));
        assertEquals(Lists.newArrayList("Melon", "Berry", "Grapes"),
                auraUITestingUtil.getEval(String.format(attributeEvalScript, defaultLocalId, "stringArrayDefault")));
    }

    /**
     * Verify use of integration service to inject a component and initialize events with javascript function handlers. 
     */
    public void testComponentWithRegisteredEvents()throws Exception{
        String bodyMarkup = "<aura:attribute name='attr' type='String' default='Oranges'/> "
                +"<aura:registerevent name='press' type='ui:press'/>" +
                "<aura:registerevent name='change' type='ui:change'/>" +
                "<div class='dataFromAttribute' aura:id='dataFromAttribute'>{!v.attr}</div>" + 
                "<div class='click_t' onclick='{!c.clickHndlr}'>Click Me</div>" +
                "<input class='change_t' onchange='{!c.changeHndlr}' type='text'/>";
        DefDescriptor<ComponentDef> cmpToInject = addSourceAutoCleanup(ComponentDef.class,
                String.format(AuraImplTestCase.baseComponentTag, "", bodyMarkup));
        DefDescriptor<ControllerDef> jsControllerdesc = Aura.getDefinitionService()
                .getDefDescriptor(
                        String.format("%s://%s.%s", DefDescriptor.JAVASCRIPT_PREFIX, cmpToInject.getNamespace(),
                                cmpToInject.getName()), ControllerDef.class);
        addSourceAutoCleanup(jsControllerdesc, 
                "{" + 
                        "clickHndlr:function(cmp, evt){var e = cmp.getEvent('press');e.setParams({'domEvent': evt});e.fire();}," +
                        "changeHndlr:function(cmp, evt){var e = cmp.getEvent('change');e.fire();}"+
        		"}");
        
        DefDescriptor<ComponentDef> customStub = addSourceAutoCleanup(
                ComponentDef.class,
                getIntegrationStubMarkup(
{'fraction_non_alphanumeric': 0.1252576857578202, 'fraction_numerical': 0.0010307430312808102, 'mean_word_length': 3.3729178914364097, 'pattern_counts': {'":': 0, '<': 85, '<?xml version=': 0, '>': 87, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6964157', 'n_tokens_mistral': 6091, 'n_tokens_neox': 5768, 'n_words': 1104}