Jean-Michel Garcia
Jean-Michel Garcia

Reputation: 2389

Does GWT support inclusion of stylesheet during GWTTestCase?

I have some simple GWT client code that I would like to test using GWTTestCase and GWTTestSuite.

However, those tests require the use of some CSS stylesheet (only needed for the tests).

My first attempt was :

  1. Define a ClientBundle that provide the CSS file used for tests.
  2. in my gwtSetUp(), call the `ensureInjected() method to add the CSS file before my tests get called
  3. Test if the CSS style was applied to a particular element in the DOM, for example

code:

assertEquals("blue", element.getStyle().getBorderColor());

I got no errors, but this not seems to work.

Looking throught the logs of the Console (during the Junit test) I found this :

Initializing ResourceGenerator
Finding operable CssResource subtypes
Computing CSS class replacements
Preparing method css
Finding resources
Parsing CSS stylesheet file:/D:/Workspace/libraries/gwt-text/gwt-text/src/test/java/com/t/i/client/CSSDecorationTest.css
Scanning CSS for requirements
Creating fields
Creating assignment for css()
Creating image sprite classes
Replacing property-based @if blocks
Replacing CSS class names
Performing substitution in node border-color : ..... ;

My css file simple contains :

.d {
    border-color: blue;
}

EDIT

Please see this link : https://stackoverflow.com/a/10575244/921244

This was the wrong approach as getStyle do not provide the CSS computed style. Too bad.

Upvotes: 0

Views: 224

Answers (1)

Thomas Broyer
Thomas Broyer

Reputation: 64541

If you don't need to do anything special with your CSS (i.e. it's a simple CSS, not a CssResource), then you could simply create a GWT module (gwt.xml file) dedicated for your tests (make it <inherits> your module-under-test and have your GWTTestCase's getModuleName return the name of the test-specific module instead of the module-under-test) in which you'd add a <stylesheet src="…" /> to include your stylesheet (simplest way if the stylesheet is only to be used for tests, put it in a public subfolder next to the test-specific gwt.xml file).
See https://developers.google.com/web-toolkit/doc/latest/DevGuideOrganizingProjects#DevGuideAutomaticResourceInclusion

Alternatively, using CssResource as you did, you might want to call StyleInjector.flush() just after your call to ensureInjected().

Upvotes: 2

Related Questions