blob: 9ebb880dd5a5f7f5ddb4290611e5c934da8f43ac [file] [log] [blame]
Justin Klaassen4d01eea2018-04-03 23:21:57 -04001/*
2 * Copyright (C) 2017 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.lifecycle;
18
19import static androidx.lifecycle.Lifecycle.Event.ON_ANY;
20import static androidx.lifecycle.Lifecycle.Event.ON_RESUME;
21
22import static org.hamcrest.CoreMatchers.instanceOf;
23import static org.hamcrest.CoreMatchers.is;
24import static org.hamcrest.MatcherAssert.assertThat;
25import static org.mockito.Mockito.mock;
26import static org.mockito.Mockito.when;
27
28import static java.util.Arrays.asList;
29import static java.util.Collections.singletonList;
30
31import org.junit.Before;
32import org.junit.Test;
33import org.junit.runner.RunWith;
34import org.junit.runners.JUnit4;
35
36import java.util.ArrayList;
37import java.util.List;
38
39@RunWith(JUnit4.class)
40public class GeneratedAdaptersTest {
41
42 private LifecycleOwner mOwner;
43 @SuppressWarnings("FieldCanBeLocal")
44 private Lifecycle mLifecycle;
45
46 @Before
47 public void initMocks() {
48 mOwner = mock(LifecycleOwner.class);
49 mLifecycle = mock(Lifecycle.class);
50 when(mOwner.getLifecycle()).thenReturn(mLifecycle);
51 }
52
53 static class SimpleObserver implements LifecycleObserver {
54 List<String> mLog;
55
56 SimpleObserver(List<String> log) {
57 mLog = log;
58 }
59
60 @OnLifecycleEvent(Lifecycle.Event.ON_CREATE)
61 void onCreate() {
62 mLog.add("onCreate");
63 }
64 }
65
66 @Test
67 public void testSimpleSingleGeneratedAdapter() {
68 List<String> actual = new ArrayList<>();
69 GenericLifecycleObserver callback = Lifecycling.getCallback(new SimpleObserver(actual));
70 callback.onStateChanged(mOwner, Lifecycle.Event.ON_CREATE);
71 assertThat(callback, instanceOf(SingleGeneratedAdapterObserver.class));
72 assertThat(actual, is(singletonList("onCreate")));
73 }
74
75 static class TestObserver implements LifecycleObserver {
76 List<String> mLog;
77
78 TestObserver(List<String> log) {
79 mLog = log;
80 }
81
82 @OnLifecycleEvent(Lifecycle.Event.ON_CREATE)
83 void onCreate() {
84 mLog.add("onCreate");
85 }
86
87 @OnLifecycleEvent(ON_ANY)
88 void onAny() {
89 mLog.add("onAny");
90 }
91 }
92
93 @Test
94 public void testOnAny() {
95 List<String> actual = new ArrayList<>();
96 GenericLifecycleObserver callback = Lifecycling.getCallback(new TestObserver(actual));
97 callback.onStateChanged(mOwner, Lifecycle.Event.ON_CREATE);
98 assertThat(callback, instanceOf(SingleGeneratedAdapterObserver.class));
99 assertThat(actual, is(asList("onCreate", "onAny")));
100 }
101
102 interface OnPauses extends LifecycleObserver {
103 @OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
104 void onPause();
105
106 @OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
107 void onPause(LifecycleOwner owner);
108 }
109
110 interface OnPauseResume extends LifecycleObserver {
111 @OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
112 void onPause();
113
114 @OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
115 void onResume();
116 }
117
118 class Impl1 implements OnPauses, OnPauseResume {
119
120 List<String> mLog;
121
122 Impl1(List<String> log) {
123 mLog = log;
124 }
125
126 @Override
127 public void onPause() {
128 mLog.add("onPause_0");
129 }
130
131 @Override
132 public void onResume() {
133 mLog.add("onResume");
134 }
135
136 @Override
137 public void onPause(LifecycleOwner owner) {
138 mLog.add("onPause_1");
139 }
140 }
141
142 @Test
143 public void testClashingInterfaces() {
144 List<String> actual = new ArrayList<>();
145 GenericLifecycleObserver callback = Lifecycling.getCallback(new Impl1(actual));
146 callback.onStateChanged(mOwner, Lifecycle.Event.ON_PAUSE);
147 assertThat(callback, instanceOf(CompositeGeneratedAdaptersObserver.class));
148 assertThat(actual, is(asList("onPause_0", "onPause_1")));
149 actual.clear();
150 callback.onStateChanged(mOwner, Lifecycle.Event.ON_RESUME);
151 assertThat(actual, is(singletonList("onResume")));
152 }
153
154 class Base implements LifecycleObserver {
155
156 List<String> mLog;
157
158 Base(List<String> log) {
159 mLog = log;
160 }
161
162 @OnLifecycleEvent(ON_ANY)
163 void onAny() {
164 mLog.add("onAny_0");
165 }
166
167 @OnLifecycleEvent(ON_ANY)
168 void onAny(LifecycleOwner owner) {
169 mLog.add("onAny_1");
170 }
171
172 @OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
173 void onResume() {
174 mLog.add("onResume");
175 }
176 }
177
178 interface OnAny extends LifecycleObserver {
179 @OnLifecycleEvent(ON_ANY)
180 void onAny();
181
182 @OnLifecycleEvent(ON_ANY)
183 void onAny(LifecycleOwner owner, Lifecycle.Event event);
184 }
185
186 class Derived extends Base implements OnAny {
187 Derived(List<String> log) {
188 super(log);
189 }
190
191 @Override
192 public void onAny() {
193 super.onAny();
194 }
195
196 @Override
197 public void onAny(LifecycleOwner owner, Lifecycle.Event event) {
198 mLog.add("onAny_2");
199 assertThat(event, is(ON_RESUME));
200 }
201 }
202
203 @Test
204 public void testClashingClassAndInterface() {
205 List<String> actual = new ArrayList<>();
206 GenericLifecycleObserver callback = Lifecycling.getCallback(new Derived(actual));
207 callback.onStateChanged(mOwner, Lifecycle.Event.ON_RESUME);
208 assertThat(callback, instanceOf(CompositeGeneratedAdaptersObserver.class));
209 assertThat(actual, is(asList("onResume", "onAny_0", "onAny_1", "onAny_2")));
210 }
211
212}