|
| 1 | +/**************************************************************************/ |
| 2 | +/* CommandLineFileParserTest.kt */ |
| 3 | +/**************************************************************************/ |
| 4 | +/* This file is part of: */ |
| 5 | +/* GODOT ENGINE */ |
| 6 | +/* https://godotengine.org */ |
| 7 | +/**************************************************************************/ |
| 8 | +/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ |
| 9 | +/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ |
| 10 | +/* */ |
| 11 | +/* Permission is hereby granted, free of charge, to any person obtaining */ |
| 12 | +/* a copy of this software and associated documentation files (the */ |
| 13 | +/* "Software"), to deal in the Software without restriction, including */ |
| 14 | +/* without limitation the rights to use, copy, modify, merge, publish, */ |
| 15 | +/* distribute, sublicense, and/or sell copies of the Software, and to */ |
| 16 | +/* permit persons to whom the Software is furnished to do so, subject to */ |
| 17 | +/* the following conditions: */ |
| 18 | +/* */ |
| 19 | +/* The above copyright notice and this permission notice shall be */ |
| 20 | +/* included in all copies or substantial portions of the Software. */ |
| 21 | +/* */ |
| 22 | +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ |
| 23 | +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ |
| 24 | +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ |
| 25 | +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ |
| 26 | +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ |
| 27 | +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ |
| 28 | +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ |
| 29 | +/**************************************************************************/ |
| 30 | + |
| 31 | +package org.godotengine.godot.utils |
| 32 | + |
| 33 | +import org.junit.Test |
| 34 | +import org.junit.runner.RunWith |
| 35 | +import org.junit.runners.Parameterized |
| 36 | +import java.io.ByteArrayInputStream |
| 37 | +import java.io.InputStream |
| 38 | + |
| 39 | +// Godot saves command line params in the `assets/_cl_` file on exporting an apk. By default, |
| 40 | +// without any other commands specified in `command_line/extra_args` in Export window, the content |
| 41 | +// of that _cl_ file consists of only the `--xr_mode_regular` and `--use_immersive` flags. |
| 42 | +// The `CL_` prefix here refers to that file |
| 43 | +private val CL_DEFAULT_NO_EXTRA_ARGS = byteArrayOf(2, 0, 0, 0, 17, 0, 0, 0, 45, 45, 120, 114, 95, 109, 111, 100, 101, 95, 114, 101, 103, 117, 108, 97, 114, 15, 0, 0, 0, 45, 45, 117, 115, 101, 95, 105, 109, 109, 101, 114, 115, 105, 118, 101) |
| 44 | +private val CL_ONE_EXTRA_ARG = byteArrayOf(3, 0, 0, 0, 15, 0, 0, 0, 45, 45, 117, 110, 105, 116, 95, 116, 101, 115, 116, 95, 97, 114, 103, 17, 0, 0, 0, 45, 45, 120, 114, 95, 109, 111, 100, 101, 95, 114, 101, 103, 117, 108, 97, 114, 15, 0, 0, 0, 45, 45, 117, 115, 101, 95, 105, 109, 109, 101, 114, 115, 105, 118, 101) |
| 45 | +private val CL_TWO_EXTRA_ARGS = byteArrayOf(4, 0, 0, 0, 16, 0, 0, 0, 45, 45, 117, 110, 105, 116, 95, 116, 101, 115, 116, 95, 97, 114, 103, 49, 16, 0, 0, 0, 45, 45, 117, 110, 105, 116, 95, 116, 101, 115, 116, 95, 97, 114, 103, 50, 17, 0, 0, 0, 45, 45, 120, 114, 95, 109, 111, 100, 101, 95, 114, 101, 103, 117, 108, 97, 114, 15, 0, 0, 0, 45, 45, 117, 115, 101, 95, 105, 109, 109, 101, 114, 115, 105, 118, 101) |
| 46 | +private val CL_EMPTY = byteArrayOf() |
| 47 | +private val CL_HEADER_TOO_SHORT = byteArrayOf(0, 0, 0) |
| 48 | +private val CL_INCOMPLETE_FIRST_ARG = byteArrayOf(2, 0, 0, 0, 17, 0, 0) |
| 49 | +private val CL_LENGTH_TOO_LONG_IN_FIRST_ARG = byteArrayOf(2, 0, 0, 0, 17, 0, 0, 45, 45, 120, 114, 95, 109, 111, 100, 101, 95, 114, 101, 103, 117, 108, 97, 114, 15, 0, 0, 0, 45, 45, 117, 115, 101, 95, 105, 109, 109, 101, 114, 115, 105, 118, 101) |
| 50 | +private val CL_MISMATCHED_ARG_LENGTH_AND_HEADER_ONE_ARG = byteArrayOf(2, 0, 0, 0, 10, 0, 0, 0, 45, 45, 120, 114) |
| 51 | +private val CL_MISMATCHED_ARG_LENGTH_AND_HEADER_IN_FIRST_ARG = byteArrayOf(2, 0, 0, 0, 17, 0, 0, 0, 45, 45, 120, 114, 95, 109, 111, 100, 101, 95, 114, 101, 103, 117, 108, 97, 15, 0, 0, 0, 45, 45, 117, 115, 101, 95, 105, 109, 109, 101, 114, 115, 105, 118, 101) |
| 52 | + |
| 53 | +@RunWith(Parameterized::class) |
| 54 | +class CommandLineFileParserTest( |
| 55 | + private val inputStreamArg: InputStream, |
| 56 | + private val expectedResult: List<String>, |
| 57 | +) { |
| 58 | + |
| 59 | + private val commandLineFileParser = CommandLineFileParser() |
| 60 | + |
| 61 | + companion object { |
| 62 | + @JvmStatic |
| 63 | + @Parameterized.Parameters |
| 64 | + fun data() = listOf( |
| 65 | + arrayOf(ByteArrayInputStream(CL_EMPTY), listOf<String>()), |
| 66 | + arrayOf(ByteArrayInputStream(CL_HEADER_TOO_SHORT), listOf<String>()), |
| 67 | + |
| 68 | + arrayOf(ByteArrayInputStream(CL_DEFAULT_NO_EXTRA_ARGS), listOf( |
| 69 | + "--xr_mode_regular", |
| 70 | + "--use_immersive", |
| 71 | + )), |
| 72 | + |
| 73 | + arrayOf(ByteArrayInputStream(CL_ONE_EXTRA_ARG), listOf( |
| 74 | + "--unit_test_arg", |
| 75 | + "--xr_mode_regular", |
| 76 | + "--use_immersive", |
| 77 | + )), |
| 78 | + |
| 79 | + arrayOf(ByteArrayInputStream(CL_TWO_EXTRA_ARGS), listOf( |
| 80 | + "--unit_test_arg1", |
| 81 | + "--unit_test_arg2", |
| 82 | + "--xr_mode_regular", |
| 83 | + "--use_immersive", |
| 84 | + )), |
| 85 | + |
| 86 | + arrayOf(ByteArrayInputStream(CL_INCOMPLETE_FIRST_ARG), listOf<String>()), |
| 87 | + arrayOf(ByteArrayInputStream(CL_LENGTH_TOO_LONG_IN_FIRST_ARG), listOf<String>()), |
| 88 | + arrayOf(ByteArrayInputStream(CL_MISMATCHED_ARG_LENGTH_AND_HEADER_ONE_ARG), listOf<String>()), |
| 89 | + arrayOf(ByteArrayInputStream(CL_MISMATCHED_ARG_LENGTH_AND_HEADER_IN_FIRST_ARG), listOf<String>()), |
| 90 | + ) |
| 91 | + } |
| 92 | + |
| 93 | + @Test |
| 94 | + fun `Given inputStream, When parsing command line, Then a correct list is returned`() { |
| 95 | + // given |
| 96 | + val inputStream = inputStreamArg |
| 97 | + |
| 98 | + // when |
| 99 | + val result = commandLineFileParser.parseCommandLine(inputStream) |
| 100 | + |
| 101 | + // then |
| 102 | + assert(result == expectedResult) { "Expected: $expectedResult Actual: $result" } |
| 103 | + } |
| 104 | +} |
0 commit comments