• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package com.android.onboarding.bedsteadonboarding.fakes
2 
3 import android.content.Intent
4 import android.os.Build
5 import android.os.Bundle
6 import com.android.onboarding.contracts.ContractResult
7 
8 /** Contains helper methods for [FakeActivityNode] */
9 object FakeActivityNodeHelper {
10 
11   private const val FAKE_CONTRACT_CLASS_IDENTIFIER = "FAKE_CONTRACT_CLASS_IDENTIFIER"
12   private const val CONTRACT_RESULT_INTENT = "CONTRACT_RESULT_INTENT"
13   private const val CONTRACT_RESULT_CODE = "CONTRACT_RESULT_CODE"
14   private const val CONTRACT_RESULT_TYPE = "CONTRACT_RESULT_TYPE"
15   private const val CONTRACT_RESULT_FAILURE_REASON = "CONTRACT_RESULT_FAILURE_REASON"
16   private const val CONTRACT_RESULT_TYPE_SUCCESS = "CONTRACT_RESULT_SUCCESS"
17   private const val CONTRACT_RESULT_TYPE_FAILURE = "CONTRACT_RESULT_FAILURE"
18 
19   /** Serializes the given [contractResult] and [contractIdentifier] to bundle. */
createBundleFromContractResultAndIdentifiernull20   fun createBundleFromContractResultAndIdentifier(
21     contractResult: ContractResult,
22     contractIdentifier: String,
23   ): Bundle {
24     return Bundle().apply {
25       putParcelable(CONTRACT_RESULT_INTENT, contractResult.intent)
26       putInt(CONTRACT_RESULT_CODE, contractResult.resultCode)
27       putString(FAKE_CONTRACT_CLASS_IDENTIFIER, contractIdentifier)
28       putString(CONTRACT_RESULT_TYPE, getContractResultType(contractResult))
29       if ((contractResult is ContractResult.Failure) && (contractResult.reason != null)) {
30         putString(CONTRACT_RESULT_FAILURE_REASON, contractResult.reason)
31       }
32     }
33   }
34 
35   /**
36    * Extracts and returns the [contractResult] and [contractIdentifier] from given [bundle]. This
37    * will return null when the bundle does not contain [contractIdentifier] information.
38    */
extractContractIdentifierAndContractResultFromBundlenull39   fun extractContractIdentifierAndContractResultFromBundle(
40     bundle: Bundle
41   ): Pair<String, ContractResult>? {
42     val contractClassIdentifier = bundle.getString(FAKE_CONTRACT_CLASS_IDENTIFIER) ?: return null
43 
44     return Pair(contractClassIdentifier, createContractResultFromBundle(bundle))
45   }
46 
getContractResultTypenull47   private fun getContractResultType(contractResult: ContractResult): String {
48     return when (contractResult) {
49       is ContractResult.Success -> CONTRACT_RESULT_TYPE_SUCCESS
50       is ContractResult.Failure -> CONTRACT_RESULT_TYPE_FAILURE
51       else -> error("Unsupported contractResult type $contractResult")
52     }
53   }
54 
createContractResultFromBundlenull55   private fun createContractResultFromBundle(bundle: Bundle): ContractResult {
56     val resultCode = bundle.getInt(CONTRACT_RESULT_CODE)
57     val responseIntent =
58       if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
59         bundle.getParcelable(CONTRACT_RESULT_INTENT, Intent::class.java)
60       } else {
61         bundle.getParcelable(CONTRACT_RESULT_INTENT)
62       }
63 
64     val contractResultType =
65       bundle.getString(CONTRACT_RESULT_TYPE)
66         ?: error("ContractResult type shouldn't be null in bundle $bundle")
67     return when (contractResultType) {
68       CONTRACT_RESULT_TYPE_SUCCESS -> ContractResult.Success(resultCode, responseIntent)
69       CONTRACT_RESULT_TYPE_FAILURE -> {
70         val failureReason = bundle.getString(CONTRACT_RESULT_FAILURE_REASON)
71         ContractResult.Failure(resultCode, responseIntent, failureReason)
72       }
73       else -> error("Invalid contract result type $contractResultType")
74     }
75   }
76 }
77