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