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.lifecycle.runtime.lint.stubs
18 
19 import com.android.tools.lint.checks.infrastructure.LintDetectorTest
20 import com.android.tools.lint.checks.infrastructure.TestFiles
21 
22 internal val LIFECYCLE_STUB =
23     TestFiles.kt(
24             "androidx/lifecycle/PausingDispatcher.kt",
25             """
26         package androidx.lifecycle;
27 
28         import kotlinx.coroutines.CoroutineScope
29 
30         abstract class Lifecycle {
31             enum class State { CREATED, STARTED }
isAtLeastnull32             fun isAtLeast(state: State): Boolean {
33                 return true
34             }
35         }
36 
37         interface LifecycleOwner {
38             val lifecycle: Lifecycle
39         }
40 
41         suspend fun <T> Lifecycle.whenCreated(block: suspend CoroutineScope.() -> T): T {
42             throw Error()
43         }
44 
45         suspend fun <T> Lifecycle.whenStarted(block: suspend CoroutineScope.() -> T): T {
46             throw Error()
47         }
48 
49         suspend fun <T> Lifecycle.whenResumed(block: suspend CoroutineScope.() -> T): T {
50             throw Error()
51         }
52 
53         suspend fun <T> LifecycleOwner.whenCreated(block: suspend CoroutineScope.() -> T): T {
54             throw Error()
55         }
56 
57         suspend fun <T> LifecycleOwner.whenStarted(block: suspend CoroutineScope.() -> T): T {
58             throw Error()
59         }
60 
61         suspend fun <T> LifecycleOwner.whenResumed(block: suspend CoroutineScope.() -> T): T {
62             throw Error()
63         }
64     """
65         )
66         .indented()
67         .within("src")
68 
69 internal val COROUTINES_STUB =
70     TestFiles.kt(
71             "kotlinx/coroutines/GlobalScope.kt",
72             """
73         package kotlinx.coroutines;
74 
75         import kotlinx.coroutines.CoroutineScope
76 
77         interface CoroutineScope {}
78 
79         object GlobalScope {
80             fun launch(block: suspend () -> Unit) {}
81         }
82 
83     """
84         )
85         .indented()
86         .within("src")
87 
88 internal val VIEW_STUB =
89     TestFiles.kt(
90             """
91         package android.view
92 
93         class View {}
94 
95         class FooView: View() {
96             fun foo() {}
97         }
98     """
99         )
100         .indented()
101         .within("src")
102 
103 private val FRAGMENT_STUB =
104     LintDetectorTest.java(
105         """
106     package androidx.fragment.app;
107 
108     import androidx.lifecycle.LifecycleOwner;
109 
110     public class Fragment implements LifecycleOwner {
111         public LifecycleOwner getViewLifecycleOwner() {}
112     }
113 """
114     )
115 
116 private val ACTIVITY_STUB =
117     LintDetectorTest.java(
118         """
119     package androidx.core.app;
120 
121     import androidx.lifecycle.LifecycleOwner;
122 
123     public class ComponentActivity implements LifecycleOwner {}
124 """
125     )
126 
127 // This stub depends on definitions in LIFECYCLE_STUB
128 private val REPEAT_ON_LIFECYCLE_STUB =
129     TestFiles.kt(
130             "androidx/lifecycle/RepeatOnLifecycle.kt",
131             """
132         package androidx.lifecycle;
133 
134         import androidx.lifecycle.LifecycleOwner
135         import kotlinx.coroutines.CoroutineScope
136 
137         public suspend fun Lifecycle.repeatOnLifecycle(
138             state: Lifecycle.State,
139             block: suspend CoroutineScope.() -> Unit
140         ) {
141             throw Error()
142         }
143 
144         public suspend fun LifecycleOwner.repeatOnLifecycle(
145             state: Lifecycle.State,
146             block: suspend CoroutineScope.() -> Unit
147         ) {
148             throw Error()
149         }
150     """
151         )
152         .indented()
153         .within("src")
154 
155 // stubs for testing calls to LiveData.observe calls
156 internal val REPEAT_ON_LIFECYCLE_STUBS =
157     arrayOf(REPEAT_ON_LIFECYCLE_STUB, LIFECYCLE_STUB, FRAGMENT_STUB, ACTIVITY_STUB, COROUTINES_STUB)
158