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 android.ext.services.notification;
18 
19 import static android.ext.services.notification.ChannelImpressions.DEFAULT_DISMISS_TO_VIEW_RATIO_LIMIT;
20 import static android.ext.services.notification.ChannelImpressions.DEFAULT_STREAK_LIMIT;
21 
22 import static junit.framework.Assert.assertFalse;
23 import static junit.framework.Assert.assertTrue;
24 
25 import static org.junit.Assert.assertEquals;
26 
27 import org.junit.Test;
28 
29 public class ChannelImpressionsTest {
30 
31     @Test
testNoResultNoBlock()32     public void testNoResultNoBlock() {
33         ChannelImpressions ci = new ChannelImpressions();
34         assertFalse(ci.shouldTriggerBlock());
35     }
36 
37     @Test
testNoStreakNoBlock()38     public void testNoStreakNoBlock() {
39         ChannelImpressions ci = new ChannelImpressions();
40 
41         for (int i = 0; i < DEFAULT_STREAK_LIMIT - 1; i++) {
42             ci.incrementViews();
43             ci.incrementDismissals();
44         }
45 
46         assertFalse(ci.shouldTriggerBlock());
47     }
48 
49     @Test
testNoStreakNoBlock_breakStreak()50     public void testNoStreakNoBlock_breakStreak() {
51         ChannelImpressions ci = new ChannelImpressions();
52 
53         for (int i = 0; i < DEFAULT_STREAK_LIMIT; i++) {
54             ci.incrementViews();
55             ci.incrementDismissals();
56             if (i == DEFAULT_STREAK_LIMIT - 1) {
57                 ci.resetStreak();
58             }
59         }
60 
61         assertFalse(ci.shouldTriggerBlock());
62     }
63 
64     @Test
testStreakBlock()65     public void testStreakBlock() {
66         ChannelImpressions ci = new ChannelImpressions();
67 
68         for (int i = 0; i <= DEFAULT_STREAK_LIMIT; i++) {
69             ci.incrementViews();
70             ci.incrementDismissals();
71         }
72 
73         assertTrue(ci.shouldTriggerBlock());
74     }
75 
76     @Test
testRatio_NoBlockEvenWithStreak()77     public void testRatio_NoBlockEvenWithStreak() {
78         ChannelImpressions ci = new ChannelImpressions();
79 
80         for (int i = 0; i < DEFAULT_STREAK_LIMIT; i++) {
81             ci.incrementViews();
82             ci.incrementDismissals();
83             ci.incrementViews();
84         }
85 
86         assertFalse(ci.shouldTriggerBlock());
87     }
88 
89     @Test
testAppend()90     public void testAppend() {
91         ChannelImpressions ci = new ChannelImpressions();
92         ci.incrementViews();
93         ci.incrementDismissals();
94 
95         ChannelImpressions ci2 = new ChannelImpressions();
96         ci2.incrementViews();
97         ci2.incrementDismissals();
98         ci2.incrementViews();
99 
100         ci.append(ci2);
101         assertEquals(3, ci.getViews());
102         assertEquals(2, ci.getDismissals());
103         assertEquals(2, ci.getStreak());
104 
105         assertEquals(2, ci2.getViews());
106         assertEquals(1, ci2.getDismissals());
107         assertEquals(1, ci2.getStreak());
108 
109         // no crash
110         ci.append(null);
111     }
112 
113     @Test
testUpdateThresholds_streakLimitsCorrectlyApplied()114     public void testUpdateThresholds_streakLimitsCorrectlyApplied() {
115         int updatedStreakLimit = DEFAULT_STREAK_LIMIT + 3;
116         ChannelImpressions ci = new ChannelImpressions();
117         ci.updateThresholds(DEFAULT_DISMISS_TO_VIEW_RATIO_LIMIT, updatedStreakLimit);
118 
119         for (int i = 0; i <= updatedStreakLimit; i++) {
120             ci.incrementViews();
121             ci.incrementDismissals();
122         }
123 
124         ChannelImpressions ci2 = new ChannelImpressions();
125         ci2.updateThresholds(DEFAULT_DISMISS_TO_VIEW_RATIO_LIMIT, updatedStreakLimit);
126 
127         for (int i = 0; i < updatedStreakLimit; i++) {
128             ci2.incrementViews();
129             ci2.incrementDismissals();
130         }
131 
132         assertTrue(ci.shouldTriggerBlock());
133         assertFalse(ci2.shouldTriggerBlock());
134     }
135 
136     @Test
testUpdateThresholds_ratioLimitsCorrectlyApplied()137     public void testUpdateThresholds_ratioLimitsCorrectlyApplied() {
138         float updatedDismissRatio = .99f;
139         ChannelImpressions ci = new ChannelImpressions();
140         ci.updateThresholds(updatedDismissRatio, DEFAULT_STREAK_LIMIT);
141 
142         // N views, N-1 dismissals, which doesn't satisfy the ratio = 1 criteria.
143         for (int i = 0; i <= DEFAULT_STREAK_LIMIT; i++) {
144             ci.incrementViews();
145             if (i != DEFAULT_STREAK_LIMIT) {
146                 ci.incrementDismissals();
147             }
148         }
149 
150         ChannelImpressions ci2 = new ChannelImpressions();
151         ci2.updateThresholds(updatedDismissRatio, DEFAULT_STREAK_LIMIT);
152 
153         for (int i = 0; i <= DEFAULT_STREAK_LIMIT; i++) {
154             ci2.incrementViews();
155             ci2.incrementDismissals();
156         }
157 
158         assertFalse(ci.shouldTriggerBlock());
159         assertTrue(ci2.shouldTriggerBlock());
160     }
161 }
162