﻿




// (c) Copyright Microsoft Corporation.
// This source is subject to the Microsoft Permissive License.
// See http://www.microsoft.com/resources/sharedsource/licensingbasics/sharedsourcelicenses.mspx.
// All other rights reserved.

// Script objects that should be loaded before we run
var typeDependencies = ['AjaxControlToolkit.CollapsiblePanelBehavior'];

// Test Harness
var testHarness = null;

// Controls in the test page
var label = null;
var hlabel = null;
var panel = null;
var hpanel = null;
var panelParent = null;
var hpanelParent = null;
var btn = null;
var behavior = null;

var collapseComplete = false;
var expandComplete = false;

// Check if the content is collapsed
function checkCollapsedV() {
    // sometimes this comes back 3 when the content is, in fact, collapsed.
    testHarness.assertTrue(panelParent.offsetHeight <= 5, "Content should be vertically collapsed, it's " + panelParent.offsetHeight + 'px');
    testHarness.assertEqual(label.innerHTML, 'Collapsed', "TextLabel should display 'Collapsed'");
}

// Check if the content is expanded
function checkExpandedV() {
    testHarness.assertNotEqual(panelParent.offsetHeight, 0, "Content should be vertically expanded, it's " + panelParent.offsetHeight + 'px');
    testHarness.assertEqual(label.innerHTML, 'Opened', "TextLabel should display 'Opened'");
}

// Check if the content is collapsed
function checkCollapsedH() {
    // sometimes this comes back 3 when the content is, in fact, collapsed.
    testHarness.assertTrue(hpanelParent.offsetWidth <= 5, "Content should be horizontally collapsed, it's " + hpanelParent.offsetWidth + 'px');
    testHarness.assertEqual(label.innerHTML, 'Collapsed', "TextLabel should display 'Collapsed'");
}

// Check if the content is expanded
function checkExpandedH() {
    testHarness.assertNotEqual(hpanelParent.offsetWidth, 0, "Content should be horizontially expanded, it's " + hpanelParent.offsetWidth + 'px');
    testHarness.assertEqual(label.innerHTML, 'Opened', "TextLabel should display 'Opened'");
}

function checkBothExpanded() {
    checkExpandedV();
    checkExpandedH();
}

function checkBothCollapsed() {
    checkCollapsedV();
    checkCollapsedH();
}

function clickBoth() {
    testHarness.fireEvent(panel, 'onclick');
    testHarness.fireEvent(hpanel, 'onclick');
}

function waitCollapsed() {
    try {
        checkBothCollapsed();
        return true;
    } catch (ex) {
        return false;
    }
}

function waitExpanded() {
    try {
        checkBothExpanded();
        return true;
    } catch (ex) {
        return false;
    }
}

// Register the tests
function registerTests(harness) {
    testHarness = harness;

    var waitLength = 150;


    // Get the controls on the page
    panel = testHarness.getElement('ctl00_ContentPlaceHolder1_Panel');
    hpanel = testHarness.getElement('ctl00_ContentPlaceHolder1_HPanel');
    label = testHarness.getElement('ctl00_ContentPlaceHolder1_TextLabel');
    hlabel = testHarness.getElement('ctl00_ContentPlaceHolder1_HTextLabel');
    panelParent = testHarness.getElement('panelParent');
    btn = testHarness.getElement('ctl00_ContentPlaceHolder1_Button1');
    hpanelParent = testHarness.getElement('hpanelParent');
    behavior = testHarness.getObject('ctl00_ContentPlaceHolder1_cpp');

    var test = testHarness.addTest('Initial State');
    test.addStep(checkBothExpanded);

    test = testHarness.addTest('Collapse');
    test.addStep(checkBothExpanded);
    test.addStep(clickBoth, waitCollapsed, waitLength, 5000);

    test = testHarness.addTest('Expand');
    test.addStep(checkBothExpanded);
    test.addStep(clickBoth, waitCollapsed, waitLength, 5000);
    test.addStep(clickBoth, waitExpanded, waitLength, 5000);

    test = testHarness.addTest('Collapse Again');
    test.addStep(checkBothExpanded);
    test.addStep(clickBoth, waitCollapsed, waitLength, 5000);
    test.addStep(clickBoth, waitExpanded, waitLength, 5000);
    test.addStep(clickBoth, waitCollapsed, waitLength, 5000);

    test = testHarness.addTest('Collapsed preserved on PostBack');
    test.addStep(checkBothExpanded);
    test.addStep(clickBoth, waitCollapsed, waitLength, 5000);
    test.addPostBack(btn);
    test.addStep(checkBothCollapsed);

    test = testHarness.addTest('Expanded preserved on PostBack');
    test.addStep(checkBothExpanded);
    test.addStep(clickBoth, waitCollapsed, waitLength, 5000);
    test.addStep(clickBoth, waitExpanded, waitLength, 5000);
    test.addPostBack(btn);
    test.addStep(checkBothExpanded);

    test = testHarness.addTest('Behavior Collapse');
    test.addStep(checkBothExpanded);
    test.addStep(function() { behavior.set_SuppressPostBack.call(behavior, false); });  // To avoid assignments to event.*
    test.addStep(function() { behavior._doClose.call(behavior); },
                function() { try { checkCollapsedV(); return true; } catch (ex) { return false; } }, waitLength, 5000);

    test = testHarness.addTest('Behavior Expand');
    test.addStep(checkBothExpanded);
    test.addStep(function() { behavior.set_SuppressPostBack.call(behavior, false); });  // To avoid assignments to event.*
    test.addStep(function() { behavior._doClose.call(behavior); },
                function() { try { checkCollapsedV(); return true; } catch (ex) { return false; } }, waitLength, 5000);
    test.addStep(function() { behavior._doOpen.call(behavior); },
                function() { try { checkExpandedV(); return true; } catch (ex) { return false; } }, waitLength, 5000);
}

// (c) Copyright Microsoft Corporation.
// This source is subject to the Microsoft Permissive License.
// See http://www.microsoft.com/resources/sharedsource/licensingbasics/sharedsourcelicenses.mspx.
// All other rights reserved.

// Script objects that should be loaded before we run
var typeDependencies = ['AjaxControlToolkit.AccordionBehavior'];

// Reference to the Test Harness
var testHarness = null;

// Controls in the test page
var accordion = null;
var boundAccordion = null;
var xmlAccordion = null;
var behavior = null;
var btn;

function setSelectedIndex(behavior, index) {
    return function() {
        behavior.set_SelectedIndex(index);
    };
}

function checkVisibility(behavior, expectedSelected) {
    return function() {
        var length = behavior.get_Count();
        for (var i = 0; i < length; i++) {
            var height = behavior.get_Pane(i).content.offsetHeight;
            if (i == expectedSelected) {
                testHarness.assertNotEqual(height, 0, 'Height of pane ' + i + ' should not be 0');
            } else {
                testHarness.assertEqual(height, 0, 'Height of pane ' + i + ' is ' + height + ', not 0');
            }
        }
    }
}

function checkHeaderCSS(behavior, expectedCSS) {
    return function() {
        var selectedIndex = behavior.get_SelectedIndex();
        var cssClass = behavior.get_Pane(selectedIndex).header.className;
        testHarness.assertEqual(cssClass, expectedCSS, 'Css class should be ' + expectedCSS + ' but it is ' + cssClass);
    }
}

function checkOpacity(behavior, index) {
    return function() {
        var pane = behavior.get_Pane(index);
        var opacity = 1;
        if (behavior.get_AutoSize() === AjaxControlToolkit.AutoSize.Fill) {
            opacity = CommonToolkitScripts.getElementOpacity(pane.content._original);
        } else {
            opacity = CommonToolkitScripts.getElementOpacity(pane.content);
        }
        testHarness.assertNotEqual(1, opacity);
    }
}

// Register the AlwaysVisibleControl test cases
function registerTests(harness) {
    testHarness = harness;

    // Get the controls on the page
    accordion = testHarness.getElement("ctl00_ContentPlaceHolder1_MyAccordion");
    behavior = testHarness.getObject("<%= MyAccordion.ClientID %>_AccordionExtender");
    btn = testHarness.getElement('ctl00_ContentPlaceHolder1_Button1');
    boundAccordion = testHarness.getElement("ctl00_ContentPlaceHolder1_dictionaryBound");
    var boundProperties = testHarness.getObject("<%= dictionaryBound.ClientID %>_AccordionExtender");
    xmlAccordion = testHarness.getElement("ctl00_ContentPlaceHolder1_xmlBound");
    var xmlProperties = testHarness.getObject("<%= xmlBound.ClientID %>_AccordionExtender");


    var test = testHarness.addTest("Verify array binding");
    test.addStep(function() { testHarness.assertEqual(4, boundProperties.get_Count(), "Expected 4 panes in array bound Accordion, got " + boundProperties.get_Count()) });
    test.addStep(checkVisibility(boundProperties, 0));
    test.addStep(setSelectedIndex(boundProperties, 1), function() { try { checkVisibility(boundProperties, 1)(); return true; } catch (ex) { return false; } }, 500, 5000);

    var test = testHarness.addTest("Verify XML binding");
    test.addStep(checkVisibility(xmlProperties, 0));
    test.addStep(function() { testHarness.assertEqual(3, xmlProperties.get_Count(), "Expected 3 panes in XML bound Accordion, got " + xmlProperties.get_Count()); });
    test.addStep(setSelectedIndex(xmlProperties, 1), function() { try { checkVisibility(xmlProperties, 1)(); return true; } catch (ex) { return false; } }, 500, 5000);

    var test = testHarness.addTest('Panes Exist after DataBind()');
    test.addStep(function() { testHarness.assertEqual(10, behavior.get_Count(), "Expected 10 panes in unbound Accordion, got " + behavior.get_Count()); });

    var test = testHarness.addTest('Selected Index');
    test.addStep(checkVisibility(behavior, 0));
    test.addStep(checkHeaderCSS(behavior, 'accordionHeaderSelected'));

    var test = testHarness.addTest('Change Panes');
    test.addStep(checkVisibility(behavior, 0));
    test.addStep(setSelectedIndex(behavior, 1), function() { try { checkVisibility(behavior, 1)(); return true; } catch (ex) { return false; } }, 500, 5000);
    test.addStep(checkHeaderCSS(behavior, 'accordionHeaderSelected'));

    var test = testHarness.addTest('Fade Transitions');
    test.addStep(function() { behavior.set_FadeTransitions(true); });
    test.addStep(checkVisibility(behavior, 0));
    test.addStep(setSelectedIndex(behavior, 1), function() { try { checkVisibility(behavior, 1)(); return true; } catch (ex) { return false; } }, 500, 5000);
    test.addStep(checkOpacity(behavior, 0));

    var test = testHarness.addTest('AutoSize: None');
    test.addStep(function() { behavior.get_element().style.height = '100px'; behavior.set_AutoSize(AjaxControlToolkit.AutoSize.None); });
    test.addStep(checkVisibility(behavior, 0));
    test.addStep(setSelectedIndex(behavior, 1), function() { try { checkVisibility(behavior, 1)(); return true; } catch (ex) { return false; } }, 500, 5000);
    test.addStep(function() { testHarness.assertTrue(behavior.get_element().offsetHeight > 100, 'Accordion should have a height greater than 100, it has ' + behavior.get_element().offsetHeight); });

    var test = testHarness.addTest('AutoSize: Limit');
    test.addStep(function() { behavior.get_element().style.height = '299px'; behavior.set_AutoSize(AjaxControlToolkit.AutoSize.Limit); });
    test.addStep(checkVisibility(behavior, 0));
    test.addStep(setSelectedIndex(behavior, 1), function() { try { checkVisibility(behavior, 1)(); return true; } catch (ex) { return false; } }, 500, 5000);
    test.addStep(function() { testHarness.assertTrue(behavior.get_element().offsetHeight <= 300, 'Accordion should have a height less than or equal to 300, not ' + behavior.get_element().offsetHeight); });

    var test = testHarness.addTest('AutoSize: Fill');
    test.addStep(function() { behavior.get_element().style.height = '499px'; behavior.set_AutoSize(AjaxControlToolkit.AutoSize.Fill); });
    test.addStep(checkVisibility(behavior, 0));
    test.addStep(setSelectedIndex(behavior, 1), function() { try { checkVisibility(behavior, 1)(); return true; } catch (ex) { return false; } }, 500, 5000);
    test.addStep(function() { testHarness.assertTrue(behavior.get_element().offsetHeight <= 500, 'Accordion should have a height equal to 500, not ' + behavior.get_element().offsetHeight); });


    var test = testHarness.addTest('Index preserved on Postback');
    test.addStep(checkVisibility(behavior, 0));
    test.addStep(setSelectedIndex(behavior, 1), function() { try { checkVisibility(behavior, 1)(); return true; } catch (ex) { return false; } }, 500, 5000);
    test.addPostBack(btn);
    test.addStep(checkVisibility(behavior, 1));
}          
