/*
 * See the NOTICE file distributed with this work for additional
 * information regarding copyright ownership.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package org.xwiki.platform.notifications.test.ui;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;

import org.junit.Test;
import org.xwiki.platform.notifications.test.po.NotificationsTrayPage;
import org.xwiki.platform.notifications.test.po.NotificationsUserProfilePage;
import org.xwiki.test.ui.AbstractTest;

/**
 * Perform tests on the notifications module.
 * 
 * @version $Id$
 * @since 9.4RC1
 */
public class NotificationsTest extends AbstractTest
{
    private static final String FIRST_USER_NAME = "user1";
    private static final String SECOND_USER_NAME = "user2";

    private static final String FIRST_USER_PASSWORD = "notificationsUser1";
    private static final String SECOND_USER_PASSWORD = "notificationsUser2";

    // Number of pages that have to be created in order for the notifications badge to show «X+»
    private static final int PAGES_TOP_CREATION_COUNT = 21;

    private void setUpUsers() throws Exception
    {
        // Remove any previous registered users
        getUtil().rest().deletePage("XWiki", FIRST_USER_NAME);
        getUtil().rest().deletePage("XWiki", FIRST_USER_NAME);

        // Create the two users we will be using
        getUtil().createUser(FIRST_USER_NAME, FIRST_USER_PASSWORD, "", "");
        getUtil().createUser(SECOND_USER_NAME, SECOND_USER_PASSWORD, "", "");
        
        NotificationsUserProfilePage p;

        getUtil().login(FIRST_USER_NAME, FIRST_USER_PASSWORD);
        p = NotificationsUserProfilePage.gotoPage(FIRST_USER_NAME);
        p.disableAllParameters();

        getUtil().login(SECOND_USER_NAME, SECOND_USER_PASSWORD);
        p = NotificationsUserProfilePage.gotoPage(SECOND_USER_NAME);
        p.disableAllParameters();
    }

    @Test
    public void testNotifications() throws Exception
    {
        NotificationsUserProfilePage p;
        NotificationsTrayPage tray;

        // Create the users
        this.setUpUsers();

        // The user 1 creates a new page, the user 2 shouldn’t recieve any notification
        getUtil().login(FIRST_USER_NAME, FIRST_USER_PASSWORD);
        getUtil().createPage("Main", "WebHome", "Content from " + FIRST_USER_NAME, "Page title");
        getUtil().gotoPage("Main", "WebHome");

        getUtil().login(SECOND_USER_NAME, SECOND_USER_PASSWORD);
        getUtil().gotoPage("Main", "WebHome");

        tray = new NotificationsTrayPage();
        assertFalse(tray.areNotificationsAvailable());

        // The user 2 will now enable his notifications for new pages
        getUtil().login(SECOND_USER_NAME, SECOND_USER_PASSWORD);
        p = NotificationsUserProfilePage.gotoPage(SECOND_USER_NAME);
        p.setPageCreated(true);

        // We create a lot of pages in order to test the notification badge
        getUtil().login(FIRST_USER_NAME, FIRST_USER_PASSWORD);
        for (int i = 1; i < PAGES_TOP_CREATION_COUNT; i++) {
            getUtil().createPage("Main", "Page" + i, "Simple content", "Simple title");
        }
        getUtil().createPage("Main", "DTP", "Deletion test page", "Deletion test content");

        // Check that the badge is showing «20+»
        getUtil().login(SECOND_USER_NAME, SECOND_USER_PASSWORD);
        getUtil().gotoPage("Main", "WebHome");
        tray = new NotificationsTrayPage();
        assertEquals(Integer.MAX_VALUE, tray.getNotificationsCount());

        // Reset the notifications count of the user 2
        tray.clearAllNotifications();
        assertEquals(0, tray.getNotificationsCount());
        assertFalse(tray.areNotificationsAvailable());

        // The user 2 will get notifications only for pages deletions
        p = NotificationsUserProfilePage.gotoPage(SECOND_USER_NAME);
        p.setPageCreated(false);
        p.setPageDeleted(true);

        // Delete the "Deletion test page" and test the notification
        getUtil().login(FIRST_USER_NAME, FIRST_USER_PASSWORD);
        getUtil().deletePage("Main", "DTP");
        
        getUtil().login(SECOND_USER_NAME, SECOND_USER_PASSWORD);
        getUtil().gotoPage("Main", "WebHome");
        tray = new NotificationsTrayPage();
        assertEquals(1, tray.getNotificationsCount());
        tray.clearAllNotifications();
    }
}

@@ -1028,16 +1028,41 @@
 False;%0A%0A
+import org.junit.Before;%0A
 import o
@@ -1847,23 +1847,34 @@
 1;%0A%0A    
-private
+@Before%0A    public
  void se
@@ -1911,183 +1911,8 @@
   %7B%0A
-        // Remove any previous registered users%0A        getUtil().rest().deletePage(%22XWiki%22, FIRST_USER_NAME);%0A        getUtil().rest().deletePage(%22XWiki%22, FIRST_USER_NAME);%0A%0A
     
@@ -2651,64 +2651,8 @@
 y;%0A%0A
-        // Create the users%0A        this.setUpUsers();%0A%0A
     
@@ -2823,30 +2823,42 @@
 .createPage(
-%22Main%22
+getTestClassName()
 , %22WebHome%22,
@@ -2927,38 +2927,50 @@
 Util().gotoPage(
-%22Main%22
+getTestClassName()
 , %22WebHome%22);%0A%0A 
@@ -3052,38 +3052,50 @@
 Util().gotoPage(
-%22Main%22
+getTestClassName()
 , %22WebHome%22);%0A%0A 
@@ -3663,22 +3663,34 @@
 atePage(
-%22Main%22
+getTestClassName()
 , %22Page%22
@@ -3765,30 +3765,42 @@
 .createPage(
-%22Main%22
+getTestClassName()
 , %22DTP%22, %22De
@@ -3978,38 +3978,50 @@
 Util().gotoPage(
-%22Main%22
+getTestClassName()
 , %22WebHome%22);%0A  
@@ -4704,22 +4704,34 @@
 etePage(
-%22Main%22
+getTestClassName()
 , %22DTP%22)
@@ -4837,14 +4837,26 @@
 age(
-%22Main%22
+getTestClassName()
 , %22W

Create functional tests for the Notifications UI * Fix code compliance