Skip to content

Commit be31f6e

Browse files
committed
View clone()
- cast Views - added getAndroidClassName() to return widget class
1 parent c569f22 commit be31f6e

3 files changed

Lines changed: 67 additions & 9 deletions

File tree

src/com/dtmilano/android/viewclient.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
@author: Diego Torres Milano
1919
'''
2020

21-
__version__ = '15.3.1'
21+
__version__ = '15.4.0'
2222

2323
import sys
2424
import warnings
@@ -209,6 +209,17 @@ def __copy(cls, view):
209209

210210
return cls(view.map, view.device, view.version, view.forceviewserveruse, view.windowId, view.uiAutomatorHelper)
211211

212+
@classmethod
213+
def clone(cls, view):
214+
_map = view.map.copy()
215+
# enforce the correct class in the map
216+
_map['class'] = cls.getAndroidClassName()
217+
return cls(_map, view.device, view.version, view.forceviewserveruse, view.windowId, view.uiAutomatorHelper)
218+
219+
@classmethod
220+
def getAndroidClassName(cls):
221+
return 'android.widget.View'
222+
212223
def __init__(self, _map, device, version=-1, forceviewserveruse=False, windowId=None, uiAutomatorHelper=None):
213224
'''
214225
Constructor
@@ -1158,13 +1169,19 @@ class TextView(View):
11581169
TextView class.
11591170
'''
11601171

1161-
pass
1172+
@classmethod
1173+
def getAndroidClassName(cls):
1174+
return 'android.widget.TextView'
11621175

11631176
class EditText(TextView):
11641177
'''
11651178
EditText class.
11661179
'''
11671180

1181+
@classmethod
1182+
def getAndroidClassName(cls):
1183+
return 'android.widget.EditText'
1184+
11681185
def type(self, text, alreadyTouched=False):
11691186
if not text:
11701187
return

tests/com/dtmilano/android/mocks.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -752,6 +752,15 @@ def shutdownMockViewServer(self):
752752
self.viewServer.shutdown()
753753
#del(self.viewServer)
754754

755+
def touch(self, x, y, eventType):
756+
pass
757+
758+
def press(self, k, e, repeat=1):
759+
pass
760+
761+
def type(self, text):
762+
pass
763+
755764
import sys
756765
import time
757766
import select

tests/com/dtmilano/android/viewclienttests.py

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,9 @@
77
@author: diego
88
'''
99

10-
import sys
11-
import os
12-
import time
13-
import StringIO
14-
import unittest
1510
import exceptions
16-
import platform
11+
import os
12+
import sys
1713

1814
# PyDev sets PYTHONPATH, use it
1915
try:
@@ -29,7 +25,7 @@
2925
pass
3026

3127
from com.dtmilano.android.viewclient import *
32-
from mocks import MockDevice, MockViewServer
28+
from mocks import MockDevice
3329
from mocks import DUMP, DUMP_SAMPLE_UI, VIEW_MAP, VIEW_MAP_API_8, VIEW_MAP_API_17, RUNNING, STOPPED, WINDOWS
3430

3531
os_name = platform.system()
@@ -276,6 +272,42 @@ def testIsClickable_api17(self):
276272
v = View(VIEW_MAP_API_17, MockDevice(), 17)
277273
self.assertTrue(v.isClickable())
278274

275+
def testCopyConstructor(self):
276+
device = MockDevice()
277+
mv = VIEW_MAP_API_17.copy()
278+
mv['class'] = u'android.widget.View'
279+
v = View(mv, device, 17)
280+
self.assertEqual(v.getClass(), u'android.widget.View')
281+
282+
mt = VIEW_MAP_API_17.copy()
283+
mt['class'] = u'android.widget.TextView'
284+
t = TextView(mt, device, 17)
285+
self.assertEqual(t.getClass(), u'android.widget.TextView')
286+
287+
me = VIEW_MAP_API_17.copy()
288+
me['class'] = u'android.widget.EditText'
289+
e = TextView(me, device, 17)
290+
self.assertEqual(e.getClass(), u'android.widget.EditText')
291+
292+
v1 = View.clone(v)
293+
self.assertEqual(u'android.widget.View', v1.getClass())
294+
295+
t1 = TextView.clone(t)
296+
self.assertEqual(u'android.widget.TextView', t1.getClass())
297+
298+
e1 = EditText.clone(e)
299+
self.assertEqual(u'android.widget.EditText', e1.getClass())
300+
301+
t2 = TextView.clone(v)
302+
self.assertEqual(u'android.widget.TextView', t2.getClass())
303+
304+
e2 = EditText.clone(v)
305+
self.assertEqual(u'android.widget.EditText', e2.getClass())
306+
307+
e2.setText("hello")
308+
309+
310+
279311
class ViewClientTest(unittest.TestCase):
280312

281313
def setUp(self):

0 commit comments

Comments
 (0)