blob: 8607ec66a5ba6680152ba8484614d0e0915ed66a [file] [log] [blame]
Nick Chalko7206f3b2018-06-19 10:17:03 -07001/*
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 */
16package com.android.server.hdmi;
17
18import android.hardware.hdmi.HdmiPortInfo;
19import android.hardware.tv.cec.V1_0.SendMessageResult;
20import android.os.MessageQueue;
Nick Chalko90d44322018-07-27 08:35:12 -070021
Nick Chalko10e52c92018-07-25 16:07:16 -070022import com.android.internal.annotations.VisibleForTesting;
Nick Chalko7206f3b2018-06-19 10:17:03 -070023import com.android.server.hdmi.HdmiCecController.NativeWrapper;
Nick Chalko90d44322018-07-27 08:35:12 -070024
25import com.google.common.collect.Iterables;
26
Shubangeff3c882018-07-19 16:00:29 -070027import java.util.ArrayList;
28import java.util.List;
Nick Chalko7206f3b2018-06-19 10:17:03 -070029
30/** Fake {@link NativeWrapper} useful for testing. */
31final class FakeNativeWrapper implements NativeWrapper {
32 private final int[] mPollAddressResponse =
33 new int[] {
34 SendMessageResult.NACK,
35 SendMessageResult.NACK,
36 SendMessageResult.NACK,
37 SendMessageResult.NACK,
38 SendMessageResult.NACK,
39 SendMessageResult.NACK,
40 SendMessageResult.NACK,
41 SendMessageResult.NACK,
42 SendMessageResult.NACK,
43 SendMessageResult.NACK,
44 SendMessageResult.NACK,
45 SendMessageResult.NACK,
46 SendMessageResult.NACK,
47 SendMessageResult.NACK,
48 SendMessageResult.NACK,
49 };
50
Shubangeff3c882018-07-19 16:00:29 -070051 private final List<HdmiCecMessage> mResultMessages = new ArrayList<>();
Amyac1a55c2018-07-13 16:38:23 -070052 private int mMyPhysicalAddress = 0;
Amya00e1182018-10-17 16:45:03 -070053 private HdmiPortInfo[] mHdmiPortInfo = null;
Nick Chalko7206f3b2018-06-19 10:17:03 -070054
55 @Override
56 public long nativeInit(HdmiCecController handler, MessageQueue messageQueue) {
57 return 1L;
58 }
59
60 @Override
61 public int nativeSendCecCommand(
62 long controllerPtr, int srcAddress, int dstAddress, byte[] body) {
63 if (body.length == 0) {
64 return mPollAddressResponse[dstAddress];
65 } else {
Shubangeff3c882018-07-19 16:00:29 -070066 mResultMessages.add(HdmiCecMessageBuilder.of(srcAddress, dstAddress, body));
Nick Chalko7206f3b2018-06-19 10:17:03 -070067 }
68 return SendMessageResult.SUCCESS;
69 }
70
71 @Override
72 public int nativeAddLogicalAddress(long controllerPtr, int logicalAddress) {
73 return 0;
74 }
75
76 @Override
77 public void nativeClearLogicalAddress(long controllerPtr) {}
78
79 @Override
80 public int nativeGetPhysicalAddress(long controllerPtr) {
Amyac1a55c2018-07-13 16:38:23 -070081 return mMyPhysicalAddress;
Nick Chalko7206f3b2018-06-19 10:17:03 -070082 }
83
84 @Override
85 public int nativeGetVersion(long controllerPtr) {
86 return 0;
87 }
88
89 @Override
90 public int nativeGetVendorId(long controllerPtr) {
91 return 0;
92 }
93
94 @Override
95 public HdmiPortInfo[] nativeGetPortInfos(long controllerPtr) {
Amya00e1182018-10-17 16:45:03 -070096 if (mHdmiPortInfo == null) {
97 mHdmiPortInfo = new HdmiPortInfo[1];
98 mHdmiPortInfo[0] = new HdmiPortInfo(1, 1, 0x1000, true, true, true);
99 }
100 return mHdmiPortInfo;
Nick Chalko7206f3b2018-06-19 10:17:03 -0700101 }
102
103 @Override
104 public void nativeSetOption(long controllerPtr, int flag, boolean enabled) {}
105
106 @Override
107 public void nativeSetLanguage(long controllerPtr, String language) {}
108
109 @Override
110 public void nativeEnableAudioReturnChannel(long controllerPtr, int port, boolean flag) {}
111
112 @Override
113 public boolean nativeIsConnected(long controllerPtr, int port) {
114 return false;
115 }
116
Shubangeff3c882018-07-19 16:00:29 -0700117 public List<HdmiCecMessage> getResultMessages() {
118 return new ArrayList<>(mResultMessages);
119 }
120
Nick Chalko90d44322018-07-27 08:35:12 -0700121 public HdmiCecMessage getOnlyResultMessage() throws IllegalArgumentException {
122 return Iterables.getOnlyElement(mResultMessages);
123 }
124
125 public void clearResultMessages() {
126 mResultMessages.clear();
Nick Chalko7206f3b2018-06-19 10:17:03 -0700127 }
128
129 public void setPollAddressResponse(int logicalAddress, int response) {
130 mPollAddressResponse[logicalAddress] = response;
131 }
Amyac1a55c2018-07-13 16:38:23 -0700132
133 @VisibleForTesting
134 protected void setPhysicalAddress(int physicalAddress) {
135 mMyPhysicalAddress = physicalAddress;
136 }
Amya00e1182018-10-17 16:45:03 -0700137
138 @VisibleForTesting
139 protected void setPortInfo(HdmiPortInfo[] hdmiPortInfo) {
140 mHdmiPortInfo = new HdmiPortInfo[hdmiPortInfo.length];
141 System.arraycopy(hdmiPortInfo, 0, mHdmiPortInfo, 0, hdmiPortInfo.length);
142 }
Nick Chalko7206f3b2018-06-19 10:17:03 -0700143}