1#!/usr/bin/env python3
2# -*- coding: utf-8 -*-
3
4#
5# Copyright (c) 2024 Huawei Device Co., Ltd.
6# Licensed under the Apache License, Version 2.0 (the "License");
7# you may not use this file except in compliance with the License.
8# You may obtain a copy of the License at
9#
10#     http://www.apache.org/licenses/LICENSE-2.0
11#
12# Unless required by applicable law or agreed to in writing, software
13# distributed under the License is distributed on an "AS IS" BASIS,
14# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15# See the License for the specific language governing permissions and
16# limitations under the License.
17#
18
19from src.beans.base_bean import BaseBean
20from src.keywords import hittest_node_keyword, get_sample_key, get_sample_separator_count
21from src.utils.log_wrapper import log_info
22from src.utils.value_parser import get_value_as_int, get_value_as_str
23
24
25# nodeId: 0, parentId: -1, tag: root, monopolizeEvents: 0, isHit: 1, hitTestMode: 0, responseRegion: RectT (0.00,
26# 0.00) - [720.00 x 1280.00]
27class FrameNode(BaseBean):
28    nodeId = 0
29    parentId = -1
30    tag = ''
31    com_id = ''
32    monopolizeEvents = 0
33    isHit = 1
34    hitTestMode = 0
35    responseRegion = ''
36
37    original_str = ''
38
39    def __init__(self, node_dump_str):
40        super().__init__()
41        self.original_str = node_dump_str
42        self.nodeId = get_value_as_int(node_dump_str, get_sample_key(hittest_node_keyword, 'nodeId'),
43                                       get_sample_separator_count(hittest_node_keyword, 'nodeId'))
44        self.parentId = get_value_as_int(node_dump_str, get_sample_key(hittest_node_keyword, 'parentId'),
45                                         get_sample_separator_count(hittest_node_keyword, 'parentId'))
46        self.tag = get_value_as_str(node_dump_str, get_sample_key(hittest_node_keyword, 'tag'),
47                                    get_sample_separator_count(hittest_node_keyword, 'tag'))
48        self.com_id = get_value_as_str(node_dump_str, get_sample_key(hittest_node_keyword, 'comId'),
49                                       get_sample_separator_count(hittest_node_keyword, 'comId'))
50        self.monopolizeEvents = get_value_as_int(node_dump_str,
51                                                 get_sample_key(hittest_node_keyword, 'monopolizeEvents'),
52                                                 get_sample_separator_count(hittest_node_keyword, 'monopolizeEvents'))
53        self.isHit = get_value_as_int(node_dump_str, get_sample_key(hittest_node_keyword, 'isHit'),
54                                      get_sample_separator_count(hittest_node_keyword, 'isHit'))
55        self.hitTestMode = get_value_as_int(node_dump_str, get_sample_key(hittest_node_keyword, 'hitTestMode'),
56                                            get_sample_separator_count(hittest_node_keyword, 'hitTestMode'))
57        self.responseRegion = get_value_as_str(node_dump_str, get_sample_key(hittest_node_keyword, 'responseRegion'),
58                                               get_sample_separator_count(hittest_node_keyword, 'responseRegion'), True)
59        self.check_parse_result()
60
61    def check_parse_result(self):
62        if (self.nodeId is None or self.parentId is None or self.tag is None or self.monopolizeEvents is None or
63                self.isHit is None or self.hitTestMode is None or self.responseRegion is None):
64            self.parse_failed()
65        else:
66            self.parse_succeed()
67
68    def to_string(self):
69        result_str = 'nodeId: {}, parentId: {}, tag: {}'.format(self.nodeId, self.parentId, self.tag)
70        if self.com_id is not None:
71            result_str += ', comId: {}'.format(self.com_id)
72        result_str += ', monopolizeEvents: {}, isHit: {}, hitTestMode: {}, responseRegion: {}'.format(
73            self.monopolizeEvents, self.isHit, self.hitTestMode, self.responseRegion)
74        return result_str
75
76    def get_summary_string(self):
77        result_str = '{}({}) , isHit: {}'.format(self.tag, self.nodeId, self.isHit)
78        return result_str
79
80    def get_showup_string(self):
81        result_str = ("{}({})\n\nisHit: {}\nhitTestMode: {}"
82                      .format(self.tag, self.nodeId, self.isHit, self.hitTestMode))
83        return result_str
84
85    def dump(self):
86        log_info(self.to_string())
87