Skip to content

Commit

Permalink
优化布局分析工具: 打印数据增加depth和index的属性
Browse files Browse the repository at this point in the history
  • Loading branch information
TonyJiangWJ committed Sep 24, 2020
1 parent 2e0b8a0 commit 87e1d0f
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 43 deletions.
3 changes: 2 additions & 1 deletion lib/prototype/LogUtils.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* @Author: TonyJiangWJ
* @Last Modified by: TonyJiangWJ
* @Last Modified time: 2020-09-22 21:21:47
* @Last Modified time: 2020-09-24 09:43:05
* @Description: 日志工具
*/
importClass(java.lang.Thread)
Expand Down Expand Up @@ -221,6 +221,7 @@ function SyncLogger () {
let enqueueStart = new Date().getTime()
// 同步方式写入文件
let { logType, content, dateTime, threadId } = logData
content += '\n'
let verboseLog = dateTime + ' ' + logType + ' [E:' + ENGINE_ID + ' T:' + threadId + '] - ' + content
let log = dateTime + ' ' + content
let start = new Date().getTime()
Expand Down
2 changes: 1 addition & 1 deletion lib/prototype/WidgetUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* @Author: TonyJiangWJ
* @Date: 2019-11-05 09:12:00
* @Last Modified by: TonyJiangWJ
* @Last Modified time: 2020-09-22 21:21:54
* @Last Modified time: 2020-09-24 20:04:30
* @Description:
*/
let { config: _config } = require('../../config.js')(runtime, this)
Expand Down
76 changes: 55 additions & 21 deletions unit/获取当前页面的布局信息.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* @Author: TonyJiangWJ
* @Date: 2020-04-29 14:44:49
* @Last Modified by: TonyJiangWJ
* @Last Modified time: 2020-09-22 21:22:12
* @Last Modified time: 2020-09-24 19:58:33
* @Description:
*/
let singletonRequire = require('../lib/SingletonRequirer.js')(runtime, this)
Expand Down Expand Up @@ -57,7 +57,18 @@ if (any) {
id, content,
boundsInfo.left, boundsInfo.top, boundsInfo.width(), boundsInfo.height()
])
let resultList = iterateAll(root, 1)
let resultList = iterateAll(root).filter(v => v !== null).sort((a, b) => {
let depth1 = getCompareDepth(a)
let depth2 = getCompareDepth(b)
logUtils.debugInfo(['depth1:{} depth2: {}', depth1, depth2])
if (depth1 > depth2) {
return 1
} else if (depth1 === depth2) {
return 0
} else {
return -1
}
})
uiObjectInfoList = flatMap(flatArrayList, resultList)

floatyInstance.setPosition(parseInt(config.device_width / 5), parseInt(config.device_height / 2))
Expand All @@ -72,8 +83,10 @@ if (uiObjectInfoList) {
let timeCost = new Date().getTime() - start
let total = uiObjectInfoList.length
let logInfoList = uiObjectInfoList.filter(v => v && v.hasUsableInfo()).map(v => v.toString())
let content = removeMinPrefix(logInfoList).join('\n')
logUtils.debugInfo(content)
// let content = removeMinPrefix(logInfoList).join('\n')
let content = logInfoList.join('\n')
logUtils.infoLog('\n' + content)
logUtils.logInfo('布局层次结果已经保存到logs/info.log')
dialogs.build({
title: '布局分析结果',
content: commonFunctions.formatString("总分析耗时:{}ms 总控件数:{}\n{}", timeCost, total, content),
Expand All @@ -100,33 +113,35 @@ function getRootContainer (target) {
}


function iterateAll (root, depth) {
function iterateAll (root, depth, index) {
if (isEmpty(root)) {
return null
}
depth = depth || 1
let uiObjectInfo = new UiObjectInfo(root, depth)
index = index || 0
depth = depth || 0
let uiObjectInfo = new UiObjectInfo(root, depth, index)
logUtils.logInfo(uiObjectInfo.toString())
if (root.getChildCount() > 0) {
return [uiObjectInfo].concat(root.children().map(child => iterateAll(child, depth + 1)))
return [uiObjectInfo].concat(root.children().map((child, index) => iterateAll(child, depth + 1, index)))
} else {
return uiObjectInfo
}
}

function UiObjectInfo (uiObject, depth) {
function UiObjectInfo (uiObject, depth, index) {
this.content = uiObject.text() || uiObject.desc() || ''
this.isDesc = typeof uiObject.desc() !== 'undefined' && uiObject.desc() !== ''
this.id = uiObject.id()
this.boundsInfo = uiObject.bounds()
this.depth = depth
this.index = index


this.toString = function () {
return commonFunctions.formatString(
// ---- id:[] [text/desc]content:[] bounds:[]
'{}{}{}{}',
new Array(this.depth).join('-'),
// ----[depth:index] id:[] [text/desc]content:[] bounds:[]
'{}[{}:{}]{}{}{}',
new Array(this.depth + 1).join('-'), this.depth, this.index,
this.isEmpty(this.id) ? '' : 'id:[' + this.id + ']',
this.isEmpty(this.content) ? '' :
commonFunctions.formatString(
Expand Down Expand Up @@ -159,6 +174,34 @@ function flatMap (f, list) {
return list.map(f).reduce((x, y) => x.concat(y), [])
}

/**
* 将嵌套数组转换成单个数组
* @param {*} a
*/
function flatArrayList (a) {
if (a instanceof UiObjectInfo) {
return a
} else {
return flatMap(flatArrayList, a)
}
}

function getCompareDepth (a) {
if (a instanceof Array) {
for (let i = 0; i < a.length; i++) {
if (a[i] instanceof Array) {
return getCompareDepth(a[i])
} else {
let data = a[i]
if (data.id || data.content)
return data.depth
}
}
} else {
return a.depth
}
}

function Queue (size) {
this.size = size || 10
this.pushTime = 0
Expand All @@ -181,15 +224,6 @@ function Queue (size) {
}
}


function flatArrayList (a) {
if (a instanceof UiObjectInfo) {
return a
} else {
return flatMap(flatArrayList, a)
}
}

function removeMinPrefix (list) {
let min = 10000000
let minQueue = new Queue(3)
Expand Down
74 changes: 54 additions & 20 deletions unit/获取锁屏界面控件信息.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* @Author: TonyJiangWJ
* @Date: 2020-09-22 10:47:53
* @Last Modified by: TonyJiangWJ
* @Last Modified time: 2020-09-22 21:22:19
* @Last Modified time: 2020-09-24 19:59:28
* @Description:
*/
let { config } = require('../config.js')(runtime, this)
Expand Down Expand Up @@ -132,7 +132,19 @@ function analyzeLayout (desc) {
id, content,
boundsInfo.left, boundsInfo.top, boundsInfo.width(), boundsInfo.height()
])
let resultList = iterateAll(root, 1)

let resultList = iterateAll(root).filter(v => v !== null).sort((a, b) => {
let depth1 = getCompareDepth(a)
let depth2 = getCompareDepth(b)
logUtils.debugInfo(['depth1:{} depth2: {}', depth1, depth2])
if (depth1 > depth2) {
return 1
} else if (depth1 === depth2) {
return 0
} else {
return -1
}
})
uiObjectInfoList = flatMap(flatArrayList, resultList)

floatyInstance.setPosition(parseInt(config.device_width / 5), parseInt(config.device_height / 2))
Expand All @@ -146,7 +158,8 @@ function analyzeLayout (desc) {
let cost = new Date().getTime() - start
if (uiObjectInfoList) {
let logInfoList = uiObjectInfoList.filter(v => v && v.hasUsableInfo()).map(v => v.toString())
let content = removeMinPrefix(logInfoList).join('\n')
// let content = removeMinPrefix(logInfoList).join('\n')
let content = logInfoList.join('\n')
logUtils.debugInfo(['{} 分析耗时:{}ms', desc, cost])
return { content: content, total: logInfoList.length, cost: cost, desc: desc }
}
Expand All @@ -167,33 +180,35 @@ function getRootContainer (target) {
}


function iterateAll (root, depth) {
function iterateAll (root, depth, index) {
if (isEmpty(root)) {
return null
}
depth = depth || 1
let uiObjectInfo = new UiObjectInfo(root, depth)
index = index || 0
depth = depth || 0
let uiObjectInfo = new UiObjectInfo(root, depth, index)
logUtils.logInfo(uiObjectInfo.toString())
if (root.getChildCount() > 0) {
return [uiObjectInfo].concat(root.children().map(child => iterateAll(child, depth + 1)))
return [uiObjectInfo].concat(root.children().map((child, index) => iterateAll(child, depth + 1, index)))
} else {
return uiObjectInfo
}
}

function UiObjectInfo (uiObject, depth) {
function UiObjectInfo (uiObject, depth, index) {
this.content = uiObject.text() || uiObject.desc() || ''
this.isDesc = typeof uiObject.desc() !== 'undefined' && uiObject.desc() !== ''
this.id = uiObject.id()
this.boundsInfo = uiObject.bounds()
this.depth = depth
this.index = index


this.toString = function () {
return commonFunctions.formatString(
// ---- id:[] [text/desc]content:[] bounds:[]
'{}{}{}{}',
new Array(this.depth).join('-'),
// ----[depth:index] id:[] [text/desc]content:[] bounds:[]
'{}[{}:{}]{}{}{}',
new Array(this.depth + 1).join('-'), this.depth, this.index,
this.isEmpty(this.id) ? '' : 'id:[' + this.id + ']',
this.isEmpty(this.content) ? '' :
commonFunctions.formatString(
Expand Down Expand Up @@ -226,6 +241,34 @@ function flatMap (f, list) {
return list.map(f).reduce((x, y) => x.concat(y), [])
}

/**
* 将嵌套数组转换成单个数组
* @param {*} a
*/
function flatArrayList (a) {
if (a instanceof UiObjectInfo) {
return a
} else {
return flatMap(flatArrayList, a)
}
}

function getCompareDepth (a) {
if (a instanceof Array) {
for (let i = 0; i < a.length; i++) {
if (a[i] instanceof Array) {
return getCompareDepth(a[i])
} else {
let data = a[i]
if (data.id || data.content)
return data.depth
}
}
} else {
return a.depth
}
}

function Queue (size) {
this.size = size || 10
this.pushTime = 0
Expand All @@ -248,15 +291,6 @@ function Queue (size) {
}
}


function flatArrayList (a) {
if (a instanceof UiObjectInfo) {
return a
} else {
return flatMap(flatArrayList, a)
}
}

function removeMinPrefix (list) {
let min = 10000000
let minQueue = new Queue(3)
Expand Down

0 comments on commit 87e1d0f

Please sign in to comment.