Created
March 5, 2014 09:20
-
-
Save Saulis/9363939 to your computer and use it in GitHub Desktop.
VAudio unit testing using GwtMockito
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Copyright 2000-2013 Vaadin Ltd. | |
* | |
* Licensed under the Apache 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://www.apache.org/licenses/LICENSE-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. | |
*/ | |
package com.vaadin.client.ui; | |
import static org.mockito.Mockito.mock; | |
import static org.mockito.Mockito.verify; | |
import static org.mockito.Mockito.when; | |
import org.junit.Before; | |
import org.junit.Test; | |
import org.junit.runner.RunWith; | |
import com.google.gwt.dom.client.AudioElement; | |
import com.google.gwt.dom.client.Document; | |
import com.google.gwt.dom.client.Element; | |
import com.google.gwt.dom.client.NodeList; | |
import com.google.gwt.dom.client.SourceElement; | |
import com.google.gwt.dom.client.Text; | |
import com.google.gwtmockito.GwtMock; | |
import com.google.gwtmockito.GwtMockitoTestRunner; | |
/** | |
* | |
* @since | |
* @author Vaadin Ltd | |
*/ | |
// com.google.gwt.user.client.Element is deprecated. | |
@SuppressWarnings("deprecation") | |
@RunWith(GwtMockitoTestRunner.class) | |
public class VAudioTest { | |
@GwtMock | |
AudioElement audio; | |
Document document; | |
VAudio sut; | |
@Before | |
public void setup() { | |
document = Document.get(); | |
when(document.createAudioElement()).thenReturn(audio); | |
// This part of Arrange is the same for all tests - constructor call, so | |
// we'll do it in setup. | |
sut = new VAudio(); | |
} | |
// @Test TODO: Get this working. | |
public void styleNameIsSet() { | |
verify(audio).setClassName("v-audio"); | |
} | |
@Test | |
public void audioIsPlayed() { | |
sut.play(); | |
verify(audio).play(); | |
} | |
@Test | |
public void audioIsPaused() { | |
sut.pause(); | |
verify(audio).pause(); | |
} | |
@Test | |
public void audioIsLoaded() { | |
sut.load(); | |
verify(audio).load(); | |
} | |
@Test | |
public void altTextIsAdded() { | |
Text text = mockText("foobar"); | |
sut.setAltText("foobar"); | |
verify(audio).appendChild(text); | |
} | |
private Text mockText(String data) { | |
Text text = mock(Text.class); | |
when(document.createTextNode(data)).thenReturn(text); | |
return text; | |
} | |
@Test | |
public void altTextIsModified() { | |
Text text = mockText("foo"); | |
sut.setAltText("foo"); | |
sut.setAltText("bar"); | |
verify(text).setNodeValue("bar"); | |
} | |
@Test | |
public void controlsAreShown() { | |
sut.setControls(true); | |
verify(audio).setControls(true); | |
} | |
@Test | |
public void controlsAreHidden() { | |
sut.setControls(false); | |
verify(audio).setControls(false); | |
} | |
@Test | |
public void autoPlayIsTurnedOn() { | |
sut.setAutoplay(true); | |
verify(audio).setAutoplay(true); | |
} | |
@Test | |
public void autoPlayIsTurnedOff() { | |
sut.setAutoplay(false); | |
verify(audio).setAutoplay(false); | |
} | |
@Test | |
public void muteIsTurnedOn() { | |
sut.setMuted(true); | |
verify(audio).setMuted(true); | |
} | |
@Test | |
public void muteIsTurnedOff() { | |
sut.setMuted(false); | |
verify(audio).setMuted(false); | |
} | |
@Test | |
public void allSourcesAreRemoved() { | |
Element sourceElement = mockSourceElement(); | |
NodeList<Element> elements = mock(NodeList.class); | |
when(audio.getElementsByTagName(SourceElement.TAG)) | |
.thenReturn(elements); | |
when(elements.getLength()).thenReturn(1); | |
when(elements.getItem(0)).thenReturn(sourceElement); | |
sut.removeAllSources(); | |
verify(audio).removeChild(sourceElement); | |
} | |
@Test | |
public void sourceHasCorrectAttributes() { | |
com.google.gwt.user.client.Element element = mockElement(); | |
sut.addSource("http://foo.bar", "foobar"); | |
verify(element).setAttribute("src", "http://foo.bar"); | |
verify(element).setAttribute("type", "foobar"); | |
} | |
@Test | |
public void sourceIsAdded() { | |
com.google.gwt.user.client.Element element = mockElement(); | |
sut.addSource("http://foo.bar", "foobar"); | |
verify(audio).appendChild(element); | |
} | |
private com.google.gwt.user.client.Element mockElement() { | |
com.google.gwt.user.client.Element element = mock(com.google.gwt.user.client.Element.class); | |
when(mockSourceElement().cast()).thenReturn(element); | |
return element; | |
} | |
private Element mockSourceElement() { | |
Element sourceElement = mock(Element.class); | |
when(document.createElement(SourceElement.TAG)).thenReturn( | |
sourceElement); | |
return sourceElement; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Looks like this could work fairly well, but it's hard to say without hands-on experience. I'd suggest trying this out for real and adjusting if needed (extracting helpers, etc).