|
| 1 | +/** |
| 2 | + * Copyright (C) 2010-2014 eBusiness Information, Excilys Group |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 5 | + * use this file except in compliance with the License. You may obtain a copy of |
| 6 | + * 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, WITHOUT |
| 12 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | + * License for the specific language governing permissions and limitations under |
| 14 | + * the License. |
| 15 | + */ |
| 16 | +package org.androidannotations.test15.trace; |
| 17 | + |
| 18 | +import static org.fest.assertions.Assertions.assertThat; |
| 19 | +import static org.junit.Assert.assertTrue; |
| 20 | + |
| 21 | +import java.io.IOException; |
| 22 | +import java.util.List; |
| 23 | + |
| 24 | +import org.junit.Before; |
| 25 | +import org.junit.Test; |
| 26 | +import org.junit.runner.RunWith; |
| 27 | +import org.robolectric.Robolectric; |
| 28 | +import org.robolectric.RobolectricTestRunner; |
| 29 | +import org.robolectric.shadows.ShadowLog; |
| 30 | +import org.robolectric.shadows.ShadowLog.LogItem; |
| 31 | + |
| 32 | +import android.content.Intent; |
| 33 | +import android.net.Uri; |
| 34 | + |
| 35 | +@RunWith(RobolectricTestRunner.class) |
| 36 | +public class TracedActivityTest { |
| 37 | + |
| 38 | + private TracedActivity activity; |
| 39 | + |
| 40 | + @Before |
| 41 | + public void setup() { |
| 42 | + activity = Robolectric.buildActivity(TracedActivity_.class).create() |
| 43 | + .start().resume().get(); |
| 44 | + } |
| 45 | + |
| 46 | + @Test |
| 47 | + public void servicesAreInjected() throws IOException { |
| 48 | + |
| 49 | + assertThat(activity.tracedMethodCalled).isFalse(); |
| 50 | + activity.tracedMethod(null, null); |
| 51 | + assertThat(activity.tracedMethodCalled).isTrue(); |
| 52 | + |
| 53 | + assertThat(activity.voidTracedMethodCalled).isFalse(); |
| 54 | + activity.voidTracedMethod(null, null); |
| 55 | + assertThat(activity.voidTracedMethodCalled).isTrue(); |
| 56 | + |
| 57 | + assertThat(activity.voidTracedMethodDebugCalled).isFalse(); |
| 58 | + activity.voidTracedMethodDebug(); |
| 59 | + assertThat(activity.voidTracedMethodDebugCalled).isTrue(); |
| 60 | + |
| 61 | + assertThat(activity.voidTracedMethodErrorCalled).isFalse(); |
| 62 | + activity.voidTracedMethodError(); |
| 63 | + assertThat(activity.voidTracedMethodErrorCalled).isTrue(); |
| 64 | + |
| 65 | + assertThat(activity.voidTracedMethodInfoCalled).isFalse(); |
| 66 | + activity.voidTracedMethodInfo(); |
| 67 | + assertThat(activity.voidTracedMethodInfoCalled).isTrue(); |
| 68 | + |
| 69 | + assertThat(activity.voidTracedMethodVerboseCalled).isFalse(); |
| 70 | + activity.voidTracedMethodVerbose(); |
| 71 | + assertThat(activity.voidTracedMethodVerboseCalled).isTrue(); |
| 72 | + |
| 73 | + assertThat(activity.voidTracedMethodWarnCalled).isFalse(); |
| 74 | + activity.voidTracedMethodWarn(); |
| 75 | + assertThat(activity.voidTracedMethodWarnCalled).isTrue(); |
| 76 | + |
| 77 | + assertThat(activity.overloadedMethodInt).isFalse(); |
| 78 | + activity.overloadedMethod(0); |
| 79 | + assertThat(activity.overloadedMethodInt).isTrue(); |
| 80 | + |
| 81 | + assertThat(activity.overloadedMethodIntFLoat).isFalse(); |
| 82 | + activity.overloadedMethod(0, 0f); |
| 83 | + assertThat(activity.overloadedMethodIntFLoat).isTrue(); |
| 84 | + } |
| 85 | + |
| 86 | + @Test |
| 87 | + public void noReturnNoParam() { |
| 88 | + activity.noReturnNoParam(); |
| 89 | + |
| 90 | + assertTrue(logContains("Entering [void noReturnNoParam()]")); |
| 91 | + assertTrue(logContains("Exiting [void noReturnNoParam()], duration in ms: ")); |
| 92 | + } |
| 93 | + |
| 94 | + @Test |
| 95 | + public void noReturnStringParam() { |
| 96 | + activity.noReturnStringParam("test"); |
| 97 | + |
| 98 | + assertTrue(logContains("Entering [void noReturnStringParam(param = test)]")); |
| 99 | + assertTrue(logContains("Exiting [void noReturnStringParam(String)], duration in ms: ")); |
| 100 | + } |
| 101 | + |
| 102 | + @Test |
| 103 | + public void noReturnIntArrayParam() { |
| 104 | + activity.noReturnIntArrayParam(new int[] { 1, 2, 3 }); |
| 105 | + |
| 106 | + assertTrue(logContains("Entering [void noReturnIntArrayParam(param = [1, 2, 3])]")); |
| 107 | + assertTrue(logContains("Exiting [void noReturnIntArrayParam(int[])], duration in ms: ")); |
| 108 | + } |
| 109 | + |
| 110 | + @Test |
| 111 | + public void noReturnStringAndIntArrayParam() { |
| 112 | + activity.noReturnStringAndIntArrayParam("test", new int[] { 1, 2, 3 }); |
| 113 | + |
| 114 | + assertTrue(logContains("Entering [void noReturnStringAndIntArrayParam(param1 = test, param2 = [1, 2, 3])]")); |
| 115 | + assertTrue(logContains("Exiting [void noReturnStringAndIntArrayParam(String, int[])], duration in ms: ")); |
| 116 | + } |
| 117 | + |
| 118 | + @Test |
| 119 | + public void noReturnIntentParam() { |
| 120 | + activity.noReturnIntentParam(new Intent("TEST", |
| 121 | + Uri.parse("http://www.androidannotations.org"))); |
| 122 | + |
| 123 | + assertTrue(logContains("Entering [void noReturnIntentParam(param = Intent{action=TEST, extras=Bundle[{}], data=http://www.androidannotations.org})]")); |
| 124 | + assertTrue(logContains("Exiting [void noReturnIntentParam(Intent)], duration in ms: ")); |
| 125 | + } |
| 126 | + |
| 127 | + @Test |
| 128 | + public void booleanReturnNoParam() { |
| 129 | + activity.booleanReturnNoParam(); |
| 130 | + |
| 131 | + assertTrue(logContains("Entering [boolean booleanReturnNoParam()]")); |
| 132 | + assertTrue(logContains("Exiting [boolean booleanReturnNoParam() returning: true], duration in ms: ")); |
| 133 | + } |
| 134 | + |
| 135 | + @Test |
| 136 | + public void stringReturnStringParam() { |
| 137 | + activity.stringReturnStringParam("test"); |
| 138 | + |
| 139 | + assertTrue(logContains("Entering [java.lang.String stringReturnStringParam(param = test)]")); |
| 140 | + assertTrue(logContains("Exiting [java.lang.String stringReturnStringParam(String) returning: test], duration in ms: ")); |
| 141 | + } |
| 142 | + |
| 143 | + public void intArrayReturnIntArrayParam() { |
| 144 | + activity.intArrayReturnIntArrayParam(new int[] { 1, 2, 3 }); |
| 145 | + |
| 146 | + assertTrue(logContains("Entering [int[] intArrayReturnIntArrayParam(param = [1, 2, 3])]")); |
| 147 | + assertTrue(logContains("Exiting [int[] intArrayReturnIntArrayParam(int[]) returning: [1, 2, 3]], duration in ms: ")); |
| 148 | + } |
| 149 | + |
| 150 | + public void intentReturnIntentParam() { |
| 151 | + activity.intentReturnIntentParam(new Intent("TEST", |
| 152 | + Uri.parse("http://www.androidannotations.org"))); |
| 153 | + |
| 154 | + assertTrue(logContains("Entering [java.lang.String intentReturnIntentParam(param = Intent{action=TEST, extras=Bundle[{}], data=http://www.androidannotations.org})]")); |
| 155 | + assertTrue(logContains("Exiting [java.lang.String intentReturnIntentParam(Intent) returning: test], duration in ms: ")); |
| 156 | + } |
| 157 | + |
| 158 | + /** |
| 159 | + * Check if a message has been logged |
| 160 | + * |
| 161 | + * @param msg |
| 162 | + * @return {@code true} if a log entry starting with {@code msg} exists |
| 163 | + */ |
| 164 | + private static boolean logContains(String msg) { |
| 165 | + List<LogItem> logs = ShadowLog.getLogs(); |
| 166 | + boolean found = false; |
| 167 | + for (LogItem logItem : logs) { |
| 168 | + if (logItem.msg.startsWith(msg)) |
| 169 | + found = true; |
| 170 | + } |
| 171 | + return found; |
| 172 | + } |
| 173 | +} |
0 commit comments