Skip to content

Commit 85e205b

Browse files
committed
Added XHRArrayBufferTest.js and restored XHRTest.js
1 parent 7a21df1 commit 85e205b

File tree

5 files changed

+137
-32
lines changed

5 files changed

+137
-32
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -104,3 +104,5 @@ frameworks/js-bindings/bindings/proj.ios_mac/build/
104104

105105
# Ignore files copied in compilation
106106
samples/*/project/proj.android/src
107+
108+
*.jsc

samples/js-tests/project.json

+1
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@
176176
"src/CocoStudioTest/SceneTest/SceneEditorTest.js",
177177
"src/CocoStudioTest/CocoStudioTest.js",
178178
"src/XHRTest/XHRTest.js",
179+
"src/XHRTest/XHRArrayBufferTest.js",
179180

180181
"src/Box2dTest/Box2dTest.js",
181182
"src/ChipmunkTest/ChipmunkTest.js",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
/****************************************************************************
2+
Copyright (c) 2008-2010 Ricardo Quesada
3+
Copyright (c) 2011-2012 cocos2d-x.org
4+
Copyright (c) 2013-2014 Chukong Technologies Inc.
5+
6+
http://www.cocos2d-x.org
7+
8+
Permission is hereby granted, free of charge, to any person obtaining a copy
9+
of this software and associated documentation files (the "Software"), to deal
10+
in the Software without restriction, including without limitation the rights
11+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
copies of the Software, and to permit persons to whom the Software is
13+
furnished to do so, subject to the following conditions:
14+
15+
The above copyright notice and this permission notice shall be included in
16+
all copies or substantial portions of the Software.
17+
18+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
THE SOFTWARE.
25+
****************************************************************************/
26+
27+
//some utils functions
28+
function ensureLeftAligned (label) {
29+
label.anchorX = 0;
30+
label.anchorY = 1;
31+
label.textAlign = cc.TEXT_ALIGNMENT_LEFT;
32+
}
33+
34+
function streamXHREventsToLabel ( xhr, label, textbox, method ) {
35+
// Simple events
36+
['loadstart', 'abort', 'error', 'load', 'loadend', 'timeout'].forEach(function (eventname) {
37+
xhr["on" + eventname] = function () {
38+
label.string += "\nEvent : " + eventname
39+
}
40+
});
41+
42+
// Special event
43+
xhr.onreadystatechange = function () {
44+
if (xhr.readyState == 4 && (xhr.status >= 200 && xhr.status <= 207)) {
45+
var httpStatus = xhr.statusText;
46+
var response = xhr.responseText.substring(0, 100) + "...";
47+
textbox.string = method + " Response (100 chars):\n"
48+
textbox.string += response
49+
label.string += "\nStatus: Got " + method + " response! " + httpStatus
50+
}
51+
}
52+
}
53+
54+
55+
var XHRArrayBufferTestScene = TestScene.extend({
56+
ctor:function () {
57+
this._super(true);
58+
var xhrLayer = new XHRArrayBufferTestLayer();
59+
this.addChild(xhrLayer);
60+
},
61+
runThisTest:function () {
62+
cc.director.runScene(this);
63+
},
64+
MainMenuCallback:function (sender) {
65+
this._super(sender);
66+
}
67+
});
68+
69+
var XHRArrayBufferTestLayer = cc.Layer.extend({
70+
ctor:function () {
71+
this._super();
72+
},
73+
74+
onEnter: function() {
75+
this._super();
76+
var l = new cc.LabelTTF("Get infos via XHR", "Thonburi", 16);
77+
this.addChild(l, 1);
78+
l.x = winSize.width / 2;
79+
l.y = winSize.height - 60;
80+
81+
this.sendPostArrayBuffer();
82+
},
83+
84+
sendPostArrayBuffer: function() {
85+
var statusPostLabel = new cc.LabelTTF("Status:", "Thonburi", 12);
86+
this.addChild(statusPostLabel, 1);
87+
88+
statusPostLabel.x = 10;
89+
statusPostLabel.y = winSize.height - 100;
90+
ensureLeftAligned(statusPostLabel);
91+
statusPostLabel.setString("Status: Send Post Request to httpbin.org with ArrayBuffer");
92+
93+
94+
var responseLabel = new cc.LabelTTF("", "Thonburi", 16);
95+
this.addChild(responseLabel, 1);
96+
ensureLeftAligned(responseLabel);
97+
responseLabel.x = 10;
98+
responseLabel.y = winSize.height / 2;
99+
100+
var xhr = cc.loader.getXMLHttpRequest();
101+
streamXHREventsToLabel(xhr, statusPostLabel, responseLabel, "POST");
102+
103+
xhr.open("POST", "http://httpbin.org/post");
104+
//set Content-type "text/plain" to post ArrayBuffer or ArrayBufferView
105+
xhr.setRequestHeader("Content-Type","text/plain");
106+
// Uint8Array is an ArrayBufferView
107+
xhr.send(new Uint8Array([1,2,3,4,5]));
108+
},
109+
110+
scrollViewDidScroll:function (view) {
111+
},
112+
scrollViewDidZoom:function (view) {
113+
}
114+
});
115+
116+
var runXHRArrayBufferTest = function () {
117+
var pScene = new cc.Scene();
118+
var pLayer = new XHRArrayBufferTestLayer();
119+
pScene.addChild(pLayer);
120+
cc.director.runScene(pScene);
121+
};

samples/js-tests/src/XHRTest/XHRTest.js

+5-32
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,6 @@ var XHRTestLayer = cc.Layer.extend({
8181
this.sendGetRequest();
8282
this.sendPostPlainText();
8383
this.sendPostForms();
84-
this.sendPostArrayBuffer();
8584
},
8685

8786
sendGetRequest: function() {
@@ -114,7 +113,7 @@ var XHRTestLayer = cc.Layer.extend({
114113
var statusPostLabel = new cc.LabelTTF("Status:", "Thonburi", 12);
115114
this.addChild(statusPostLabel, 1);
116115

117-
statusPostLabel.x = winSize.width / 10 * 2.5;
116+
statusPostLabel.x = winSize.width / 10 * 3;
118117
statusPostLabel.y = winSize.height - 100;
119118
ensureLeftAligned(statusPostLabel);
120119
statusPostLabel.setString("Status: Send Post Request to httpbin.org with plain text");
@@ -123,7 +122,7 @@ var XHRTestLayer = cc.Layer.extend({
123122
var responseLabel = new cc.LabelTTF("", "Thonburi", 16);
124123
this.addChild(responseLabel, 1);
125124
ensureLeftAligned(responseLabel);
126-
responseLabel.x = winSize.width / 10 * 2.5;
125+
responseLabel.x = winSize.width / 10 * 3;
127126
responseLabel.y = winSize.height / 2;
128127

129128
var xhr = cc.loader.getXMLHttpRequest();
@@ -139,7 +138,7 @@ var XHRTestLayer = cc.Layer.extend({
139138
var statusPostLabel = new cc.LabelTTF("Status:", "Thonburi", 12);
140139
this.addChild(statusPostLabel, 1);
141140

142-
statusPostLabel.x = winSize.width / 10 * 5;
141+
statusPostLabel.x = winSize.width / 10 * 7;
143142
statusPostLabel.y = winSize.height - 100;
144143
ensureLeftAligned(statusPostLabel);
145144
statusPostLabel.setString("Status: Send Post Request to httpbin.org width form data");
@@ -148,7 +147,7 @@ var XHRTestLayer = cc.Layer.extend({
148147
this.addChild(responseLabel, 1);
149148

150149
ensureLeftAligned(responseLabel);
151-
responseLabel.x = winSize.width / 10 * 5;
150+
responseLabel.x = winSize.width / 10 * 7;
152151
responseLabel.y = winSize.height / 2;
153152

154153
var xhr = cc.loader.getXMLHttpRequest();
@@ -168,32 +167,6 @@ var XHRTestLayer = cc.Layer.extend({
168167
xhr.send(args);
169168
},
170169

171-
sendPostArrayBuffer: function() {
172-
var statusPostLabel = new cc.LabelTTF("Status:", "Thonburi", 12);
173-
this.addChild(statusPostLabel, 1);
174-
175-
statusPostLabel.x = winSize.width / 10 * 7.5;
176-
statusPostLabel.y = winSize.height - 100;
177-
ensureLeftAligned(statusPostLabel);
178-
statusPostLabel.setString("Status: Send Post Request to httpbin.org with ArrayBuffer");
179-
180-
181-
var responseLabel = new cc.LabelTTF("", "Thonburi", 16);
182-
this.addChild(responseLabel, 1);
183-
ensureLeftAligned(responseLabel);
184-
responseLabel.x = winSize.width / 10 * 7.5;
185-
responseLabel.y = winSize.height / 2;
186-
187-
var xhr = cc.loader.getXMLHttpRequest();
188-
streamXHREventsToLabel(xhr, statusPostLabel, responseLabel, "POST");
189-
190-
xhr.open("POST", "http://httpbin.org/post");
191-
//set Content-type "text/plain" to post ArrayBuffer or ArrayBufferView
192-
xhr.setRequestHeader("Content-Type","text/plain");
193-
// Uint8Array is an ArrayBufferView
194-
xhr.send(new Uint8Array([1,2,3,4,5]));
195-
},
196-
197170
scrollViewDidScroll:function (view) {
198171
},
199172
scrollViewDidZoom:function (view) {
@@ -205,4 +178,4 @@ var runXHRTest = function () {
205178
var pLayer = new XHRTestLayer();
206179
pScene.addChild(pLayer);
207180
cc.director.runScene(pScene);
208-
};
181+
};

samples/js-tests/src/tests-main.js

+8
Original file line numberDiff line numberDiff line change
@@ -673,6 +673,14 @@ var testNames = [
673673
testScene:function () {
674674
return new XHRTestScene();
675675
}
676+
},
677+
{
678+
title:"XMLHttpRequest send ArrayBuffer",
679+
platforms: PLATFORM_ALL,
680+
linksrc:"src/XHRTest/XHRArrayBufferTest.js",
681+
testScene:function () {
682+
return new XHRArrayBufferTestScene();
683+
}
676684
}
677685

678686
//"UserDefaultTest",

0 commit comments

Comments
 (0)