| OLD | NEW |
| (Empty) | |
| 1 # Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. |
| 4 """Tests for dexdiffer.""" |
| 5 |
| 6 import dexdiffer |
| 7 import json |
| 8 import unittest |
| 9 |
| 10 |
| 11 class DexdifferTest(unittest.TestCase): |
| 12 |
| 13 def testReadDict(self): |
| 14 mapping_file = [ |
| 15 'package.ClassName -> package.qq:\n', |
| 16 'android.support.v8.MenuPopupHelper -> android.support.v8.v:', |
| 17 ' android.view.LayoutInflater mInflater -> d\n', |
| 18 ' 117:118:void setForceShowIcon(boolean) -> b', |
| 19 ' 1:1:package.ClassName <init>(int,int) -> <init>', |
| 20 ] |
| 21 expected = { |
| 22 'package.qq': ['package.ClassName', {}], |
| 23 'android.support.v8.v': |
| 24 ['android.support.v8.MenuPopupHelper', |
| 25 {'android.view.LayoutInflater d': |
| 26 'android.view.LayoutInflater mInflater', |
| 27 'void b(boolean)': 'void setForceShowIcon(boolean)', |
| 28 'package.ClassName <init>(int,int)': |
| 29 'package.ClassName <init>(int,int)'}], |
| 30 } |
| 31 |
| 32 actual = dexdiffer._ReadMappingDict(mapping_file) |
| 33 self.assertDeepEqual(actual, expected) |
| 34 |
| 35 def testGetLineTokens1(self): |
| 36 line = ' public void <init> (android.content.Context,float); // Cxr' |
| 37 expected = ['void', '<init>', 'android.content.Context', 'float'] |
| 38 actual = dexdiffer._GetLineTokens(line) |
| 39 self.assertDeepEqual(actual, expected) |
| 40 |
| 41 def testGetLineTokens2(self): |
| 42 line = '/** asdfasdf /**/ private final static /*asdf*/int varname$1;' |
| 43 expected = ['int', 'varname$1'] |
| 44 actual = dexdiffer._GetLineTokens(line) |
| 45 self.assertDeepEqual(actual, expected) |
| 46 |
| 47 def testGetLineTokens3(self): |
| 48 line = 'int[] varname_1;' |
| 49 expected = ['int[]', 'varname_1'] |
| 50 actual = dexdiffer._GetLineTokens(line) |
| 51 self.assertDeepEqual(actual, expected) |
| 52 |
| 53 def testGetLineTokensEmpty(self): |
| 54 line = '/***/ /*asdf*/ //comment;' |
| 55 expected = [] |
| 56 actual = dexdiffer._GetLineTokens(line) |
| 57 self.assertDeepEqual(actual, expected) |
| 58 |
| 59 def testGetMemberIdentifier(self): |
| 60 line_tokens = ['void', 'b', 'boolean', 'rnmd[]'] |
| 61 expected = 'void foo(boolean,renamed.type[])' |
| 62 renamed_class_name = 'renamed.class' |
| 63 mapping_dict = { renamed_class_name : [ 'actual.class.name', { |
| 64 'void b': 'void variable_name', |
| 65 'boolean a': 'boolean variable_name2', |
| 66 'void b(boolean,renamed.type)': |
| 67 'void wrong_function(boolean,renamed.type)', |
| 68 'void b(boolean,renamed.type[])': expected, |
| 69 }], 'rnmd': ['renamed.type', {}]} |
| 70 actual = dexdiffer._GetMemberIdentifier(line_tokens, mapping_dict, |
| 71 renamed_class_name, True) |
| 72 self.assertEqual(expected, actual) |
| 73 |
| 74 def testIsLineFunctionDefinition(self): |
| 75 line = 'java.lang.String CONSOLE_ELISION= "[(0)]"' |
| 76 self.assertFalse(dexdiffer._IsLineFunctionDefinition(line)) |
| 77 |
| 78 def testStripQuotes(self): |
| 79 string = 'abc\'123\'"456"def\'"7"\'' |
| 80 self.assertEqual('abcdef', dexdiffer._StripQuotes(string)) |
| 81 |
| 82 def testBuildMappedDexDict(self): |
| 83 dextra_output = [ |
| 84 '/* 3396 */ public class org.chromium.chrome.browser.widget.a', |
| 85 ' extends android.view.View {', |
| 86 ' /** 2 Instance Fields **/', |
| 87 ' private org.chromium.chrome.browser.widget.b$1 x;', |
| 88 ' private int mPosition;', |
| 89 ' /** 1 Direct Methods **/', |
| 90 ' public void <init> (android.content.Context, android.util.Attribute' |
| 91 'Set); // Constructor', |
| 92 ' /** 2 Virtual Methods **/', |
| 93 ' public void init (int, int);', |
| 94 ' protected void onDraw (android.graphics.Canvas);', |
| 95 ' } // end class org.chromium.chrome.browser.widget.a', |
| 96 '/* 3397 */ class org.chromium.chrome.browser.widget.b$1', |
| 97 ' implements android.text.TextWatcher {', |
| 98 ' /** 1 Instance Fields **/', |
| 99 ' final org.chromium.chrome.browser.widget.FloatLabelLayout' |
| 100 ' this$0;', |
| 101 ' /** 1 Direct Methods **/', |
| 102 ' bool <init> (); // Constructor', |
| 103 ' /** 1 Virtual Methods **/', |
| 104 ' public void x (org.chromium.chrome.browser.widget.a, int, int,' |
| 105 ' int);', |
| 106 ' } // end class org.chromium.chrome.browser.widget.b$1'] |
| 107 mapping_dict = { |
| 108 'org.chromium.chrome.browser.widget.a': ['class_name_a', { |
| 109 'class_name_b$1 x': 'class_name_b$1 full_member_variable', |
| 110 'int mPosition': 'int mPosition', |
| 111 'void <init>(android.content.Context,android.util.AttributeSet)': |
| 112 'void <init>(android.content.Context,android.util.AttributeSet)', |
| 113 'void init(int,int)': 'void init(int,int)', |
| 114 'void onDraw(android.graphics.Canvas)': |
| 115 'void onDraw(android.graphics.Canvas)', |
| 116 }], |
| 117 'org.chromium.chrome.browser.widget.b$1': ['class_name_b$1', { |
| 118 'org.chromium.chrome.browser.widget.FloatLabelLayout this$0': |
| 119 'org.chromium.chrome.browser.widget.FloatLabelLayout this$0', |
| 120 'boolean <init>()': 'boolean <init>()', |
| 121 'void x(class_name_a,int,int,int)': |
| 122 'void full_function_name(class_name_a,int,int,int)' |
| 123 }], |
| 124 } |
| 125 actual = dexdiffer._BuildMappedDexDict(dextra_output, mapping_dict) |
| 126 expected = { 'class_name_a': [ |
| 127 'class_name_b$1 full_member_variable', |
| 128 'int mPosition', |
| 129 'void <init>(android.content.Context,android.util.AttributeSet)', |
| 130 'void init(int,int)', |
| 131 'void onDraw(android.graphics.Canvas)' |
| 132 ], 'class_name_b$1': [ |
| 133 'org.chromium.chrome.browser.widget.FloatLabelLayout this$0', |
| 134 'boolean <init>()', |
| 135 'void full_function_name(class_name_a,int,int,int)' |
| 136 ]} |
| 137 self.assertDeepEqual(actual, expected) |
| 138 |
| 139 def testParseMappingLine(self): |
| 140 orig_name = "abc.q12$1" |
| 141 new_name = "a$1" |
| 142 line = orig_name + " -> " + new_name |
| 143 actual_orig_name, actual_new_name = dexdiffer._ParseMappingLine(line) |
| 144 self.assertEqual(orig_name, actual_orig_name) |
| 145 self.assertEqual(new_name, actual_new_name) |
| 146 |
| 147 def testDiffDexDicts(self): |
| 148 base_dict = { 'class_name_a': [ |
| 149 'class_name_b$1 full_member_variable', |
| 150 'int mPosition', |
| 151 'void <init>(android.content.Context,android.util.AttributeSet)', |
| 152 'void init(int,int)', |
| 153 'void onDraw(android.graphics.Canvas)' |
| 154 ], 'class_name_b$1': [ |
| 155 'org.chromium.chrome.browser.widget.FloatLabelLayout this$0', |
| 156 'void <init>()', |
| 157 'void full_function_name(class_name_a,int,int)' |
| 158 ], 'class_name_deleted': []} |
| 159 new_dict = { 'class_name_a': [ |
| 160 'class_name_b$1 full_member_variable', |
| 161 'int mPosition', |
| 162 'void <init>(android.content.Context,android.util.AttributeSet)', |
| 163 'void init(int,int)', |
| 164 'void onDraw(android.graphics.Canvas)' |
| 165 ], 'class_name_b$1': [ |
| 166 'org.chromium.chrome.browser.widget.FloatLabelLayout this$0', |
| 167 'void <init>()', |
| 168 'void full_function_name(int,int,int)' |
| 169 ], 'class_name_new': []} |
| 170 actual = dexdiffer._DiffDexDicts(base_dict, new_dict) |
| 171 expected = [('class_name_b$1\n' |
| 172 '- void full_function_name(class_name_a,int,int)\n' |
| 173 '+ void full_function_name(int,int,int)'), |
| 174 '-class class_name_deleted', |
| 175 '+class class_name_new'] |
| 176 |
| 177 self.assertDeepEqual(actual, expected) |
| 178 |
| 179 def assertDeepEqual(self, actual, expected): |
| 180 # Only designed to work for json-able types |
| 181 a = json.dumps(actual, sort_keys=True) |
| 182 e = json.dumps(expected, sort_keys=True) |
| 183 self.assertEqual(a, e) |
| 184 |
| 185 if __name__ == '__main__': |
| 186 unittest.main() |
| OLD | NEW |