Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: bc4c55b05c
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 200 lines (157 sloc) 6.598 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
<html>
    <head>
        <title>Character.js test</title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

        <script type="text/javascript"
                src="webcanvas/character.js"></script>

        <script type="text/javascript">

            function AssertException(message) { this.message = message; }

            AssertException.prototype.toString = function () {
                return 'AssertException: ' + this.message;
            }

            function assert(exp, msg) {
                if (!exp) {
                    document.write("F");
                    throw new AssertException(msg);
                }
                else
                    document.write(".");
            }

            function assertEquals(leftop, rightop) {
                msg = leftop.toString().quote() + " != " +
                      rightop.toString().quote();

                assert(leftop == rightop, msg);
            }

            String.prototype.quote = function() {
                return "\"" + this.replace('"', '\\"') + "\"";
            }

        </script>

    </head>

    <body>
        <script type="text/javascript">

            function testPointToXML() {
                var point = new Point();
                point.x = 2;
                point.y = 4;

                var expected = "<point x=\"2\" y=\"4\" />";
                assertEquals(point.toXML(), expected);

                point = new Point();
                point.x = 4;
                point.y = 5;
                point.timestamp = 6

                expected = "<point x=\"4\" y=\"5\" timestamp=\"6\" />";
                assertEquals(point.toXML(), expected);
            }

            function testStrokeToXML() {
                var point = new Point();
                point.x = 2;
                point.y = 4;
                point.timestamp = 0;

                var point2 = new Point();
                point2.x = 4;
                point2.y = 5;
                point2.timestamp = 10;

                var stroke = new Stroke();
                stroke.appendPoint(point);
                stroke.appendPoint(point2);

                var expected = "<stroke>\n";
                expected += " <point x=\"2\" y=\"4\" timestamp=\"0\" />\n";
                expected += " <point x=\"4\" y=\"5\" timestamp=\"10\" />\n";
                expected += "</stroke>"
                assertEquals(stroke.toXML(), expected);
            }

            function _getWriting() {
                var writing = new Writing();

                var point = new Point();
                point.x = 2;
                point.y = 4;
                point.timestamp = 0;

                var point2 = new Point();
                point2.x = 4;
                point2.y = 5;
                point2.timestamp = 10;

                var point3 = new Point();
                point3.x = 20;
                point3.y = 30;
                point3.timestamp = 15;

                var point4 = new Point();
                point4.x = 22;
                point4.y = 40;
                point4.timestamp = 22;

                var point5 = new Point();
                point5.x = 100;
                point5.y = 1000;
                point5.timestamp = 38;

                writing.moveToPoint(point);
                writing.lineToPoint(point2);
                writing.lineToPoint(point3);

                writing.moveToPoint(point4);
                writing.lineToPoint(point5);

                return writing;
            }

            WRITING = "<width>1000</width>\n";
            WRITING += "<height>1000</height>\n";
            WRITING += "<strokes>\n";
            WRITING += " <stroke>\n";
            WRITING += " <point x=\"2\" y=\"4\" timestamp=\"0\" />\n";
            WRITING += " <point x=\"4\" y=\"5\" timestamp=\"10\" />\n";
            WRITING += " <point x=\"20\" y=\"30\" timestamp=\"15\" />\n";
            WRITING += " </stroke>\n";
            WRITING += " <stroke>\n";
            WRITING += " <point x=\"22\" y=\"40\" timestamp=\"22\" />\n";
            WRITING += " <point x=\"100\" y=\"1000\" timestamp=\"38\" />\n";
            WRITING += " </stroke>\n";
            WRITING += "</strokes>";

            function testWritingToXML() {
                var writing = _getWriting();

                assertEquals(writing.toXML(), WRITING);
            }

            CHAR = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
            CHAR += "<character>\n";
            CHAR += " <utf8>漢</utf8>\n";
            CHAR += " <width>1000</width>\n";
            CHAR += " <height>1000</height>\n";
            CHAR += " <strokes>\n";
            CHAR += " <stroke>\n";
            CHAR += " <point x=\"2\" y=\"4\" timestamp=\"0\" />\n";
            CHAR += " <point x=\"4\" y=\"5\" timestamp=\"10\" />\n";
            CHAR += " <point x=\"20\" y=\"30\" timestamp=\"15\" />\n";
            CHAR += " </stroke>\n";
            CHAR += " <stroke>\n";
            CHAR += " <point x=\"22\" y=\"40\" timestamp=\"22\" />\n";
            CHAR += " <point x=\"100\" y=\"1000\" timestamp=\"38\" />\n";
            CHAR += " </stroke>\n";
            CHAR += " </strokes>\n";
            CHAR += "</character>";

            function testCharacterToXML() {
                var writing = _getWriting();

                var character = new Character();
                character.setUTF8("漢");
                
                character.setWriting(writing);

                assertEquals(character.toXML(), CHAR);
            }

            function testDuration() {
                var writing = _getWriting();
                var stroke1 = writing.getStrokes()[0];
                var stroke2 = writing.getStrokes()[1];

                assertEquals(stroke1.getDuration(), 15);
                assertEquals(stroke2.getDuration(), 16);

                assertEquals(writing.getDuration(), 38);
            }

            try {
                tests = ["testPointToXML", "testStrokeToXML",
                         "testWritingToXML", "testCharacterToXML",
                         "testDuration"];

                for (var i = 0; i < tests.length; i++) {
                    document.write("<br /><br />" + tests[i] + " ");
                    eval(tests[i]).call();
                }

            }
            catch (e) {
                if (e instanceof AssertException) {
                    alert("AssertException\n" + e.message);
                }
            }

        </script>
    </body>
</html>
Something went wrong with that request. Please try again.