Bug 1890790 - Skip unnecessary initialisations in Focus Robolectric tests. r=android...
[gecko.git] / mobile / android / focus-android / app / src / test / java / org / mozilla / focus / TestFocusApplication.kt
blob985d395001bc8f0a73000c7665f14c1d1954f41a
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2  * License, v. 2.0. If a copy of the MPL was not distributed with this
3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 package org.mozilla.focus
7 import android.content.Context
8 import android.util.AttributeSet
9 import android.util.JsonReader
10 import android.util.JsonWriter
11 import mozilla.components.browser.engine.gecko.profiler.Profiler
12 import mozilla.components.concept.engine.DefaultSettings
13 import mozilla.components.concept.engine.Engine
14 import mozilla.components.concept.engine.EngineSession
15 import mozilla.components.concept.engine.EngineSessionState
16 import mozilla.components.concept.engine.EngineView
17 import mozilla.components.concept.engine.Settings
18 import mozilla.components.concept.engine.utils.EngineVersion
19 import mozilla.components.concept.engine.webextension.WebExtensionDelegate
20 import mozilla.components.concept.fetch.Client
21 import mozilla.components.concept.fetch.Request
22 import mozilla.components.concept.fetch.Response
23 import org.json.JSONObject
25 /**
26  * [FocusApplication] override for unit tests. This allows us to override some parameters and inputs
27  * since an application object gets created without much control otherwise.
28  */
29 class TestFocusApplication : FocusApplication() {
30     override val components: Components by lazy {
31         Components(this, engineOverride = FakeEngine(), clientOverride = FakeClient())
32     }
34     override fun initializeNimbus() = Unit
35     override fun initializeTelemetry() = Unit
36     override fun finishSetupMegazord() = Unit
38     override fun initializeWebExtensionSupport() = Unit
41 /**
42  * Empty [FocusApplication] override for unit tests.
43  */
44 class EmptyFocusApplication : FocusApplication() {
45     override fun onCreate() {
46         //
47     }
50 // Borrowed this from AC unit tests. This is something we should consider moving to support-test, so
51 // that everyone who needs an Engine in unit tests can use it. It also allows us to enhance this mock
52 // and maybe do some actual things that help in tests. :)
53 class FakeEngine : Engine {
54     override val version: EngineVersion
55         get() = throw NotImplementedError("Not needed for test")
57     override fun createView(context: Context, attrs: AttributeSet?): EngineView =
58         throw UnsupportedOperationException()
60     override fun createSession(private: Boolean, contextId: String?): EngineSession =
61         throw UnsupportedOperationException()
63     override fun createSessionState(json: JSONObject) = FakeEngineSessionState()
65     override fun createSessionStateFrom(reader: JsonReader): EngineSessionState {
66         reader.beginObject()
67         reader.endObject()
68         return FakeEngineSessionState()
69     }
71     override fun name(): String =
72         throw UnsupportedOperationException()
74     override fun speculativeConnect(url: String) =
75         throw UnsupportedOperationException()
77     override val profiler: Profiler
78         get() = throw NotImplementedError("Not needed for test")
80     override val settings: Settings = DefaultSettings()
82     override fun registerWebExtensionDelegate(webExtensionDelegate: WebExtensionDelegate) {
83         // Intentionally empty to avoid "UnsupportedOperationException: Web extension support
84         // is not available in this engine" error in unit tests
85     }
88 class FakeEngineSessionState : EngineSessionState {
89     override fun writeTo(writer: JsonWriter) {
90         writer.beginObject()
91         writer.endObject()
92     }
95 class FakeClient : Client() {
96     override fun fetch(request: Request): Response {
97         throw UnsupportedOperationException()
98     }