LEFT | RIGHT |
(no file at all) | |
| 1 // Copyright 2011 Google Inc. All Rights Reserved. |
| 2 // |
| 3 // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 // you may not use this file except in compliance with the License. |
| 5 // You may obtain a copy of the License at |
| 6 // |
| 7 // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 // |
| 9 // Unless required by applicable law or agreed to in writing, software |
| 10 // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 // See the License for the specific language governing permissions and |
| 13 // limitations under the License. |
| 14 |
| 15 package com.google.android.apps.analytics.easytracking; |
| 16 |
| 17 import com.google.android.apps.analytics.Item; |
| 18 import com.google.android.apps.analytics.Transaction; |
| 19 import com.google.android.apps.analytics.easytracking.GoogleAnalyticsTrackerDele
gate; |
| 20 |
| 21 import android.content.Context; |
| 22 |
| 23 import java.util.HashMap; |
| 24 import java.util.Map; |
| 25 |
| 26 /** |
| 27 * Mock of implementation of the GoogleAnalyticsTrackerDelegate interface used |
| 28 * for testing the EasyTracking library. This Mock tracks the number of calls |
| 29 * made for each method that handles tracking and also retains the values of |
| 30 * parameters set (like debug or dryRun). It provides getter methods to inspect |
| 31 * those parameters. Used for testing the EasyTracker class. |
| 32 */ |
| 33 public class MockGoogleAnalyticsTracker implements GoogleAnalyticsTrackerDelegat
e { |
| 34 ·· |
| 35 private boolean debug; |
| 36 private boolean dryRun; |
| 37 private int sampleRate; |
| 38 private boolean anonymizeIp; |
| 39 private int dispatchPeriod; |
| 40 private String accountId; |
| 41 private Map<String, Integer>callsMade = new HashMap<String, Integer>(); |
| 42 |
| 43 public String getAccountId() { |
| 44 return this.accountId; |
| 45 } |
| 46 ·· |
| 47 public int getDispatchPeriod() { |
| 48 return this.dispatchPeriod; |
| 49 } |
| 50 |
| 51 public Map<String, Integer> getCallsMade() { |
| 52 return callsMade; |
| 53 } |
| 54 |
| 55 /** |
| 56 * Returns the number of calls made for the method described by the input |
| 57 * parameter methodName. |
| 58 * |
| 59 * @param methodName |
| 60 * @return the number of calls made, or 0 if none. |
| 61 */ |
| 62 public int getNumberCallsMade(String methodName) { |
| 63 if (callsMade.containsKey(methodName)) { |
| 64 return callsMade.get(methodName); |
| 65 } else { |
| 66 return 0; |
| 67 } |
| 68 } |
| 69 |
| 70 /** |
| 71 * Increment the number of calls made for the method described by the input |
| 72 * parameter callMade. |
| 73 * |
| 74 * @param callMade name of the method call |
| 75 */ |
| 76 private void incrementCallsMade(String callMade) { |
| 77 int previousCalls = 0; |
| 78 if (callsMade.containsKey(callMade)) { |
| 79 previousCalls = callsMade.get(callMade); |
| 80 } |
| 81 callsMade.put(callMade, previousCalls + 1); |
| 82 } |
| 83 |
| 84 @Override |
| 85 public void startNewSession(String accountId, int dispatchPeriod, Context ctx)
{ |
| 86 incrementCallsMade("startNewSession"); |
| 87 this.accountId = accountId; |
| 88 this.dispatchPeriod = dispatchPeriod; |
| 89 } |
| 90 |
| 91 @Override |
| 92 public void trackEvent(String category, String action, String label, int value
) { |
| 93 incrementCallsMade("trackEvent"); |
| 94 } |
| 95 |
| 96 @Override |
| 97 public void trackPageView(String pageUrl) { |
| 98 incrementCallsMade("trackPageView"); |
| 99 } |
| 100 |
| 101 @Override |
| 102 public boolean dispatch() { |
| 103 incrementCallsMade("dispatch"); |
| 104 return false; |
| 105 } |
| 106 |
| 107 @Override |
| 108 public void stopSession() { |
| 109 incrementCallsMade("stopSession"); |
| 110 } |
| 111 |
| 112 @Override |
| 113 public boolean setCustomVar(int index, String name, String value, int scope) { |
| 114 // Not used |
| 115 return false; |
| 116 } |
| 117 |
| 118 @Override |
| 119 public boolean setCustomVar(int index, String name, String value) { |
| 120 // Not used |
| 121 return false; |
| 122 } |
| 123 |
| 124 @Override |
| 125 public void addTransaction(Transaction transaction) { |
| 126 // Not used |
| 127 } |
| 128 |
| 129 @Override |
| 130 public void addItem(Item item) { |
| 131 // Not used |
| 132 } |
| 133 |
| 134 @Override |
| 135 public void trackTransactions() { |
| 136 // Not used |
| 137 clearTransactions(); |
| 138 } |
| 139 |
| 140 @Override |
| 141 public void clearTransactions() { |
| 142 // Not used |
| 143 } |
| 144 |
| 145 @Override |
| 146 public void setAnonymizeIp(boolean anonymizeIp) { |
| 147 this.anonymizeIp = anonymizeIp; |
| 148 } |
| 149 |
| 150 public boolean getAnonymizeIp() { |
| 151 return this.anonymizeIp; |
| 152 } |
| 153 |
| 154 @Override |
| 155 public void setSampleRate(int sampleRate) { |
| 156 this.sampleRate = sampleRate; |
| 157 } |
| 158 |
| 159 public int getSampleRate() { |
| 160 return this.sampleRate; |
| 161 } |
| 162 |
| 163 @Override |
| 164 public boolean setReferrer(String referrer) { |
| 165 // Not used |
| 166 return false; |
| 167 } |
| 168 |
| 169 @Override |
| 170 public void setDebug(boolean debug) { |
| 171 this.debug = debug; |
| 172 } |
| 173 |
| 174 public boolean getDebug() { |
| 175 return this.debug; |
| 176 } |
| 177 |
| 178 @Override |
| 179 public void setDryRun(boolean dryRun) { |
| 180 this.dryRun = dryRun; |
| 181 } |
| 182 |
| 183 public boolean isDryRun() { |
| 184 return this.dryRun; |
| 185 } |
| 186 |
| 187 } |
LEFT | RIGHT |