1 /*
2  * Copyright (C) 2024 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.graphics.pdf.logging;
18 
19 import static android.graphics.pdf.PdfStatsLog.PDF_API_USAGE_REPORTED;
20 import static android.graphics.pdf.PdfStatsLog.PDF_API_USAGE_REPORTED__API_RESPONSE_STATUS__RESPONSE_FAILURE;
21 import static android.graphics.pdf.PdfStatsLog.PDF_API_USAGE_REPORTED__API_RESPONSE_STATUS__RESPONSE_SUCCESS;
22 import static android.graphics.pdf.PdfStatsLog.PDF_API_USAGE_REPORTED__API_RESPONSE_STATUS__RESPONSE_UNKNOWN;
23 import static android.graphics.pdf.PdfStatsLog.PDF_API_USAGE_REPORTED__API_TYPE__API_TYPE_SELECT_CONTENT;
24 import static android.graphics.pdf.PdfStatsLog.PDF_API_USAGE_REPORTED__API_TYPE__API_TYPE_UNKNOWN;
25 import static android.graphics.pdf.PdfStatsLog.PDF_LOAD_REPORTED;
26 import static android.graphics.pdf.PdfStatsLog.PDF_LOAD_REPORTED__LOAD_RESULT__RESULT_ERROR;
27 import static android.graphics.pdf.PdfStatsLog.PDF_LOAD_REPORTED__LOAD_RESULT__RESULT_LOADED;
28 import static android.graphics.pdf.PdfStatsLog.PDF_LOAD_REPORTED__LOAD_RESULT__RESULT_UNKNOWN;
29 import static android.graphics.pdf.PdfStatsLog.PDF_LOAD_REPORTED__LOAD_RESULT__RESULT_WRONG_PASSWORD;
30 import static android.graphics.pdf.PdfStatsLog.PDF_LOAD_REPORTED__TYPE__LINEARIZED_TYPE;
31 import static android.graphics.pdf.PdfStatsLog.PDF_LOAD_REPORTED__TYPE__NON_LINEARIZED_TYPE;
32 import static android.graphics.pdf.PdfStatsLog.PDF_LOAD_REPORTED__TYPE__UNKNOWN_TYPE;
33 import static android.graphics.pdf.PdfStatsLog.PDF_SEARCH_REPORTED;
34 import static android.graphics.pdf.PdfStatsLog.PDF_SEARCH_REPORTED__API_RESPONSE_STATUS__RESPONSE_FAILURE;
35 import static android.graphics.pdf.PdfStatsLog.PDF_SEARCH_REPORTED__API_RESPONSE_STATUS__RESPONSE_SUCCESS;
36 import static android.graphics.pdf.logging.PdfEventLogger.ApiResponseTypes.FAILURE;
37 import static android.graphics.pdf.logging.PdfEventLogger.ApiResponseTypes.SUCCESS;
38 import static android.graphics.pdf.logging.PdfEventLogger.ApiTypes.SELECT_CONTENT;
39 import static android.graphics.pdf.logging.PdfEventLogger.LinearizationTypes.LINEARIZED;
40 import static android.graphics.pdf.logging.PdfEventLogger.LinearizationTypes.NON_LINEARIZED;
41 import static android.graphics.pdf.logging.PdfEventLogger.PdfLoadResults.ERROR;
42 import static android.graphics.pdf.logging.PdfEventLogger.PdfLoadResults.LOADED;
43 import static android.graphics.pdf.logging.PdfEventLogger.PdfLoadResults.UNKNOWN;
44 import static android.graphics.pdf.logging.PdfEventLogger.PdfLoadResults.WRONG_PASSWORD;
45 
46 import static org.mockito.ArgumentMatchers.eq;
47 import static org.mockito.Mockito.times;
48 
49 import android.graphics.pdf.PdfStatsLog;
50 
51 import com.android.dx.mockito.inline.extended.ExtendedMockito;
52 import com.android.modules.utils.testing.ExtendedMockitoRule;
53 
54 import org.junit.Before;
55 import org.junit.Rule;
56 import org.junit.Test;
57 
58 public class PdfEventLoggerTest {
59     private static final long DOC_ID = 12345;
60     private static final int PROCESS_UID = 1234;
61     private static final long LOAD_DURATION_MILLIS = 123;
62     private static final float PDF_SIZE_IN_KB = 12;
63     private static final int NUM_PAGES = 1;
64     private static final int QUERY_PAGE_NUMBER = 123;
65     private static final int MATCH_COUNT = 1;
66     private static final int QUERY_LENGTH = 1;
67     @Rule
68     public final ExtendedMockitoRule mExtendedMockitoRule = new ExtendedMockitoRule.Builder(
69             this).mockStatic(PdfStatsLog.class).build();
70     private PdfEventLogger mPdfEventLogger;
71 
72     @Before
setUp()73     public void setUp() {
74         mPdfEventLogger = new PdfEventLogger(PROCESS_UID, DOC_ID);
75     }
76 
77     @Test
logPdfLoadReportedEvent_pdfLoadedSuccessfully()78     public void logPdfLoadReportedEvent_pdfLoadedSuccessfully() {
79         mPdfEventLogger.logPdfLoadReportedEvent(LOAD_DURATION_MILLIS, PDF_SIZE_IN_KB, LOADED,
80                 PdfEventLogger.LinearizationTypes.UNKNOWN, NUM_PAGES);
81 
82         // then
83         ExtendedMockito.verify(() -> PdfStatsLog.write(eq(PDF_LOAD_REPORTED), eq(PROCESS_UID),
84                 eq(LOAD_DURATION_MILLIS), eq(PDF_SIZE_IN_KB),
85                 eq(PDF_LOAD_REPORTED__LOAD_RESULT__RESULT_LOADED),
86                 eq(PDF_LOAD_REPORTED__TYPE__UNKNOWN_TYPE), eq(NUM_PAGES), eq(DOC_ID)), times(1));
87     }
88 
89     @Test
logPdfLoadReportedEvent_pdfLoadFailed()90     public void logPdfLoadReportedEvent_pdfLoadFailed() {
91         mPdfEventLogger.logPdfLoadReportedEvent(LOAD_DURATION_MILLIS, PDF_SIZE_IN_KB, ERROR,
92                 PdfEventLogger.LinearizationTypes.UNKNOWN, NUM_PAGES);
93 
94         // then
95         ExtendedMockito.verify(() -> PdfStatsLog.write(eq(PDF_LOAD_REPORTED), eq(PROCESS_UID),
96                 eq(LOAD_DURATION_MILLIS), eq(PDF_SIZE_IN_KB),
97                 eq(PDF_LOAD_REPORTED__LOAD_RESULT__RESULT_ERROR),
98                 eq(PDF_LOAD_REPORTED__TYPE__UNKNOWN_TYPE), eq(NUM_PAGES), eq(DOC_ID)), times(1));
99     }
100 
101     @Test
logPdfLoadReportedEvent_pdfLoadedWithWrongPassword()102     public void logPdfLoadReportedEvent_pdfLoadedWithWrongPassword() {
103         mPdfEventLogger.logPdfLoadReportedEvent(LOAD_DURATION_MILLIS, PDF_SIZE_IN_KB,
104                 WRONG_PASSWORD, PdfEventLogger.LinearizationTypes.UNKNOWN, NUM_PAGES);
105 
106         // then
107         ExtendedMockito.verify(() -> PdfStatsLog.write(eq(PDF_LOAD_REPORTED), eq(PROCESS_UID),
108                 eq(LOAD_DURATION_MILLIS), eq(PDF_SIZE_IN_KB),
109                 eq(PDF_LOAD_REPORTED__LOAD_RESULT__RESULT_WRONG_PASSWORD),
110                 eq(PDF_LOAD_REPORTED__TYPE__UNKNOWN_TYPE), eq(NUM_PAGES), eq(DOC_ID)), times(1));
111     }
112 
113     @Test
logPdfLoadReportedEvent_withLinearizedPdf()114     public void logPdfLoadReportedEvent_withLinearizedPdf() {
115         mPdfEventLogger.logPdfLoadReportedEvent(LOAD_DURATION_MILLIS, PDF_SIZE_IN_KB, UNKNOWN,
116                 LINEARIZED, NUM_PAGES);
117 
118         // then
119         ExtendedMockito.verify(() -> PdfStatsLog.write(eq(PDF_LOAD_REPORTED), eq(PROCESS_UID),
120                 eq(LOAD_DURATION_MILLIS), eq(PDF_SIZE_IN_KB),
121                 eq(PDF_LOAD_REPORTED__LOAD_RESULT__RESULT_UNKNOWN),
122                 eq(PDF_LOAD_REPORTED__TYPE__LINEARIZED_TYPE), eq(NUM_PAGES), eq(DOC_ID)), times(1));
123     }
124 
125     @Test
logPdfLoadReportedEvent_withNonLinearizedPdf()126     public void logPdfLoadReportedEvent_withNonLinearizedPdf() {
127         mPdfEventLogger.logPdfLoadReportedEvent(LOAD_DURATION_MILLIS, PDF_SIZE_IN_KB, UNKNOWN,
128                 NON_LINEARIZED, NUM_PAGES);
129 
130         // then
131         ExtendedMockito.verify(() -> PdfStatsLog.write(eq(PDF_LOAD_REPORTED), eq(PROCESS_UID),
132                         eq(LOAD_DURATION_MILLIS), eq(PDF_SIZE_IN_KB),
133                         eq(PDF_LOAD_REPORTED__LOAD_RESULT__RESULT_UNKNOWN),
134                         eq(PDF_LOAD_REPORTED__TYPE__NON_LINEARIZED_TYPE), eq(NUM_PAGES),
135                         eq(DOC_ID)),
136                 times(1));
137     }
138 
139     @Test
logPdfSearchReportedEvent_pdfSearchSuccessful()140     public void logPdfSearchReportedEvent_pdfSearchSuccessful() {
141         mPdfEventLogger.logSearchReportedEvent(LOAD_DURATION_MILLIS, QUERY_LENGTH,
142                 QUERY_PAGE_NUMBER, SUCCESS, NUM_PAGES, MATCH_COUNT);
143 
144         // then
145         ExtendedMockito.verify(() -> PdfStatsLog.write(eq(PDF_SEARCH_REPORTED), eq(PROCESS_UID),
146                 eq(LOAD_DURATION_MILLIS), eq(QUERY_LENGTH), eq(QUERY_PAGE_NUMBER),
147                 eq(PDF_SEARCH_REPORTED__API_RESPONSE_STATUS__RESPONSE_SUCCESS), eq(DOC_ID),
148                 eq(NUM_PAGES), eq(MATCH_COUNT)), times(1));
149     }
150 
151     @Test
logPdfSearchReportedEvent_pdfSearchFailed()152     public void logPdfSearchReportedEvent_pdfSearchFailed() {
153         mPdfEventLogger.logSearchReportedEvent(LOAD_DURATION_MILLIS, QUERY_LENGTH,
154                 QUERY_PAGE_NUMBER, FAILURE, NUM_PAGES, MATCH_COUNT);
155 
156         // then
157         ExtendedMockito.verify(() -> PdfStatsLog.write(eq(PDF_SEARCH_REPORTED), eq(PROCESS_UID),
158                 eq(LOAD_DURATION_MILLIS), eq(QUERY_LENGTH), eq(QUERY_PAGE_NUMBER),
159                 eq(PDF_SEARCH_REPORTED__API_RESPONSE_STATUS__RESPONSE_FAILURE), eq(DOC_ID),
160                 eq(NUM_PAGES), eq(MATCH_COUNT)), times(1));
161     }
162 
163     @Test
logPdfApiUsageReportedEvent_selectContent_withSuccess()164     public void logPdfApiUsageReportedEvent_selectContent_withSuccess() {
165         mPdfEventLogger.logPdfApiUsageReportedEvent(SELECT_CONTENT, SUCCESS);
166 
167         // then
168         ExtendedMockito.verify(
169                 () -> PdfStatsLog.write(eq(PDF_API_USAGE_REPORTED), eq(PROCESS_UID), eq(DOC_ID),
170                         eq(PDF_API_USAGE_REPORTED__API_TYPE__API_TYPE_SELECT_CONTENT),
171                         eq(PDF_API_USAGE_REPORTED__API_RESPONSE_STATUS__RESPONSE_SUCCESS)),
172                 times(1));
173     }
174 
175     @Test
logPdfApiUsageReportedEvent_selectContent_withFailure()176     public void logPdfApiUsageReportedEvent_selectContent_withFailure() {
177         mPdfEventLogger.logPdfApiUsageReportedEvent(SELECT_CONTENT, FAILURE);
178 
179         // then
180         ExtendedMockito.verify(
181                 () -> PdfStatsLog.write(eq(PDF_API_USAGE_REPORTED), eq(PROCESS_UID), eq(DOC_ID),
182                         eq(PDF_API_USAGE_REPORTED__API_TYPE__API_TYPE_SELECT_CONTENT),
183                         eq(PDF_API_USAGE_REPORTED__API_RESPONSE_STATUS__RESPONSE_FAILURE)),
184                 times(1));
185     }
186 
187     @Test
logPdfApiUsageReportedEvent_withUnknownAPI()188     public void logPdfApiUsageReportedEvent_withUnknownAPI() {
189         mPdfEventLogger.logPdfApiUsageReportedEvent(PdfEventLogger.ApiTypes.UNKNOWN,
190                 PdfEventLogger.ApiResponseTypes.UNKNOWN);
191 
192         // then
193         ExtendedMockito.verify(
194                 () -> PdfStatsLog.write(eq(PDF_API_USAGE_REPORTED), eq(PROCESS_UID), eq(DOC_ID),
195                         eq(PDF_API_USAGE_REPORTED__API_TYPE__API_TYPE_UNKNOWN),
196                         eq(PDF_API_USAGE_REPORTED__API_RESPONSE_STATUS__RESPONSE_UNKNOWN)),
197                 times(1));
198     }
199 }
200