How do I test filehandling in GWT

34 views
Skip to first unread message

Richard Wallis

unread,
Jun 5, 2014, 6:10:29 AM6/5/14
to gwt-pl...@googlegroups.com
I want to test an epub parser I've written against 30 example epub files I have in my test resources directory.

In normal operation the epub parser gets the file as an Int8Array provided by the HTML5 file api.  ( A user opens the file using the file input or by drag and drop)

But I don't know how to transform a file in my test resources directory into an Int8Array.  If I wasn't testing gwt code I could just load the file using standard Java, but the epub parser uses gwt XMLParser so it must be a GWTTestCase.

Is there a way to mix standard java into a GWTTestCase or can I access the files in some other way?

Richard Wallis

unread,
Aug 13, 2014, 5:56:10 AM8/13/14
to gwt-pl...@googlegroups.com
You can do this by converting the files to base64 first and then reading them in from json:

You can use the following html page to create the json base64 version of your file, then copy the textarea into a js file and read that into your testcase:

<!doctype html>
<html>
<head>
    <title>Convert files to base64</title>
</head>
<body>
    <input type="file" id="files" multiple />
    <br>
    <textarea id="output" cols="120" rows="40"></textarea>
    
    <script>
        function handleFileSelect(evt) {
            var files = evt.target.files;
            
            for (var i = 0; i < files.length; i++) {
    
                var reader = new FileReader();
                
                reader.onload = (function(theFile) {
                    return function(e) {
                        document.getElementById("output").innerHTML += "//" + theFile.name + "\nvar file" + i + " = \"" + btoa(e.target.result) + "\";\n\n";
                    }
                })(files[i]);
                reader.readAsBinaryString(files[i]);
            }
        }

        document.getElementById('files').addEventListener('change', handleFileSelect, false);
    </script>
</body>
</html>


--
You received this message because you are subscribed to the Google Groups "GWTP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to gwt-platform...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Christian Goudreau

unread,
Aug 13, 2014, 9:50:03 AM8/13/14
to gwt-pl...@googlegroups.com
You should be able to do it with GwtTestCase. Else, this is a simple FileReader JSO:

If you upload a file from a from for exemple, you can get that file client side through another JSO (JsFile in this example), then readAsDataURL will return a Base64 representation of that file.

I'm not sure if that can help :D
--
Christian Goudreau | CEO - Président
M: 1.877.635.1585 | S: christian.goudreau

Richard Wallis

unread,
Aug 13, 2014, 10:09:55 AM8/13/14
to gwt-pl...@googlegroups.com
Thanks because my base64 solution above isn't really working.

I can read the file in javascript my big issue is getting the file in the first place.  In normal operation the user clicks a file dialog and then selects the file and I get it from there.  But I don't know how to provide the file for the testcase.

Christian Goudreau

unread,
Aug 13, 2014, 10:26:29 AM8/13/14
to gwt-pl...@googlegroups.com
So the problem isn't in your application, it's in your test case?

Richard Wallis

unread,
Aug 13, 2014, 10:32:12 AM8/13/14
to gwt-pl...@googlegroups.com
Yes, I just can't open files from GWTTestCase

Christian Goudreau

unread,
Aug 13, 2014, 10:48:10 AM8/13/14
to gwt-pl...@googlegroups.com
You could probably create a Test .gwt.xml that include a ResourceBundle in witch you have Data resource.

That test .gwt.xml would be under your test three and would inherit the gwt module you wish to test. 

Christian Goudreau

unread,
Aug 13, 2014, 10:48:31 AM8/13/14
to gwt-pl...@googlegroups.com

Richard Wallis

unread,
Aug 13, 2014, 10:51:00 AM8/13/14
to gwt-pl...@googlegroups.com
No that won't work, I think I've got the solution, I'll post it in a short while.

Richard Wallis

unread,
Aug 13, 2014, 12:00:42 PM8/13/14
to gwt-pl...@googlegroups.com
Here's how you do it for Int8Array:

Convert the file into a Json Array using something like:

<!doctype html>
<html>
<head>
    <title>Convert files to base64</title>
</head>
<body>
    <input type="file" id="files" multiple />
    <br>
    <textarea id="output" cols="120" rows="40"></textarea>
    
    <script>
        

        function handleFileSelect(evt) {
            var files = evt.target.files;
            
            for (var i = 0; i < files.length; i++) {
    
                var reader = new FileReader();
                
                reader.onload = (function(theFile) {
                    return function(e) {
                        var i8 = new Int8Array(e.target.result);
                        //window.console.log("Length 1: " + i8.length);
                        var a = [];
                        for(var j = 0; j < i8.length; j++){
                            a.push(i8[j]);
                        }
                        document.getElementById("output").innerHTML += "//" + theFile.name + "\n$wnd.HP1 = " + JSON.stringify(a) + ";\n\n";
                    }
                })(files[i]);
                reader.readAsArrayBuffer(files[i]);
            }
        }

        document.getElementById('files').addEventListener('change', handleFileSelect, false);
    </script>
</body>
</html>

*The above pretty much crashes chrome but works ok in firefox.

Then create a text resource for your javascript file and inject it into your test case.  Then you can get the Int8Array in your test case with a method like:

private native Int8Array getInt8Array(final String name)/*-{
        return new Int8Array($wnd[name]);
}-*/;
Reply all
Reply to author
Forward
0 new messages