aboutsummaryrefslogtreecommitdiff
path: root/tests/ui/src/com/android/car/calendar/CarCalendarUiTest.java
blob: d3992f68e887ae829cc94f5b2572350bed6309ce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
/*
 * Copyright 2020 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.car.calendar;

import static androidx.test.espresso.Espresso.onView;
import static androidx.test.espresso.action.ViewActions.click;
import static androidx.test.espresso.assertion.ViewAssertions.doesNotExist;
import static androidx.test.espresso.assertion.ViewAssertions.matches;
import static androidx.test.espresso.matcher.ViewMatchers.isDisplayed;
import static androidx.test.espresso.matcher.ViewMatchers.withId;
import static androidx.test.espresso.matcher.ViewMatchers.withText;

import static org.hamcrest.CoreMatchers.not;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.database.Cursor;
import android.database.MatrixCursor;
import android.net.Uri;
import android.os.Bundle;
import android.os.CancellationSignal;
import android.provider.CalendarContract;
import android.test.mock.MockContentProvider;
import android.test.mock.MockContentResolver;

import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.test.core.app.ActivityScenario;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import androidx.test.filters.LargeTest;
import androidx.test.platform.app.InstrumentationRegistry;
import androidx.test.rule.GrantPermissionRule;
import androidx.test.runner.lifecycle.ActivityLifecycleCallback;
import androidx.test.runner.lifecycle.ActivityLifecycleMonitorRegistry;
import androidx.test.runner.lifecycle.Stage;

import com.android.car.calendar.common.Event;
import com.android.car.calendar.common.EventsLiveData;

import com.google.common.collect.ImmutableList;

import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;

import java.time.Clock;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

@LargeTest
@RunWith(AndroidJUnit4.class)
public class CarCalendarUiTest {
    private static final ZoneId BERLIN_ZONE_ID = ZoneId.of("Europe/Berlin");
    private static final ZoneId UTC_ZONE_ID = ZoneId.of("UTC");
    private static final Locale LOCALE = Locale.ENGLISH;
    private static final ZonedDateTime CURRENT_DATE_TIME =
            LocalDateTime.of(2019, 12, 10, 10, 10, 10, 500500).atZone(BERLIN_ZONE_ID);
    private static final ZonedDateTime START_DATE_TIME =
            CURRENT_DATE_TIME.truncatedTo(ChronoUnit.HOURS);
    private static final String EVENT_TITLE = "the title";
    private static final String EVENT_LOCATION = "the location";
    private static final String EVENT_DESCRIPTION = "the description";
    private static final String CALENDAR_NAME = "the calendar name";
    private static final int CALENDAR_COLOR = 0xCAFEBABE;
    private static final int EVENT_ATTENDEE_STATUS =
            CalendarContract.Attendees.ATTENDEE_STATUS_ACCEPTED;

    private final ActivityLifecycleCallback mLifecycleCallback = this::onActivityLifecycleChanged;

    @Rule
    public final GrantPermissionRule permissionRule =
            GrantPermissionRule.grant(Manifest.permission.READ_CALENDAR);

    private List<Object[]> mTestEventRows;

    // If set to true fake dependencies will not be set and the real provider will be used.
    private boolean mDoNotSetFakeDependencies;

    // These can be set in the test thread and read on the main thread.
    private volatile CountDownLatch mEventChangesLatch;

    @Before
    public void setUp() {
        ActivityLifecycleMonitorRegistry.getInstance().addLifecycleCallback(mLifecycleCallback);
        mTestEventRows = new ArrayList<>();
        mDoNotSetFakeDependencies = false;
    }

    private void onActivityLifecycleChanged(Activity activity, Stage stage) {
        if (mDoNotSetFakeDependencies) return;

        if (stage.equals(Stage.PRE_ON_CREATE)) {
            setActivityDependencies((CarCalendarActivity) activity);
        } else if (stage.equals(Stage.CREATED)) {
            observeEventsLiveData((CarCalendarActivity) activity);
        }
    }

    private void setActivityDependencies(CarCalendarActivity activity) {
        Clock fixedTimeClock = Clock.fixed(CURRENT_DATE_TIME.toInstant(), BERLIN_ZONE_ID);
        Context context = InstrumentationRegistry.getInstrumentation().getTargetContext();
        MockContentResolver mockContentResolver = new MockContentResolver(context);
        TestCalendarContentProvider testCalendarContentProvider =
                new TestCalendarContentProvider(context);
        mockContentResolver.addProvider(CalendarContract.AUTHORITY, testCalendarContentProvider);
        activity.mDependencies =
                new CarCalendarActivity.Dependencies(LOCALE, fixedTimeClock, mockContentResolver);
    }

    private void observeEventsLiveData(CarCalendarActivity activity) {
        CarCalendarViewModel carCalendarViewModel =
                new ViewModelProvider(activity).get(CarCalendarViewModel.class);
        EventsLiveData eventsLiveData = carCalendarViewModel.getEventsLiveData();
        mEventChangesLatch = new CountDownLatch(1);

        // Notifications occur on the main thread.
        eventsLiveData.observeForever(
                new Observer<ImmutableList<Event>>() {
                    // Ignore the first change event triggered on registration with default value.
                    boolean mIgnoredFirstChange;

                    @Override
                    public void onChanged(ImmutableList<Event> events) {
                        if (mIgnoredFirstChange) {
                            // Signal that the events were changed and notified on main thread.
                            mEventChangesLatch.countDown();
                        }
                        mIgnoredFirstChange = true;
                    }
                });
    }

    @After
    public void tearDown() {
        ActivityLifecycleMonitorRegistry.getInstance().removeLifecycleCallback(mLifecycleCallback);
    }

    @Test
    public void withFakeDependencies_titleShows() {
        try (ActivityScenario<CarCalendarActivity> ignored =
                     ActivityScenario.launch(CarCalendarActivity.class)) {
            onView(withText(R.string.app_name)).check(matches(isDisplayed()));
        }
    }

    @Test
    public void withoutFakeDependencies_titleShows() {
        mDoNotSetFakeDependencies = true;
        try (ActivityScenario<CarCalendarActivity> ignored =
                     ActivityScenario.launch(CarCalendarActivity.class)) {
            onView(withText(R.string.app_name)).check(matches(isDisplayed()));
        }
    }

    @Test
    public void event_displayed() {
        mTestEventRows.add(buildTestRow(START_DATE_TIME, 1, EVENT_TITLE, false));
        try (ActivityScenario<CarCalendarActivity> ignored =
                ActivityScenario.launch(CarCalendarActivity.class)) {
            waitForEventsChange();

            // Wait for the UI to be updated with changed events.
            InstrumentationRegistry.getInstrumentation().waitForIdleSync();

            onView(withText(EVENT_TITLE)).check(matches(isDisplayed()));
        }
    }

    @Test
    public void singleAllDayEvent_notCollapsed() {
        // All day events are stored in UTC time.
        ZonedDateTime utcDayStartTime =
                START_DATE_TIME.withZoneSameInstant(UTC_ZONE_ID).truncatedTo(ChronoUnit.DAYS);

        mTestEventRows.add(buildTestRow(utcDayStartTime, 24, EVENT_TITLE, true));

        try (ActivityScenario<CarCalendarActivity> ignored =
                ActivityScenario.launch(CarCalendarActivity.class)) {
            waitForEventsChange();

            // Wait for the UI to be updated with changed events.
            InstrumentationRegistry.getInstrumentation().waitForIdleSync();

            // A single all-day event should not be collapsible.
            onView(withId(R.id.expand_collapse_icon)).check(doesNotExist());
            onView(withText(EVENT_TITLE)).check(matches(isDisplayed()));
        }
    }

    @Test
    public void multipleAllDayEvents_collapsed() {
        mTestEventRows.add(buildTestRowAllDay(EVENT_TITLE));
        mTestEventRows.add(buildTestRowAllDay("Another all day event"));

        try (ActivityScenario<CarCalendarActivity> ignored =
                ActivityScenario.launch(CarCalendarActivity.class)) {
            waitForEventsChange();

            // Wait for the UI to be updated with changed events.
            InstrumentationRegistry.getInstrumentation().waitForIdleSync();

            // Multiple all-day events should be collapsed.
            onView(withId(R.id.expand_collapse_icon)).check(matches(isDisplayed()));
            onView(withText(EVENT_TITLE)).check(matches(not(isDisplayed())));
        }
    }

    @Test
    public void multipleAllDayEvents_expands() {
        mTestEventRows.add(buildTestRowAllDay(EVENT_TITLE));
        mTestEventRows.add(buildTestRowAllDay("Another all day event"));

        try (ActivityScenario<CarCalendarActivity> ignored =
                ActivityScenario.launch(CarCalendarActivity.class)) {
            waitForEventsChange();

            // Wait for the UI to be updated with changed events.
            InstrumentationRegistry.getInstrumentation().waitForIdleSync();

            // Multiple all-day events should be collapsed.
            onView(withId(R.id.expand_collapse_icon)).perform(click());
            InstrumentationRegistry.getInstrumentation().waitForIdleSync();
            onView(withText(EVENT_TITLE)).check(matches(isDisplayed()));
        }
    }

    private void waitForEventsChange() {
        try {
            mEventChangesLatch.await(10, TimeUnit.SECONDS);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }

    private class TestCalendarContentProvider extends MockContentProvider {
        TestCalendarContentProvider(Context context) {
            super(context);
        }

        @Override
        public Cursor query(
                Uri uri,
                String[] projection,
                Bundle queryArgs,
                CancellationSignal cancellationSignal) {
            if (uri.toString().startsWith(CalendarContract.Instances.CONTENT_URI.toString())) {
                MatrixCursor cursor =
                        new MatrixCursor(
                                new String[] {
                                    CalendarContract.Instances.TITLE,
                                    CalendarContract.Instances.ALL_DAY,
                                    CalendarContract.Instances.BEGIN,
                                    CalendarContract.Instances.END,
                                    CalendarContract.Instances.DESCRIPTION,
                                    CalendarContract.Instances.EVENT_LOCATION,
                                    CalendarContract.Instances.SELF_ATTENDEE_STATUS,
                                    CalendarContract.Instances.CALENDAR_COLOR,
                                    CalendarContract.Instances.CALENDAR_DISPLAY_NAME,
                                });
                for (Object[] row : mTestEventRows) {
                    cursor.addRow(row);
                }
                return cursor;
            } else if (uri.equals(CalendarContract.Calendars.CONTENT_URI)) {
                MatrixCursor cursor = new MatrixCursor(new String[] {" Test name"});
                cursor.addRow(new String[] {"Test value"});
                return cursor;
            }
            throw new IllegalStateException("Unexpected query uri " + uri);
        }
    }

    private Object[] buildTestRowAllDay(String title) {
        // All day events are stored in UTC time.
        ZonedDateTime utcDayStartTime =
                START_DATE_TIME.withZoneSameInstant(UTC_ZONE_ID).truncatedTo(ChronoUnit.DAYS);
        return buildTestRow(utcDayStartTime, 24, title, true);
    }

    private static Object[] buildTestRow(
            ZonedDateTime startDateTime, int eventDurationHours, String title, boolean allDay) {
        return new Object[] {
            title,
            allDay ? 1 : 0,
            startDateTime.toInstant().toEpochMilli(),
            startDateTime.plusHours(eventDurationHours).toInstant().toEpochMilli(),
            EVENT_DESCRIPTION,
            EVENT_LOCATION,
            EVENT_ATTENDEE_STATUS,
            CALENDAR_COLOR,
            CALENDAR_NAME
        };
    }
}