blob: 9c010a07135d419a6853eb949c171c2a6e0f33d3 [file] [log] [blame]
Lucas Dupin65b47652017-07-19 17:32:26 -07001/*
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;
Lucas Dupin416fe952017-08-14 10:58:40 -070020import static org.junit.Assert.assertTrue;
Lucas Dupin65b47652017-07-19 17:32:26 -070021
22import android.app.WallpaperColors;
Lucas Dupin416fe952017-08-14 10:58:40 -070023import android.os.Handler;
24import android.os.Message;
Lucas Dupin65b47652017-07-19 17:32:26 -070025import android.os.SystemClock;
26import android.service.wallpaper.WallpaperService;
Lucas Dupin416fe952017-08-14 10:58:40 -070027import android.support.test.annotation.UiThreadTest;
Lucas Dupin65b47652017-07-19 17:32:26 -070028import 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;
Lucas Dupin416fe952017-08-14 10:58:40 -070035import java.util.function.Supplier;
Lucas Dupin65b47652017-07-19 17:32:26 -070036
37@SmallTest
38@RunWith(AndroidJUnit4.class)
39public class WallpaperServiceTests {
40
Lucas Dupin416fe952017-08-14 10:58:40 -070041 @UiThreadTest
Lucas Dupin65b47652017-07-19 17:32:26 -070042 @Test
43 public void testNotifyColorsChanged_rateLimit() throws Exception {
Lucas Dupin416fe952017-08-14 10:58:40 -070044 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
Lucas Dupin65b47652017-07-19 17:32:26 -070055 CountDownLatch eventCountdown = new CountDownLatch(2);
56 WallpaperService service = new WallpaperService() {
57 @Override
58 public Engine onCreateEngine() {
Lucas Dupin416fe952017-08-14 10:58:40 -070059 return new WallpaperService.Engine(clockFunction, handler) {
Lucas Dupin65b47652017-07-19 17:32:26 -070060 @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());
Lucas Dupin416fe952017-08-14 10:58:40 -070079 // 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;
Lucas Dupin65b47652017-07-19 17:32:26 -070084 engine.notifyColorsChanged();
85 assertEquals("OnComputeColors should have been deferred.",
86 0, eventCountdown.getCount());
87 }
88}