1 /*
2  * Copyright (C) 2020 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.utils;
18 
19 import static org.junit.Assert.assertEquals;
20 import static org.junit.Assert.assertTrue;
21 
22 /**
23  * A class to count the number of notifications received.
24  */
25 public class WatchableTester extends Watcher {
26 
27     // The count of changes.
28     public int mChanges = 0;
29 
30     // The change count at the last verifyChangeReported() call.
31     public int mLastChangeCount = 0;
32 
33     // The single Watchable that this monitors.
34     public final Watchable mWatched;
35 
36     // The key, used for messages
37     public String mKey;
38 
39     // Clear the changes count, for when the tester is reused.
clear()40     public void clear() {
41         mChanges = 0;
42     }
43 
44     /**
45      * Create the WatchableTester with a Watcher and a key.  The key is used for logging
46      * test failures.
47      * @param w The {@link Watchable} under test
48      * @param k A key that is prefixed to any test failures.
49      **/
WatchableTester(Watchable w, String k)50     public WatchableTester(Watchable w, String k) {
51         mWatched = w;
52         mKey = k;
53     }
54 
55     // Listen for events
register()56     public void register() {
57         mWatched.registerObserver(this);
58     }
59 
60     // Stop listening for events
unregister()61     public void unregister() {
62         mWatched.unregisterObserver(this);
63     }
64 
65     // Count the number of notifications received.
66     @Override
onChange(Watchable what)67     public void onChange(Watchable what) {
68         mChanges++;
69     }
70 
71     // Verify the count.
verify(int want, String msg)72     public void verify(int want, String msg) {
73         assertEquals(mKey + " " + msg, want, mChanges);
74     }
75 
76     // Verify that at least one change was reported since the last verify.  The actual
77     // number of changes is not important.  This resets the count of changes.
verifyChangeReported(String msg)78     public void verifyChangeReported(String msg) {
79         assertTrue(mKey + " " + msg, mLastChangeCount < mChanges);
80         mLastChangeCount = mChanges;
81     }
82 
83     // Verify that no change was reported since the last verify.
verifyNoChangeReported(String msg)84     public void verifyNoChangeReported(String msg) {
85         assertTrue(mKey + " " + msg, mLastChangeCount == mChanges);
86     }
87 }
88