Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Postunittest #5

Open
wants to merge 67 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
67 commits
Select commit Hold shift + click to select a range
3cc25b7
Update strings.xml
wslany Jul 22, 2021
791fa94
Update strings.xml
wslany Jul 22, 2021
612ed91
Create brick_post_web_request.xml
wslany Jul 22, 2021
3cf3b61
Update Brick.java
wslany Jul 22, 2021
87c2489
Update brick_post_web_request.xml
wslany Jul 22, 2021
0b77d76
Update ActionFactory.java
wslany Jul 22, 2021
a4c88b7
Create PostWebRequestBrick.kt
wslany Jul 22, 2021
6c0d10b
Update strings.xml
wslany Jul 22, 2021
76064d4
Update strings.xml
wslany Jul 22, 2021
7ae202b
Update Brick.java
wslany Jul 23, 2021
246ddbd
Update PostWebRequestBrick.kt
wslany Jul 23, 2021
5975e6b
Create PostWebRequestAction.kt
wslany Jul 23, 2021
ee823fc
Create PostWebAction.kt
wslany Jul 23, 2021
dec4673
Update PostWebRequestAction.kt
wslany Jul 23, 2021
3317e45
Update PostWebAction.kt
wslany Jul 23, 2021
4515dcb
Create PostWebConnection.kt
wslany Jul 23, 2021
d8b1a0f
Update PostWebConnection.kt
wslany Jul 23, 2021
30daccf
Update Constants.java
wslany Jul 23, 2021
f08c604
Update strings.xml
wslany Jul 23, 2021
bedd808
Update BrickValues.java
wslany Jul 23, 2021
ab9b85d
Update CategoryBricksFactory.java
wslany Jul 23, 2021
83c0687
Update XstreamSerializer.java
wslany Jul 23, 2021
411367e
Update BricksHelpUrlTest.java
wslany Jul 23, 2021
8b04d46
Update PostWebConnection.kt
wslany Jul 23, 2021
7839a13
Create PostWebConnectionHolder.kt
wslany Jul 23, 2021
ba5a94e
Update StageListener.java
wslany Jul 23, 2021
c87ca2c
Update PostWebConnection.kt
wslany Jul 23, 2021
dcfca96
Update PostWebAction.kt
wslany Jul 23, 2021
5c20c45
Update PostWebAction.kt
wslany Jul 23, 2021
e27653d
Update PostWebConnection.kt
wslany Jul 23, 2021
39c66cd
Update PostWebConnection.kt
wslany Jul 23, 2021
57ce77e
Update ActionFactory.java
wslany Jul 23, 2021
8b3786d
Update BrickDialogManager.kt
wslany Jul 25, 2021
412f6fa
Update PostWebAction.kt
wslany Jul 25, 2021
f617929
Update BrickDialogManager.kt
wslany Jul 25, 2021
f10deef
Update ActionFactory.java
wslany Jul 25, 2021
d7fd3a1
Update StageListener.java
wslany Jul 25, 2021
a2ea575
Update PostWebConnection.kt
wslany Jul 25, 2021
93e8caa
Update BrickDialogManager.kt
wslany Jul 25, 2021
5ca0e81
Update PostWebConnection.kt
wslany Jul 25, 2021
c72e1bd
Update PostWebConnection.kt
wslany Jul 25, 2021
52a41cb
Update BrickCategoryTest.java
wslany Jul 25, 2021
e1b851e
Update BrickDialogManager.kt
wslany Jul 25, 2021
aa7f219
Update brick_post_web_request.xml
wslany Jul 25, 2021
235d421
Update PostWebAction.kt
wslany Jul 25, 2021
1d8a24b
Update PostWebAction.kt
wslany Jul 25, 2021
c398233
Update PostWebConnection.kt
wslany Jul 25, 2021
7a60127
Update PostWebAction.kt
wslany Jul 25, 2021
797c687
Update PostWebAction.kt
wslany Jul 25, 2021
f387393
Update PostWebAction.kt
wslany Jul 25, 2021
98ff17d
Do not store catroid APKs two times...
wslany Oct 24, 2021
c2f3726
Merge pull request #4364 from wslany/no-duplicate-apks
wslany Oct 24, 2021
d1bd7ff
Update Constants.java
wslany Oct 24, 2021
c32f80e
Revert to current develop
wslany Oct 24, 2021
e4dde47
Refactored to kotlin...
wslany Oct 24, 2021
30435db
Revert to develop to fix conflicts
wslany Oct 24, 2021
08f8489
Merge pull request #1 from Catrobat/develop
wslany Oct 24, 2021
4d639e7
Reinserting post-brick
wslany Oct 25, 2021
f795077
Reinserting POST brick changes
wslany Oct 25, 2021
becaec6
Forgot reinserting second previous commit...
wslany Oct 25, 2021
9a9a72f
Indentations and white spaces
wslany Oct 25, 2021
7ec6d2d
Whitespace
wslany Oct 25, 2021
213983f
CATROID-1311 (CATROID#1311)
Nayab56 Jan 25, 2022
c7fbec0
CATROID#1311
Nayab56 Jan 25, 2022
5df3447
Update PostWebConnectionTest.kt
Nayab56 Jan 25, 2022
9f36e71
CATROID
Nayab56 Jan 25, 2022
935cdbf
CATROID#1311
Nayab56 Jan 25, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Jenkinsfile
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ pipeline {
-Pdownload='https://share.catrob.at/pocketcode/download/817.catrobat'"""

renameApks("${env.BRANCH_NAME}-${env.BUILD_NUMBER}")
archiveArtifacts '**/*.apk'
archiveArtifacts '**/catroid-standalone*.apk'
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@ public class BricksHelpUrlTest {
"https://wiki.catrobat.org/bin/view/Documentation/BrickDocumentation/LookRequestBrick");
brickToHelpUrlMapping.put("org.catrobat.catroid.content.bricks.BackgroundRequestBrick",
"https://wiki.catrobat.org/bin/view/Documentation/BrickDocumentation/BackgroundRequestBrick");
brickToHelpUrlMapping.put("org.catrobat.catroid.content.bricks.PostWebRequestBrick",
"https://wiki.catrobat.org/bin/view/Documentation/BrickDocumentation/PostWebRequestBrick");
brickToHelpUrlMapping.put("org.catrobat.catroid.content.bricks.LegoEv3MotorMoveBrick",
"https://wiki.catrobat.org/bin/view/Documentation/BrickDocumentation/LegoEv3MotorMoveBrick");
brickToHelpUrlMapping.put("org.catrobat.catroid.content.bricks.HideBrick",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@
import org.catrobat.catroid.content.bricks.PlaySoundBrick;
import org.catrobat.catroid.content.bricks.PointInDirectionBrick;
import org.catrobat.catroid.content.bricks.PointToBrick;
import org.catrobat.catroid.content.bricks.PostWebRequestBrick;
import org.catrobat.catroid.content.bricks.PreviousLookBrick;
import org.catrobat.catroid.content.bricks.RaspiIfLogicBeginBrick;
import org.catrobat.catroid.content.bricks.RaspiPwmBrick;
Expand Down Expand Up @@ -410,6 +411,7 @@ public static Collection<Object[]> data() {
StoreCSVIntoUserListBrick.class,
WebRequestBrick.class,
LookRequestBrick.class,
PostWebRequestBrick.class,
AskBrick.class,
AskSpeechBrick.class,
StartListeningBrick.class)},
Expand All @@ -418,6 +420,7 @@ public static Collection<Object[]> data() {
WhenTouchDownBrick.class,
WebRequestBrick.class,
LookRequestBrick.class,
PostWebRequestBrick.class,
OpenUrlBrick.class,
VibrationBrick.class,
SpeakBrick.class,
Expand Down Expand Up @@ -496,7 +499,8 @@ public static Collection<Object[]> data() {
TouchAndSlideBrick.class,
FinishStageBrick.class,
StoreCSVIntoUserListBrick.class,
WebRequestBrick.class)},
WebRequestBrick.class,
PostWebRequestBrick.class)},
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,9 @@ public final class BrickValues {
public static final int DEFAULT_TEMPO = 60;
public static final int CHANGE_TEMPO = 10;
// Note: No constant default value for the "Send web request" brick, as it is localized in strings.xml
public static final String POST_WEB_REQUEST_URL = "https://httpbin.org/post";
public static final String POST_WEB_REQUEST_HEADER = "User-Agent: Mozilla/5.0 (compatible; Catrobatbot/1.0; +https://catrob.at/bot)\naccept: application/json";
// Note: No constant default value for the data content of the "Post web request" brick, as it is localized in strings.xml

private BrickValues() {
throw new AssertionError("No.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@
import org.catrobat.catroid.content.actions.PlaySoundAction;
import org.catrobat.catroid.content.actions.PointInDirectionAction;
import org.catrobat.catroid.content.actions.PointToAction;
import org.catrobat.catroid.content.actions.PostWebRequestAction;
import org.catrobat.catroid.content.actions.RaspiIfLogicAction;
import org.catrobat.catroid.content.actions.RaspiPwmAction;
import org.catrobat.catroid.content.actions.RaspiSendDigitalValueAction;
Expand Down Expand Up @@ -1541,6 +1542,18 @@ public Action createWebRequestAction(Sprite sprite, SequenceAction sequence, For
return action;
}

public Action createPostWebRequestAction(Sprite sprite, SequenceAction sequence, Formula urlFormula,
Formula headerFormula, Formula dataFormula, UserVariable userVariable) {
PostWebRequestAction action = action(PostWebRequestAction.class);
Scope scope = new Scope(ProjectManager.getInstance().getCurrentProject(), sprite, sequence);
action.setScope(scope);
action.setUrlFormula(urlFormula);
action.setHeaderFormula(headerFormula);
action.setDataFormula(dataFormula);
action.setUserVariable(userVariable);
return action;
}

public Action createLookRequestAction(Sprite sprite, SequenceAction sequence,
Formula variableFormula) {
LookRequestAction action = action(LookRequestAction.class);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
/*
* Catroid: An on-device visual programming system for Android devices
* Copyright (C) 2010-2021 The Catrobat Team
* (<http://developer.catrobat.org/credits>)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* An additional term exception under section 7 of the GNU Affero
* General Public License, version 3, is available at
* http://developer.catrobat.org/license_additional_term
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.catrobat.catroid.content.actions

import android.util.Log
import androidx.annotation.CallSuper
import com.badlogic.gdx.scenes.scene2d.Action
import okhttp3.Response
import org.catrobat.catroid.TrustedDomainManager
import org.catrobat.catroid.common.Constants
import org.catrobat.catroid.content.Scope
import org.catrobat.catroid.formulaeditor.Formula
import org.catrobat.catroid.formulaeditor.InterpretationException
import org.catrobat.catroid.stage.BrickDialogManager
import org.catrobat.catroid.stage.StageActivity
import org.catrobat.catroid.stage.StageActivity.stageListener
import org.catrobat.catroid.web.PostWebConnection
import org.catrobat.catroid.web.PostWebConnection.PostWebRequestListener

abstract class PostWebAction : Action(), PostWebRequestListener {
private var postWebConnection: PostWebConnection? = null
var urlFormula: Formula? = null
var headerFormula: Formula? = null
var dataFormula: Formula? = null
var scope: Scope? = null
var url: String? = null
var header: String? = null
var data: String? = null

enum class RequestStatus {
NOT_SENT, WAITING, FINISHED
}
var requestStatus: RequestStatus = RequestStatus.NOT_SENT

enum class PermissionStatus {
UNKNOWN, PENDING, DENIED, GRANTED
}
private var permissionStatus: PermissionStatus = PermissionStatus.UNKNOWN

private fun interpretableUrl(): Boolean {
return try {
if (url == null) {
url = urlFormula!!.interpretString(scope)
}
true
} catch (exception: InterpretationException) {
Log.d(javaClass.simpleName, "Couldn't interpret url formula", exception)
false
}
}

private fun interpretableHeader(): Boolean {
return try {
if (header == null) {
header = headerFormula!!.interpretString(scope)
}
true
} catch (exception: InterpretationException) {
Log.d(javaClass.simpleName, "Couldn't interpret header formula", exception)
false
}
}

private fun interpretableData(): Boolean {
return try {
if (data == null) {
data = dataFormula!!.interpretString(scope)
}
true
} catch (exception: InterpretationException) {
Log.d(javaClass.simpleName, "Couldn't interpret data formula", exception)
false
}
}

private fun askForPermission() {
if (StageActivity.messageHandler == null) {
denyPermission()
} else {
permissionStatus = PermissionStatus.PENDING
val params = arrayListOf(BrickDialogManager.DialogType.POST_WEB_ACCESS_DIALOG, this, url!!)
StageActivity.messageHandler.obtainMessage(StageActivity.SHOW_DIALOG, params).sendToTarget()
}
}

fun grantPermission() {
permissionStatus = PermissionStatus.GRANTED
}

fun denyPermission() {
permissionStatus = PermissionStatus.DENIED
}

override fun act(delta: Float): Boolean {
if (!interpretableUrl() || !interpretableHeader() || !interpretableData()) {
return true
}

when (permissionStatus) {
PermissionStatus.UNKNOWN -> checkPermission()
PermissionStatus.DENIED -> {
handleError(Constants.ERROR_AUTHENTICATION_REQUIRED.toString())
return true
}
else -> {}
}

if (permissionStatus == PermissionStatus.PENDING) {
return false
}

if (requestStatus == RequestStatus.NOT_SENT && !sendRequest()) {
handleError(Constants.ERROR_TOO_MANY_REQUESTS.toString())
return true
}
if (requestStatus == RequestStatus.WAITING) {
return false
}

stageListener.postWebConnectionHolder.removeConnection(postWebConnection)
handleResponse()
return true
}

private fun checkPermission() =
if (TrustedDomainManager.isURLTrusted(url!!)) {
grantPermission()
} else {
askForPermission()
}

private fun sendRequest(): Boolean {
requestStatus = RequestStatus.WAITING
postWebConnection = PostWebConnection(this, url!!, header!!, data!!)

return if (stageListener.postWebConnectionHolder.addConnection(postWebConnection!!)) {
postWebConnection!!.sendPostWebRequest()
true
} else false
}

abstract fun handleResponse()
abstract fun handleError(error: String)

@CallSuper
override fun onRequestSuccess(httpResponse: Response) {
requestStatus = RequestStatus.FINISHED
}

@CallSuper
override fun onRequestError(httpError: String) {
requestStatus = RequestStatus.FINISHED
}

@CallSuper
override fun restart() {
stageListener.postWebConnectionHolder.removeConnection(postWebConnection)
postWebConnection = null
url = null
requestStatus = RequestStatus.NOT_SENT
permissionStatus = PermissionStatus.UNKNOWN
}

@CallSuper
override fun onCancelledCall() {
postWebConnection = null
url = null
requestStatus = RequestStatus.NOT_SENT
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* Catroid: An on-device visual programming system for Android devices
* Copyright (C) 2010-2021 The Catrobat Team
* (<http://developer.catrobat.org/credits>)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* An additional term exception under section 7 of the GNU Affero
* General Public License, version 3, is available at
* http://developer.catrobat.org/license_additional_term
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.catrobat.catroid.content.actions

import android.util.Log
import okhttp3.Response
import org.catrobat.catroid.formulaeditor.UserVariable
import java.io.IOException

class PostWebRequestAction : PostWebAction() {
var userVariable: UserVariable? = null
var response: String? = null

override fun act(delta: Float): Boolean {
val scopeInitialized = scope?.sprite != null && scope?.sequence != null
return if (userVariable == null || urlFormula == null || !scopeInitialized) {
true
} else super.act(delta)
}

override fun handleResponse() {
userVariable!!.value = response
}

override fun handleError(error: String) {
userVariable!!.value = error
}

override fun restart() {
response = null
super.restart()
}

override fun onRequestSuccess(httpResponse: Response) {
response = try {
httpResponse.body()?.string() ?: ""
} catch (exception: IOException) {
Log.d(javaClass.simpleName, "HTTP response body is empty", exception)
""
}
super.onRequestSuccess(httpResponse)
}

override fun onRequestError(httpError: String) {
response = httpError
super.onRequestError(httpError)
}

override fun onCancelledCall() {
response = null
super.onCancelledCall()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,13 @@ interface FormulaField extends Serializable {
enum BrickField implements FormulaField {
COLOR, COLOR_CHANGE, BRIGHTNESS, BRIGHTNESS_CHANGE, X_POSITION, Y_POSITION, X_POSITION_CHANGE, Y_POSITION_CHANGE,
TRANSPARENCY, TRANSPARENCY_CHANGE, SIZE, SIZE_CHANGE, VOLUME, VOLUME_CHANGE, X_DESTINATION, Y_DESTINATION, STEPS,
DURATION_IN_SECONDS, DEGREES, TURN_RIGHT_DEGREES, TURN_LEFT_DEGREES, TIME_TO_WAIT_IN_SECONDS, VARIABLE,

VARIABLE_CHANGE, WEB_REQUEST, LOOK_REQUEST, LOOK_NEW, LOOK_COPY, BACKGROUND_REQUEST, WRITE_FILENAME,
READ_FILENAME, TEMPO, HORIZONTAL_FLEXIBILITY, VERTICAL_FLEXIBILITY,
TEMPO_CHANGE, BEATS_TO_PAUSE, NOTE_TO_PLAY, BEATS_TO_PLAY_NOTE, OPEN_URL, PLAY_DRUM,
DURATION_IN_SECONDS, DEGREES, TURN_RIGHT_DEGREES, TURN_LEFT_DEGREES, TIME_TO_WAIT_IN_SECONDS, VARIABLE, VARIABLE_CHANGE,
WEB_REQUEST, LOOK_REQUEST, POST_WEB_REQUEST_URL, POST_WEB_REQUEST_HEADER, POST_WEB_REQUEST_DATA,
LOOK_NEW, LOOK_COPY, BACKGROUND_REQUEST, WRITE_FILENAME, READ_FILENAME,
HORIZONTAL_FLEXIBILITY, VERTICAL_FLEXIBILITY,

TEMPO, TEMPO_CHANGE, BEATS_TO_PAUSE, NOTE_TO_PLAY, BEATS_TO_PLAY_NOTE, OPEN_URL, PLAY_DRUM,

PEN_SIZE, PEN_COLOR_RED, PEN_COLOR_GREEN, PEN_COLOR_BLUE,

Expand Down
Loading