1/*
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 com.android.server.wallpaper;
18
19import static org.junit.Assert.assertEquals;
20import static org.junit.Assert.assertTrue;
21
22import android.app.WallpaperColors;
23import android.os.Handler;
24import android.os.Message;
25import android.os.SystemClock;
26import android.service.wallpaper.WallpaperService;
27import android.support.test.annotation.UiThreadTest;
28import android.support.test.filters.SmallTest;
29import android.support.test.runner.AndroidJUnit4;
30
31import org.junit.Test;
32import org.junit.runner.RunWith;
33
34import java.util.concurrent.CountDownLatch;
35import java.util.function.Supplier;
36
37@SmallTest
38@RunWith(AndroidJUnit4.class)
39public class WallpaperServiceTests {
40
41    @UiThreadTest
42    @Test
43    public void testNotifyColorsChanged_rateLimit() throws Exception {
44        long[] clockOffset = {0};
45        boolean[] postDelayed = {false};
46        Supplier<Long> clockFunction = () -> SystemClock.elapsedRealtime() + clockOffset[0];
47        Handler handler = new Handler() {
48            @Override
49            public boolean sendMessageAtTime(Message msg, long uptimeMillis) {
50                postDelayed[0] = true;
51                return super.sendMessageAtTime(msg, uptimeMillis);
52            }
53        };
54
55        CountDownLatch eventCountdown = new CountDownLatch(2);
56        WallpaperService service = new WallpaperService() {
57            @Override
58            public Engine onCreateEngine() {
59                return new WallpaperService.Engine(clockFunction, handler) {
60                    @Override
61                    public WallpaperColors onComputeColors() {
62                        eventCountdown.countDown();
63                        return null;
64                    }
65                };
66            }
67        };
68        WallpaperService.Engine engine = service.onCreateEngine();
69
70        // Called because it's the first time.
71        engine.notifyColorsChanged();
72        assertEquals("OnComputeColors should have been called.",
73                1, eventCountdown.getCount());
74
75        // Ignored since the call should be throttled.
76        engine.notifyColorsChanged();
77        assertEquals("OnComputeColors should have been throttled.",
78                1, eventCountdown.getCount());
79        // Should have been posted to the handler.
80        assertTrue("Event should have been delayed", postDelayed[0]);
81
82        // Called again after being deferred.
83        clockOffset[0] = 1500;
84        engine.notifyColorsChanged();
85        assertEquals("OnComputeColors should have been deferred.",
86                0, eventCountdown.getCount());
87    }
88}
89