From b0cfa3ee5c3a2c881a3a8a2306fa68200cdf563c Mon Sep 17 00:00:00 2001 From: Michael Comella Date: Wed, 1 Apr 2020 13:45:01 -0700 Subject: [PATCH] For #9605: add FenixRobolectricTestRunner. This class will help us reduce duplication by specifying the configuration within the test runner rather than each test needing to specify it itself. This class is adapted from Firefox for Fire TV: https://github.com/mozilla-mobile/firefox-tv/blob/master/app/src/test/java/org/mozilla/tv/firefox/helpers/FirefoxRobolectricTestRunner.kt --- .../helpers/FenixRobolectricTestRunner.kt | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 app/src/test/java/org/mozilla/fenix/helpers/FenixRobolectricTestRunner.kt diff --git a/app/src/test/java/org/mozilla/fenix/helpers/FenixRobolectricTestRunner.kt b/app/src/test/java/org/mozilla/fenix/helpers/FenixRobolectricTestRunner.kt new file mode 100644 index 000000000..3e9c0194a --- /dev/null +++ b/app/src/test/java/org/mozilla/fenix/helpers/FenixRobolectricTestRunner.kt @@ -0,0 +1,32 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +package org.mozilla.fenix.helpers + +import org.mozilla.fenix.TestApplication +import org.robolectric.RobolectricTestRunner +import org.robolectric.annotation.Config + +/** + * A test runner that starts Robolectric with our custom configuration for use in unit tests. + * + * usage: + * ``` + * @RunWith(FenixRobolectricTestRunner::class) + * class ExampleUnitTest { + * ``` + * + * IMPORTANT NOTES: + * - This should ALWAYS be used instead of RobolectricTestRunner and AndroidJUnit4 (note: the latter + * just delegates to the former) + * - You should only use Robolectric when necessary because it non-trivially increases test duration. + */ +class FenixRobolectricTestRunner(testClass: Class<*>) : RobolectricTestRunner(testClass) { + + override fun buildGlobalConfig(): Config { + return Config.Builder() + .setApplication(TestApplication::class.java) + .build() + } +}