Damien Martin-Guillerez | f88f4d8 | 2015-09-25 13:56:55 +0000 | [diff] [blame] | 1 | # Copyright 2015 The Bazel Authors. All rights reserved. |
Alex Humesky | a4ecde6 | 2015-05-21 17:08:42 +0000 | [diff] [blame] | 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | # you may not use this file except in compliance with the License. |
| 5 | # You may obtain a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | # See the License for the specific language governing permissions and |
| 13 | # limitations under the License. |
| 14 | |
| 15 | """Unit tests for stubify_incremental_install.""" |
| 16 | |
| 17 | import os |
| 18 | import unittest |
| 19 | import zipfile |
| 20 | |
| 21 | from tools.android import incremental_install |
| 22 | from third_party.py import mock |
| 23 | |
| 24 | |
| 25 | class MockAdb(object): |
| 26 | """Mocks the Android ADB binary.""" |
| 27 | |
| 28 | def __init__(self): |
| 29 | # Map of file name -> contents. |
| 30 | self.files = {} |
Lukacs Berki | faff718 | 2015-08-20 14:27:42 +0000 | [diff] [blame] | 31 | self.split_apks = set() |
Alex Humesky | a4ecde6 | 2015-05-21 17:08:42 +0000 | [diff] [blame] | 32 | self._error = None |
| 33 | self.package_timestamp = None |
Lukacs Berki | faff718 | 2015-08-20 14:27:42 +0000 | [diff] [blame] | 34 | self._last_package_timestamp = 1 |
Alex Humesky | a4ecde6 | 2015-05-21 17:08:42 +0000 | [diff] [blame] | 35 | self.shell_cmdlns = [] |
Lukacs Berki | a3a33d7 | 2015-08-19 08:34:55 +0000 | [diff] [blame] | 36 | self.abi = "armeabi-v7a" |
Alex Humesky | a4ecde6 | 2015-05-21 17:08:42 +0000 | [diff] [blame] | 37 | |
| 38 | def Exec(self, args): |
| 39 | if self._error: |
| 40 | error_info, arg = self._error # pylint: disable=unpacking-non-sequence |
| 41 | if not arg or arg in args: |
| 42 | return self._CreatePopenMock(*error_info) |
| 43 | |
| 44 | returncode = 0 |
| 45 | stdout = "" |
| 46 | stderr = "" |
| 47 | cmd = args[1] |
| 48 | if cmd == "push": |
| 49 | # "/test/adb push local remote" |
| 50 | with open(args[2]) as f: |
| 51 | content = f.read() |
| 52 | self.files[args[3]] = content |
| 53 | elif cmd == "pull": |
| 54 | # "/test/adb pull remote local" |
| 55 | remote = args[2] |
| 56 | local = args[3] |
| 57 | content = self.files.get(remote) |
| 58 | if content is not None: |
| 59 | with open(local, "w") as f: |
| 60 | f.write(content) |
| 61 | else: |
| 62 | returncode = 1 |
| 63 | stderr = "remote object '%s' does not exist\n" % remote |
| 64 | elif cmd == "install": |
| 65 | self.package_timestamp = self._last_package_timestamp |
| 66 | self._last_package_timestamp += 1 |
| 67 | return self._CreatePopenMock(0, "Success", "") |
Lukacs Berki | faff718 | 2015-08-20 14:27:42 +0000 | [diff] [blame] | 68 | elif cmd == "install-multiple": |
| 69 | if args[3] == "-p": |
| 70 | with open(args[5]) as f: |
| 71 | content = f.read() |
| 72 | self.split_apks.add(content) |
| 73 | else: |
| 74 | self.package_timestamp = self._last_package_timestamp |
| 75 | self._last_package_timestamp += 1 |
| 76 | return self._CreatePopenMock(0, "Success", "") |
| 77 | elif cmd == "uninstall": |
| 78 | self._CreatePopenMock(0, "Success", "") |
| 79 | self.split_apks = set() |
| 80 | self.package_timestamp = None |
Alex Humesky | a4ecde6 | 2015-05-21 17:08:42 +0000 | [diff] [blame] | 81 | elif cmd == "shell": |
| 82 | # "/test/adb shell ..." |
| 83 | # mkdir, rm, am (application manager), or monkey |
| 84 | shell_cmdln = args[2] |
| 85 | self.shell_cmdlns.append(shell_cmdln) |
Googler | 73d4fc9 | 2015-07-30 20:39:46 +0000 | [diff] [blame] | 86 | if shell_cmdln.startswith(("mkdir", "am", "monkey", "input")): |
Alex Humesky | a4ecde6 | 2015-05-21 17:08:42 +0000 | [diff] [blame] | 87 | pass |
| 88 | elif shell_cmdln.startswith("dumpsys package "): |
Lukacs Berki | 0dffeac | 2015-09-15 07:18:47 +0000 | [diff] [blame] | 89 | if self.package_timestamp is not None: |
| 90 | timestamp = "firstInstallTime=%s" % self.package_timestamp |
| 91 | else: |
| 92 | timestamp = "" |
| 93 | return self._CreatePopenMock(0, timestamp, "") |
Alex Humesky | a4ecde6 | 2015-05-21 17:08:42 +0000 | [diff] [blame] | 94 | elif shell_cmdln.startswith("rm"): |
| 95 | file_path = shell_cmdln.split()[2] |
| 96 | self.files.pop(file_path, None) |
Lukacs Berki | ecbf242 | 2015-07-28 07:57:45 +0000 | [diff] [blame] | 97 | elif shell_cmdln.startswith("getprop ro.product.cpu.abi"): |
Lukacs Berki | a3a33d7 | 2015-08-19 08:34:55 +0000 | [diff] [blame] | 98 | return self._CreatePopenMock(0, self.abi, "") |
Alex Humesky | a4ecde6 | 2015-05-21 17:08:42 +0000 | [diff] [blame] | 99 | else: |
| 100 | raise Exception("Unknown shell command line: %s" % shell_cmdln) |
| 101 | # Return a mock subprocess.Popen object |
| 102 | return self._CreatePopenMock(returncode, stdout, stderr) |
| 103 | |
| 104 | def _CreatePopenMock(self, returncode, stdout, stderr): |
| 105 | return mock.Mock( |
| 106 | returncode=returncode, communicate=lambda: (stdout, stderr)) |
| 107 | |
| 108 | def SetError(self, returncode, stdout, stderr, for_arg=None): |
| 109 | self._error = ((returncode, stdout, stderr), for_arg) |
| 110 | |
Lukacs Berki | a3a33d7 | 2015-08-19 08:34:55 +0000 | [diff] [blame] | 111 | def SetAbi(self, abi): |
| 112 | self.abi = abi |
| 113 | |
Alex Humesky | a4ecde6 | 2015-05-21 17:08:42 +0000 | [diff] [blame] | 114 | |
| 115 | class IncrementalInstallTest(unittest.TestCase): |
| 116 | """Unit tests for incremental install.""" |
| 117 | |
| 118 | _DEXMANIFEST = "dexmanifest.txt" |
| 119 | _ADB_PATH = "/test/adb" |
| 120 | _OUTPUT_MARKER = "full_deploy_marker" |
| 121 | _APK = "myapp_incremental.apk" |
| 122 | _RESOURCE_APK = "incremental.ap_" |
| 123 | _STUB_DATAFILE = "stub_application_data.txt" |
| 124 | _OLD_APP_PACKGE = "old.app.package" |
| 125 | _APP_PACKAGE = "new.app.package" |
| 126 | _EXEC_ROOT = "." |
| 127 | |
| 128 | def setUp(self): |
| 129 | os.chdir(os.environ["TEST_TMPDIR"]) |
| 130 | |
| 131 | self._mock_adb = MockAdb() |
| 132 | |
| 133 | # Write the stub datafile which contains the package name of the app. |
| 134 | with open(self._STUB_DATAFILE, "w") as f: |
| 135 | f.write("\n".join([self._OLD_APP_PACKGE, self._APP_PACKAGE])) |
| 136 | |
| 137 | # Write the local resource apk file. |
| 138 | with open(self._RESOURCE_APK, "w") as f: |
| 139 | f.write("resource apk") |
| 140 | |
| 141 | # Mock out subprocess.Popen to use our mock adb. |
| 142 | self._popen_patch = mock.patch.object(incremental_install, "subprocess") |
| 143 | self._popen = self._popen_patch.start().Popen |
| 144 | self._popen.side_effect = lambda args, **kwargs: self._mock_adb.Exec(args) |
| 145 | |
| 146 | def tearDown(self): |
| 147 | self._popen_patch.stop() |
| 148 | |
| 149 | def _CreateZip(self, name="zip1", *files): |
| 150 | if not files: |
| 151 | files = [("zp1", "content1"), ("zp2", "content2")] |
| 152 | with zipfile.ZipFile(name, "w") as z: |
| 153 | for f, content in files: |
| 154 | z.writestr(f, content) |
| 155 | |
| 156 | def _CreateLocalManifest(self, *lines): |
| 157 | content = "\n".join(lines) |
| 158 | with open(self._DEXMANIFEST, "w") as f: |
| 159 | f.write(content) |
| 160 | return content |
| 161 | |
| 162 | def _CreateRemoteManifest(self, *lines): |
| 163 | self._PutDeviceFile("dex/manifest", "\n".join(lines)) |
| 164 | |
| 165 | def _GetDeviceAppPath(self, f): |
| 166 | return os.path.join( |
| 167 | incremental_install.DEVICE_DIRECTORY, self._APP_PACKAGE, f) |
| 168 | |
| 169 | def _GetDeviceFile(self, f): |
| 170 | return self._mock_adb.files[self._GetDeviceAppPath(f)] |
| 171 | |
| 172 | def _PutDeviceFile(self, f, content): |
| 173 | self._mock_adb.files[self._GetDeviceAppPath(f)] = content |
| 174 | |
Lukacs Berki | b4b19bc | 2015-07-30 11:45:35 +0000 | [diff] [blame] | 175 | def _DeleteDeviceFile(self, f): |
| 176 | self._mock_adb.files.pop(self._GetDeviceAppPath(f), None) |
| 177 | |
Lukacs Berki | ecbf242 | 2015-07-28 07:57:45 +0000 | [diff] [blame] | 178 | def _CallIncrementalInstall(self, incremental, native_libs=None, |
Lukacs Berki | faff718 | 2015-08-20 14:27:42 +0000 | [diff] [blame] | 179 | split_main_apk=None, split_apks=None, |
Googler | 73d4fc9 | 2015-07-30 20:39:46 +0000 | [diff] [blame] | 180 | start_type="no"): |
Lukacs Berki | faff718 | 2015-08-20 14:27:42 +0000 | [diff] [blame] | 181 | if split_main_apk: |
| 182 | apk = split_main_apk |
| 183 | elif incremental: |
Alex Humesky | a4ecde6 | 2015-05-21 17:08:42 +0000 | [diff] [blame] | 184 | apk = None |
| 185 | else: |
| 186 | apk = self._APK |
Lukacs Berki | ecbf242 | 2015-07-28 07:57:45 +0000 | [diff] [blame] | 187 | |
Alex Humesky | a4ecde6 | 2015-05-21 17:08:42 +0000 | [diff] [blame] | 188 | incremental_install.IncrementalInstall( |
| 189 | adb_path=self._ADB_PATH, |
| 190 | execroot=self._EXEC_ROOT, |
| 191 | stub_datafile=self._STUB_DATAFILE, |
| 192 | dexmanifest=self._DEXMANIFEST, |
| 193 | apk=apk, |
| 194 | resource_apk=self._RESOURCE_APK, |
Lukacs Berki | faff718 | 2015-08-20 14:27:42 +0000 | [diff] [blame] | 195 | split_main_apk=split_main_apk, |
| 196 | split_apks=split_apks, |
Lukacs Berki | ecbf242 | 2015-07-28 07:57:45 +0000 | [diff] [blame] | 197 | native_libs=native_libs, |
Alex Humesky | a4ecde6 | 2015-05-21 17:08:42 +0000 | [diff] [blame] | 198 | output_marker=self._OUTPUT_MARKER, |
| 199 | adb_jobs=1, |
Googler | 73d4fc9 | 2015-07-30 20:39:46 +0000 | [diff] [blame] | 200 | start_type=start_type, |
Alex Humesky | a4ecde6 | 2015-05-21 17:08:42 +0000 | [diff] [blame] | 201 | user_home_dir="/home/root") |
| 202 | |
| 203 | def testUploadToPristineDevice(self): |
Alex Humesky | a4ecde6 | 2015-05-21 17:08:42 +0000 | [diff] [blame] | 204 | self._CreateZip() |
| 205 | |
| 206 | with open("dex1", "w") as f: |
| 207 | f.write("content3") |
| 208 | |
| 209 | manifest = self._CreateLocalManifest( |
| 210 | "zip1 zp1 ip1 0", |
| 211 | "zip1 zp2 ip2 0", |
| 212 | "dex1 - ip3 0") |
| 213 | |
| 214 | self._CallIncrementalInstall(incremental=False) |
| 215 | |
| 216 | resources_checksum_path = self._GetDeviceAppPath("resources_checksum") |
| 217 | self.assertTrue(resources_checksum_path in self._mock_adb.files) |
| 218 | self.assertEquals(manifest, self._GetDeviceFile("dex/manifest")) |
| 219 | self.assertEquals("content1", self._GetDeviceFile("dex/ip1")) |
| 220 | self.assertEquals("content2", self._GetDeviceFile("dex/ip2")) |
| 221 | self.assertEquals("content3", self._GetDeviceFile("dex/ip3")) |
| 222 | self.assertEquals("resource apk", self._GetDeviceFile("resources.ap_")) |
| 223 | |
Lukacs Berki | faff718 | 2015-08-20 14:27:42 +0000 | [diff] [blame] | 224 | def testSplitInstallToPristineDevice(self): |
| 225 | with open("split1", "w") as f: |
| 226 | f.write("split_content1") |
| 227 | |
| 228 | with open("main", "w") as f: |
| 229 | f.write("main_Content") |
| 230 | |
| 231 | self._CallIncrementalInstall( |
| 232 | incremental=False, split_main_apk="main", split_apks=["split1"]) |
| 233 | self.assertEquals(set(["split_content1"]), self._mock_adb.split_apks) |
| 234 | |
| 235 | def testSplitInstallUnchanged(self): |
| 236 | with open("split1", "w") as f: |
| 237 | f.write("split_content1") |
| 238 | |
| 239 | with open("main", "w") as f: |
| 240 | f.write("main_Content") |
| 241 | |
| 242 | self._CallIncrementalInstall( |
| 243 | incremental=False, split_main_apk="main", split_apks=["split1"]) |
| 244 | self.assertEquals(set(["split_content1"]), self._mock_adb.split_apks) |
| 245 | self._mock_adb.split_apks = set() |
| 246 | self._CallIncrementalInstall( |
| 247 | incremental=False, split_main_apk="main", split_apks=["split1"]) |
| 248 | self.assertEquals(set([]), self._mock_adb.split_apks) |
| 249 | |
| 250 | def testSplitInstallChanges(self): |
| 251 | with open("split1", "w") as f: |
| 252 | f.write("split_content1") |
| 253 | |
| 254 | with open("main", "w") as f: |
| 255 | f.write("main_Content") |
| 256 | |
| 257 | self._CallIncrementalInstall( |
| 258 | incremental=False, split_main_apk="main", split_apks=["split1"]) |
| 259 | self.assertEquals(set(["split_content1"]), self._mock_adb.split_apks) |
| 260 | |
| 261 | with open("split1", "w") as f: |
| 262 | f.write("split_content2") |
| 263 | self._mock_adb.split_apks = set() |
| 264 | self._CallIncrementalInstall( |
| 265 | incremental=False, split_main_apk="main", split_apks=["split1"]) |
| 266 | self.assertEquals(set(["split_content2"]), self._mock_adb.split_apks) |
| 267 | |
Lukacs Berki | b4b19bc | 2015-07-30 11:45:35 +0000 | [diff] [blame] | 268 | def testMissingNativeManifestWithIncrementalInstall(self): |
| 269 | self._CreateZip() |
| 270 | with open("liba.so", "w") as f: |
| 271 | f.write("liba_1") |
| 272 | |
| 273 | # Upload a library to the device. |
| 274 | native_libs = ["armeabi-v7a:liba.so"] |
| 275 | self._CallIncrementalInstall(incremental=False, native_libs=native_libs) |
| 276 | self.assertEquals("liba_1", self._GetDeviceFile("native/liba.so")) |
| 277 | |
| 278 | # Delete the manifest, overwrite the library and check that even an |
| 279 | # incremental install straightens things out. |
| 280 | self._PutDeviceFile("native/liba.so", "GARBAGE") |
| 281 | self._CallIncrementalInstall(incremental=False, native_libs=native_libs) |
| 282 | self.assertEquals("liba_1", self._GetDeviceFile("native/liba.so")) |
| 283 | |
| 284 | def testNonIncrementalInstallOverwritesNativeLibs(self): |
| 285 | self._CreateZip() |
| 286 | with open("liba.so", "w") as f: |
| 287 | f.write("liba_1") |
| 288 | |
| 289 | # Upload a library to the device. |
| 290 | native_libs = ["armeabi-v7a:liba.so"] |
| 291 | self._CallIncrementalInstall(incremental=False, native_libs=native_libs) |
| 292 | self.assertEquals("liba_1", self._GetDeviceFile("native/liba.so")) |
| 293 | |
| 294 | # Change a library on the device. Incremental install should not replace the |
| 295 | # changed file, because it only checks the manifest. |
| 296 | self._PutDeviceFile("native/liba.so", "GARBAGE") |
| 297 | self._CallIncrementalInstall(incremental=True, native_libs=native_libs) |
| 298 | self.assertEquals("GARBAGE", self._GetDeviceFile("native/liba.so")) |
| 299 | |
| 300 | # However, a full install should overwrite it. |
| 301 | self._CallIncrementalInstall(incremental=False, native_libs=native_libs) |
| 302 | self.assertEquals("liba_1", self._GetDeviceFile("native/liba.so")) |
| 303 | |
Lukacs Berki | a3a33d7 | 2015-08-19 08:34:55 +0000 | [diff] [blame] | 304 | def testNativeAbiCompatibility(self): |
| 305 | self._CreateZip() |
| 306 | with open("liba.so", "w") as f: |
| 307 | f.write("liba") |
| 308 | |
| 309 | native_libs = ["armeabi:liba.so"] |
| 310 | self._mock_adb.SetAbi("arm64-v8a") |
| 311 | self._CallIncrementalInstall(incremental=False, native_libs=native_libs) |
| 312 | self.assertEquals("liba", self._GetDeviceFile("native/liba.so")) |
| 313 | |
Lukacs Berki | ecbf242 | 2015-07-28 07:57:45 +0000 | [diff] [blame] | 314 | def testUploadNativeLibs(self): |
| 315 | self._CreateZip() |
| 316 | with open("liba.so", "w") as f: |
| 317 | f.write("liba_1") |
| 318 | with open("libb.so", "w") as f: |
| 319 | f.write("libb_1") |
Alex Humesky | a4ecde6 | 2015-05-21 17:08:42 +0000 | [diff] [blame] | 320 | |
Lukacs Berki | ecbf242 | 2015-07-28 07:57:45 +0000 | [diff] [blame] | 321 | native_libs = ["armeabi-v7a:liba.so", "armeabi-v7a:libb.so"] |
| 322 | self._CallIncrementalInstall(incremental=False, native_libs=native_libs) |
| 323 | self.assertEquals("liba_1", self._GetDeviceFile("native/liba.so")) |
| 324 | self.assertEquals("libb_1", self._GetDeviceFile("native/libb.so")) |
| 325 | |
| 326 | # Change a library |
| 327 | with open("libb.so", "w") as f: |
| 328 | f.write("libb_2") |
| 329 | self._CallIncrementalInstall(incremental=True, native_libs=native_libs) |
| 330 | self.assertEquals("libb_2", self._GetDeviceFile("native/libb.so")) |
| 331 | |
| 332 | # Delete a library |
| 333 | self._CallIncrementalInstall( |
| 334 | incremental=True, native_libs=["armeabi-v7a:liba.so"]) |
| 335 | self.assertFalse( |
| 336 | self._GetDeviceAppPath("native/libb.so") in self._mock_adb.files) |
| 337 | |
| 338 | # Add the deleted library back |
| 339 | self._CallIncrementalInstall(incremental=True, native_libs=native_libs) |
| 340 | self.assertEquals("libb_2", self._GetDeviceFile("native/libb.so")) |
| 341 | |
| 342 | def testUploadWithOneChangedFile(self): |
Alex Humesky | a4ecde6 | 2015-05-21 17:08:42 +0000 | [diff] [blame] | 343 | # Existing manifest from a previous install. |
| 344 | self._CreateRemoteManifest( |
| 345 | "zip1 zp1 ip1 0", |
| 346 | "zip1 zp2 ip2 1") |
| 347 | |
| 348 | # Existing files from a previous install. |
| 349 | self._PutDeviceFile("dex/ip1", "old content1") |
| 350 | self._PutDeviceFile("dex/ip2", "old content2") |
| 351 | self._PutDeviceFile("install_timestamp", "0") |
| 352 | self._mock_adb.package_timestamp = "0" |
| 353 | |
| 354 | self._CreateZip() |
| 355 | |
| 356 | # Updated dex manifest. |
| 357 | self._CreateLocalManifest( |
| 358 | "zip1 zp1 ip1 0", |
| 359 | "zip1 zp2 ip2 2") |
| 360 | |
| 361 | self._CallIncrementalInstall(incremental=True) |
| 362 | |
| 363 | # This is a bit of a dishonest test: the local content for "ip1" is |
| 364 | # "content1" and the remote content for it is "old content1", but |
| 365 | # the checksums for that file are the same in the local and remote manifest. |
| 366 | # We just want to make sure that only one file was updated, so to |
| 367 | # distinguish that we force the local and remote content to be different but |
| 368 | # keep the checksum the same. |
| 369 | self.assertEquals("old content1", self._GetDeviceFile("dex/ip1")) |
| 370 | self.assertEquals("content2", self._GetDeviceFile("dex/ip2")) |
| 371 | |
| 372 | def testFullUploadWithOneChangedFile(self): |
| 373 | |
| 374 | # Existing manifest from a previous install. |
| 375 | self._CreateRemoteManifest( |
| 376 | "zip1 zp1 ip1 0", |
| 377 | "zip1 zp2 ip2 1") |
| 378 | |
| 379 | self._PutDeviceFile("dex/ip1", "old content1") |
| 380 | self._PutDeviceFile("dex/ip2", "old content2") |
| 381 | self._PutDeviceFile("install_timestamp", "0") |
| 382 | self._mock_adb.package_timestamp = "0" |
| 383 | |
| 384 | self._CreateZip() |
| 385 | |
| 386 | self._CreateLocalManifest( |
| 387 | "zip1 zp1 ip1 0", |
| 388 | "zip1 zp2 ip2 2") |
| 389 | |
| 390 | self._CallIncrementalInstall(incremental=False) |
| 391 | |
| 392 | # Even though the checksums for ip1 were the same, the file still got |
| 393 | # updated. This is a bit of a dishonest test because the local and remote |
| 394 | # content for ip1 were different, but their checksums were the same. |
| 395 | self.assertEquals("content1", self._GetDeviceFile("dex/ip1")) |
| 396 | self.assertEquals("content2", self._GetDeviceFile("dex/ip2")) |
| 397 | |
| 398 | def testUploadWithNewFile(self): |
| 399 | |
| 400 | self._CreateRemoteManifest("zip1 zp1 ip1 0") |
| 401 | self._PutDeviceFile("dex/ip1", "content1") |
| 402 | self._PutDeviceFile("install_timestamp", "0") |
| 403 | self._mock_adb.package_timestamp = "0" |
| 404 | |
| 405 | self._CreateLocalManifest( |
| 406 | "zip1 zp1 ip1 0", |
| 407 | "zip1 zp2 ip2 1") |
| 408 | |
| 409 | self._CreateZip() |
| 410 | |
| 411 | self._CallIncrementalInstall(incremental=True) |
| 412 | |
| 413 | self.assertEquals("content1", self._GetDeviceFile("dex/ip1")) |
| 414 | self.assertEquals("content2", self._GetDeviceFile("dex/ip2")) |
| 415 | |
| 416 | def testDeletesFile(self): |
| 417 | |
| 418 | self._CreateRemoteManifest( |
| 419 | "zip1 zp1 ip1 0", |
| 420 | "zip1 zip2 ip2 1") |
| 421 | self._PutDeviceFile("dex/ip1", "content1") |
| 422 | self._PutDeviceFile("dex/ip2", "content2") |
Lukacs Berki | faff718 | 2015-08-20 14:27:42 +0000 | [diff] [blame] | 423 | self._PutDeviceFile("install_timestamp", "1") |
| 424 | self._mock_adb.package_timestamp = "1" |
Alex Humesky | a4ecde6 | 2015-05-21 17:08:42 +0000 | [diff] [blame] | 425 | |
| 426 | self._CreateZip("zip1", ("zp1", "content1")) |
| 427 | self._CreateLocalManifest("zip1 zp1 ip1 0") |
| 428 | |
| 429 | self.assertTrue(self._GetDeviceAppPath("dex/ip2") in self._mock_adb.files) |
| 430 | self._CallIncrementalInstall(incremental=True) |
| 431 | self.assertFalse(self._GetDeviceAppPath("dex/ip2") in self._mock_adb.files) |
| 432 | |
| 433 | def testNothingToUpdate(self): |
| 434 | self._CreateRemoteManifest( |
| 435 | "zip1 zp1 ip1 0", |
| 436 | "zip1 zip2 ip2 1", |
| 437 | "dex1 - ip3 0") |
| 438 | self._PutDeviceFile("dex/ip1", "content1") |
| 439 | self._PutDeviceFile("dex/ip2", "content2") |
| 440 | self._PutDeviceFile("dex/ip3", "content3") |
| 441 | self._PutDeviceFile("install_timestamp", "0") |
| 442 | self._mock_adb.package_timestamp = "0" |
| 443 | |
| 444 | self._CreateZip() |
| 445 | self._CreateLocalManifest( |
| 446 | "zip1 zp1 ip1 0", |
| 447 | "zip1 zip2 ip2 1", |
| 448 | "dex1 - ip3 0") |
| 449 | |
| 450 | self._CallIncrementalInstall(incremental=True) |
| 451 | self.assertEquals("content1", self._GetDeviceFile("dex/ip1")) |
| 452 | self.assertEquals("content2", self._GetDeviceFile("dex/ip2")) |
| 453 | self.assertEquals("content3", self._GetDeviceFile("dex/ip3")) |
| 454 | |
| 455 | def testNoResourcesToUpdate(self): |
| 456 | self._CreateRemoteManifest("zip1 zp1 ip1 0") |
| 457 | self._PutDeviceFile("dex/ip1", "content1") |
| 458 | # The local file is actually "resource apk", but the checksum on the device |
| 459 | # for the resources file is set to be the same as the checksum for the |
| 460 | # local file so that we can confirm that it was not updated. |
| 461 | self._PutDeviceFile("resources.ap_", "resources") |
| 462 | self._PutDeviceFile("resources_checksum", |
| 463 | incremental_install.Checksum(self._RESOURCE_APK)) |
| 464 | self._PutDeviceFile("install_timestamp", "0") |
| 465 | self._mock_adb.package_timestamp = "0" |
| 466 | |
| 467 | self._CreateZip() |
| 468 | self._CreateLocalManifest("zip1 zp1 ip1 0") |
| 469 | |
| 470 | self._CallIncrementalInstall(incremental=True) |
| 471 | self.assertEquals("resources", self._GetDeviceFile("resources.ap_")) |
| 472 | |
| 473 | def testUpdateResources(self): |
| 474 | self._CreateRemoteManifest("zip1 zp1 ip1 0") |
| 475 | self._PutDeviceFile("dex/ip1", "content1") |
| 476 | self._PutDeviceFile("resources.ap_", "resources") |
| 477 | self._PutDeviceFile("resources_checksum", "checksum") |
| 478 | self._PutDeviceFile("install_timestamp", "0") |
| 479 | self._mock_adb.package_timestamp = "0" |
| 480 | |
| 481 | self._CreateZip() |
| 482 | self._CreateLocalManifest("zip1 zp1 ip1 0") |
| 483 | |
| 484 | self._CallIncrementalInstall(incremental=True) |
| 485 | self.assertEquals("resource apk", self._GetDeviceFile("resources.ap_")) |
| 486 | |
| 487 | def testNoDevice(self): |
| 488 | self._mock_adb.SetError(1, "", "device not found") |
| 489 | try: |
| 490 | self._CallIncrementalInstall(incremental=True) |
| 491 | self.fail("Should have quit if there is no device") |
Alex Humesky | f0a5ac6 | 2015-09-22 00:41:11 +0000 | [diff] [blame] | 492 | except SystemExit as e: |
| 493 | # make sure it's the right SystemExit reason |
| 494 | self.assertTrue("Device not found" in str(e)) |
Alex Humesky | a4ecde6 | 2015-05-21 17:08:42 +0000 | [diff] [blame] | 495 | |
| 496 | def testUnauthorizedDevice(self): |
| 497 | self._mock_adb.SetError(1, "", "device unauthorized. Please check the " |
| 498 | "confirmation dialog on your device") |
| 499 | try: |
| 500 | self._CallIncrementalInstall(incremental=True) |
| 501 | self.fail("Should have quit if the device is unauthorized.") |
Alex Humesky | f0a5ac6 | 2015-09-22 00:41:11 +0000 | [diff] [blame] | 502 | except SystemExit as e: |
| 503 | # make sure it's the right SystemExit reason |
| 504 | self.assertTrue("Device unauthorized." in str(e)) |
Alex Humesky | a4ecde6 | 2015-05-21 17:08:42 +0000 | [diff] [blame] | 505 | |
| 506 | def testInstallFailure(self): |
Lukacs Berki | 60f6cf6 | 2015-12-16 08:57:12 +0000 | [diff] [blame] | 507 | self._mock_adb.SetError(0, "Failure", "INSTALL_FAILED", for_arg="install") |
Alex Humesky | a4ecde6 | 2015-05-21 17:08:42 +0000 | [diff] [blame] | 508 | self._CreateZip() |
| 509 | self._CreateLocalManifest("zip1 zp1 ip1 0") |
| 510 | try: |
| 511 | self._CallIncrementalInstall(incremental=False) |
| 512 | self.fail("Should have quit if the install failed.") |
Alex Humesky | f0a5ac6 | 2015-09-22 00:41:11 +0000 | [diff] [blame] | 513 | except SystemExit as e: |
| 514 | # make sure it's the right SystemExit reason |
| 515 | self.assertTrue("Failure" in str(e)) |
Alex Humesky | a4ecde6 | 2015-05-21 17:08:42 +0000 | [diff] [blame] | 516 | |
Googler | 73d4fc9 | 2015-07-30 20:39:46 +0000 | [diff] [blame] | 517 | def testStartCold(self): |
Alex Humesky | a4ecde6 | 2015-05-21 17:08:42 +0000 | [diff] [blame] | 518 | # Based on testUploadToPristineDevice |
| 519 | self._CreateZip() |
| 520 | |
| 521 | with open("dex1", "w") as f: |
| 522 | f.write("content3") |
| 523 | |
| 524 | self._CreateLocalManifest( |
| 525 | "zip1 zp1 ip1 0", |
| 526 | "zip1 zp2 ip2 0", |
| 527 | "dex1 - ip3 0") |
| 528 | |
Googler | 73d4fc9 | 2015-07-30 20:39:46 +0000 | [diff] [blame] | 529 | self._CallIncrementalInstall(incremental=False, start_type="cold") |
Alex Humesky | a4ecde6 | 2015-05-21 17:08:42 +0000 | [diff] [blame] | 530 | |
| 531 | self.assertTrue(("monkey -p %s -c android.intent.category.LAUNCHER 1" % |
| 532 | self._APP_PACKAGE) in self._mock_adb.shell_cmdlns) |
| 533 | |
Googler | 73d4fc9 | 2015-07-30 20:39:46 +0000 | [diff] [blame] | 534 | def testColdStop(self): |
| 535 | self._CreateRemoteManifest( |
| 536 | "zip1 zp1 ip1 0", |
| 537 | "zip1 zip2 ip2 1", |
| 538 | "dex1 - ip3 0") |
| 539 | self._PutDeviceFile("dex/ip1", "content1") |
| 540 | self._PutDeviceFile("dex/ip2", "content2") |
| 541 | self._PutDeviceFile("dex/ip3", "content3") |
| 542 | self._PutDeviceFile("install_timestamp", "0") |
| 543 | self._mock_adb.package_timestamp = "0" |
| 544 | |
| 545 | self._CreateZip() |
| 546 | self._CreateLocalManifest( |
| 547 | "zip1 zp1 ip1 0", |
| 548 | "zip1 zip2 ip2 1", |
| 549 | "dex1 - ip3 0") |
| 550 | self._CallIncrementalInstall(incremental=True, start_type="cold") |
| 551 | |
| 552 | stop_cmd = "am force-stop %s" % self._APP_PACKAGE |
| 553 | self.assertTrue(stop_cmd in self._mock_adb.shell_cmdlns) |
| 554 | |
| 555 | def testWarmStop(self): |
| 556 | self._CreateRemoteManifest( |
| 557 | "zip1 zp1 ip1 0", |
| 558 | "zip1 zip2 ip2 1", |
| 559 | "dex1 - ip3 0") |
| 560 | self._PutDeviceFile("dex/ip1", "content1") |
| 561 | self._PutDeviceFile("dex/ip2", "content2") |
| 562 | self._PutDeviceFile("dex/ip3", "content3") |
| 563 | self._PutDeviceFile("install_timestamp", "0") |
| 564 | self._mock_adb.package_timestamp = "0" |
| 565 | |
| 566 | self._CreateZip() |
| 567 | self._CreateLocalManifest( |
| 568 | "zip1 zp1 ip1 0", |
| 569 | "zip1 zip2 ip2 1", |
| 570 | "dex1 - ip3 0") |
| 571 | self._CallIncrementalInstall(incremental=True, start_type="warm") |
| 572 | |
| 573 | background_cmd = "input keyevent KEYCODE_APP_SWITCH" |
| 574 | stop_cmd = "am kill %s" % self._APP_PACKAGE |
| 575 | self.assertTrue(background_cmd in self._mock_adb.shell_cmdlns) |
| 576 | self.assertTrue(stop_cmd in self._mock_adb.shell_cmdlns) |
| 577 | |
Alex Humesky | a4ecde6 | 2015-05-21 17:08:42 +0000 | [diff] [blame] | 578 | def testMultipleDevicesError(self): |
| 579 | errors = [ |
| 580 | "more than one device and emulator", |
| 581 | "more than one device", |
| 582 | "more than one emulator", |
| 583 | ] |
| 584 | for error in errors: |
Alex Humesky | f0a5ac6 | 2015-09-22 00:41:11 +0000 | [diff] [blame] | 585 | self._mock_adb.SetError(1, "", error) |
Alex Humesky | a4ecde6 | 2015-05-21 17:08:42 +0000 | [diff] [blame] | 586 | try: |
| 587 | self._CallIncrementalInstall(incremental=True) |
| 588 | self.fail("Should have quit if there were multiple devices.") |
Alex Humesky | f0a5ac6 | 2015-09-22 00:41:11 +0000 | [diff] [blame] | 589 | except SystemExit as e: |
| 590 | # make sure it's the right SystemExit reason |
| 591 | self.assertTrue("Try specifying a device serial" in str(e)) |
Alex Humesky | a4ecde6 | 2015-05-21 17:08:42 +0000 | [diff] [blame] | 592 | |
| 593 | def testIncrementalInstallOnPristineDevice(self): |
| 594 | self._CreateZip() |
| 595 | self._CreateLocalManifest( |
| 596 | "zip1 zp1 ip1 0", |
| 597 | "zip1 zip2 ip2 1", |
| 598 | "dex1 - ip3 0") |
| 599 | |
| 600 | try: |
| 601 | self._CallIncrementalInstall(incremental=True) |
| 602 | self.fail("Should have quit for incremental install on pristine device") |
| 603 | except SystemExit: |
| 604 | pass |
| 605 | |
| 606 | def testIncrementalInstallWithWrongInstallTimestamp(self): |
| 607 | self._CreateRemoteManifest( |
| 608 | "zip1 zp1 ip1 0", |
| 609 | "zip1 zip2 ip2 1", |
| 610 | "dex1 - ip3 0") |
| 611 | self._PutDeviceFile("dex/ip1", "content1") |
| 612 | self._PutDeviceFile("dex/ip2", "content2") |
| 613 | self._PutDeviceFile("dex/ip3", "content3") |
| 614 | self._mock_adb.package_timestamp = "WRONG" |
| 615 | |
| 616 | self._CreateZip() |
| 617 | self._CreateLocalManifest( |
| 618 | "zip1 zp1 ip1 0", |
| 619 | "zip1 zip2 ip2 1", |
| 620 | "dex1 - ip3 0") |
| 621 | |
| 622 | try: |
| 623 | self._CallIncrementalInstall(incremental=True) |
| 624 | self.fail("Should have quit if install timestamp is wrong") |
| 625 | except SystemExit: |
| 626 | pass |
| 627 | |
Alex Humesky | f0a5ac6 | 2015-09-22 00:41:11 +0000 | [diff] [blame] | 628 | def testSdkTooOld(self): |
| 629 | self._mock_adb.SetError( |
| 630 | 0, "INSTALL_FAILED_OLDER_SDK", "", for_arg="install") |
| 631 | self._CreateZip() |
| 632 | self._CreateLocalManifest("zip1 zp1 ip1 0") |
| 633 | try: |
| 634 | self._CallIncrementalInstall(incremental=False) |
| 635 | self.fail("Should have quit if the SDK is too old.") |
| 636 | except SystemExit as e: |
| 637 | # make sure it's the right SystemExit reason |
| 638 | self.assertTrue("minSdkVersion" in str(e)) |
| 639 | |
| 640 | |
Alex Humesky | a4ecde6 | 2015-05-21 17:08:42 +0000 | [diff] [blame] | 641 | if __name__ == "__main__": |
| 642 | unittest.main() |