Skip to content

Commit

Permalink
feat: 更改Page事件的测量协议
Browse files Browse the repository at this point in the history
  • Loading branch information
LitterSun committed Dec 3, 2020
1 parent 0d0ef96 commit 1d2a0e3
Show file tree
Hide file tree
Showing 24 changed files with 160 additions and 193 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -305,19 +305,19 @@ private static final class PagesAttributesParams {
protected void onReceivedPageEvents(JSONArray jsonArray) throws JSONException {
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
String pageName = jsonObject.getString("pageName");
mReceivedPages.put(pageName, jsonObject.getLong("timestamp"));
String path = jsonObject.getString("path");
mReceivedPages.put(path, jsonObject.getLong("timestamp"));
}
}

@Override
protected void onReceivedPageAttributesEvents(JSONArray jsonArray) throws JSONException {
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
String pageName = jsonObject.getString("pageName");
String path = jsonObject.getString("path");
long pageShowTimestamp = jsonObject.getLong("pageShowTimestamp");
HashMap<String, String> attributes = (HashMap<String, String>) JsonUtil.copyToMap(jsonObject.getJSONObject("attributes"));
mReceivedPagesAttributes.put(pageName, new PagesAttributesParams(pageShowTimestamp, attributes));
mReceivedPagesAttributes.put(path, new PagesAttributesParams(pageShowTimestamp, attributes));
}
}

Expand All @@ -328,17 +328,17 @@ public void reset() {

public boolean checkEvents() {
for (Map.Entry<String, HashMap<String, String>> entry : mExpectPagesAttributes.entrySet()) {
String pageName = entry.getKey();
PagesAttributesParams receivedAttributesParams = mReceivedPagesAttributes.get(pageName);
long expectPageTimestamp = mReceivedPages.get(pageName);
String path = entry.getKey();
PagesAttributesParams receivedAttributesParams = mReceivedPagesAttributes.get(path);
long expectPageTimestamp = mReceivedPages.get(path);
long actualPageTimestamp = receivedAttributesParams.pageShowTimestamp;
if (actualPageTimestamp != expectPageTimestamp) {
Truth.assertWithMessage(pageName + " Page show timestamp is " + expectPageTimestamp
Truth.assertWithMessage(path + " Page show timestamp is " + expectPageTimestamp
+ ", but PageAttributesEvent pageShowTimestamp is " + actualPageTimestamp).fail();
}
HashMap<String, String> expectAttributes = entry.getValue();
if (!expectAttributes.equals(receivedAttributesParams.attributes)) {
Truth.assertWithMessage(pageName + " Page attributes is " + receivedAttributesParams.attributes + "not equals expect attributes " + expectAttributes).fail();
Truth.assertWithMessage(path + " Page attributes is " + receivedAttributesParams.attributes + "not equals expect attributes " + expectAttributes).fail();
}
}
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public void activityPageEventTest() {
@Override
protected void onReceivedPageEvents(JSONArray jsonArray) throws JSONException {
JSONObject jsonObject = jsonArray.getJSONObject(0);
if (jsonObject.getString("pageName").equals("/MainActivity")
if (jsonObject.getString("path").equals("/MainActivity")
&& jsonObject.getString("orientation").equals("PORTRAIT")
&& jsonObject.getString("title").equals("demos")) {
receivedEvent.set(true);
Expand Down Expand Up @@ -237,7 +237,7 @@ public void setActivityPageAliasTest() {
@Override
protected void onReceivedPageEvents(JSONArray jsonArray) throws JSONException {
JSONObject jsonObject = jsonArray.getJSONObject(0);
if (jsonObject.getString("pageName").equals("/TestActivityPage")
if (jsonObject.getString("path").equals("/TestActivityPage")
&& jsonObject.getString("orientation").equals("PORTRAIT")
&& jsonObject.getString("title").equals("demos")) {
receivedEvent.set(true);
Expand Down Expand Up @@ -446,21 +446,21 @@ private static final class OnReceivedPageEventsListener extends MockEventsApiSer

private final Map<String, Long> mReceivedPages = new HashMap<>();

OnReceivedPageEventsListener(AtomicBoolean receivedAllEvents, String... pageNames) {
OnReceivedPageEventsListener(AtomicBoolean receivedAllEvents, String... paths) {
mReceivedAllEvents = receivedAllEvents;
mReceivedAllEvents.set(false);
mPages = Arrays.asList(pageNames);
mPages = Arrays.asList(paths);
}

@Override
protected void onReceivedPageEvents(JSONArray jsonArray) throws JSONException {
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
String pageName = jsonObject.getString("pageName");
if (mPages.contains(pageName) && jsonObject.getString("orientation").equals("PORTRAIT")) {
mReceivedPages.put(pageName, jsonObject.getLong("timestamp"));
String path = jsonObject.getString("path");
if (mPages.contains(path) && jsonObject.getString("orientation").equals("PORTRAIT")) {
mReceivedPages.put(path, jsonObject.getLong("timestamp"));
} else {
Truth.assertWithMessage("Received " + pageName + " Page Event").fail();
Truth.assertWithMessage("Received " + path + " Page Event").fail();
}
}

Expand Down Expand Up @@ -494,8 +494,8 @@ private static final class StopReceivedPageEventsListener extends MockEventsApiS
protected void onReceivedPageEvents(JSONArray jsonArray) throws JSONException {
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
String pageName = jsonObject.getString("pageName");
Truth.assertWithMessage("Received " + pageName + " Page Event").fail();
String path = jsonObject.getString("path");
Truth.assertWithMessage("Received " + path + " Page Event").fail();
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,13 @@ public void editTextChangeEventTest() {
getEventsApiServer().setOnReceivedEventListener(new OnReceivedViewChangeEventsListener(
receivedEvent,
new ViewElementEvent.Builder()
.setPageName("/LoginActivity")
.setPath("/LoginActivity")
.setXpath("/Page/LinearLayout[0]/FrameLayout[0]/FitWindowsLinearLayout[0]#action_bar_root/ContentFrameLayout[0]/ConstraintLayout[0]#container/AppCompatEditText[0]#username")
.setTextValue("")
.setIndex(-1)
.build(),
new ViewElementEvent.Builder()
.setPageName("/LoginActivity")
.setPath("/LoginActivity")
.setXpath("/Page/LinearLayout[0]/FrameLayout[0]/FitWindowsLinearLayout[0]#action_bar_root/ContentFrameLayout[0]/ConstraintLayout[0]#container/AppCompatEditText[1]#password")
.setTextValue("")
.setIndex(-1)
Expand Down Expand Up @@ -102,8 +102,8 @@ private static final class OnReceivedViewChangeEventsListener extends MockEvents
protected void onReceivedPageEvents(JSONArray jsonArray) throws JSONException {
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
String pageName = jsonObject.getString("pageName");
mReceivedPages.put(pageName, jsonObject.getLong("timestamp"));
String path = jsonObject.getString("path");
mReceivedPages.put(path, jsonObject.getLong("timestamp"));
}
}

Expand All @@ -112,13 +112,13 @@ protected void onReceivedViewChangeEvents(JSONArray jsonArray) throws JSONExcept
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
for (int j = 0; j < mExpectReceivedChanges.size(); j++) {
String pageName = jsonObject.getString("pageName");
String path = jsonObject.getString("path");
ViewElementEvent viewElementEvent = mExpectReceivedChanges.get(j);
if (pageName.equals(viewElementEvent.getPageName())
if (path.equals(viewElementEvent.getPath())
&& jsonObject.getString("xpath").equals(viewElementEvent.getXpath())
&& jsonObject.optString("textValue").equals(viewElementEvent.getTextValue())
&& jsonObject.getInt("index") == viewElementEvent.getIndex()
&& jsonObject.getLong("pageShowTimestamp") == mReceivedPages.get(pageName)) {
&& jsonObject.getLong("pageShowTimestamp") == mReceivedPages.get(path)) {
mExpectReceivedChanges.remove(j);
break;
}
Expand Down
Loading

0 comments on commit 1d2a0e3

Please sign in to comment.