Rahul Ravikumar | 9359ac2 | 2019-07-15 17:07:52 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package androidx.work |
| 18 | |
| 19 | import android.content.Context |
| 20 | import androidx.test.core.app.ApplicationProvider |
| 21 | import androidx.test.ext.junit.runners.AndroidJUnit4 |
| 22 | import androidx.test.filters.SmallTest |
| 23 | import org.hamcrest.CoreMatchers.notNullValue |
| 24 | import org.hamcrest.MatcherAssert.assertThat |
| 25 | import org.hamcrest.Matchers.`is` |
| 26 | import org.junit.Before |
| 27 | import org.junit.Test |
| 28 | import org.junit.runner.RunWith |
| 29 | import org.mockito.Mockito.mock |
| 30 | import org.mockito.Mockito.times |
| 31 | import org.mockito.Mockito.verify |
| 32 | |
| 33 | @RunWith(AndroidJUnit4::class) |
| 34 | @SmallTest |
| 35 | class InputMergerFactoryTest { |
| 36 | private lateinit var context: Context |
| 37 | private lateinit var factory: InputMergerFactory |
| 38 | private lateinit var configuration: Configuration |
| 39 | |
| 40 | @Before |
| 41 | fun setUp() { |
| 42 | context = ApplicationProvider.getApplicationContext() |
| 43 | factory = mock(InputMergerFactory::class.java) |
| 44 | configuration = Configuration.Builder() |
| 45 | .setInputMergerFactory(factory) |
| 46 | .build() |
| 47 | } |
| 48 | |
| 49 | @Test |
| 50 | fun testInputMergerFactory() { |
| 51 | val name = ArrayCreatingInputMerger::class.java.name |
| 52 | val merger = configuration.inputMergerFactory.createInputMergerWithDefaultFallback(name) |
| 53 | assertThat(merger, notNullValue()) |
| 54 | verify(factory, times(1)).createInputMerger(name) |
| 55 | } |
| 56 | |
| 57 | @Test |
| 58 | fun testInputMergerFactory2() { |
| 59 | factory = object : InputMergerFactory() { |
| 60 | override fun createInputMerger(className: String): InputMerger? { |
| 61 | return OverwritingInputMerger() |
| 62 | } |
| 63 | } |
| 64 | configuration = Configuration.Builder() |
| 65 | .setInputMergerFactory(factory) |
| 66 | .build() |
| 67 | |
| 68 | val name = ArrayCreatingInputMerger::class.java.name |
| 69 | val merger = configuration.inputMergerFactory.createInputMergerWithDefaultFallback(name) |
| 70 | val instanceCheck = merger is OverwritingInputMerger |
| 71 | assertThat(merger, notNullValue()) |
| 72 | assertThat(instanceCheck, `is`(true)) |
| 73 | } |
| 74 | } |