/*!
 * Copyright 2014 Apereo Foundation (AF) Licensed under the
 * Educational Community 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://opensource.org/licenses/ECL-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.
 */

casper.test.begin('Widget - Content Shared', function(test) {

    /**
     * Open the content shared modal with assertions
     */
    var verifyOpenContentShared = function() {
        casper.waitForSelector('#content-clip-container .oae-clip-content > button', function() {
            casper.click('#content-clip-container .oae-clip-content > button');
            test.assertExists('.oae-trigger-contentshared', 'Content shared trigger exists');
            casper.click('.oae-trigger-contentshared');
            casper.waitUntilVisible('#contentshared-modal', function() {
                test.assertVisible('#contentshared-modal', 'Content shared pane is showing after trigger');
                casper.click('#content-clip-container .oae-clip-content > button');
            });
        });
    };

    /**
     * Verify that all elements are present in the content shared modal
     *
     * @param {User}    user1    The first test user profile object
     * @param {User}    user2    The second test user profile object
     */
    var verifyContentSharedElements = function(user1, user2) {
        test.assertExists('#contentshared-modal .modal-header h3', 'Verify that the modal has a header');
        test.assertSelectorHasText('#contentshared-modal .modal-header h3', 'Shared with', 'Verify that the modal header reads \'Shared with\'');
        test.assertExists('#contentshared-modal .modal-body ul.oae-list li', 'Verify that the modal shows who the content is shared with');
        test.assertExists('#contentshared-modal .modal-body ul.oae-list li .oae-thumbnail', 'Verify that the modal shows the pictures of the members');
        casper.waitForText(user1.displayName, function() {
            test.assertSelectorHasText('#contentshared-modal .modal-body ul.oae-list li[data-id="' + user1.id + '"] .oae-listitem-metadata h3', user1.displayName, 'Verify that the manager is shown in the members list');
            casper.waitForText(user2.displayName, function() {
                test.assertSelectorHasText('#contentshared-modal .modal-body ul.oae-list li[data-id="' + user2.id + '"] .oae-listitem-metadata h3', user2.displayName, 'Verify that the added member is shown in the members list');
                test.assertExists('#contentshared-modal .modal-body ul.oae-list li[data-id="' + user2.id + '"] .oae-listitem-metadata small', 'Verify that the modal shows the tenant of the members');
                test.assertSelectorHasText('#contentshared-modal .modal-body ul.oae-list li[data-id="' + user2.id + '"] .oae-listitem-metadata small', 'CasperJS Tenant', 'Verify that the metadata shows the correct tenant name');
                test.assertExists('#contentshared-modal .modal-body ul.oae-list li[data-id="' + user1.id + '"] a[href="' + user1.profilePath + '"][target="_blank"]', 'Verify that the members have a link to their profile that opens in a new page');
                test.assertExists('#contentshared-modal .modal-body ul.oae-list li[data-id="' + user2.id + '"] a[href="/me"][target="_blank"]', 'Verify that the the link to the current user\'s profile points to /me');
                casper.waitForSelector('#contentshared-modal .modal-body ul.oae-list li:nth-child(9)', function() {
                    test.assertEvalEquals(function() {
                        return $('#contentshared-modal .modal-body ul.oae-list li').length;
                    }, 9, 'Verify that there are exactly 9 users this content is shared with');
                });
            });
        });
    };

    casper.start(configUtil().tenantUI, function() {
        // Create a couple of users to test with
        userUtil().createUsers(9, function(_users) {
            users = _users;
        });

        // Login with the first user
        casper.then(function() {
            userUtil().doLogIn(users[0].username, configUtil().defaultUserPassword);
        });

        var contentURL = null;
        casper.then(function() {
            casper.echo('Verify open content shared modal', 'INFO');
            contentUtil().createFile(null, null, [users[1].id, users[2].id, users[3].id, users[4].id, users[5].id, users[6].id, users[7].id, users[8].id], function(_contentURL) {
                contentURL = configUtil().tenantUI + _contentURL;

                casper.then(function() {
                    userUtil().doLogOut();
                });

                casper.then(function() {
                    userUtil().doLogIn(users[1].username, configUtil().defaultUserPassword);
                });

                casper.thenOpen(contentURL, function() {
                    casper.waitForSelector('#content-clip-container .oae-clip-content > button', function() {
                        verifyOpenContentShared();
                    });
                });
            });
        });

        casper.then(function() {
            casper.echo('Verify content shared elements', 'INFO');
            verifyContentSharedElements(users[0], users[1]);
        });

        casper.then(function() {
            userUtil().doLogOut();
        });

    });

    casper.run(function() {
        test.done();
    });
});

@@ -2282,30 +2282,94 @@
 tFor
-Text(user1.displayName
+Selector('#contentshared-modal .modal-body ul.oae-list li%5Bdata-id=%22' + user1.id + '%22%5D'
 , fu
@@ -2629,30 +2629,94 @@
 tFor
-Text(user2.displayName
+Selector('#contentshared-modal .modal-body ul.oae-list li%5Bdata-id=%22' + user2.id + '%22%5D'
 , fu

Improve contentshared list selectors