Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
Classes | Functions | Variables
android_skp_capture Namespace Reference

Classes

class  App
 
class  DragAction
 
class  PressAction
 

Functions

 parse_action (action_dict)
 
 check_output (cmd)
 
 adb_shell (cmd)
 
 remote_file_exists (filename)
 
 capture_skp (skp_file, package, device)
 
 load_app (filename)
 
 main ()
 

Variables

int WAIT_FOR_SKP_CAPTURE = 1
 

Function Documentation

◆ adb_shell()

android_skp_capture.adb_shell (   cmd)
Run the given ADB shell command and emulate the exit code.

Definition at line 90 of file android_skp_capture.py.

90def adb_shell(cmd):
91 """Run the given ADB shell command and emulate the exit code."""
92 output = check_output(['adb', 'shell', cmd + '; echo $?']).strip()
93 lines = output.splitlines()
94 if lines[-1] != '0':
95 raise Exception('ADB command failed: %s\n\nOutput:\n%s' % (cmd, output))
96 return '\n'.join(lines[:-1])
97
98

◆ capture_skp()

android_skp_capture.capture_skp (   skp_file,
  package,
  device 
)
Capture an SKP.

Definition at line 108 of file android_skp_capture.py.

108def capture_skp(skp_file, package, device):
109 """Capture an SKP."""
110 remote_path = '/data/data/%s/cache/%s' % (package, os.path.basename(skp_file))
111 try:
112 adb_shell('rm %s' % remote_path)
113 except Exception:
114 if remote_file_exists(remote_path):
115 raise
116
117 adb_shell('setprop debug.hwui.capture_frame_as_skp %s' % remote_path)
118 try:
119 # Spin, wait for the SKP to be written.
120 timeout = 10 # Seconds
121 start = time.time()
122 device.drag((300, 300), (300, 350), 1, 10) # Arbitrary action to force a draw.
123 while not remote_file_exists(remote_path):
124 if time.time() - start > timeout:
125 raise Exception('Timed out waiting for SKP capture.')
126 time.sleep(1)
127
128 # Pull the SKP from the device.
129 cmd = ['adb', 'pull', remote_path, skp_file]
130 check_output(cmd)
131
132 finally:
133 adb_shell('setprop debug.hwui.capture_frame_as_skp ""')
134
135

◆ check_output()

android_skp_capture.check_output (   cmd)
Convenience implementation of subprocess.check_output.

Definition at line 82 of file android_skp_capture.py.

82def check_output(cmd):
83 """Convenience implementation of subprocess.check_output."""
84 proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
85 if proc.wait() != 0:
86 raise Exception('Command failed: %s' % ' '.join(cmd))
87 return proc.communicate()[0]
88
89

◆ load_app()

android_skp_capture.load_app (   filename)
Load the JSON file describing an app and return an App instance.

Definition at line 136 of file android_skp_capture.py.

136def load_app(filename):
137 """Load the JSON file describing an app and return an App instance."""
138 with open(filename) as f:
139 app_dict = ast.literal_eval(f.read())
140 return App(app_dict['name'],
141 app_dict['package'],
142 app_dict['activity'],
143 app_dict['app_launch_delay'],
144 app_dict['actions'])
145
146

◆ main()

android_skp_capture.main ( )
Capture SKPs for all apps.

Definition at line 147 of file android_skp_capture.py.

147def main():
148 """Capture SKPs for all apps."""
149 device = MonkeyRunner.waitForConnection()
150
151 # TODO(borenet): Kill all apps.
152 device.wake()
153 device.drag((600, 600), (10, 10), 0.2, 10)
154
155 apps_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'apps')
156 app_files = [os.path.join(apps_dir, app) for app in os.listdir(apps_dir)]
157
158 for app_file in app_files:
159 app = load_app(app_file)
160 print(app.name)
161 print(' Package %s' % app.package)
162 app.launch(device)
163 print(' Launched activity %s' % app.activity)
164
165 for action in app.actions:
166 print(' %s' % action.__class__.__name__)
167 action.run(device)
168
169 time.sleep(WAIT_FOR_SKP_CAPTURE)
170 print(' Capturing SKP.')
171 skp_file = '%s.skp' % app.name
172 capture_skp(skp_file, app.package, device)
173 print(' Wrote SKP to %s' % skp_file)
174 print
175 app.kill()
176
177
void print(void *str)
Definition bridge.cpp:126
Definition main.py:1

◆ parse_action()

android_skp_capture.parse_action (   action_dict)
Parse a dict describing an action and return an Action object.

Definition at line 49 of file android_skp_capture.py.

49def parse_action(action_dict):
50 """Parse a dict describing an action and return an Action object."""
51 if action_dict['type'] == 'drag':
52 return DragAction(tuple(action_dict['start']),
53 tuple(action_dict['end']),
54 action_dict['duration'],
55 action_dict['points'])
56 elif action_dict['type'] == 'press':
57 return PressAction(action_dict['button'], action_dict['press_type'])
58 else:
59 raise TypeError('Unsupported action type: %s' % action_dict['type'])
60
61

◆ remote_file_exists()

android_skp_capture.remote_file_exists (   filename)
Return True if the given file exists on the device and False otherwise.

Definition at line 99 of file android_skp_capture.py.

99def remote_file_exists(filename):
100 """Return True if the given file exists on the device and False otherwise."""
101 try:
102 adb_shell('test -f %s' % filename)
103 return True
104 except Exception:
105 return False
106
107

Variable Documentation

◆ WAIT_FOR_SKP_CAPTURE

int android_skp_capture.WAIT_FOR_SKP_CAPTURE = 1

Definition at line 22 of file android_skp_capture.py.