FileObserverTest.java revision 1a44d5dcabc18cd5ef111f732ccff91683a1a093
1/* 2 * Copyright (C) 2006 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 17package android.os; 18 19import com.google.android.collect.Lists; 20import com.google.android.collect.Maps; 21 22import android.os.FileObserver; 23import android.test.AndroidTestCase; 24import android.test.suitebuilder.annotation.LargeTest; 25import android.util.Log; 26 27import java.io.File; 28import java.io.FileOutputStream; 29import java.util.Iterator; 30import java.util.List; 31import java.util.Map; 32 33public class FileObserverTest extends AndroidTestCase { 34 private Observer mObserver; 35 private File mTestFile; 36 37 private static class Observer extends FileObserver { 38 public List<Map> events = Lists.newArrayList(); 39 public int totalEvents = 0; 40 41 public Observer(String path) { 42 super(path); 43 } 44 45 public void onEvent(int event, String path) { 46 synchronized (this) { 47 totalEvents++; 48 Map<String, Object> map = Maps.newHashMap(); 49 50 map.put("event", event); 51 map.put("path", path); 52 53 events.add(map); 54 55 this.notifyAll(); 56 } 57 } 58 } 59 60 @Override 61 protected void setUp() throws Exception { 62 mTestFile = File.createTempFile(".file_observer_test", ".txt"); 63 } 64 65 @Override 66 protected void tearDown() throws Exception { 67 if (mTestFile != null && mTestFile.exists()) { 68 mTestFile.delete(); 69 } 70 } 71 72 @LargeTest 73 public void testRun() throws Exception { 74 // make file changes and wait for them 75 assertTrue(mTestFile.exists()); 76 assertNotNull(mTestFile.getParent()); 77 78 mObserver = new Observer(mTestFile.getParent()); 79 mObserver.startWatching(); 80 81 FileOutputStream out = new FileOutputStream(mTestFile); 82 try { 83 out.write(0x20); 84 waitForEvent(); // open 85 waitForEvent(); // modify 86 87 mTestFile.delete(); 88 waitForEvent(); // modify 89 waitForEvent(); // delete 90 91 mObserver.stopWatching(); 92 93 // Ensure that we have seen at least 3 events. 94 assertTrue(mObserver.totalEvents > 3); 95 } finally { 96 out.close(); 97 } 98 } 99 100 private void waitForEvent() { 101 synchronized (mObserver) { 102 boolean done = false; 103 while (!done) { 104 try { 105 mObserver.wait(2000); 106 done = true; 107 } catch (InterruptedException e) { 108 } 109 } 110 111 Iterator<Map> it = mObserver.events.iterator(); 112 113 while (it.hasNext()) { 114 Map map = it.next(); 115 Log.i("FileObserverTest", "event: " + getEventString((Integer)map.get("event")) + " path: " + map.get("path")); 116 } 117 118 mObserver.events.clear(); 119 } 120 } 121 122 private String getEventString(int event) { 123 switch (event) { 124 case FileObserver.ACCESS: 125 return "ACCESS"; 126 case FileObserver.MODIFY: 127 return "MODIFY"; 128 case FileObserver.ATTRIB: 129 return "ATTRIB"; 130 case FileObserver.CLOSE_WRITE: 131 return "CLOSE_WRITE"; 132 case FileObserver.CLOSE_NOWRITE: 133 return "CLOSE_NOWRITE"; 134 case FileObserver.OPEN: 135 return "OPEN"; 136 case FileObserver.MOVED_FROM: 137 return "MOVED_FROM"; 138 case FileObserver.MOVED_TO: 139 return "MOVED_TO"; 140 case FileObserver.CREATE: 141 return "CREATE"; 142 case FileObserver.DELETE: 143 return "DELETE"; 144 case FileObserver.DELETE_SELF: 145 return "DELETE_SELF"; 146 case FileObserver.MOVE_SELF: 147 return "MOVE_SELF"; 148 default: 149 return "UNKNOWN"; 150 } 151 } 152} 153