blob: 34617a7bccfc922a0264b4130213e7bcbce37d65 [file] [log] [blame]
# Copyright 2016 The Vanadium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style
# license that can be found in the LICENSE file.
"""View class definition."""
from collections import Counter
class View(object):
"""Base class for all views.
Includes the view hierarchy, screenshot, and information about clickable
components and their resulting views.
"""
def __init__(self, activity, frag_list, hierarchy, screenshot, num):
"""Constructor for View class."""
self.activity = activity
self.frag_list = frag_list
self.hierarchy = hierarchy
self.screenshot = screenshot
self.num = num
self.clickable = []
self.preceding = []
self.click_dict = {}
def get_name(self):
"""Returns the identifying name of the View."""
return self.activity + '-' + self.frag_list[0] + '-' + str(self.num)
def num_components(self):
return len(self.hierarchy)
def is_duplicate(self, cv_activity, cv_frag_list, cv_hierarchy):
"""Determines if the passed-in information is identical to this View."""
# Since the fragment names are hashable, this is the most efficient method
# to compare two unordered lists according to
# http://stackoverflow.com/questions/7828867/how-to-efficiently-compare-two-unordered-lists-not-sets-in-python
# We also use it below to compare hierarchy ids.
if (self.activity != cv_activity or
Counter(self.frag_list) != Counter(cv_frag_list)):
return False
if self.num_components() != len(cv_hierarchy):
return False
hierarchy_ids = [h['uniqueId'] for h in self.hierarchy]
curr_view_ids = [cv['uniqueId'] for cv in cv_hierarchy]
return Counter(hierarchy_ids) == Counter(curr_view_ids)
def is_duplicate_view(self, other_view):
"""Determines if the passed-in View is identical to this View."""
if (self.activity != other_view.activity or
Counter(self.frag_list) != Counter(other_view.frag_list)):
return False
if self.num_components() != len(other_view.hierarchy):
return False
hierarchy_ids = [h['uniqueId'] for h in self.hierarchy]
other_view_ids = [ov['uniqueId'] for ov in other_view.hierarchy]
return Counter(hierarchy_ids) == Counter(other_view_ids)
def print_info(self):
"""Prints out information about the view."""
print 'Activity: ' + self.activity
print 'Fragment: ' + self.frag_list
print 'Num: " + str(self.num)'
print 'Screenshot path:' + self.screenshot
print 'Hierarchy: '
for component in self.hierarchy:
print component