@Test public void equalsHashCode() { /* Empty objects. */ UserExtension a = new UserExtension(); UserExtension b = new UserExtension(); checkEquals(a, b); /* Locale. */ a.setLocale("a1"); checkNotEquals(a, b); b.setLocale("b1"); checkNotEquals(a, b); b.setLocale("a1"); checkEquals(a, b); /* LocalId */ a.setLocalId("id-a"); checkNotEquals(a, b); b.setLocalId("id-b"); checkNotEquals(a, b); b.setLocalId("id-a"); checkEquals(a, b); } }
@Override public void write(JSONStringer writer) throws JSONException { JSONUtils.write(writer, LOCAL_ID, getLocalId()); JSONUtils.write(writer, LOCALE, getLocale()); }
@Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Extensions that = (Extensions) o; if (metadata != null ? !metadata.equals(that.metadata) : that.metadata != null) return false; if (protocol != null ? !protocol.equals(that.protocol) : that.protocol != null) return false; if (user != null ? !user.equals(that.user) : that.user != null) return false; if (device != null ? !device.equals(that.device) : that.device != null) return false; if (os != null ? !os.equals(that.os) : that.os != null) return false; if (app != null ? !app.equals(that.app) : that.app != null) return false; if (net != null ? !net.equals(that.net) : that.net != null) return false; if (sdk != null ? !sdk.equals(that.sdk) : that.sdk != null) return false; return loc != null ? loc.equals(that.loc) : that.loc == null; }
@Override public void read(JSONObject object) { setLocalId(object.optString(LOCAL_ID, null)); setLocale(object.optString(LOCALE, null)); }
@Test public void setNonPrefixUserId() { CommonSchemaLog log = new CommonSchemaEventLog(); log.setExt(new Extensions()); log.getExt().setUser(new UserExtension()); /* Get property configurator and set properties. */ PropertyConfigurator pc = Analytics.getTransmissionTarget("test").getPropertyConfigurator(); pc.setUserId("bob"); /* Simulate what the pipeline does to convert from App Center to Common Schema. */ log.addTransmissionTarget("test"); log.setTag(Analytics.getTransmissionTarget("test")); pc.onPreparingLog(log, "groupName"); /* Check prefix was added. */ assertEquals("c:bob", log.getExt().getUser().getLocalId()); }
UserExtension user = new UserExtension(); user.setLocale("fr-FR"); a.setUser(user); checkNotEquals(a, b); b.setUser(new UserExtension()); checkNotEquals(a, b); b.setUser(a.getUser());
UserExtension user = new UserExtension(); user.read(object.getJSONObject(USER)); setUser(user);
@Test public void compareDifferentType() { TestUtils.compareSelfNullClass(new UserExtension()); }
user.setLocalId(mUserId); } else { for (AnalyticsTransmissionTarget target = mTransmissionTarget.mParentTarget; target != null; target = target.mParentTarget) { String parentUserId = target.getPropertyConfigurator().getUserId(); if (parentUserId != null) { user.setLocalId(parentUserId); break;
@Override public int hashCode() { int result = metadata != null ? metadata.hashCode() : 0; result = 31 * result + (protocol != null ? protocol.hashCode() : 0); result = 31 * result + (user != null ? user.hashCode() : 0); result = 31 * result + (device != null ? device.hashCode() : 0); result = 31 * result + (os != null ? os.hashCode() : 0); result = 31 * result + (app != null ? app.hashCode() : 0); result = 31 * result + (net != null ? net.hashCode() : 0); result = 31 * result + (sdk != null ? sdk.hashCode() : 0); result = 31 * result + (loc != null ? loc.hashCode() : 0); return result; } }
getUser().write(writer); writer.endObject();
@Test public void setInvalidUserId() { CommonSchemaLog log = new CommonSchemaEventLog(); log.setExt(new Extensions()); log.getExt().setApp(new AppExtension()); log.getExt().setUser(new UserExtension()); /* Get property configurator and set properties. */ PropertyConfigurator pc = Analytics.getTransmissionTarget("test").getPropertyConfigurator(); pc.setUserId("x:bob"); /* Simulate what the pipeline does to convert from App Center to Common Schema. */ log.addTransmissionTarget("test"); log.setTag(Analytics.getTransmissionTarget("test")); pc.onPreparingLog(log, "groupName"); /* Assert property not set on common schema. */ assertNull(log.getExt().getUser().getLocalId()); /* Set user id with just the prefix. */ pc.setUserId("c:"); /* Assert property not set on common schema. */ assertNull(log.getExt().getUser().getLocalId()); /* Set empty user id. */ pc.setUserId(""); /* Assert property not set on common schema. */ assertNull(log.getExt().getUser().getLocalId()); }
@Test public void defaultTargetIsNotReturnedFromGetTransmissionTarget() { /* Start the application with a token. */ Analytics analytics = Analytics.getInstance(); analytics.onStarting(mAppCenterHandler); String defaultToken = "test"; analytics.onStarted(mock(Context.class), mChannel, null, defaultToken, true); /* Create the test target with the same default token. */ AnalyticsTransmissionTarget target = Analytics.getTransmissionTarget(defaultToken); /* Verify it's not the same instance. */ assertNotSame(target, analytics.mDefaultTransmissionTarget); /* Set a Part A property on the target. */ target.getPropertyConfigurator().setAppName("someName"); /* Simulate what the pipeline does to convert from App Center to Common Schema. */ CommonSchemaLog log = new CommonSchemaEventLog(); log.setExt(new Extensions()); log.getExt().setApp(new AppExtension()); log.getExt().setUser(new UserExtension()); log.addTransmissionTarget("test"); log.setTag(target); /* When the callback is called on the default target. */ analytics.mDefaultTransmissionTarget.getPropertyConfigurator().onPreparingLog(log, "groupName"); /* Then the log is not modified. */ assertNull(log.getExt().getApp().getName()); /* When the callback is called on the returned target. */ target.getPropertyConfigurator().onPreparingLog(log, "groupName"); /* Check the property is added to the log. */ assertEquals("someName", log.getExt().getApp().getName()); }
@Test public void unsetUserId() { CommonSchemaLog log = new CommonSchemaEventLog(); log.setExt(new Extensions()); log.getExt().setApp(new AppExtension()); log.getExt().setUser(new UserExtension()); /* Get property configurator and set properties. */ PropertyConfigurator pc = Analytics.getTransmissionTarget("test").getPropertyConfigurator(); pc.setUserId("c:bob"); /* Simulate what the pipeline does to convert from App Center to Common Schema. */ log.addTransmissionTarget("test"); log.setTag(Analytics.getTransmissionTarget("test")); pc.onPreparingLog(log, "groupName"); /* Assert properties set on common schema. */ assertEquals("c:bob", log.getExt().getUser().getLocalId()); /* Create second log. */ CommonSchemaLog log2 = new CommonSchemaEventLog(); log2.setExt(new Extensions()); log2.getExt().setApp(new AppExtension()); log2.getExt().setUser(new UserExtension()); /* Un-set user ID. */ pc.setUserId(null); /* Simulate what the pipeline does to convert from App Center to Common Schema. */ log2.addTransmissionTarget("test"); log2.setTag(Analytics.getTransmissionTarget("test")); pc.onPreparingLog(log2, "groupName"); /* Assert properties set on common schema. */ assertNull(log2.getExt().getUser().getLocalId()); }
dest.getExt().setUser(new UserExtension()); dest.getExt().getUser().setLocalId(UserIdContext.getPrefixedUserId(src.getUserId())); dest.getExt().getUser().setLocale(device.getLocale().replace("_", "-"));
assertEquals("oemName", commonSchemaLog.getExt().getProtocol().getDevMake()); assertNotNull(commonSchemaLog.getExt().getUser()); assertEquals("c:alice", commonSchemaLog.getExt().getUser().getLocalId()); assertEquals("en-US", commonSchemaLog.getExt().getUser().getLocale()); assertNotNull(commonSchemaLog.getExt().getOs()); assertEquals("osName", commonSchemaLog.getExt().getOs().getName());
logBeforeSetProperty.setExt(new Extensions()); logBeforeSetProperty.getExt().setApp(new AppExtension()); logBeforeSetProperty.getExt().setUser(new UserExtension()); logBeforeSetProperty.getExt().setDevice(new DeviceExtension()); logAfterSetProperty.setExt(new Extensions()); logAfterSetProperty.getExt().setApp(new AppExtension()); logAfterSetProperty.getExt().setUser(new UserExtension()); logAfterSetProperty.getExt().setDevice(new DeviceExtension()); assertNull(logBeforeSetProperty.getExt().getApp().getLocale()); assertNull(logBeforeSetProperty.getExt().getApp().getName()); assertNull(logBeforeSetProperty.getExt().getUser().getLocalId()); assertNull(logBeforeSetProperty.getExt().getDevice().getLocalId()); assertEquals("appLocale", logAfterSetProperty.getExt().getApp().getLocale()); assertEquals("appName", logAfterSetProperty.getExt().getApp().getName()); assertEquals("c:alice", logAfterSetProperty.getExt().getUser().getLocalId()); assertEquals("a:mockDeviceId", logAfterSetProperty.getExt().getDevice().getLocalId());
log.getExt().setUser(new UserExtension()); checkSerialization(serializer, log); log.getExt().getUser().setLocalId("d:1234"); checkSerialization(serializer, log); log.getExt().getUser().setLocale("en-US"); checkSerialization(serializer, log);
@Test public void setCommonSchemaProperties() { CommonSchemaLog log = new CommonSchemaEventLog(); log.setExt(new Extensions()); log.getExt().setApp(new AppExtension()); log.getExt().setUser(new UserExtension()); /* Get property configurator and set properties. */ PropertyConfigurator pc = Analytics.getTransmissionTarget("test").getPropertyConfigurator(); pc.setAppVersion("appVersion"); pc.setAppName("appName"); pc.setAppLocale("appLocale"); pc.setUserId("c:bob"); /* Simulate what the pipeline does to convert from App Center to Common Schema. */ log.addTransmissionTarget("test"); log.setTag(Analytics.getTransmissionTarget("test")); pc.onPreparingLog(log, "groupName"); /* Assert properties set on common schema. */ assertEquals("appVersion", log.getExt().getApp().getVer()); assertEquals("appName", log.getExt().getApp().getName()); assertEquals("appLocale", log.getExt().getApp().getLocale()); assertEquals("c:bob", log.getExt().getUser().getLocalId()); }
@Test public void grandparentsHaveNoPropertiesSet() { CommonSchemaLog log = new CommonSchemaEventLog(); log.setExt(new Extensions()); log.getExt().setApp(new AppExtension()); log.getExt().setUser(new UserExtension()); /* Set up empty chain of parents. */ AnalyticsTransmissionTarget grandparent = Analytics.getTransmissionTarget("grandparent"); AnalyticsTransmissionTarget parent = grandparent.getTransmissionTarget("parent"); AnalyticsTransmissionTarget child = parent.getTransmissionTarget("child"); /* Simulate channel callbacks. */ log.addTransmissionTarget("child"); log.setTag(child); grandparent.getPropertyConfigurator().onPreparingLog(log, "groupName"); parent.getPropertyConfigurator().onPreparingLog(log, "groupName"); child.getPropertyConfigurator().onPreparingLog(log, "groupName"); /* Assert properties not set on common schema for child. */ assertNull(log.getExt().getApp().getVer()); assertNull(log.getExt().getApp().getName()); assertNull(log.getExt().getApp().getLocale()); assertNull(log.getExt().getUser().getLocalId()); }