Coverage Report - org.eclipse.swtbot.swt.finder.widgets.SWTBotBrowser
 
Classes in this File Line Coverage Branch Coverage Complexity
SWTBotBrowser
0%
0/33
N/A
1.094
SWTBotBrowser$1
0%
0/9
0%
0/2
1.094
SWTBotBrowser$2
0%
0/3
N/A
1.094
SWTBotBrowser$3
0%
0/4
N/A
1.094
SWTBotBrowser$4
0%
0/3
N/A
1.094
SWTBotBrowser$5
0%
0/3
N/A
1.094
SWTBotBrowser$6
0%
0/9
0%
0/2
1.094
SWTBotBrowser$7
0%
0/9
0%
0/2
1.094
SWTBotBrowser$8
0%
0/4
N/A
1.094
SWTBotBrowser$InternalProgressListener
0%
0/11
N/A
1.094
SWTBotBrowser$WaitForBrowserLoadsPage
0%
0/6
N/A
1.094
 
 1  0
 /*******************************************************************************
 2  
  * Copyright (c) 2010 Red Hat, Inc. and others.
 3  
  * All rights reserved. This program and the accompanying materials
 4  
  * are made available under the terms of the Eclipse Public License v1.0
 5  
  * which accompanies this distribution, and is available at
 6  
  * http://www.eclipse.org/legal/epl-v10.html
 7  
  *
 8  
  * Contributors:
 9  
  *     Libor Zoubek, Red Hat - initial API and implementation
 10  
  *     Ketan Padegaonkar - cleanup to conform to SWTBot standards
 11  
  *******************************************************************************/
 12  
 package org.eclipse.swtbot.swt.finder.widgets;
 13  
 
 14  
 import org.eclipse.swt.browser.Browser;
 15  
 import org.eclipse.swt.browser.ProgressEvent;
 16  
 import org.eclipse.swt.browser.ProgressListener;
 17  
 import org.eclipse.swtbot.swt.finder.ReferenceBy;
 18  
 import org.eclipse.swtbot.swt.finder.SWTBot;
 19  
 import org.eclipse.swtbot.swt.finder.SWTBotWidget;
 20  
 import org.eclipse.swtbot.swt.finder.exceptions.WidgetNotFoundException;
 21  
 import org.eclipse.swtbot.swt.finder.finders.UIThreadRunnable;
 22  
 import org.eclipse.swtbot.swt.finder.results.Result;
 23  
 import org.eclipse.swtbot.swt.finder.results.VoidResult;
 24  
 import org.eclipse.swtbot.swt.finder.utils.Credentials;
 25  
 import org.eclipse.swtbot.swt.finder.utils.internal.Assert;
 26  
 import org.eclipse.swtbot.swt.finder.waits.DefaultCondition;
 27  
 import org.hamcrest.SelfDescribing;
 28  
 
 29  
 /**
 30  
  * This represents a {@link Browser} widget.
 31  
  * 
 32  
  * @author Libor Zoubek <lzoubek [at] redhat [dot] com>
 33  
  */
 34  
 @SWTBotWidget(clasz = Browser.class, preferredName = "browser", referenceBy = { ReferenceBy.LABEL })
 35  0
 public class SWTBotBrowser extends AbstractSWTBotControl<Browser> {
 36  
 
 37  0
         private final InternalProgressListener                                                progressListener;
 38  0
         private final static BrowserAuthenticationListenerDelegate        authListener        = new BrowserAuthenticationListenerDelegate();
 39  
 
 40  
         /**
 41  
          * Constructs an instance of this object with the given browser
 42  
          * 
 43  
          * @param browser the widget.
 44  
          * @throws WidgetNotFoundException if the widget is <code>null</code> or widget has been disposed.
 45  
          * @since 2.0
 46  
          */
 47  
         public SWTBotBrowser(Browser browser) {
 48  0
                 this(browser, null);
 49  0
         }
 50  
 
 51  
         /**
 52  
          * Constructs an instance of this object with the given browser
 53  
          * 
 54  
          * @param browser the widget.
 55  
          * @param description the description of the widget, this will be reported by {@link #toString()}
 56  
          * @throws WidgetNotFoundException if the widget is <code>null</code> or widget has been disposed.
 57  
          * @since 2.0
 58  
          */
 59  
         public SWTBotBrowser(Browser browser, SelfDescribing description) {
 60  0
                 super(browser, description);
 61  0
                 progressListener = new InternalProgressListener(this);
 62  0
                 authListener.init(widget);
 63  0
         }
 64  
 
 65  
         /**
 66  
          * Loads given URI into browser, the page is loaded asynchronously (see {@link #isPageLoaded()})
 67  
          * 
 68  
          * @param url
 69  
          */
 70  
         public void setUrl(final String url) {
 71  0
                 UIThreadRunnable.syncExec(new VoidResult() {
 72  
                         public void run() {
 73  0
                                 progressListener.setDone(false);
 74  0
                                 widget.addProgressListener(progressListener);
 75  0
                                 boolean result = widget.setUrl(url);
 76  0
                                 if (!result) {
 77  0
                                         progressListener.setDone(true);
 78  0
                                         widget.removeProgressListener(progressListener);
 79  
                                 }
 80  0
                         }
 81  
                 });
 82  0
         }
 83  
 
 84  
         /**
 85  
          * @return the current URL or an empty String if there is no current URL
 86  
          */
 87  
         public String getUrl() {
 88  0
                 waitForPageLoaded();
 89  0
                 return UIThreadRunnable.syncExec(new Result<String>() {
 90  
                         public String run() {
 91  0
                                 return widget.getUrl();
 92  
                         }
 93  
                 });
 94  
         }
 95  
 
 96  
         @Override
 97  
         public String getText() {
 98  0
                 waitForPageLoaded();
 99  0
                 return super.getText();
 100  
         }
 101  
 
 102  
         /**
 103  
          * Executes script in browser asynchronously
 104  
          * 
 105  
          * @param script
 106  
          */
 107  
         public void execute(final String script) {
 108  0
                 waitForPageLoaded();
 109  0
                 UIThreadRunnable.asyncExec(new VoidResult() {
 110  
                         public void run() {
 111  0
                                 widget.execute(script);
 112  0
                         }
 113  
                 });
 114  0
         }
 115  
 
 116  
         /**
 117  
          * @return the receiver's back command enabled state
 118  
          */
 119  
         public boolean isBackEnabled() {
 120  0
                 return UIThreadRunnable.syncExec(new Result<Boolean>() {
 121  
                         public Boolean run() {
 122  0
                                 return widget.isBackEnabled();
 123  
                         }
 124  
                 });
 125  
         }
 126  
 
 127  
         /**
 128  
          * @return the receiver's forward command enabled state
 129  
          */
 130  
         public boolean isForwardEnabled() {
 131  0
                 return UIThreadRunnable.syncExec(new Result<Boolean>() {
 132  
                         public Boolean run() {
 133  0
                                 return widget.isForwardEnabled();
 134  
                         }
 135  
                 });
 136  
         }
 137  
 
 138  
         /**
 139  
          * Navigate to the previous session history item.
 140  
          */
 141  
         public void back() {
 142  0
                 UIThreadRunnable.asyncExec(new VoidResult() {
 143  
                         public void run() {
 144  0
                                 progressListener.setDone(false);
 145  0
                                 widget.addProgressListener(progressListener);
 146  0
                                 boolean result = widget.back();
 147  0
                                 if (!result) {
 148  0
                                         progressListener.setDone(true);
 149  0
                                         widget.removeProgressListener(progressListener);
 150  
                                 }
 151  0
                         }
 152  
                 });
 153  
 
 154  0
         }
 155  
 
 156  
         /**
 157  
          * Navigate to the next session history item.
 158  
          */
 159  
         public void forward() {
 160  0
                 UIThreadRunnable.asyncExec(new VoidResult() {
 161  
                         public void run() {
 162  0
                                 progressListener.setDone(false);
 163  0
                                 widget.addProgressListener(progressListener);
 164  0
                                 boolean result = widget.forward();
 165  0
                                 if (!result) {
 166  0
                                         progressListener.setDone(true);
 167  0
                                         widget.removeProgressListener(progressListener);
 168  
                                 }
 169  0
                         }
 170  
                 });
 171  
 
 172  0
         }
 173  
 
 174  
         /**
 175  
          * Refreshes browser
 176  
          */
 177  
         public void refresh() {
 178  0
                 UIThreadRunnable.asyncExec(new VoidResult() {
 179  
                         public void run() {
 180  0
                                 widget.refresh();
 181  0
                         }
 182  
                 });
 183  0
         }
 184  
 
 185  
         /**
 186  
          * @return <code>true</code> by default or when page was completely loaded by browser after asynchronous page load
 187  
          *         invoked by {@link #goURL(String)} was finished, this method returns false only during page loading
 188  
          */
 189  
         public boolean isPageLoaded() {
 190  0
                 return progressListener.isDone();
 191  
         }
 192  
 
 193  
         /**
 194  
          * Waits until browser loads page.
 195  
          * 
 196  
          * @throws TimeoutException if page is not loaded after default timeout
 197  
          */
 198  
         public void waitForPageLoaded() {
 199  
                 new SWTBot().waitUntil(new WaitForBrowserLoadsPage(this));
 200  0
         }
 201  
 
 202  
         /**
 203  
          * Sets credentials, which will be used when page requires authentication, if both username and password set to null
 204  
          * authentication (if requested) will be canceled
 205  
          * <p>
 206  
          * <b>Note:</b> Credentials are shared by all {@link SWTBotBrowser} instances.
 207  
          * </p>
 208  
          * instances
 209  
          * 
 210  
          * @param username the username
 211  
          * @param password the password
 212  
          */
 213  
         public void setCredentials(String username, String password) {
 214  0
                 setCredentials(new Credentials(username, password));
 215  0
         }
 216  
 
 217  
         /**
 218  
          * Sets credentials, which will be used when page requires authentication, if both username and password set to null
 219  
          * authentication (if requested) will be canceled
 220  
          * <p>
 221  
          * <b>Note:</b> Credentials are shared by all {@link SWTBotBrowser} instances.
 222  
          * </p>
 223  
          * instances
 224  
          * 
 225  
          * @param credentials the credentials with the username and password
 226  
          */
 227  
         public void setCredentials(Credentials credentials) {
 228  0
                 authListener.setCredentials(credentials);
 229  0
         }
 230  
 
 231  
         /**
 232  
          * Gets credentials.
 233  
          * 
 234  
          * @return the credentials containing the username and password.
 235  
          */
 236  
         public Credentials getCredentials() {
 237  0
                 return authListener.getCredentials();
 238  
         }
 239  
 
 240  
         /**
 241  
          * This represents internal progress listener notified when browser finishes loading of URL
 242  
          * 
 243  
          * @author Libor Zoubek &lt;lzoubek [at] redhat [dot] com&gt;
 244  
          */
 245  
         class InternalProgressListener implements ProgressListener {
 246  
                 private final SWTBotBrowser        browser;
 247  0
                 private boolean                                done        = true;
 248  
 
 249  0
                 public InternalProgressListener(SWTBotBrowser browser) {
 250  0
                         this.browser = browser;
 251  0
                 }
 252  
 
 253  
                 public synchronized boolean isDone() {
 254  0
                         return done;
 255  
                 }
 256  
 
 257  
                 public synchronized void setDone(boolean done) {
 258  0
                         this.done = done;
 259  0
                 }
 260  
 
 261  
                 public void changed(ProgressEvent event) {
 262  0
                 }
 263  
 
 264  
                 public void completed(ProgressEvent event) {
 265  0
                         setDone(true);
 266  0
                         browser.widget.removeProgressListener(this);
 267  0
                 }
 268  
 
 269  
         }
 270  
 
 271  
         private static final class WaitForBrowserLoadsPage extends DefaultCondition {
 272  
 
 273  
                 private final SWTBotBrowser        browser;
 274  
 
 275  0
                 public WaitForBrowserLoadsPage(SWTBotBrowser browser) {
 276  0
                         Assert.isNotNull(browser, "The browser can not be null"); //$NON-NLS-1$                
 277  0
                         this.browser = browser;
 278  0
                 }
 279  
 
 280  
                 public String getFailureMessage() {
 281  0
                         return "Browser dit not finish loading page before timeout."; //$NON-NLS-1$        
 282  
                 }
 283  
 
 284  
                 public boolean test() throws Exception {
 285  0
                         return browser.isPageLoaded();
 286  
                 }
 287  
 
 288  
         }
 289  
 
 290  
 }