1 /* 2 * Copyright (C) 2013 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.inputmethod.research; 18 19 import android.test.AndroidTestCase; 20 import android.test.suitebuilder.annotation.SmallTest; 21 import android.util.JsonReader; 22 23 import com.android.inputmethod.research.MotionEventReader.ReplayData; 24 25 import java.io.IOException; 26 import java.io.StringReader; 27 28 @SmallTest 29 public class MotionEventReaderTests extends AndroidTestCase { 30 private MotionEventReader mMotionEventReader = new MotionEventReader(); 31 private ReplayData mReplayData; 32 33 @Override 34 protected void setUp() throws Exception { 35 super.setUp(); 36 mReplayData = new ReplayData(); 37 } 38 39 private JsonReader jsonReaderForString(final String s) { 40 return new JsonReader(new StringReader(s)); 41 } 42 43 public void testTopLevelDataVariant() { 44 final JsonReader jsonReader = jsonReaderForString( 45 "{" 46 + "\"_ct\": 1359590400000," 47 + "\"_ut\": 4381933," 48 + "\"_ty\": \"MotionEvent\"," 49 + "\"action\": \"UP\"," 50 + "\"isLoggingRelated\": false," 51 + "\"x\": 100.0," 52 + "\"y\": 200.0" 53 + "}" 54 ); 55 try { 56 mMotionEventReader.readLogStatement(jsonReader, mReplayData); 57 } catch (IOException e) { 58 e.printStackTrace(); 59 fail("IOException thrown"); 60 } 61 assertEquals("x set correctly", (int) mReplayData.mPointerCoordsArrays.get(0)[0].x, 100); 62 assertEquals("y set correctly", (int) mReplayData.mPointerCoordsArrays.get(0)[0].y, 200); 63 assertEquals("only one pointer", mReplayData.mPointerCoordsArrays.get(0).length, 1); 64 assertEquals("only one MotionEvent", mReplayData.mPointerCoordsArrays.size(), 1); 65 } 66 67 public void testNestedDataVariant() { 68 final JsonReader jsonReader = jsonReaderForString( 69 "{" 70 + " \"_ct\": 135959040000," 71 + " \"_ut\": 4382702," 72 + " \"_ty\": \"MotionEvent\"," 73 + " \"action\": \"MOVE\"," 74 + " \"isLoggingRelated\": false," 75 + " \"motionEvent\": {" 76 + " \"pointerIds\": [" 77 + " 0" 78 + " ]," 79 + " \"xyt\": [" 80 + " {" 81 + " \"t\": 4382551," 82 + " \"d\": [" 83 + " {" 84 + " \"x\": 100.0," 85 + " \"y\": 200.0," 86 + " \"toma\": 999.0," 87 + " \"tomi\": 999.0," 88 + " \"o\": 0.0" 89 + " }" 90 + " ]" 91 + " }," 92 + " {" 93 + " \"t\": 4382559," 94 + " \"d\": [" 95 + " {" 96 + " \"x\": 300.0," 97 + " \"y\": 400.0," 98 + " \"toma\": 999.0," 99 + " \"tomi\": 999.0," 100 + " \"o\": 0.0" 101 + " }" 102 + " ]" 103 + " }" 104 + " ]" 105 + " }" 106 + "}" 107 ); 108 try { 109 mMotionEventReader.readLogStatement(jsonReader, mReplayData); 110 } catch (IOException e) { 111 e.printStackTrace(); 112 fail("IOException thrown"); 113 } 114 assertEquals("x1 set correctly", (int) mReplayData.mPointerCoordsArrays.get(0)[0].x, 100); 115 assertEquals("y1 set correctly", (int) mReplayData.mPointerCoordsArrays.get(0)[0].y, 200); 116 assertEquals("x2 set correctly", (int) mReplayData.mPointerCoordsArrays.get(1)[0].x, 300); 117 assertEquals("y2 set correctly", (int) mReplayData.mPointerCoordsArrays.get(1)[0].y, 400); 118 assertEquals("only one pointer", mReplayData.mPointerCoordsArrays.get(0).length, 1); 119 assertEquals("two MotionEvents", mReplayData.mPointerCoordsArrays.size(), 2); 120 } 121 122 public void testNestedDataVariantMultiPointer() { 123 final JsonReader jsonReader = jsonReaderForString( 124 "{" 125 + " \"_ct\": 135959040000," 126 + " \"_ut\": 4382702," 127 + " \"_ty\": \"MotionEvent\"," 128 + " \"action\": \"MOVE\"," 129 + " \"isLoggingRelated\": false," 130 + " \"motionEvent\": {" 131 + " \"pointerIds\": [" 132 + " 1" 133 + " ]," 134 + " \"xyt\": [" 135 + " {" 136 + " \"t\": 4382551," 137 + " \"d\": [" 138 + " {" 139 + " \"x\": 100.0," 140 + " \"y\": 200.0," 141 + " \"toma\": 999.0," 142 + " \"tomi\": 999.0," 143 + " \"o\": 0.0" 144 + " }," 145 + " {" 146 + " \"x\": 300.0," 147 + " \"y\": 400.0," 148 + " \"toma\": 999.0," 149 + " \"tomi\": 999.0," 150 + " \"o\": 0.0" 151 + " }" 152 + " ]" 153 + " }" 154 + " ]" 155 + " }" 156 + "}" 157 ); 158 try { 159 mMotionEventReader.readLogStatement(jsonReader, mReplayData); 160 } catch (IOException e) { 161 e.printStackTrace(); 162 fail("IOException thrown"); 163 } 164 assertEquals("x1 set correctly", (int) mReplayData.mPointerCoordsArrays.get(0)[0].x, 100); 165 assertEquals("y1 set correctly", (int) mReplayData.mPointerCoordsArrays.get(0)[0].y, 200); 166 assertEquals("x2 set correctly", (int) mReplayData.mPointerCoordsArrays.get(0)[1].x, 300); 167 assertEquals("y2 set correctly", (int) mReplayData.mPointerCoordsArrays.get(0)[1].y, 400); 168 assertEquals("two pointers", mReplayData.mPointerCoordsArrays.get(0).length, 2); 169 assertEquals("one MotionEvent", mReplayData.mPointerCoordsArrays.size(), 1); 170 } 171 } 172