Skip to content

Commit cf6303b

Browse files
Implement unit tests for QRCodeOnboardingPayload (#27587)
* Implement unit tests for QRCodeOnboardingPayload * Restyled by gn * Address review comments --------- Co-authored-by: Restyled.io <commits@restyled.io>
1 parent 34d9500 commit cf6303b

File tree

5 files changed

+353
-34
lines changed

5 files changed

+353
-34
lines changed

.github/workflows/java-tests.yaml

+2-1
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,8 @@ jobs:
7979
chip.tlv.TlvReadWriteTest \
8080
chip.tlv.TlvReaderTest \
8181
chip.jsontlv.JsonToTlvToJsonTest \
82-
chip.onboardingpayload.ManualCodeTest
82+
chip.onboardingpayload.ManualCodeTest \
83+
chip.onboardingpayload.QRCodeTest
8384
- name: Build Java Matter Controller and all clusters app
8485
run: |
8586
scripts/run_in_build_env.sh './scripts/build_python.sh --install_virtual_env out/venv'

kotlin-detect-config.yaml

+6
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ style:
4040
examples/android/CHIPTest/app/src/androidTest/java/com/tcl/chip/chiptest/ExampleInstrumentedTest.kt"
4141
- "**/src/controller/java/tests/chip/jsontlv/JsonToTlvToJsonTest.kt"
4242
- "**/src/controller/java/tests/chip/onboardingpayload/ManualCodeTest.kt"
43+
- "**/src/controller/java/tests/chip/onboardingpayload/QRCodeTest.kt"
4344
- "**/src/controller/java/tests/chip/tlv/TlvReaderTest.kt"
4445
- "**/src/controller/java/tests/chip/tlv/TlvReadWriteTest.kt"
4546
- "**/src/controller/java/tests/chip/tlv/TlvWriterTest.kt"
@@ -128,6 +129,7 @@ style:
128129
- "**/src/controller/java/src/chip/onboardingpayload/OnboardingPayload.kt"
129130
- "**/src/controller/java/src/chip/onboardingpayload/QRCodeBasicOnboardingPayloadGenerator.kt"
130131
- "**/src/controller/java/tests/chip/onboardingpayload/ManualCodeTest.kt"
132+
- "**/src/controller/java/tests/chip/onboardingpayload/QRCodeTest.kt"
131133
UnusedParameter:
132134
excludes:
133135
- "**/examples/android/CHIPTool/app/src/main/java/com/google/chip/chiptool/bluetooth/BluetoothManager.kt"
@@ -201,6 +203,7 @@ exceptions:
201203
- "**/examples/java-matter-controller/java/src/com/matter/controller/commands/common/Argument.kt"
202204
- "**/examples/java-matter-controller/java/src/com/matter/controller/Main.kt"
203205
- "**/src/controller/java/tests/chip/onboardingpayload/ManualCodeTest.kt"
206+
- "**/src/controller/java/tests/chip/onboardingpayload/QRCodeTest.kt"
204207
SwallowedException:
205208
excludes:
206209
- "**/examples/android/CHIPTool/app/src/main/java/com/google/chip/chiptool/clusterclient/SensorClientFragment.kt"
@@ -221,6 +224,7 @@ naming:
221224
- "**/src/controller/java/tests/chip/tlv/TlvReaderTest.kt"
222225
- "**/src/controller/java/tests/chip/jsontlv/JsonToTlvToJsonTest.kt"
223226
- "**/src/controller/java/tests/chip/onboardingpayload/ManualCodeTest.kt"
227+
- "**/src/controller/java/tests/chip/onboardingpayload/QRCodeTest.kt"
224228
- "**/examples/android/CHIPTest/app/src/test/java/com/tcl/chip/chiptest/ExampleUnitTest.kt"
225229
ConstructorParameterNaming:
226230
excludes:
@@ -264,6 +268,7 @@ complexity:
264268
- "**/src/controller/java/src/chip/tlv/TlvWriter.kt"
265269
- "**/src/controller/java/tests/chip/jsontlv/JsonToTlvToJsonTest.kt"
266270
- "**/src/controller/java/tests/chip/onboardingpayload/ManualCodeTest.kt"
271+
- "**/src/controller/java/tests/chip/onboardingpayload/QRCodeTest.kt"
267272
- "**/src/controller/java/tests/chip/tlv/TlvReadWriteTest.kt"
268273
NestedBlockDepth:
269274
excludes:
@@ -282,6 +287,7 @@ complexity:
282287
- "**/src/controller/java/src/chip/onboardingpayload/ManualOnboardingPayloadParser.kt"
283288
- "**/src/controller/java/tests/chip/jsontlv/JsonToTlvToJsonTest.kt"
284289
- "**/src/controller/java/tests/chip/onboardingpayload/ManualCodeTest.kt"
290+
- "**/src/controller/java/tests/chip/onboardingpayload/QRCodeTest.kt"
285291
- "**/src/controller/java/tests/chip/tlv/TlvReaderTest.kt"
286292
LargeClass:
287293
excludes:

src/controller/java/BUILD.gn

+15
Original file line numberDiff line numberDiff line change
@@ -275,10 +275,25 @@ kotlin_library("onboardingpayload_manual_code_test") {
275275
kotlinc_flags = [ "-Xlint:deprecation" ]
276276
}
277277

278+
kotlin_library("onboardingpayload_qr_code_test") {
279+
output_name = "OnboardingPayloadQRCodeTest.jar"
280+
281+
deps = [
282+
":onboarding_payload",
283+
"${chip_root}/third_party/java_deps:junit-4",
284+
"${chip_root}/third_party/java_deps:truth",
285+
]
286+
287+
sources = [ "tests/chip/onboardingpayload/QRCodeTest.kt" ]
288+
289+
kotlinc_flags = [ "-Xlint:deprecation" ]
290+
}
291+
278292
group("unit_tests") {
279293
deps = [
280294
":json_to_tlv_to_json_test",
281295
":onboardingpayload_manual_code_test",
296+
":onboardingpayload_qr_code_test",
282297
":tlv_read_write_test",
283298
":tlv_reader_test",
284299
":tlv_writer_test",

src/controller/java/src/chip/onboardingpayload/QRCodeOnboardingPayloadParser.kt

+9-33
Original file line numberDiff line numberDiff line change
@@ -24,39 +24,6 @@ import java.util.concurrent.atomic.AtomicInteger
2424
* to a OnboardingPayload object
2525
*/
2626
class QRCodeOnboardingPayloadParser(private val mBase38Representation: String) {
27-
private fun extractPayload(inString: String): String {
28-
var chipSegment = ""
29-
val delimiter = '%'
30-
val startIndices = mutableListOf<Int>()
31-
startIndices.add(0)
32-
33-
for (i in inString.indices) {
34-
if (inString[i] == delimiter) {
35-
startIndices.add(i + 1)
36-
}
37-
}
38-
39-
// Find the first string between delimiters that starts with kQRCodePrefix
40-
for (i in 0 until startIndices.size) {
41-
val startIndex = startIndices[i]
42-
val endIndex = if (i == startIndices.size - 1) inString.length else startIndices[i + 1] - 1
43-
val length = if (endIndex != inString.length) endIndex - startIndex else inString.length
44-
val segment = inString.substring(startIndex, startIndex + length)
45-
46-
// Find a segment that starts with kQRCodePrefix
47-
if (segment.startsWith(kQRCodePrefix) && segment.length > kQRCodePrefix.length) {
48-
chipSegment = segment
49-
break
50-
}
51-
}
52-
53-
if (chipSegment.length > 0) {
54-
return chipSegment.substring(kQRCodePrefix.length) // strip out prefix before returning
55-
}
56-
57-
return chipSegment
58-
}
59-
6027
fun populatePayload(outPayload: OnboardingPayload) {
6128
var indexToReadFrom: AtomicInteger = AtomicInteger(0)
6229

@@ -113,5 +80,14 @@ class QRCodeOnboardingPayloadParser(private val mBase38Representation: String) {
11380
index.addAndGet(numberOfBitsToRead)
11481
return dest
11582
}
83+
84+
fun extractPayload(inString: String?): String {
85+
return inString
86+
?.split('%')
87+
?.filter { s -> s.startsWith(kQRCodePrefix) }
88+
?.firstOrNull()
89+
?.substring(kQRCodePrefix.length)
90+
?: ""
91+
}
11692
}
11793
}

0 commit comments

Comments
 (0)