summaryrefslogtreecommitdiff
path: root/espresso/espresso-lib/src/main/java/com/google/android/apps/common/testing/ui/espresso/base/EventInjector.java
blob: 07283319b150a37d088f691473c54771710c9997 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/*
 * Copyright (C) 2014 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.android.apps.common.testing.ui.espresso.base;

import static com.google.common.base.Preconditions.checkNotNull;

import com.google.android.apps.common.testing.ui.espresso.InjectEventSecurityException;

import android.os.Build;
import android.os.SystemClock;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;

/**
 * Responsible for selecting the proper strategy for injecting MotionEvents to the application under
 * test.
 */
final class EventInjector {
  private static final String TAG = EventInjector.class.getSimpleName();
  private final EventInjectionStrategy injectionStrategy;

  EventInjector(EventInjectionStrategy injectionStrategy) {
    this.injectionStrategy = checkNotNull(injectionStrategy);
  }

  boolean injectKeyEvent(KeyEvent event) throws InjectEventSecurityException {
    long downTime = event.getDownTime();
    long eventTime = event.getEventTime();
    int action = event.getAction();
    int code = event.getKeyCode();
    int repeatCount = event.getRepeatCount();
    int metaState = event.getMetaState();
    int deviceId = event.getDeviceId();
    int scancode = event.getScanCode();
    int flags = event.getFlags();

    if (eventTime == 0) {
      eventTime = SystemClock.uptimeMillis();
    }

    if (downTime == 0) {
      downTime = eventTime;
    }

    // API < 9 does not have constructor with source (nor has source field).
    KeyEvent newEvent;
    if (Build.VERSION.SDK_INT < 9) {
      newEvent = new KeyEvent(downTime,
          eventTime,
          action,
          code,
          repeatCount,
          metaState,
          deviceId,
          scancode,
          flags | KeyEvent.FLAG_FROM_SYSTEM);
    } else {
      int source = event.getSource();
      newEvent = new KeyEvent(downTime,
          eventTime,
          action,
          code,
          repeatCount,
          metaState,
          deviceId,
          scancode,
          flags | KeyEvent.FLAG_FROM_SYSTEM,
          source);
    }

    Log.v(
        "ESP_TRACE",
        String.format(
            "%s:Injecting event for character (%c) with key code (%s) downtime: (%s)", TAG,
            newEvent.getUnicodeChar(), newEvent.getKeyCode(), newEvent.getDownTime()));

    return injectionStrategy.injectKeyEvent(newEvent);
  }

  boolean injectMotionEvent(MotionEvent event) throws InjectEventSecurityException {
    return injectionStrategy.injectMotionEvent(event);
  }

}