Lucas Dupin | 65b4765 | 2017-07-19 17:32:26 -0700 | [diff] [blame] | 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; |
Lucas Dupin | 416fe95 | 2017-08-14 10:58:40 -0700 | [diff] [blame] | 20 | import static org.junit.Assert.assertTrue; |
Lucas Dupin | 65b4765 | 2017-07-19 17:32:26 -0700 | [diff] [blame] | 21 | |
| 22 | import android.app.WallpaperColors; |
Lucas Dupin | 416fe95 | 2017-08-14 10:58:40 -0700 | [diff] [blame] | 23 | import android.os.Handler; |
| 24 | import android.os.Message; |
Lucas Dupin | 65b4765 | 2017-07-19 17:32:26 -0700 | [diff] [blame] | 25 | import android.os.SystemClock; |
| 26 | import android.service.wallpaper.WallpaperService; |
Lucas Dupin | 416fe95 | 2017-08-14 10:58:40 -0700 | [diff] [blame] | 27 | import android.support.test.annotation.UiThreadTest; |
Lucas Dupin | 65b4765 | 2017-07-19 17:32:26 -0700 | [diff] [blame] | 28 | import android.support.test.filters.SmallTest; |
| 29 | import android.support.test.runner.AndroidJUnit4; |
| 30 | |
| 31 | import org.junit.Test; |
| 32 | import org.junit.runner.RunWith; |
| 33 | |
| 34 | import java.util.concurrent.CountDownLatch; |
Lucas Dupin | 416fe95 | 2017-08-14 10:58:40 -0700 | [diff] [blame] | 35 | import java.util.function.Supplier; |
Lucas Dupin | 65b4765 | 2017-07-19 17:32:26 -0700 | [diff] [blame] | 36 | |
| 37 | @SmallTest |
| 38 | @RunWith(AndroidJUnit4.class) |
| 39 | public class WallpaperServiceTests { |
| 40 | |
Lucas Dupin | 416fe95 | 2017-08-14 10:58:40 -0700 | [diff] [blame] | 41 | @UiThreadTest |
Lucas Dupin | 65b4765 | 2017-07-19 17:32:26 -0700 | [diff] [blame] | 42 | @Test |
| 43 | public void testNotifyColorsChanged_rateLimit() throws Exception { |
Lucas Dupin | 416fe95 | 2017-08-14 10:58:40 -0700 | [diff] [blame] | 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 | |
Lucas Dupin | 65b4765 | 2017-07-19 17:32:26 -0700 | [diff] [blame] | 55 | CountDownLatch eventCountdown = new CountDownLatch(2); |
| 56 | WallpaperService service = new WallpaperService() { |
| 57 | @Override |
| 58 | public Engine onCreateEngine() { |
Lucas Dupin | 416fe95 | 2017-08-14 10:58:40 -0700 | [diff] [blame] | 59 | return new WallpaperService.Engine(clockFunction, handler) { |
Lucas Dupin | 65b4765 | 2017-07-19 17:32:26 -0700 | [diff] [blame] | 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()); |
Lucas Dupin | 416fe95 | 2017-08-14 10:58:40 -0700 | [diff] [blame] | 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; |
Lucas Dupin | 65b4765 | 2017-07-19 17:32:26 -0700 | [diff] [blame] | 84 | engine.notifyColorsChanged(); |
| 85 | assertEquals("OnComputeColors should have been deferred.", |
| 86 | 0, eventCountdown.getCount()); |
| 87 | } |
| 88 | } |