1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.watchlist.test.po

File WatchlistMacro.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart10.png
0% of files have more coverage

Code metrics

8
28
9
2
133
70
13
0.46
3.11
4.5
1.44

Classes

Class Line # Actions
WatchlistMacro 37 20 0% 9 2
0.937593.8%
70 8 0% 4 1
0.923076992.3%
 

Contributing tests

This file is covered by 3 tests. .

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
19    */
20    package org.xwiki.watchlist.test.po;
21   
22    import java.util.List;
23   
24    import org.openqa.selenium.By;
25    import org.openqa.selenium.WebDriver;
26    import org.openqa.selenium.WebElement;
27    import org.openqa.selenium.support.ui.ExpectedCondition;
28    import org.xwiki.test.ui.po.BaseElement;
29    import org.xwiki.test.ui.po.LiveTableElement;
30   
31    /**
32    * Represents a WatchList macro visible on the current page
33    *
34    * @version $Id: e6ff30f3d0adc73a0c1911444b6350466467677f $
35    * @since 6.0M1
36    */
 
37    public class WatchlistMacro extends BaseElement
38    {
39   
40    /**
41    * a formatter to create a CSS expression to a "remove from watchlist" link. The arguments to pass to this
42    * formatter are: removal action and reference to removed.
43    */
44    private static final String WATCHLIST_REMOVE_BUTTON_PATTERN_CSS
45    = "tbody#mywatchlist-0-display>tr>td>a[href$='?xpage=watch&do=%s&reference=%s']";
46   
 
47  33 toggle public LiveTableElement getWatchList() {
48  33 LiveTableElement liveTableElement = new LiveTableElement("mywatchlist-0");
49  33 liveTableElement.waitUntilReady();
50   
51  33 return liveTableElement;
52    }
53   
 
54  20 toggle public boolean isWatched(String space, String page) {
55    // Make sure the livetable is loaded
56  20 getWatchList();
57   
58  20 return !getDriver().findElementsWithoutWaiting(removeLink(space, page)).isEmpty();
59    }
60   
 
61  5 toggle public boolean isWatched(String space) {
62  5 return this.isWatched(space, null);
63    }
64   
 
65  3 toggle public boolean isWikiWatched() {
66  3 return this.isWatched(null, null);
67    }
68   
69    /** a helper class to find "remove"-links and wait for them to vanish */
 
70    private class ExpectNoRemoveLinks implements ExpectedCondition<Boolean>
71    {
72    private final String page;
73    private final String space;
74    private List<WebElement> links = null;
75   
 
76  8 toggle ExpectNoRemoveLinks(String space, String page) {
77  8 this.space = space;
78  8 this.page = page;
79    }
80   
 
81  13 toggle @Override
82    public Boolean apply(WebDriver input) {
83  13 getWatchList();
84  13 links = getDriver().findElements(removeLink(space, page));
85  13 return links.isEmpty();
86    }
87   
 
88  4 toggle public List<WebElement> getLinks() {
89  4 if (links == null) {
90  4 apply(getDriver());
91    }
92  4 return links;
93    }
94    }
95   
96    /**
97    * Unregister a document or a space from the watchlist.
98    *
99    * @param space
100    * the space name, if null the method tries to unregister the main wiki
101    * @param page
102    * the page name, if null the methods tries to unregister a space
103    * @return true if something has been unregistered, false otherwise
104    */
 
105  4 toggle public boolean unWatch(String space, String page) {
106  4 List<WebElement> links = new ExpectNoRemoveLinks(space, page).getLinks();
107  4 if (links.isEmpty()) {
108  0 return false;
109    }
110   
111  4 links.get(0).click();
112    // FIXME: now we should wait for the ajax call to finish and the livetable to reload
113    // this would be easier if we get any feedback about removing items from the watchlist
114    // waitForNotificationSuccessMessage(message);
115    // instead we wait for the remove link to vanish
116  4 getDriver().waitUntilCondition(new ExpectNoRemoveLinks(space, page));
117   
118  4 return true;
119    }
120   
 
121  33 toggle private By removeLink(String space, String page) {
122  33 By removeLink;
123  33 if (space == null) {
124  6 removeLink = By.cssSelector(String.format(WATCHLIST_REMOVE_BUTTON_PATTERN_CSS, "removewiki", "xwiki"));
125  27 } else if (page == null) {
126  8 removeLink = By.cssSelector(String.format(WATCHLIST_REMOVE_BUTTON_PATTERN_CSS, "removespace", "xwiki%3A" + space));
127    } else {
128  19 removeLink = By.cssSelector(String.format(WATCHLIST_REMOVE_BUTTON_PATTERN_CSS, "removedocument", "xwiki%3A" + space + '.' + page));
129    }
130  33 return removeLink;
131    }
132   
133    }