├── README.md ├── res ├── drawable │ └── testpic_1.jpg ├── drawable-hdpi │ └── ic_launcher.png ├── drawable-ldpi │ └── ic_launcher.png ├── drawable-mdpi │ └── ic_launcher.png ├── drawable-xhdpi │ └── ic_launcher.png ├── values │ └── strings.xml └── layout │ └── main.xml ├── .gitignore ├── AndroidManifest.xml └── src └── de └── raptor2101 └── GalDroid └── Testing └── ComponentTest ├── Activities ├── TaskHelper.java ├── TestImplementations │ ├── TestGalleryObjectComment.java │ ├── TestDownloadObject.java │ ├── TestGalleryObject.java │ └── TestWebGallery.java └── ImageViewActivityTest.java └── Stubs └── Listener └── GalleryProgressListener.java /README.md: -------------------------------------------------------------------------------- 1 | GalDroidTest 2 | ============ 3 | 4 | The TestingProject for GalDroid -------------------------------------------------------------------------------- /res/drawable/testpic_1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/raptor2101/GalDroidTest/master/res/drawable/testpic_1.jpg -------------------------------------------------------------------------------- /res/drawable-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/raptor2101/GalDroidTest/master/res/drawable-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /res/drawable-ldpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/raptor2101/GalDroidTest/master/res/drawable-ldpi/ic_launcher.png -------------------------------------------------------------------------------- /res/drawable-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/raptor2101/GalDroidTest/master/res/drawable-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /res/drawable-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/raptor2101/GalDroidTest/master/res/drawable-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Hello World! 5 | GalDroidTestProjectTest 6 | 7 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # built application files 2 | *.apk 3 | *.ap_ 4 | 5 | # files for the dex VM 6 | *.dex 7 | 8 | # Java class files 9 | *.class 10 | 11 | # generated files 12 | bin/ 13 | gen/ 14 | 15 | # Local configuration file (sdk path, etc) 16 | local.properties 17 | -------------------------------------------------------------------------------- /res/layout/main.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 11 | 12 | -------------------------------------------------------------------------------- /AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 8 | 9 | 10 | 11 | 14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /src/de/raptor2101/GalDroid/Testing/ComponentTest/Activities/TaskHelper.java: -------------------------------------------------------------------------------- 1 | package de.raptor2101.GalDroid.Testing.ComponentTest.Activities; 2 | 3 | import junit.framework.Assert; 4 | 5 | public abstract class TaskHelper { 6 | public static final int MAX_WAIT_TIME = 20000; 7 | 8 | public void waitForExecution(String assertMessage) throws InterruptedException { 9 | long startTime = System.currentTimeMillis(); 10 | long timeElapsed = 0; 11 | do { 12 | if (checkCondition(timeElapsed)) { 13 | return; 14 | } 15 | 16 | Thread.sleep(100); 17 | timeElapsed = System.currentTimeMillis() - startTime; 18 | } while (timeElapsed < MAX_WAIT_TIME); 19 | 20 | Assert.fail(assertMessage); 21 | } 22 | 23 | protected abstract boolean checkCondition(long timeElapsed); 24 | } 25 | -------------------------------------------------------------------------------- /src/de/raptor2101/GalDroid/Testing/ComponentTest/Stubs/Listener/GalleryProgressListener.java: -------------------------------------------------------------------------------- 1 | package de.raptor2101.GalDroid.Testing.ComponentTest.Stubs.Listener; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | import junit.framework.Assert; 7 | 8 | public class GalleryProgressListener implements de.raptor2101.GalDroid.WebGallery.Interfaces.GalleryProgressListener { 9 | public class HandleProgressCall { 10 | public final int curValue; 11 | public final int maxValue; 12 | 13 | public HandleProgressCall(int curValue, int maxValue) { 14 | this.curValue = curValue; 15 | this.maxValue = maxValue; 16 | } 17 | }; 18 | 19 | private ArrayList calls = new ArrayList(10); 20 | 21 | public List getHandleProgressCalls() { 22 | return calls; 23 | } 24 | 25 | public void reset() { 26 | calls.clear(); 27 | } 28 | 29 | @Override 30 | public void handleProgress(int curValue, int maxValue) { 31 | Assert.assertTrue("CurValue greater MaxValue", curValue <= maxValue); 32 | calls.add(new HandleProgressCall(curValue, maxValue)); 33 | } 34 | 35 | } 36 | -------------------------------------------------------------------------------- /src/de/raptor2101/GalDroid/Testing/ComponentTest/Activities/TestImplementations/TestGalleryObjectComment.java: -------------------------------------------------------------------------------- 1 | package de.raptor2101.GalDroid.Testing.ComponentTest.Activities.TestImplementations; 2 | 3 | import java.util.Date; 4 | 5 | import de.raptor2101.GalDroid.WebGallery.Interfaces.GalleryObjectComment; 6 | 7 | public class TestGalleryObjectComment implements GalleryObjectComment { 8 | 9 | private final Date mCreateDate; 10 | private final String mMessage; 11 | private final String mAuthor; 12 | 13 | public TestGalleryObjectComment(String author, String message) { 14 | mCreateDate = new Date(System.currentTimeMillis()); 15 | mMessage = message; 16 | mAuthor = author; 17 | } 18 | 19 | @Override 20 | public Date getCreateDate() { 21 | return mCreateDate; 22 | } 23 | 24 | @Override 25 | public Date getUpdateDate() { 26 | return mCreateDate; 27 | } 28 | 29 | @Override 30 | public String getMessage() { 31 | return mMessage; 32 | } 33 | 34 | @Override 35 | public String getAuthorEmail() { 36 | return mAuthor; 37 | } 38 | 39 | @Override 40 | public String getAuthorName() { 41 | return ""; 42 | } 43 | 44 | @Override 45 | public String getAuthorUrl() { 46 | return ""; 47 | } 48 | 49 | } 50 | -------------------------------------------------------------------------------- /src/de/raptor2101/GalDroid/Testing/ComponentTest/Activities/TestImplementations/TestDownloadObject.java: -------------------------------------------------------------------------------- 1 | package de.raptor2101.GalDroid.Testing.ComponentTest.Activities.TestImplementations; 2 | 3 | import de.raptor2101.GalDroid.WebGallery.Interfaces.GalleryDownloadObject; 4 | 5 | public class TestDownloadObject implements GalleryDownloadObject { 6 | @Override 7 | public int hashCode() { 8 | final int prime = 31; 9 | int result = 1; 10 | result = prime * result + ((mObjectId == null) ? 0 : mObjectId.hashCode()); 11 | result = prime * result + ((mRequestedSize == null) ? 0 : mRequestedSize.hashCode()); 12 | return result; 13 | } 14 | 15 | @Override 16 | public boolean equals(Object obj) { 17 | if (this == obj) 18 | return true; 19 | if (obj == null) 20 | return false; 21 | if (getClass() != obj.getClass()) 22 | return false; 23 | TestDownloadObject other = (TestDownloadObject) obj; 24 | if (mObjectId == null) { 25 | if (other.mObjectId != null) 26 | return false; 27 | } else if (!mObjectId.equals(other.mObjectId)) 28 | return false; 29 | if (mRequestedSize != other.mRequestedSize) 30 | return false; 31 | return true; 32 | } 33 | 34 | public enum ImageSize { 35 | Image, Thumbnail 36 | }; 37 | 38 | private ImageSize mRequestedSize; 39 | private String mObjectId; 40 | private int mImageRecourceID; 41 | 42 | public TestDownloadObject(String objectId, int imageRecourceID, ImageSize requestedSize) { 43 | mRequestedSize = requestedSize; 44 | mObjectId = objectId; 45 | mImageRecourceID = imageRecourceID; 46 | } 47 | 48 | @Override 49 | public String getUniqueId() { 50 | return String.format("%s-%s", mObjectId, mRequestedSize); 51 | } 52 | 53 | @Override 54 | public String toString() { 55 | return String.format("%s-%s", mObjectId, mRequestedSize); 56 | } 57 | 58 | public int getResourceId() { 59 | return mImageRecourceID; 60 | } 61 | 62 | public ImageSize getRequestedSize() { 63 | return mRequestedSize; 64 | } 65 | 66 | } 67 | -------------------------------------------------------------------------------- /src/de/raptor2101/GalDroid/Testing/ComponentTest/Activities/TestImplementations/TestGalleryObject.java: -------------------------------------------------------------------------------- 1 | package de.raptor2101.GalDroid.Testing.ComponentTest.Activities.TestImplementations; 2 | 3 | import java.util.Date; 4 | import java.util.List; 5 | 6 | import android.graphics.drawable.Drawable; 7 | 8 | import de.raptor2101.GalDroid.WebGallery.Interfaces.GalleryDownloadObject; 9 | import de.raptor2101.GalDroid.WebGallery.Interfaces.GalleryObject; 10 | 11 | public class TestGalleryObject implements GalleryObject { 12 | 13 | private static final long serialVersionUID = -8025907242127852748L; 14 | private final String mTitle; 15 | private final Date mDateUploaded; 16 | private final String mObjectId; 17 | private final int mImageRecourceID; 18 | private final List mChildren; 19 | 20 | public TestGalleryObject(String objectId, String title, Date dateUploaded, int imageRecourceID, List children) { 21 | mTitle = title; 22 | mDateUploaded = dateUploaded; 23 | mObjectId = objectId; 24 | mChildren = children; 25 | mImageRecourceID = imageRecourceID; 26 | } 27 | 28 | @Override 29 | public String getTitle() { 30 | return mTitle; 31 | } 32 | 33 | @Override 34 | public Date getDateUploaded() { 35 | return mDateUploaded; 36 | } 37 | 38 | @Override 39 | public boolean hasChildren() { 40 | return mChildren != null; 41 | } 42 | 43 | @Override 44 | public String getObjectId() { 45 | return mObjectId; 46 | } 47 | 48 | @Override 49 | public GalleryDownloadObject getImage() { 50 | return new TestDownloadObject(mObjectId, mImageRecourceID, TestDownloadObject.ImageSize.Image); 51 | } 52 | 53 | @Override 54 | public GalleryDownloadObject getThumbnail() { 55 | return new TestDownloadObject(mObjectId, mImageRecourceID, TestDownloadObject.ImageSize.Thumbnail); 56 | } 57 | 58 | @Override 59 | public String toString() { 60 | return mObjectId; 61 | } 62 | 63 | @Override 64 | public int hashCode() { 65 | final int prime = 31; 66 | int result = 1; 67 | result = prime * result + ((mObjectId == null) ? 0 : mObjectId.hashCode()); 68 | return result; 69 | } 70 | 71 | @Override 72 | public boolean equals(Object obj) { 73 | if (this == obj) 74 | return true; 75 | if (obj == null) 76 | return false; 77 | if (getClass() != obj.getClass()) 78 | return false; 79 | TestGalleryObject other = (TestGalleryObject) obj; 80 | if (mObjectId == null) { 81 | if (other.mObjectId != null) 82 | return false; 83 | } else if (!mObjectId.equals(other.mObjectId)) 84 | return false; 85 | return true; 86 | } 87 | 88 | public List getChildren() { 89 | return mChildren; 90 | } 91 | 92 | } 93 | -------------------------------------------------------------------------------- /src/de/raptor2101/GalDroid/Testing/ComponentTest/Activities/TestImplementations/TestWebGallery.java: -------------------------------------------------------------------------------- 1 | package de.raptor2101.GalDroid.Testing.ComponentTest.Activities.TestImplementations; 2 | 3 | import java.io.ByteArrayInputStream; 4 | import java.io.ByteArrayOutputStream; 5 | import java.io.IOException; 6 | import java.io.InputStream; 7 | import java.util.ArrayList; 8 | import java.util.Collections; 9 | import java.util.HashMap; 10 | import java.util.List; 11 | import java.util.Map; 12 | import java.util.concurrent.Semaphore; 13 | 14 | import junit.framework.Assert; 15 | 16 | import org.apache.http.client.ClientProtocolException; 17 | import org.apache.http.client.HttpClient; 18 | import org.json.JSONException; 19 | 20 | import android.content.res.Resources; 21 | import android.graphics.Bitmap; 22 | import android.graphics.drawable.BitmapDrawable; 23 | import android.util.Log; 24 | 25 | import de.raptor2101.GalDroid.WebGallery.Stream; 26 | import de.raptor2101.GalDroid.WebGallery.Interfaces.GalleryDownloadObject; 27 | import de.raptor2101.GalDroid.WebGallery.Interfaces.GalleryObject; 28 | import de.raptor2101.GalDroid.WebGallery.Interfaces.GalleryObjectComment; 29 | import de.raptor2101.GalDroid.WebGallery.Interfaces.GalleryProgressListener; 30 | import de.raptor2101.GalDroid.WebGallery.Interfaces.WebGallery; 31 | 32 | public class TestWebGallery implements WebGallery { 33 | private static final String CLASS_TAG = "TestWebGallery"; 34 | private List mTestGalleryObject; 35 | private List mRequestedObjects; 36 | 37 | private final Resources mResources; 38 | private boolean mDownloadWaitHandle; 39 | 40 | public TestWebGallery(Resources resources) { 41 | mDownloadWaitHandle = false; 42 | mResources = resources; 43 | mRequestedObjects = new ArrayList(0); 44 | } 45 | 46 | @Override 47 | public GalleryObject getDisplayObject(String path) throws ClientProtocolException, IOException, JSONException { 48 | Assert.fail("Call not implemented TestMethod"); 49 | return null; 50 | } 51 | 52 | @Override 53 | public List getDisplayObjects() { 54 | Assert.fail("Call not implemented TestMethod"); 55 | return null; 56 | } 57 | 58 | @Override 59 | public List getDisplayObjects(GalleryProgressListener progressListener) { 60 | Assert.fail("Call not implemented TestMethod"); 61 | return null; 62 | } 63 | 64 | @Override 65 | public List getDisplayObjects(String path) { 66 | Assert.fail("Call not implemented TestMethod"); 67 | return null; 68 | } 69 | 70 | @Override 71 | public List getDisplayObjects(String path, GalleryProgressListener progressListener) { 72 | Assert.fail("Call not implemented TestMethod"); 73 | return null; 74 | } 75 | 76 | @Override 77 | public List getDisplayObjects(GalleryObject galleryObject) { 78 | Assert.fail("Call not implemented TestMethod"); 79 | return null; 80 | } 81 | 82 | @Override 83 | public List getDisplayObjects(GalleryObject galleryObject, GalleryProgressListener progressListener) { 84 | if (galleryObject instanceof TestGalleryObject) { 85 | TestGalleryObject testGalleryObject = (TestGalleryObject) galleryObject; 86 | 87 | Assert.assertTrue("The requestes GalleryObject has no childen", testGalleryObject.hasChildren()); 88 | 89 | List children = testGalleryObject.getChildren(); 90 | if (progressListener != null) { 91 | 92 | progressListener.handleProgress(children.size(), children.size()); 93 | } 94 | List castedChildren = new ArrayList(children.size()); 95 | for (TestGalleryObject child : children) { 96 | castedChildren.add(child); 97 | } 98 | return castedChildren; 99 | 100 | } else { 101 | Assert.fail("Someone tries to use non-testing GalleryObjects on testing WebGallery"); 102 | return new ArrayList(0); 103 | } 104 | } 105 | 106 | private Map> expectedGetDisplayObjectTagsCalls = new HashMap>(10); 107 | 108 | public void setupGetDisplayObjectTagsCall(GalleryObject galleryObject, List returnValue) { 109 | expectedGetDisplayObjectTagsCalls.put(galleryObject, returnValue); 110 | } 111 | 112 | private Map mMapBlockedGetDisplayObjectTags = new HashMap(10); 113 | 114 | @Override 115 | public List getDisplayObjectTags(GalleryObject galleryObject, GalleryProgressListener progressListener) throws IOException { 116 | if (expectedGetDisplayObjectTagsCalls.containsKey(galleryObject)) { 117 | AquireWaitHandle("getDisplayObjectTags", galleryObject, mMapBlockedGetDisplayObjectTags); 118 | List returnValue = expectedGetDisplayObjectTagsCalls.get(galleryObject); 119 | mMapBlockedGetDisplayObjectTags.remove(galleryObject); 120 | return returnValue; 121 | } else { 122 | Assert.fail(String.format("No setup for %s - getDisplayObjectTags-Call", galleryObject)); 123 | return null; 124 | } 125 | 126 | } 127 | 128 | public void releaseGetGetDisplayObjectTags(GalleryObject galleryObject) { 129 | ReleaseWaitHandle("getDisplayObjectTags", galleryObject, mMapBlockedGetDisplayObjectTags); 130 | } 131 | 132 | private Map> expectedGetDisplayObjectCommentsCalls = new HashMap>(10); 133 | 134 | public void setupGetDisplayObjectCommentsCall(GalleryObject galleryObject, List returnValue) { 135 | expectedGetDisplayObjectCommentsCalls.put(galleryObject, returnValue); 136 | } 137 | 138 | private Map mMapBlockedGetDisplayObjectComments = new HashMap(10); 139 | 140 | @Override 141 | public List getDisplayObjectComments(GalleryObject galleryObject, GalleryProgressListener progressListener) throws IOException, ClientProtocolException, JSONException { 142 | if (expectedGetDisplayObjectCommentsCalls.containsKey(galleryObject)) { 143 | AquireWaitHandle("getDisplayObjectComments", galleryObject, mMapBlockedGetDisplayObjectComments); 144 | List returnValue = expectedGetDisplayObjectCommentsCalls.get(galleryObject); 145 | expectedGetDisplayObjectCommentsCalls.remove(galleryObject); 146 | return returnValue; 147 | } else { 148 | Assert.fail(String.format("No setup for %s - getDisplayObjectComments-Call", galleryObject)); 149 | return null; 150 | } 151 | } 152 | 153 | public void releaseGetDisplayObjectComments(GalleryObject galleryObject) { 154 | ReleaseWaitHandle("getDisplayObjectComments", galleryObject, mMapBlockedGetDisplayObjectComments); 155 | } 156 | 157 | @Override 158 | public void setPreferedDimensions(int height, int width) { 159 | // TODO currently not tracked for testing 160 | } 161 | 162 | @Override 163 | public String getSecurityToken(String user, String password) throws SecurityException { 164 | Assert.fail("Call not implemented TestMethod"); 165 | return null; 166 | } 167 | 168 | private Map mMapBlockedGetFileStream = new HashMap(10); 169 | 170 | @Override 171 | public Stream getFileStream(GalleryDownloadObject downloadObject) throws IOException, ClientProtocolException { 172 | if (downloadObject instanceof TestDownloadObject) { 173 | TestDownloadObject testDownloadObject = (TestDownloadObject) downloadObject; 174 | Assert.assertEquals("Wrong image size requested", TestDownloadObject.ImageSize.Image, testDownloadObject.getRequestedSize()); 175 | synchronized (mRequestedObjects) { 176 | mRequestedObjects.add(testDownloadObject); 177 | } 178 | 179 | if (mDownloadWaitHandle) { 180 | Semaphore semaphore; 181 | synchronized (mMapBlockedGetFileStream) { 182 | if (mMapBlockedGetFileStream.containsKey(testDownloadObject)) { 183 | Log.d(CLASS_TAG, String.format("getFileStream - Aquire existing semaphore for %s", testDownloadObject)); 184 | semaphore = mMapBlockedGetFileStream.get(testDownloadObject); 185 | } else { 186 | Log.d(CLASS_TAG, String.format("getFileStream - Creating and aquire existing semaphore for %s", testDownloadObject)); 187 | semaphore = new Semaphore(0); 188 | mMapBlockedGetFileStream.put((TestDownloadObject) downloadObject, semaphore); 189 | } 190 | } 191 | try { 192 | semaphore.acquire(); 193 | } catch (InterruptedException e) { 194 | return null; 195 | } 196 | 197 | Log.d(CLASS_TAG, String.format("getFileStream - semaphore released for %s", testDownloadObject)); 198 | } 199 | 200 | BitmapDrawable drawable = (BitmapDrawable) mResources.getDrawable(testDownloadObject.getResourceId()); 201 | Bitmap bitmap = (Bitmap) ((BitmapDrawable) drawable).getBitmap(); 202 | ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); 203 | bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream); 204 | byte[] byteArray = outputStream.toByteArray(); 205 | InputStream inputStream = new ByteArrayInputStream(byteArray); 206 | return new Stream(inputStream, byteArray.length); 207 | } else { 208 | Assert.fail("Someone tries to use non-testing GalleryObjects on testing WebGallery"); 209 | return null; 210 | } 211 | 212 | } 213 | 214 | public void releaseGetFileStream(TestDownloadObject downloadObject) { 215 | Semaphore semaphore; 216 | synchronized (mMapBlockedGetFileStream) { 217 | if (mMapBlockedGetFileStream.containsKey(downloadObject)) { 218 | Log.d(CLASS_TAG, String.format("getFileStream - Release existing semaphore for %s", downloadObject)); 219 | semaphore = mMapBlockedGetFileStream.get(downloadObject); 220 | 221 | } else { 222 | Log.d(CLASS_TAG, String.format("getFileStream - Creating and release existing semaphore for %s", downloadObject)); 223 | semaphore = new Semaphore(0); 224 | mMapBlockedGetFileStream.put(downloadObject, semaphore); 225 | } 226 | } 227 | semaphore.release(); 228 | } 229 | 230 | @Override 231 | public void setSecurityToken(String token) { 232 | Assert.fail("Call not implemented TestMethod"); 233 | } 234 | 235 | @Override 236 | public void setHttpClient(HttpClient httpClient) { 237 | Assert.fail("Call not implemented TestMethod"); 238 | } 239 | 240 | public void setTestGalleryObjects(List testGalleryObjects) { 241 | mTestGalleryObject = testGalleryObjects; 242 | synchronized (mRequestedObjects) { 243 | mRequestedObjects = new ArrayList(testGalleryObjects.size()); 244 | } 245 | } 246 | 247 | public List getTestGalleryObjects() { 248 | return mTestGalleryObject; 249 | } 250 | 251 | public void resetRequestedDownloadObjects() { 252 | synchronized (mRequestedObjects) { 253 | mRequestedObjects.clear(); 254 | } 255 | } 256 | 257 | public List getRequestedDownloadObjects() { 258 | synchronized (mRequestedObjects) { 259 | List returnList = new ArrayList(mRequestedObjects.size()); 260 | for (TestDownloadObject object : mRequestedObjects) { 261 | returnList.add(object); 262 | } 263 | return returnList; 264 | } 265 | } 266 | 267 | public boolean isDownloadWaitHandleActive() { 268 | return mDownloadWaitHandle; 269 | } 270 | 271 | public void activateDownloadWaitHandle() { 272 | mDownloadWaitHandle = true; 273 | } 274 | 275 | public void deactivateDownloadWaitHandle() { 276 | mDownloadWaitHandle = false; 277 | } 278 | 279 | private void AquireWaitHandle(String source, GalleryObject galleryObject, Map mapHandles) throws IOException { 280 | if (mDownloadWaitHandle) { 281 | Semaphore semaphore; 282 | synchronized (mapHandles) { 283 | if (mapHandles.containsKey(galleryObject)) { 284 | Log.d(CLASS_TAG, String.format("%s - Aquire existing semaphore for %s", source, galleryObject)); 285 | semaphore = mapHandles.get(galleryObject); 286 | } else { 287 | Log.d(CLASS_TAG, String.format("%s - Creating and aquire existing semaphore for %s", source, galleryObject)); 288 | semaphore = new Semaphore(0); 289 | mapHandles.put(galleryObject, semaphore); 290 | } 291 | } 292 | try { 293 | semaphore.acquire(); 294 | } catch (InterruptedException e) { 295 | throw new IOException("Interupt exception was thrown", e); 296 | } 297 | Log.d(CLASS_TAG, String.format("%s - semaphore released for %s", source, galleryObject)); 298 | } 299 | } 300 | 301 | private void ReleaseWaitHandle(String source, GalleryObject galleryObject, Map mapHandles) { 302 | Semaphore semaphore; 303 | synchronized (mapHandles) { 304 | if (mapHandles.containsKey(galleryObject)) { 305 | Log.d(CLASS_TAG, String.format("%s - Release existing semaphore for %s", source, galleryObject)); 306 | semaphore = mapHandles.get(galleryObject); 307 | 308 | } else { 309 | Log.d(CLASS_TAG, String.format("%s - Creating and release existing semaphore for %s", source, galleryObject)); 310 | semaphore = new Semaphore(0); 311 | mapHandles.put(galleryObject, semaphore); 312 | } 313 | } 314 | semaphore.release(); 315 | } 316 | } 317 | -------------------------------------------------------------------------------- /src/de/raptor2101/GalDroid/Testing/ComponentTest/Activities/ImageViewActivityTest.java: -------------------------------------------------------------------------------- 1 | package de.raptor2101.GalDroid.Testing.ComponentTest.Activities; 2 | 3 | import java.io.File; 4 | import java.security.NoSuchAlgorithmException; 5 | import java.security.acl.LastOwnerException; 6 | import java.text.DateFormat; 7 | import java.util.ArrayList; 8 | import java.util.Date; 9 | import java.util.List; 10 | import java.util.Locale; 11 | import java.util.Random; 12 | import java.util.concurrent.ExecutionException; 13 | 14 | import android.app.ActionBar; 15 | import android.app.Instrumentation; 16 | import android.content.Intent; 17 | import android.content.res.Resources; 18 | import android.graphics.PointF; 19 | import android.os.SystemClock; 20 | import android.test.ActivityInstrumentationTestCase2; 21 | import android.test.TouchUtils; 22 | import android.util.Log; 23 | import android.view.MotionEvent; 24 | import android.view.View; 25 | import android.view.ViewGroup; 26 | import android.widget.Gallery; 27 | import android.widget.TextView; 28 | import de.raptor2101.GalDroid.R; 29 | import de.raptor2101.GalDroid.Activities.GalDroidApp; 30 | import de.raptor2101.GalDroid.Activities.ImageViewActivity; 31 | import de.raptor2101.GalDroid.Activities.Helpers.ImageAdapter; 32 | import de.raptor2101.GalDroid.Activities.Views.GalleryImageView; 33 | import de.raptor2101.GalDroid.Activities.Views.ImageInformationView; 34 | import de.raptor2101.GalDroid.Config.GalDroidPreference; 35 | import de.raptor2101.GalDroid.Testing.ComponentTest.Activities.TestImplementations.TestDownloadObject; 36 | import de.raptor2101.GalDroid.Testing.ComponentTest.Activities.TestImplementations.TestGalleryObject; 37 | import de.raptor2101.GalDroid.Testing.ComponentTest.Activities.TestImplementations.TestGalleryObjectComment; 38 | import de.raptor2101.GalDroid.Testing.ComponentTest.Activities.TestImplementations.TestWebGallery; 39 | import de.raptor2101.GalDroid.WebGallery.ImageCache; 40 | import de.raptor2101.GalDroid.WebGallery.Interfaces.GalleryDownloadObject; 41 | import de.raptor2101.GalDroid.WebGallery.Interfaces.GalleryObject; 42 | import de.raptor2101.GalDroid.WebGallery.Interfaces.GalleryObjectComment; 43 | import de.raptor2101.GalDroid.WebGallery.Tasks.GalleryLoaderTask; 44 | import de.raptor2101.GalDroid.WebGallery.Tasks.ImageInformationLoaderTask; 45 | import de.raptor2101.GalDroid.WebGallery.Tasks.ImageLoaderTask; 46 | import de.raptor2101.GalDroid.WebGallery.Tasks.TaskInterface.Status; 47 | 48 | public class ImageViewActivityTest extends ActivityInstrumentationTestCase2 { 49 | private static final String CLASS_TAG = "ImageViewActivityTest"; 50 | 51 | public ImageViewActivityTest() { 52 | super("de.raptor2101.GalDroid.Activities.ImageViewActivity", ImageViewActivity.class); 53 | } 54 | 55 | private final int IMAGE_ID = de.raptor2101.GalDroid.Testing.R.drawable.testpic_1; 56 | private final int GALLERY_SAMPLE_SIZE = 300; 57 | 58 | private ImageViewActivity mActivity; 59 | private TestWebGallery mWebGallery; 60 | private ImageCache mImageCache; 61 | 62 | private Instrumentation mInstrumentation; 63 | 64 | private TestGalleryObject mCurrentGallery; 65 | private TestGalleryObject mCurrentVisibleChild; 66 | 67 | private ImageInformationView mImageInformationView; 68 | 69 | private Gallery mGalleryFullscreen; 70 | private Gallery mGalleryThumbnails; 71 | 72 | private ImageLoaderTask mImageLoaderTask; 73 | 74 | @Override 75 | protected void setUp() throws Exception { 76 | Log.d("ImageViewActivityTest", "Setup Called"); 77 | super.setUp(); 78 | mInstrumentation = getInstrumentation(); 79 | GalDroidPreference.Initialize(mInstrumentation.getTargetContext()); 80 | 81 | deleteCachedFiles(); 82 | 83 | Resources recources = mInstrumentation.getContext().getResources(); 84 | mWebGallery = createTestWebGallery(recources); 85 | 86 | GalDroidApp appContext = (GalDroidApp) mInstrumentation.getTargetContext().getApplicationContext(); 87 | appContext.setWebGallery(mWebGallery); 88 | mImageCache = appContext.getImageCache(); 89 | 90 | 91 | } 92 | 93 | private void deleteCachedFiles() throws NoSuchAlgorithmException { 94 | mImageCache = new ImageCache(this.getInstrumentation().getTargetContext()); 95 | for (File file : mImageCache.getCacheDir().listFiles()) { 96 | try { 97 | file.delete(); 98 | } catch (Exception e) { 99 | // if something goes wrong... ignore it 100 | } 101 | } 102 | 103 | GalDroidPreference asyncAccess = GalDroidPreference.GetAsyncAccess(); 104 | asyncAccess.clearCacheTable(); 105 | asyncAccess.close(); 106 | } 107 | 108 | @Override 109 | protected void tearDown() throws Exception { 110 | Log.d(CLASS_TAG,String.format("TearDown Called",mActivity)); 111 | mInstrumentation.runOnMainSync(new Runnable() { 112 | 113 | @Override 114 | public void run() { 115 | mInstrumentation.callActivityOnStop(mActivity); 116 | mActivity.finish(); 117 | } 118 | }); 119 | 120 | mActivity = null; 121 | 122 | if (mImageCache != null) { 123 | mImageCache.clearCachedBitmaps(true); 124 | } 125 | deleteCachedFiles(); 126 | 127 | super.tearDown(); 128 | } 129 | 130 | public void setupActivity(boolean showImageInformation) { 131 | Log.d(CLASS_TAG, "Create Intent"); 132 | Intent intent = new Intent(); 133 | intent.putExtra(GalDroidApp.INTENT_EXTRA_DISPLAY_GALLERY, mCurrentGallery); 134 | intent.putExtra(GalDroidApp.INTENT_EXTRA_DISPLAY_OBJECT, mCurrentVisibleChild); 135 | intent.putExtra(GalDroidApp.INTENT_EXTRA_SHOW_IMAGE_INFO, showImageInformation); 136 | setActivityIntent(intent); 137 | 138 | Log.d(CLASS_TAG, "getActivity"); 139 | 140 | mActivity = getActivity(); 141 | System.gc(); 142 | 143 | Log.d(CLASS_TAG, "extract Controls"); 144 | mGalleryFullscreen = (Gallery) mActivity.findViewById(R.id.singleImageGallery); 145 | mGalleryThumbnails = (Gallery) mActivity.findViewById(R.id.thumbnailImageGallery); 146 | 147 | ImageAdapter adapter = (ImageAdapter) mGalleryFullscreen.getAdapter(); 148 | mImageLoaderTask = adapter.getImageLoaderTask(); 149 | 150 | mImageInformationView = (ImageInformationView) mActivity.findViewById(R.id.viewImageInformations); 151 | } 152 | 153 | public void testActivityStart() throws Exception { 154 | setupActivity(false); 155 | checkStartUp(View.GONE); 156 | } 157 | 158 | public void testActivityStartWithOpeningInformations() throws Exception { 159 | setupActivity(false); 160 | checkStartUp(View.GONE); 161 | GalleryImageView imageView = (GalleryImageView) mGalleryFullscreen.getSelectedView(); 162 | TestGalleryObject galleryObject = (TestGalleryObject) imageView.getGalleryObject(); 163 | 164 | List comments = new ArrayList(5); 165 | comments.add(new TestGalleryObjectComment("Some Author", "First Comment")); 166 | comments.add(new TestGalleryObjectComment("Some other Author", "Second Comment")); 167 | comments.add(new TestGalleryObjectComment("Some Author", "give some more comment")); 168 | comments.add(new TestGalleryObjectComment("administraor", "shut the fuck up")); 169 | comments.add(new TestGalleryObjectComment("Test", "Test")); 170 | 171 | mWebGallery.setupGetDisplayObjectCommentsCall(galleryObject, comments); 172 | 173 | List tags = new ArrayList(5); 174 | tags.add("Some"); 175 | tags.add("realy"); 176 | tags.add("incredible"); 177 | tags.add("genius"); 178 | tags.add("tags"); 179 | 180 | mWebGallery.setupGetDisplayObjectTagsCall(galleryObject, tags); 181 | 182 | TouchUtils.clickView(this, mGalleryFullscreen); 183 | ActionBar actionBar = mActivity.getActionBar(); 184 | assertEquals("The actionbar don't appear", true, actionBar.isShowing()); 185 | View imageButton = mActivity.findViewById(R.id.item_additional_info_object); 186 | TouchUtils.clickView(this, imageButton); 187 | assertEquals("The ImageInformationPanel has wrong Visibility", View.VISIBLE, mImageInformationView.getVisibility()); 188 | 189 | checkImageInformationIsLoadedCorrectly(galleryObject, comments, tags); 190 | } 191 | 192 | public void testActivityStartWithInformations() throws Exception { 193 | List children = mCurrentGallery.getChildren(); 194 | TestGalleryObject galleryObject = children.get(0); 195 | 196 | List comments = new ArrayList(5); 197 | comments.add(new TestGalleryObjectComment("Some Author", "First Comment")); 198 | comments.add(new TestGalleryObjectComment("Some other Author", "Second Comment")); 199 | comments.add(new TestGalleryObjectComment("Some Author", "give some more comment")); 200 | comments.add(new TestGalleryObjectComment("administraor", "shut the fuck up")); 201 | comments.add(new TestGalleryObjectComment("Test", "Test")); 202 | 203 | mWebGallery.setupGetDisplayObjectCommentsCall(galleryObject, comments); 204 | 205 | List tags = new ArrayList(5); 206 | tags.add("Some"); 207 | tags.add("realy"); 208 | tags.add("incredible"); 209 | tags.add("genius"); 210 | tags.add("tags"); 211 | 212 | mWebGallery.setupGetDisplayObjectTagsCall(galleryObject, tags); 213 | 214 | setupActivity(true); 215 | checkStartUp(View.VISIBLE); 216 | 217 | checkImageInformationIsLoadedCorrectly(galleryObject, comments, tags); 218 | 219 | checkForUneededDownloads(); 220 | } 221 | 222 | private void checkForUneededDownloads() throws InterruptedException { 223 | // wait if some downloads are enqueued twice 224 | Thread.sleep(2000); 225 | List requestetObjects = mWebGallery.getRequestedDownloadObjects(); 226 | int size = requestetObjects.size(); 227 | if (size > 1) { 228 | for (int outerIndex = 0; outerIndex < size; outerIndex++) { 229 | TestDownloadObject outerObject = requestetObjects.get(outerIndex); 230 | Log.d(CLASS_TAG, String.format("Requested DownloadObject: %s", outerObject)); 231 | for (int innerIndex = outerIndex + 1; innerIndex < size; innerIndex++) { 232 | TestDownloadObject innerObject = requestetObjects.get(innerIndex); 233 | 234 | assertFalse(String.format("%s is requested twice", outerIndex), outerObject.equals(innerObject)); 235 | } 236 | } 237 | } 238 | } 239 | 240 | public void testAbortImageLoading() throws Exception { 241 | Log.d(CLASS_TAG, "Prepare TestEnvironment"); 242 | List children = mCurrentGallery.getChildren(); 243 | TestGalleryObject galleryObject = children.get(0); 244 | 245 | List comments = new ArrayList(1); 246 | List tags = new ArrayList(2); 247 | 248 | comments.add(new TestGalleryObjectComment("Some Author", String.format("Comment %d", 0))); 249 | 250 | tags.add("Some"); 251 | tags.add(String.format("Comment %d", 0)); 252 | 253 | Log.d(CLASS_TAG, "activate DownloadWait Handle"); 254 | mWebGallery.activateDownloadWaitHandle(); 255 | 256 | Log.d(CLASS_TAG, "Setup GalleryCalls"); 257 | mWebGallery.setupGetDisplayObjectCommentsCall(galleryObject, comments); 258 | mWebGallery.setupGetDisplayObjectTagsCall(galleryObject, tags); 259 | 260 | Log.d(CLASS_TAG, "start Activity"); 261 | setupActivity(true); 262 | 263 | Log.d(CLASS_TAG, "initial Check"); 264 | // Begin Check Startup 265 | assertEquals("The FullscreenGallery has wrong Visibility", View.VISIBLE, mGalleryFullscreen.getVisibility()); 266 | assertEquals("The ThumbnailGallery has wrong Visibility", View.GONE, mGalleryThumbnails.getVisibility()); 267 | assertEquals("The ImageInformationPanel has wrong Visibility", View.VISIBLE, mImageInformationView.getVisibility()); 268 | 269 | Log.d(CLASS_TAG, "areGalleryObjectsAvailable"); 270 | 271 | if (!mActivity.areGalleryObjectsAvailable()) { 272 | Log.d(CLASS_TAG, "Checking LoaderTask"); 273 | GalleryLoaderTask loaderTask = mActivity.getDownloadTask(); 274 | assertNotNull("No DownloadTask created although no gallery object could be loaded from the cache", loaderTask); 275 | 276 | loaderTask.get(); 277 | } 278 | 279 | checkImageAdapterIsLoaded(); 280 | checkSelectedViewIsNotNull(); 281 | 282 | GalleryImageView selectedView = checkSelectedView(mCurrentVisibleChild); 283 | 284 | checkImageInformationIsntLoadedBeforImage(); 285 | 286 | Log.d(CLASS_TAG, "Check ImageLoaderTask and ImageInformationLoaderTask"); 287 | assertFalse("ImageView is loaded without loading an image", selectedView.isLoaded()); 288 | galleryObject = (TestGalleryObject) selectedView.getGalleryObject(); 289 | final GalleryDownloadObject downloadObject = galleryObject.getImage(); 290 | assertTrue(String.format("The DownloadObject isn't enqueued for the gallerImageView %s", selectedView.getGalleryObject().getObjectId()), mImageLoaderTask.isDownloading(downloadObject)); 291 | 292 | Log.d(CLASS_TAG, "Checking ExtractorTask"); 293 | ImageInformationLoaderTask task = mImageInformationView.getImageInformationLoaderTask(); 294 | assertFalse(String.format("Information for GalleryObject %s is loaded befor Image is downloaded", galleryObject), task.isLoading(galleryObject)); 295 | 296 | Log.d(CLASS_TAG, "Abort image loading"); 297 | 298 | ImageAdapter adapter = (ImageAdapter) mGalleryFullscreen.getAdapter(); 299 | selectedView = (GalleryImageView) adapter.getView(1, selectedView, mGalleryFullscreen); 300 | 301 | Log.d(CLASS_TAG, "Releasing the FileStream"); 302 | mWebGallery.releaseGetFileStream((TestDownloadObject) selectedView.getGalleryObject().getImage()); 303 | 304 | checkImageLoaderTask(selectedView); 305 | } 306 | 307 | public void testAbortImageInformationLoading() throws Exception { 308 | Log.d(CLASS_TAG, "Prepare TestEnvironment"); 309 | List children = mCurrentGallery.getChildren(); 310 | TestGalleryObject galleryObject = children.get(0); 311 | 312 | List comments = new ArrayList(1); 313 | List tags = new ArrayList(2); 314 | 315 | comments.add(new TestGalleryObjectComment("Some Author", String.format("Comment %d", 0))); 316 | 317 | tags.add("Some"); 318 | tags.add(String.format("Comment %d", 0)); 319 | 320 | Log.d(CLASS_TAG, "activate DownloadWait Handle"); 321 | mWebGallery.activateDownloadWaitHandle(); 322 | 323 | Log.d(CLASS_TAG, "Setup GalleryCalls"); 324 | mWebGallery.setupGetDisplayObjectCommentsCall(galleryObject, comments); 325 | mWebGallery.setupGetDisplayObjectTagsCall(galleryObject, tags); 326 | 327 | Log.d(CLASS_TAG, "start Activity"); 328 | setupActivity(true); 329 | 330 | Log.d(CLASS_TAG, "initial Check"); 331 | // Begin Check Startup 332 | assertEquals("The FullscreenGallery has wrong Visibility", View.VISIBLE, mGalleryFullscreen.getVisibility()); 333 | assertEquals("The ThumbnailGallery has wrong Visibility", View.GONE, mGalleryThumbnails.getVisibility()); 334 | assertEquals("The ImageInformationPanel has wrong Visibility", View.VISIBLE, mImageInformationView.getVisibility()); 335 | 336 | Log.d(CLASS_TAG, "areGalleryObjectsAvailable"); 337 | 338 | if (!mActivity.areGalleryObjectsAvailable()) { 339 | Log.d(CLASS_TAG, "Checking LoaderTask"); 340 | GalleryLoaderTask loaderTask = mActivity.getDownloadTask(); 341 | assertNotNull("No DownloadTask created although no gallery object could be loaded from the cache", loaderTask); 342 | 343 | loaderTask.get(); 344 | } 345 | 346 | checkImageAdapterIsLoaded(); 347 | checkSelectedViewIsNotNull(); 348 | 349 | GalleryImageView selectedView = checkSelectedView(mCurrentVisibleChild); 350 | 351 | checkImageInformationIsntLoadedBeforImage(); 352 | checkImageLoaderTask(selectedView); 353 | 354 | Log.d(CLASS_TAG, "Check that the ImageInformationLoader tries to download the Tags&Comments"); 355 | ImageInformationLoaderTask task = mImageInformationView.getImageInformationLoaderTask(); 356 | assertTrue(String.format("%s isn't enqueued for loading ImageINformation altough the ImageLoaderTask is finished", galleryObject), task.isLoading(galleryObject)); 357 | 358 | Log.d(CLASS_TAG, "Do a image-change before the task finished"); 359 | PointF dragFrom = new PointF(1000, 358); 360 | PointF dragTo = new PointF(400, 358); 361 | flingGalleryAndCheckImageChange(dragFrom, dragTo); 362 | 363 | TestGalleryObject currentObject = children.get(1); 364 | 365 | Log.d(CLASS_TAG, "Setup GalleryCalls"); 366 | mWebGallery.setupGetDisplayObjectCommentsCall(currentObject, comments); 367 | mWebGallery.setupGetDisplayObjectTagsCall(currentObject, tags); 368 | 369 | GalleryImageView imageView = (GalleryImageView) mGalleryFullscreen.getSelectedView(); 370 | checkSelectedView(currentObject); 371 | 372 | TaskHelper helper = new TaskHelper() { 373 | 374 | @Override 375 | protected boolean checkCondition(long timeElapsed) { 376 | return mImageInformationView.findViewById(R.id.progressBarTags).getVisibility() == View.GONE; 377 | } 378 | }; 379 | 380 | helper.waitForExecution("ImageInformationView isn't reseted in the given time"); 381 | 382 | checkImageInformationIsntLoadedBeforImage(); 383 | checkImageLoaderTask(imageView); 384 | 385 | checkImageInformationIsLoadedCorrectly(currentObject, comments, tags); 386 | 387 | checkForUneededDownloads(); 388 | } 389 | 390 | public void testScrollTrough() throws Exception { 391 | setupActivity(false); 392 | checkStartUp(View.GONE); 393 | 394 | List children = mCurrentGallery.getChildren(); 395 | 396 | // fastForward ... to the last image... 397 | PointF dragFrom = new PointF(1000, 358); 398 | PointF dragTo = new PointF(500, 358); 399 | 400 | for (int pos = 1; pos < 5; pos++) { 401 | flingGalleryAndCheckImageChange(dragFrom, dragTo); 402 | 403 | GalleryImageView imageView = (GalleryImageView) mGalleryFullscreen.getSelectedView(); 404 | checkSelectedView(children.get(pos)); 405 | checkImageLoaderTask(imageView); 406 | } 407 | 408 | checkForUneededDownloads(); 409 | } 410 | 411 | public void testScrollTroughWithInformations() throws Exception { 412 | List children = mCurrentGallery.getChildren(); 413 | TestGalleryObject galleryObject = children.get(0); 414 | 415 | List comments = new ArrayList(1); 416 | List tags = new ArrayList(2); 417 | 418 | comments.add(new TestGalleryObjectComment("Some Author", String.format("Comment %d", 0))); 419 | 420 | tags.add("Some"); 421 | tags.add(String.format("Comment %d", 0)); 422 | 423 | mWebGallery.setupGetDisplayObjectCommentsCall(galleryObject, comments); 424 | mWebGallery.setupGetDisplayObjectTagsCall(galleryObject, tags); 425 | 426 | setupActivity(true); 427 | checkStartUp(View.VISIBLE); 428 | 429 | checkImageInformationIsLoadedCorrectly(galleryObject, comments, tags); 430 | 431 | for (int pos = 1; pos < 5; pos++) { 432 | galleryObject = children.get(pos); 433 | Log.d(CLASS_TAG, String.format("Simulating switching to %s", galleryObject)); 434 | 435 | comments.add(new TestGalleryObjectComment("Some Author", String.format("Comment %d", pos))); 436 | 437 | tags.clear(); 438 | tags.add("Some"); 439 | tags.add(String.format("Comment %d", pos)); 440 | 441 | mWebGallery.setupGetDisplayObjectCommentsCall(galleryObject, comments); 442 | mWebGallery.setupGetDisplayObjectTagsCall(galleryObject, tags); 443 | 444 | selectImage(mGalleryFullscreen, pos); 445 | 446 | GalleryImageView imageView = (GalleryImageView) mGalleryFullscreen.getSelectedView(); 447 | checkSelectedView(children.get(pos)); 448 | checkImageLoaderTask(imageView); 449 | 450 | checkImageInformationIsLoadedCorrectly(galleryObject, comments, tags); 451 | } 452 | 453 | checkForUneededDownloads(); 454 | } 455 | 456 | public void testScrollTroughWithInformationsAndDeferedLoading() throws Exception { 457 | Log.d(CLASS_TAG, "Prepare TestEnvironment"); 458 | List children = mCurrentGallery.getChildren(); 459 | TestGalleryObject galleryObject = children.get(0); 460 | 461 | List comments = new ArrayList(1); 462 | List tags = new ArrayList(2); 463 | 464 | comments.add(new TestGalleryObjectComment("Some Author", String.format("Comment %d", 0))); 465 | 466 | tags.add("Some"); 467 | tags.add(String.format("Comment %d", 0)); 468 | 469 | Log.d(CLASS_TAG, "activate DownloadWait Handle"); 470 | mWebGallery.activateDownloadWaitHandle(); 471 | 472 | Log.d(CLASS_TAG, "Setup GalleryCalls"); 473 | mWebGallery.setupGetDisplayObjectCommentsCall(galleryObject, comments); 474 | mWebGallery.setupGetDisplayObjectTagsCall(galleryObject, tags); 475 | 476 | Log.d(CLASS_TAG, "start Activity"); 477 | setupActivity(true); 478 | 479 | Log.d(CLASS_TAG, "initial Check"); 480 | 481 | // Begin Check Startup 482 | assertEquals("The FullscreenGallery has wrong Visibility", View.VISIBLE, mGalleryFullscreen.getVisibility()); 483 | assertEquals("The ThumbnailGallery has wrong Visibility", View.GONE, mGalleryThumbnails.getVisibility()); 484 | assertEquals("The ImageInformationPanel has wrong Visibility", View.VISIBLE, mImageInformationView.getVisibility()); 485 | 486 | Log.d(CLASS_TAG, "areGalleryObjectsAvailable"); 487 | 488 | if (!mActivity.areGalleryObjectsAvailable()) { 489 | Log.d(CLASS_TAG, "Checking LoaderTask"); 490 | GalleryLoaderTask loaderTask = mActivity.getDownloadTask(); 491 | assertNotNull("No DownloadTask created although no gallery object could be loaded from the cache", loaderTask); 492 | 493 | loaderTask.get(); 494 | } 495 | 496 | checkImageAdapterIsLoaded(); 497 | checkSelectedViewIsNotNull(); 498 | 499 | GalleryImageView selectedView = checkSelectedView(mCurrentVisibleChild); 500 | 501 | checkImageInformationIsntLoadedBeforImage(); 502 | 503 | checkImageLoaderTask(selectedView); 504 | checkPreLoading(selectedView, 2); 505 | // End Check Startup 506 | 507 | checkImageInformationIsLoadedCorrectly(galleryObject, comments, tags); 508 | 509 | for (int pos = 1; pos < 5; pos++) { 510 | galleryObject = children.get(pos); 511 | Log.d(CLASS_TAG, String.format("Simulating switching to %s", galleryObject)); 512 | 513 | comments.add(new TestGalleryObjectComment("Some Author", String.format("Comment %d", pos))); 514 | 515 | tags.add("Some"); 516 | tags.add(String.format("Comment %d", pos)); 517 | 518 | mWebGallery.setupGetDisplayObjectCommentsCall(galleryObject, comments); 519 | mWebGallery.setupGetDisplayObjectTagsCall(galleryObject, tags); 520 | 521 | selectImage(mGalleryFullscreen, pos); 522 | 523 | GalleryImageView imageView = (GalleryImageView) mGalleryFullscreen.getSelectedView(); 524 | checkSelectedView(children.get(pos)); 525 | checkImageInformationIsntLoadedBeforImage(); 526 | checkImageLoaderTask(imageView); 527 | 528 | checkImageInformationIsLoadedCorrectly(galleryObject, comments, tags); 529 | } 530 | 531 | checkForUneededDownloads(); 532 | } 533 | 534 | public void testScrollTroughWithInformationsAndAbortions() throws Exception { 535 | Log.d(CLASS_TAG, "Prepare TestEnvironment"); 536 | List children = mCurrentGallery.getChildren(); 537 | TestGalleryObject galleryObject = children.get(0); 538 | 539 | List comments = new ArrayList(1); 540 | List tags = new ArrayList(2); 541 | 542 | comments.add(new TestGalleryObjectComment("Some Author", String.format("Comment %d", 0))); 543 | 544 | tags.add("Some"); 545 | tags.add(String.format("Comment %d", 0)); 546 | 547 | Log.d(CLASS_TAG, "activate DownloadWait Handle"); 548 | mWebGallery.activateDownloadWaitHandle(); 549 | 550 | Log.d(CLASS_TAG, "Setup GalleryCalls"); 551 | mWebGallery.setupGetDisplayObjectCommentsCall(galleryObject, comments); 552 | mWebGallery.setupGetDisplayObjectTagsCall(galleryObject, tags); 553 | 554 | Log.d(CLASS_TAG, "start Activity"); 555 | setupActivity(true); 556 | 557 | Log.d(CLASS_TAG, "initial Check"); 558 | // Begin Check Startup 559 | assertEquals("The FullscreenGallery has wrong Visibility", View.VISIBLE, mGalleryFullscreen.getVisibility()); 560 | assertEquals("The ThumbnailGallery has wrong Visibility", View.GONE, mGalleryThumbnails.getVisibility()); 561 | assertEquals("The ImageInformationPanel has wrong Visibility", View.VISIBLE, mImageInformationView.getVisibility()); 562 | 563 | Log.d(CLASS_TAG, "areGalleryObjectsAvailable"); 564 | 565 | if (!mActivity.areGalleryObjectsAvailable()) { 566 | Log.d(CLASS_TAG, "Checking LoaderTask"); 567 | GalleryLoaderTask loaderTask = mActivity.getDownloadTask(); 568 | assertNotNull("No DownloadTask created although no gallery object could be loaded from the cache", loaderTask); 569 | 570 | loaderTask.get(); 571 | } 572 | 573 | checkImageAdapterIsLoaded(); 574 | checkSelectedViewIsNotNull(); 575 | 576 | GalleryImageView selectedView = checkSelectedView(mCurrentVisibleChild); 577 | 578 | checkImageInformationIsntLoadedBeforImage(); 579 | 580 | checkImageLoaderTask(selectedView); 581 | checkPreLoading(selectedView, 2); 582 | // End Check Startup 583 | 584 | checkImageInformationIsLoadedCorrectly(galleryObject, comments, tags); 585 | 586 | for (int pos = 1; pos < 30; pos++) { 587 | galleryObject = children.get(pos); 588 | final GalleryDownloadObject downloadObject = galleryObject.getImage(); 589 | Log.d(CLASS_TAG, String.format("Simulating switching to %s", galleryObject)); 590 | 591 | if (pos % 5 == 0) { 592 | comments.add(new TestGalleryObjectComment("Some Author", String.format("Comment %d", pos))); 593 | 594 | tags.add("Some"); 595 | tags.add(String.format("Comment %d", pos)); 596 | 597 | mWebGallery.setupGetDisplayObjectCommentsCall(galleryObject, comments); 598 | mWebGallery.setupGetDisplayObjectTagsCall(galleryObject, tags); 599 | } 600 | 601 | selectImage(mGalleryFullscreen, pos); 602 | 603 | assertEquals("the gallery doesn't select the correct image", pos, mGalleryFullscreen.getSelectedItemPosition()); 604 | 605 | GalleryImageView imageView = (GalleryImageView) mGalleryFullscreen.getSelectedView(); 606 | checkSelectedView(children.get(pos)); 607 | checkImageInformationIsntLoadedBeforImage(); 608 | if (pos % 5 == 0) { 609 | TaskHelper taskHelper = new TaskHelper() { 610 | protected boolean checkCondition(long timeElapsed) { 611 | return mImageLoaderTask.isDownloading(downloadObject); 612 | } 613 | }; 614 | taskHelper.waitForExecution("ImageDownload isn'T enqueued"); 615 | 616 | while(mImageLoaderTask.getActiveDownload() == null || !downloadObject.equals(mImageLoaderTask.getActiveDownload().getDownloadObject())) { 617 | if(mImageLoaderTask.getActiveDownload()!=null) { 618 | mImageLoaderTask.cancelActiveDownload(true); 619 | } 620 | } 621 | 622 | 623 | checkImageLoaderTask(imageView); 624 | checkImageInformationIsLoadedCorrectly(galleryObject, comments, tags); 625 | } 626 | } 627 | 628 | checkForUneededDownloads(); 629 | } 630 | 631 | private void selectImage(final Gallery gallery,final int pos) throws Exception { 632 | final GalleryObject targetObject = mCurrentGallery.getChildren().get(pos); 633 | mInstrumentation.runOnMainSync(new Runnable() { 634 | 635 | @Override 636 | public void run() { 637 | gallery.setSelection(pos); 638 | } 639 | }); 640 | 641 | TaskHelper taskHelper = new TaskHelper() { 642 | protected boolean checkCondition(long timeElapsed) { 643 | GalleryImageView view = (GalleryImageView) gallery.getSelectedView(); 644 | if(view == null) { 645 | return false; 646 | } 647 | return view.getGalleryObject().equals(targetObject); 648 | } 649 | }; 650 | taskHelper.waitForExecution("Selecting an Image takes to long."); 651 | } 652 | 653 | private void flingGalleryAndCheckImageChange(final PointF dragFrom, final PointF dragTo) throws InterruptedException { 654 | final int prePos = mGalleryFullscreen.getSelectedItemPosition(); 655 | 656 | // waiting till the Gallery received the "switch" event 657 | TaskHelper taskHelper = new TaskHelper() { 658 | @Override 659 | protected boolean checkCondition(long diffTime) { 660 | 661 | if (diffTime % 1000 < 100) { 662 | simulateFlingGallery(mGalleryFullscreen, dragFrom, dragTo, 500); 663 | } 664 | int currentPos = mGalleryFullscreen.getSelectedItemPosition(); 665 | Log.d(CLASS_TAG, String.format("FlingGallery currentPos: %d lastPos: %d", currentPos, prePos)); 666 | return currentPos > prePos; 667 | } 668 | }; 669 | taskHelper.waitForExecution("Switching of an image takes to long"); 670 | } 671 | 672 | private void checkStartUp(int ImageInformationPanel) throws Exception { 673 | assertEquals("The FullscreenGallery has wrong Visibility", View.VISIBLE, mGalleryFullscreen.getVisibility()); 674 | assertEquals("The ThumbnailGallery has wrong Visibility", View.GONE, mGalleryThumbnails.getVisibility()); 675 | assertEquals("The ImageInformationPanel has wrong Visibility", ImageInformationPanel, mImageInformationView.getVisibility()); 676 | 677 | Log.d(CLASS_TAG, "Activity is set up correctly, waiting that the gallery is loaded..."); 678 | if (!mActivity.areGalleryObjectsAvailable()) { 679 | GalleryLoaderTask task = mActivity.getDownloadTask(); 680 | assertNotNull("No DownloadTask created although no gallery object could be loaded from the cache", task); 681 | 682 | task.get(); 683 | } 684 | 685 | checkImageAdapterIsLoaded(); 686 | checkSelectedViewIsNotNull(); 687 | 688 | Log.d(CLASS_TAG, "Gallery has requested an Image, check the image..."); 689 | GalleryImageView selectedView = checkSelectedView(mCurrentVisibleChild); 690 | checkImageLoaderTask(selectedView); 691 | checkPreLoading(selectedView, 2); 692 | } 693 | 694 | private void checkSelectedViewIsNotNull() throws InterruptedException { 695 | Log.d(CLASS_TAG, "Image adapter is loaded, wait that the Gallery requestes the first image..."); 696 | TaskHelper helper = new TaskHelper() { 697 | 698 | @Override 699 | protected boolean checkCondition(long diffTime) { 700 | return mGalleryFullscreen.getSelectedView() != null; 701 | } 702 | }; 703 | helper.waitForExecution("The Gallery don't select the first View in the given time"); 704 | } 705 | 706 | private void checkImageAdapterIsLoaded() throws InterruptedException { 707 | Log.d(CLASS_TAG, "Gallery is loaded, wait that the ImageAdapter get's loaded"); 708 | final ImageAdapter adapter = (ImageAdapter) mGalleryFullscreen.getAdapter(); 709 | 710 | TaskHelper helper = new TaskHelper() { 711 | 712 | @Override 713 | protected boolean checkCondition(long diffTime) { 714 | return adapter.isLoaded(); 715 | } 716 | }; 717 | helper.waitForExecution("Loading of the GalleryImageAdapter takes to long"); 718 | } 719 | 720 | private GalleryImageView checkSelectedView(GalleryObject galleryObject) { 721 | GalleryImageView selectedView = (GalleryImageView) mGalleryFullscreen.getSelectedView(); 722 | 723 | assertNotNull("No view selected", selectedView); 724 | assertEquals("The Gallery don't shows the inteted image", galleryObject, selectedView.getGalleryObject()); 725 | return selectedView; 726 | } 727 | 728 | private void checkPreLoading(GalleryImageView currentView, int minimalLookAhead) { 729 | GalleryObject currentObject = currentView.getGalleryObject(); 730 | GalleryDownloadObject downloadObject = currentObject.getImage(); 731 | List requestedObject = mWebGallery.getRequestedDownloadObjects(); 732 | 733 | int currentIndex = requestedObject.indexOf(downloadObject); 734 | assertTrue("Current GalleryObject was never requested", currentIndex > -1); 735 | assertTrue("No preloading requestes", currentIndex >= requestedObject.size() - 1); 736 | assertTrue("Not enough preloading requestes", currentIndex + minimalLookAhead >= requestedObject.size() - 1); 737 | } 738 | 739 | private void checkImageLoaderTask(final GalleryImageView galleryImageView) throws Exception { 740 | if (!galleryImageView.isLoaded()) { 741 | GalleryObject galleryObject = galleryImageView.getGalleryObject(); 742 | final GalleryDownloadObject downloadObject = galleryObject.getImage(); 743 | assertTrue(String.format("The DownloadObject isn't enqueued for the gallerImageView %s", galleryImageView.getGalleryObject().getObjectId()), mImageLoaderTask.isDownloading(downloadObject)); 744 | if (mWebGallery.isDownloadWaitHandleActive()) { 745 | 746 | Log.d(CLASS_TAG, "Checking ExtractorTask"); 747 | ImageInformationLoaderTask task = mImageInformationView.getImageInformationLoaderTask(); 748 | assertFalse(String.format("Information for GalleryObject %s is loaded befor Image is downloaded", galleryObject), task.isLoading(galleryObject)); 749 | 750 | Log.d(CLASS_TAG, "Release LoaderTask"); 751 | mWebGallery.releaseGetFileStream((TestDownloadObject) galleryObject.getImage()); 752 | } 753 | 754 | TaskHelper taskHelper = new TaskHelper() { 755 | 756 | @Override 757 | protected boolean checkCondition(long diffTime) { 758 | return !mImageLoaderTask.isDownloading(downloadObject); 759 | } 760 | }; 761 | taskHelper.waitForExecution(String.format("The Download for the gallerImageView %s isn't finished within the given time", galleryImageView.getGalleryObject().getObjectId())); 762 | 763 | taskHelper = new TaskHelper() { 764 | 765 | @Override 766 | protected boolean checkCondition(long diffTime) { 767 | return galleryImageView.isLoaded(); 768 | } 769 | }; 770 | taskHelper.waitForExecution(String.format("The GalleryImageView %s is not loaded but the imageLoaderTask ist finished", galleryImageView.getGalleryObject().getObjectId())); 771 | } 772 | } 773 | 774 | private void checkImageInformationIsntLoadedBeforImage() throws InterruptedException { 775 | Log.d(CLASS_TAG, "Checking that the ImageInformationView is reseted correctly"); 776 | TaskHelper taskHelper = new TaskHelper() { 777 | 778 | @Override 779 | protected boolean checkCondition(long timeElapsed) { 780 | return !mImageInformationView.isLoaded(); 781 | } 782 | }; 783 | taskHelper.waitForExecution("ImageInformation - ImageInformationView is loaded before ImageLoaderTask finished"); 784 | 785 | assertEquals("ImageInformation - The TagLoader-ProgressBar is visible before ImageLoaderTask finished", View.GONE, mImageInformationView.findViewById(R.id.progressBarTags).getVisibility()); 786 | assertEquals("ImageInformation - The CommentLoader-ProgressBar is visible before ImageLoaderTask finished", View.GONE, mImageInformationView.findViewById(R.id.progressBarComments).getVisibility()); 787 | 788 | TextView textView = (TextView) mImageInformationView.findViewById(R.id.textTitle); 789 | assertEquals("ImageInformation - There is a Title setted before ImageLoaderTask finished", "", textView.getText()); 790 | 791 | textView = (TextView) mImageInformationView.findViewById(R.id.textExifExposure); 792 | assertEquals("ImageInformation - There is are ExifInformation setted before ImageLoaderTask finished", "", textView.getText()); 793 | 794 | textView = (TextView) mImageInformationView.findViewById(R.id.textTags); 795 | assertEquals("ImageInformation - There is are Tags setted before ImageLoaderTask finished", "", textView.getText()); 796 | 797 | ViewGroup rootView = (ViewGroup) mImageInformationView.findViewById(R.id.layoutComments); 798 | assertEquals("ImageInformation - There are comments displayed before ImageLoaderTask finished", 0, rootView.getChildCount()); 799 | } 800 | 801 | private void checkImageInformationIsLoadedCorrectly(final TestGalleryObject galleryObject, List comments, List tags) throws InterruptedException, ExecutionException, Exception { 802 | if (mWebGallery.isDownloadWaitHandleActive()) { 803 | final ImageInformationLoaderTask task = mImageInformationView.getImageInformationLoaderTask(); 804 | TaskHelper taskHelper = new TaskHelper() { 805 | protected boolean checkCondition(long diffTime) { 806 | return task.isLoading(galleryObject); 807 | } 808 | }; 809 | taskHelper.waitForExecution(String.format("%s isn't enqueued for loading ImageInformation in the given time, altough the ImageLoaderTask is finished", galleryObject)); 810 | 811 | taskHelper = new TaskHelper() { 812 | protected boolean checkCondition(long diffTime) { 813 | return mImageInformationView.areImageInformationsLoaded(); 814 | } 815 | }; 816 | taskHelper.waitForExecution(String.format("The loading of the ImageInformations for GalleryObject %s isn't finished within the given time", galleryObject.getObjectId())); 817 | 818 | Log.d(CLASS_TAG, String.format("Checks that the ImageInformation of %s are loaded correctly", galleryObject)); 819 | assertEquals("ImageInformation - TagLoader-ProgressBar wrong Visibility", View.VISIBLE, mImageInformationView.findViewById(R.id.progressBarTags).getVisibility()); 820 | assertEquals("ImageInformation - CommentLoader-ProgressBar wrong Visibility", View.VISIBLE, mImageInformationView.findViewById(R.id.progressBarComments).getVisibility()); 821 | checkImageInformationIsLoaded(galleryObject); 822 | 823 | TextView textView = (TextView) mImageInformationView.findViewById(R.id.textTags); 824 | assertEquals("ImageInformation - Tags are not reseted correctly", "", textView.getText().toString()); 825 | 826 | mWebGallery.releaseGetGetDisplayObjectTags(galleryObject); 827 | taskHelper = new TaskHelper() { 828 | 829 | @Override 830 | protected boolean checkCondition(long diffTime) { 831 | return mImageInformationView.areTagsLoaded(); 832 | } 833 | }; 834 | taskHelper.waitForExecution(String.format("The loading of the Tags for GalleryObject %s isn't finished within the given time", galleryObject.getObjectId())); 835 | 836 | assertEquals("ImageInformation - TagLoader-ProgressBar wrong Visibility", View.GONE, mImageInformationView.findViewById(R.id.progressBarTags).getVisibility()); 837 | assertEquals("ImageInformation - CommentLoader-ProgressBar wrong Visibility", View.VISIBLE, mImageInformationView.findViewById(R.id.progressBarComments).getVisibility()); 838 | 839 | checkTagAreLoaded(tags); 840 | 841 | mWebGallery.releaseGetDisplayObjectComments(galleryObject); 842 | 843 | taskHelper = new TaskHelper() { 844 | 845 | @Override 846 | protected boolean checkCondition(long diffTime) { 847 | return mImageInformationView.areCommentsLoaded(); 848 | } 849 | }; 850 | taskHelper.waitForExecution(String.format("The loading of the Comments for GalleryObject %s isn't finished within the given time", galleryObject.getObjectId())); 851 | 852 | assertEquals("ImageInformation - TagLoader-ProgressBar wrong Visibility", View.GONE, mImageInformationView.findViewById(R.id.progressBarTags).getVisibility()); 853 | assertEquals("ImageInformation - CommentLoader-ProgressBar wrong Visibility", View.GONE, mImageInformationView.findViewById(R.id.progressBarComments).getVisibility()); 854 | 855 | checkCommentsAreLoaded(comments); 856 | } else { 857 | if (!mImageInformationView.isLoaded()) { 858 | Log.d(CLASS_TAG, String.format("Wait for the ImageInformationLoaderTask to load %s", galleryObject)); 859 | TaskHelper taskHelper = new TaskHelper() { 860 | 861 | @Override 862 | protected boolean checkCondition(long diffTime) { 863 | return mImageInformationView.isLoaded(); 864 | } 865 | }; 866 | taskHelper.waitForExecution(String.format("The loading of Imageinformation for the GalleryObject %s isn't finished within the given time", galleryObject.getObjectId())); 867 | } 868 | 869 | Log.d(CLASS_TAG, String.format("Checks that the ImageInformation of %s are loaded correctly", galleryObject)); 870 | assertEquals("ImageInformation - The TagLoader-ProgressBar is visible although ImageLoaderTask finished", View.GONE, mImageInformationView.findViewById(R.id.progressBarTags).getVisibility()); 871 | assertEquals("ImageInformation - The CommentLoader-ProgressBar is visible although ImageLoaderTask finished", View.GONE, mImageInformationView.findViewById(R.id.progressBarComments) 872 | .getVisibility()); 873 | 874 | checkImageInformationIsLoaded(galleryObject); 875 | checkTagAreLoaded(tags); 876 | checkCommentsAreLoaded(comments); 877 | } 878 | assertNull("ImageInformation - The ImageInformationView still listening to changes altough it is finished loading", mImageInformationView.getCurrentListenedImageView()); 879 | assertNull("ImageInformation - The ImageInformationView still remember it last LoadingItam altough it is finished loading", mImageInformationView.getCurrentLoadingObject()); 880 | } 881 | 882 | private void checkImageInformationIsLoaded(TestGalleryObject galleryObject) throws Exception { 883 | checkEmbededInformationIsLoaded(galleryObject); 884 | checkExifInformationIsLoaded(); 885 | } 886 | 887 | private void checkEmbededInformationIsLoaded(TestGalleryObject galleryObject) { 888 | TextView textView = (TextView) mImageInformationView.findViewById(R.id.textTitle); 889 | assertEquals("ImageInformation - Title is set wrong", galleryObject.getTitle(), textView.getText().toString()); 890 | 891 | textView = (TextView) mImageInformationView.findViewById(R.id.textUploadDate); 892 | Log.d(CLASS_TAG, String.format("%s - %s", galleryObject.getDateUploaded().toLocaleString(), textView.getText().toString())); 893 | assertEquals("ImageInformation - UploadDate is set wrong", galleryObject.getDateUploaded().toLocaleString(), textView.getText().toString()); 894 | } 895 | 896 | private void checkExifInformationIsLoaded() { 897 | TextView textField; 898 | textField = (TextView) mImageInformationView.findViewById(R.id.textExifCreateDate); 899 | textField = (TextView) mImageInformationView.findViewById(R.id.textExifAperture); 900 | textField = (TextView) mImageInformationView.findViewById(R.id.textExifExposure); 901 | textField = (TextView) mImageInformationView.findViewById(R.id.textExifFlash); 902 | textField = (TextView) mImageInformationView.findViewById(R.id.textExifISO); 903 | textField = (TextView) mImageInformationView.findViewById(R.id.textExifModel); 904 | textField = (TextView) mImageInformationView.findViewById(R.id.textExifModel); 905 | textField = (TextView) mImageInformationView.findViewById(R.id.textExifMake); 906 | textField = (TextView) mImageInformationView.findViewById(R.id.textExifFocalLength); 907 | textField = (TextView) mImageInformationView.findViewById(R.id.textExifWhiteBalance); 908 | textField = (TextView) mImageInformationView.findViewById(R.id.textGeoLat); 909 | textField = (TextView) mImageInformationView.findViewById(R.id.textGeoLong); 910 | textField = (TextView) mImageInformationView.findViewById(R.id.textGeoHeight); 911 | } 912 | 913 | private void checkCommentsAreLoaded(List comments) throws Exception { 914 | ViewGroup rootView = (ViewGroup) mImageInformationView.findViewById(R.id.layoutComments); 915 | assertEquals("ImageInformation - Commentscount isn't correct.", comments.size(), rootView.getChildCount()); 916 | DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.MEDIUM, Locale.getDefault()); 917 | for (int count = 0; count < rootView.getChildCount(); count++) { 918 | View commentView = rootView.getChildAt(count); 919 | GalleryObjectComment comment = comments.get(count); 920 | 921 | TextView textAuthor = (TextView) commentView.findViewById(R.id.textCommentAuthor); 922 | TextView textDate = (TextView) commentView.findViewById(R.id.textCommentPosted); 923 | TextView textMessage = (TextView) commentView.findViewById(R.id.textCommentMessage); 924 | 925 | assertEquals("ImageInformation - Comment-Author is set wrong", comment.getAuthorName(), textAuthor.getText().toString()); 926 | assertEquals("ImageInformation - Comment-Date is set wrong", dateFormat.format(comment.getCreateDate()), textDate.getText().toString()); 927 | assertEquals("ImageInformation - Comment-Message is set wrong", comment.getMessage(), textMessage.getText().toString()); 928 | } 929 | } 930 | 931 | private void checkTagAreLoaded(List tags) throws Exception { 932 | 933 | StringBuilder stringBuilder = new StringBuilder(tags.size() * 10); 934 | for (String tag : tags) { 935 | stringBuilder.append(String.format("%s, ", tag)); 936 | } 937 | int length = stringBuilder.length(); 938 | if (length > 0) { 939 | stringBuilder.delete(stringBuilder.length() - 2, stringBuilder.length()); 940 | } 941 | 942 | TextView textView = (TextView) mImageInformationView.findViewById(R.id.textTags); 943 | assertEquals("ImageInformation - Tags are set wrong", stringBuilder.toString(), textView.getText().toString()); 944 | } 945 | 946 | private TestWebGallery createTestWebGallery(Resources resources) { 947 | TestWebGallery webGallery = new TestWebGallery(resources); 948 | 949 | List children = new ArrayList(GALLERY_SAMPLE_SIZE); 950 | Random random = new Random(); 951 | for (int i = 0; i < GALLERY_SAMPLE_SIZE; i++) { 952 | String objectId = String.format("TestGalObject %d-%d", i, random.nextInt(10000)); 953 | Date currentDate = new Date(System.currentTimeMillis()); 954 | 955 | children.add(new TestGalleryObject(objectId, objectId, currentDate, IMAGE_ID, null)); 956 | } 957 | String objectId = "ParentObject"; 958 | Date currentDate = new Date(System.currentTimeMillis()); 959 | List parents = new ArrayList(1); 960 | parents.add(new TestGalleryObject(objectId, objectId, currentDate, IMAGE_ID, children)); 961 | 962 | webGallery.setTestGalleryObjects(parents); 963 | 964 | mCurrentGallery = parents.get(0); 965 | mCurrentVisibleChild = children.get(0); 966 | return webGallery; 967 | } 968 | 969 | private void simulateDragGallery(PointF from, PointF to) { 970 | long startTime = SystemClock.uptimeMillis() - 1000; 971 | MotionEvent startEvent = MotionEvent.obtain(startTime, startTime, MotionEvent.ACTION_DOWN, from.x, from.y, 0); 972 | MotionEvent stopEvent = MotionEvent.obtain(startTime, SystemClock.uptimeMillis(), MotionEvent.ACTION_UP, to.x, to.y, 0); 973 | mInstrumentation.sendPointerSync(startEvent); 974 | mInstrumentation.sendPointerSync(stopEvent); 975 | } 976 | 977 | private void simulateFlingGallery(Gallery gallery, PointF from, PointF to, int time) { 978 | long startTime = SystemClock.uptimeMillis() - time; 979 | MotionEvent startEvent = MotionEvent.obtain(startTime, startTime, MotionEvent.ACTION_DOWN, from.x, from.y, 0); 980 | MotionEvent stopEvent = MotionEvent.obtain(startTime, SystemClock.uptimeMillis(), MotionEvent.ACTION_UP, to.x, to.y, 0); 981 | float seconds = time / 1000f; 982 | float velocityX = (to.x - from.x) / seconds; 983 | float velocityY = (to.y - from.y) / seconds; 984 | gallery.onFling(startEvent, stopEvent, velocityX, velocityY); 985 | } 986 | } 987 | --------------------------------------------------------------------------------