1/*
2 * Copyright 2018 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
17package androidx.navigation.safe.args.generator
18
19import androidx.navigation.safe.args.generator.NavType.INT
20import androidx.navigation.safe.args.generator.NavType.STRING
21import androidx.navigation.safe.args.generator.models.Action
22import androidx.navigation.safe.args.generator.models.Argument
23import androidx.navigation.safe.args.generator.models.Destination
24import androidx.navigation.safe.args.generator.models.ResReference
25import com.squareup.javapoet.ClassName
26import org.junit.Test
27import org.junit.runner.RunWith
28import org.junit.runners.JUnit4
29import org.hamcrest.CoreMatchers.`is`
30import org.hamcrest.MatcherAssert.assertThat
31import org.junit.Assert.fail
32
33@RunWith(JUnit4::class)
34class NavArgumentResolverTest {
35
36    private fun id(id: String) = ResReference("a.b", "id", id)
37
38    private fun createTemplateDestination(name: String) =
39            Destination(
40                    id(name), ClassName.get("foo", "Fragment${name.capitalize()}"), "test",
41                    listOf(
42                            Argument("arg1", STRING),
43                            Argument("arg2", STRING, StringValue("foo"))
44                    ), emptyList())
45
46    @Test
47    fun test() {
48        val dest1Template = createTemplateDestination("first")
49        val dest2Template = createTemplateDestination("second")
50        val outerScopeAction = Action(id("toOuterScope"), id("outerScope"),
51                listOf(Argument("boo", STRING)))
52        val dest1 = dest1Template.copy(actions = listOf(Action(id("action1"), dest2Template.id),
53                outerScopeAction))
54        val dest2 = dest2Template.copy(actions = listOf(Action(id("action2"), dest1Template.id,
55                listOf(Argument("arg1", STRING, StringValue("actionValue")),
56                        Argument("actionArg", STRING)))))
57
58        val topLevel = Destination(null, null, "test",
59                emptyList(), emptyList(), listOf(dest1, dest2))
60
61        val resolveArguments = resolveArguments(topLevel)
62        assertThat(resolveArguments.nested.size, `is`(2))
63
64        val resolvedAction1 = Action(id("action1"), dest2Template.id, dest2.args)
65        assertThat(resolveArguments.nested[0].actions, `is`(listOf(resolvedAction1,
66                outerScopeAction)))
67
68        val resolvedAction2 = Action(id("action2"), dest1Template.id, listOf(
69                Argument("arg1", STRING, StringValue("actionValue")),
70                Argument("actionArg", STRING),
71                Argument("arg2", STRING, StringValue("foo"))
72        ))
73        assertThat(resolveArguments.nested[1].actions, `is`(listOf(resolvedAction2)))
74    }
75
76    @Test
77    fun testIncompatibleTypes() {
78        val dest1 = createTemplateDestination("first")
79        val invalidAction = Action(id("action"), dest1.id, listOf(
80                Argument("arg2", INT, IntValue("11")),
81                Argument("arg1", STRING)
82        ))
83
84        val topLevel = Destination(null, null, "test", emptyList(), listOf(invalidAction),
85                listOf(dest1))
86
87        try {
88            resolveArguments(topLevel)
89            fail()
90        } catch (ex: IllegalArgumentException) {
91            // expected error
92        }
93    }
94}