• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2018 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.launcher3.util;
18 
19 /**
20  * Event tracker for reliably reproducing race conditions in tests.
21  * The app should call onEvent() for events that the test will try to reproduce in all possible
22  * orders.
23  */
24 public class RaceConditionTracker {
25     public final static boolean ENTER = true;
26     public final static boolean EXIT = false;
27     static final String ENTER_POSTFIX = "enter";
28     static final String EXIT_POSTFIX = "exit";
29 
30     public interface EventProcessor {
onEvent(String eventName)31         void onEvent(String eventName);
32     }
33 
34     private static EventProcessor sEventProcessor;
35 
setEventProcessor(EventProcessor eventProcessor)36     static void setEventProcessor(EventProcessor eventProcessor) {
37         sEventProcessor = eventProcessor;
38     }
39 
onEvent(String eventName)40     public static void onEvent(String eventName) {
41         if (sEventProcessor != null) sEventProcessor.onEvent(eventName);
42     }
43 
onEvent(String eventName, boolean isEnter)44     public static void onEvent(String eventName, boolean isEnter) {
45         if (sEventProcessor != null) {
46             sEventProcessor.onEvent(enterExitEvt(eventName, isEnter));
47         }
48     }
49 
enterExitEvt(String eventName, boolean isEnter)50     public static String enterExitEvt(String eventName, boolean isEnter) {
51         return eventName + ":" + (isEnter ? ENTER_POSTFIX : EXIT_POSTFIX);
52     }
53 
enterEvt(String eventName)54     public static String enterEvt(String eventName) {
55         return enterExitEvt(eventName, ENTER);
56     }
57 
exitEvt(String eventName)58     public static String exitEvt(String eventName) {
59         return enterExitEvt(eventName, EXIT);
60     }
61 }
62