2020 7th NAFOSTED Conference on Information and Computer Science (NICS) Generating test data for energy property in mobile applications Anh-Tu Bui1,2* , Van-Viet Nguyen2 , Ninh-Thuan Truong1 2020 7th NAFOSTED Conference on Information and Computer Science (NICS) | 978-0-7381-0553-6/20/$31.00 ©2020 IEEE | DOI: 10.1109/NICS51282.2020.9335868 VNU, University of Engineering and Technology, Ha Noi, Vietnam Thai Nguyen University of Information and Communication Technology, Thai Nguyen, Vietnam E-mail: batu@ictu.edu.vn, nvviet@ictu.edu.vn, thuantn@vnu.edu.vn Abstract—In recent years, mobile software has been rapidly developing The key requirement of mobile software is to ensure optimum power throughout its run Unlike testing other software properties, energy testing is usually done on real devices to check actual energy consumption As such, testers need to have optimal test data to save time and effort in testing In this paper, we propose an approach to automatically generating test data in mobile software testing, concentrate on energy properties The approach, firstly, builds CFGs (Control Flow Graph) from the source code of the application We then propose algorithms to select testable paths from CFG such that the mobile applications consume more energy Finally, test data are generated from the testable paths Index Terms—Power Consumption, Control Flow Graph, Software Testing I I NTRODUCTION In recent years, the number of people using mobile devices has increased significantly They use mobile phones not only for connecting people but also for entertainment and working Mobile applications are becoming more and more popular and handy Time spent on mobile devices increases day by day, so the duration usage of mobile devices requires to be increased Hardware manufacturers are increasingly expanding the capacity of their batteries while software makers are trying to optimize software when applications are in use In software engineering field, to prepare releasing of an application, one needs to test energy properties of the software application There have been many studies on testing the properties of software systems, such as safety, performance, security, etc Recently, testing the energy properties of software systems, particularly mobile software, drawing software experts’ attention Testing energy properties [3] aims to detect errors that cause of large or unnecessary power consumption It may helps software developers promptly fix software errors Unlike other types of testing, most energy properties testing are performed on real devices and are measured in reality to evaluate the energy consumption for each test set, which results in wasting a lot of time and effort Therefore, optimizing test suites and eliminating unhelpful test cases promise a solution to save much resources in testing mobile applications The paper [5] has also shown that there are essentially faults in the source code that cause the device to consume high amounts of energy In order to find energy bugs, we focus on finding code snippets that consume more power than allowed by testing In this paper, we propose an approach to automatically generating test data concentrating on energy properties The contribution of our paper includes: (1) proposing a CFG (Control Flow Graph) from mobile software’ source code; (2) building algorithms to select the testable paths which the mobile applications consume more power; (3) generating test data from the testable paths The remain structure of the paper is presented as follows Section II presents an overview of the knowledge and issues that need to be addressed in testing the energy properties of the application In Section III, we propose a new CFG graph to model the change in power consumption when the statements are executed Section IV introduces the algorithms that optimize the graph and generating the paths on the graph to generate test data We illustrate the result of the approach using a small case study in Section V The last section offers a conclusion and some directions for development of the research II BACKGROUND The techniques we propose in this paper are to create a new CFG, which can help find test data on the energy characteristics of the application [2] In this section, we discuss some of the issues of testing, energy properties and energy states proposed in previous studies [1] A The issues of using CFG in testing The input data of the test cases is very important, they decide whether to identify errors in the program or not There are many methods for determining input data for test cases [15], one of which is CFG analysis A Control Flow Graph (CFG) is a directed graph in which the vertices (or nodes) represent basic building blocks and the edges represent control flow paths [11] CFG can be generated automatically from source cod eas exemplified in Figure In CFG, a feasible and complete path is a set of consecutive vertices from the first vertex (v1 ) to the last vertex (vn ): path = {v1 , v2 , , } A set of test cases will be generated from a path Finding all the paths will provide a useful set of test data [7] 978-0-7381-0553-6/20/$31.00 IEEEBarbara Downloaded 96on June 19,2021 at 00:32:02 UTC from IEEE Xplore Restrictions apply Authorized licensed use limited to: Univ©2020 of Calif Santa 2020 7th NAFOSTED Conference on Information and Computer Science (NICS) Fig Generating a CFG Generating test data is most difficult when loops occur in CFG However, the paper [6] and [16] has solved the above problems We therefore acknowledge that CFG only includes complete and independent paths This is an important basis for creating a new graph to test the energy properties of the application B The issues of CFG in generating energy test cases Several works showing how to create test cases from CFG [12] [13] [14] When we applied these methods to test the energy properties of a mobile application, we discovered a number of issues that needed to be resolved 1) CFG has many useless paths in testing energy properties: Energy bugs are a major cause of fast energy declines on mobile devices Energy bugs have different characteristics than other functional bugs, graphical bugs, wording bugs, or performance bug For example, with the functional bugs, one tries to detect all paths in CFG and generate test data from these paths However, when looking at the paths on CFG, we find that there are a few paths that have little effect on the device’s power consumption (unhelpful paths), we focus on removing unhelpful paths to optimize the number of test cases 2) CFG data is not sufficient for testing energy properties: Another problem, the test results depend on the testing environment in some cases For example, when we download a file, network speed and file size affect the test results of the download function Poor network speeds or large file sizes increase download time, so the device consumes more power The time affects the energy consumption of mobile device However, the data generated from CFG has not been able to check this case To solve this problem, we propose a solution to mark the nodes on CFG depending on the time, as a basis for the generation of test data in the next step C Power state of Mobile applications In the previous study [1], we introduced a concept called the ”Power state of the application” - the power state of the device when it is controlled by the application Accordingly, when each statement in the application is executed, it can change the device’s power consumption, then the power consumption state changes In figure 2, when the M ediaP layer.Start() statement is executed, the speaker system of the mobile device is turned on, which consumes a higher power level, the device switches to a new state of energy consumption Fig The device’s power consumption status are changed by control statements 97on June 19,2021 at 00:32:02 UTC from IEEE Xplore Restrictions apply Authorized licensed use limited to: Univ of Calif Santa Barbara Downloaded 2020 7th NAFOSTED Conference on Information and Computer Science (NICS) However, not all statements make the device change its state of power consumption large enough, so we classify the energy consumption levels, the approximate power consumption is considered a similitude Major changes take place mainly in statements that change the status of each hardware component such as Camera, Wifi, 3G Cellular, Display, Audio From determining the energy states of the device, we created an energy automaton called P CAapp P CAapp indicates how the device’s power consumption states are changed by the execution of each statement Next, we calculate the power consumption for each state of the P CAapp By experimentally measuring on one device, we give the consumption weight for each state of P CAapp This will help us know which states consume a large amount of energy, which is the basis for further research III O UR A PPROACH It is possible to generate test data for some software properties from CF G, but it is ineffective when applied to the energy properties of the software because of the two issues raised in Part II We propose a new CF G to solve the problems in Part This approach focuses on assigning two weights (consumption and execution time) to the CF G to overcome the disadvantages of CF G in energy testing Figure depicts our approach of generating test data First, we combined the P CAA pp with the CF G to create a new graph (named CF Gc ) The graph CF Gc is the same as CF G but the energy consumption is an extra weight on the vertices, which helps to evaluate testable and unhelpful paths Next, we evaluate the statements in the source code to mark the vertices that are time-sensitive, we create the new CF Gct A Assigning energy consumption weight to CF G The work [8] and many other studies have proposed algorithms that generate CF G from source code automatically We use the result of this work to generate a CF G from a mobile software code The number of branches on CF G will be very large, we need to eliminate those that have less effect on energy consumption To determine how each branch affects the energy consumption, we calculate the weight for each vertex in the CF G using the energy automaton proposed in the previous studies [1] and [10] The new graph is called CF Gc and if v is a vertex (or node) of CF G, then vc is the new vertex of CF Gc Where: vc = {v, c} and c is power consumption weight of vc We perform algorithm to assign the energy weights into vertices of the CF Gc Figure 4(a) is the original CF G generated from the source code of the application After applying algorithm 1, the graph CF Gc have been built as shown Figure 4(b), in which vertices are assigned energy consumption weights Algorithm Assigning the power consumption weights in each vertex Input: Android application source code Output: CF Gc 1: 2: 3: 4: 5: 6: Generate CF G and P CAapp from the source code Initialize CF Gc = CF G and vc = {v, blank} ∀ vc for each vertex v in CF G Determine the corresponding state s in P CAapp vc = {v, c} and c = cs where cs is power consumption weight of state s end for B Assigning time weight to CF Gc To solve the second problem, we continue to improve CF Gc and assign time weights to the vertices The vertices of the graph are divided into types: • • Time dependent: these vertices are marked with a star (∗) Regardless of time: these vertices are left blank We call the new graph is CF Gct Figure 4(c) shows an example of some vertices depending on execution time If vc is the vertex of CF Gc , then vct = {vc , blank}|{vc , ∗} where: • • vct = {vc , blank} if vertex v contains a statement, the set of statements is independent of time vct = {vc , ∗} indicates that vertex v is time dependent Time-dependent statements are collected from the Android API library [17] To create the CF Gct , we propose the algorithm Algorithm Assigning the time weights in each vertex Input: CF Gc LA - List of Android APIs are time dependent Output: CF Gct 1: 2: 3: 4: 5: 6: 7: 8: 9: Initialize CF Gct = CF Gc and vct = {vc , blank} ∀ vct for each vertex vc in CF Gc call s is the statement represented by vertex vc if s ∈ LA then vct = {vc , ∗} else vct = {vc , blank} end if end for Based on the time-dependent vertex data, the designer can adjust the test environment (network speed, file size, etc) to design more test cases 98on June 19,2021 at 00:32:02 UTC from IEEE Xplore Restrictions apply Authorized licensed use limited to: Univ of Calif Santa Barbara Downloaded 2020 7th NAFOSTED Conference on Information and Computer Science (NICS) Fig Approach overview of generating test data Fig An example CF G, CF Gc and CF Gct IV AUTOMATICALLY GENERATE TEST DATA In order to generate test data for an Android application, we need to find the paths of the CF Gct , the set of all paths of CF Gct is called P p = {vct1 , vct2 , , vctm } P = {p1 , p2 , , pn } In reality, the number of paths of the CF Gct can be very large, so we try to select the paths that the application consumes more power Let be the allowable energy threshold of a CF Gct ’s vertex, we define a vertex with a high level of energy consumption Definition 1: A vertex of a CF Gct is called a high energy consumption vertex if its energy consumption weight (c) is larger than the The is set by the test case designer, it depends on the specific case If is set to be less than zero, all the vertices of CF Gct are high energy consumption vertices In this study, we choose as the average consumption of all vertices The is calculated by the formula: = (c1 + c2 + + cn )/n Where c1 , c2 , , cn is power consumption weight of vct1 , vct2 , , vctn and n is the number of CF Gct vertices In this study, we give an average consumption weight of all the vertices, but the tester can completely adjust the weight to redefine the high energy consumption level, then the number of paths generated can be changed In energy testing, we are interested in the vertices with high power consumption Thus, all paths passing through these vertices need to be considered Definition 2: A path of the CF Gct is called a testable path if it contains at least one high energy consumption vertex Base on the definitions, we propose two steps to optimize the test data set, including: • Step 1: Finding testable paths • Step 2: Arranging the paths in order of priority A Finding testable paths To find testable paths in the graph, we proceed to browse all paths and mark the paths with high energy consumption vertices The algorithm describes how to find testable paths 99on June 19,2021 at 00:32:02 UTC from IEEE Xplore Restrictions apply Authorized licensed use limited to: Univ of Calif Santa Barbara Downloaded 2020 7th NAFOSTED Conference on Information and Computer Science (NICS) Algorithm Finding testable paths Input: CF Gct Output: Pe The set of optimal paths for energy test 1: 2: 3: 4: 5: 6: 7: 8: 9: 10: Calculate P where P is the entire path of CF Gct for each p in P for each vct in p call c is the power consumption weight of vertex vct if c >= then Pe = Pe ∪ p break end if end for end for V A CASE STUDY To illustrate the proposed method, we experiment with a function in the source code of an Android project In the source code below, the nextSong function plays the next song in the list includes 100 songs In this function, the index parameter represents the current song playing private void nextSong(int index){ 1| if(index < 0) 2| return; else 3| if(index >= 100) 4| mediaPlayer.stop(); else{ 5| index++; 6| mediaPlayer.seekTo(index); 7| mediaPlayer.start(); } 8| } By applying Algorithm and the algorithm 2, we have CF Gct as shown in Figure B Arranging test data in order of priority In the next step, we arrange the path in Pe in order of priority The paths that consume high amounts of energy will be tested first Suppose Ci is total energy consumption of the path i in Pe , then: Ci = k j=0 cj in which: • • k is the number of vertices in path i cj is the energy consumption weight of vertex j in path i After calculating total energy consumption of the entire paths, we have: C = {C1 , C2 , Cm } in which: • • C is the set of energy consumption weight of the paths in Pe m is the number of paths included in Pe Rearranging set C in descending order, we will get a priority of the test cases C Generating automatically test data Based on the studies of Gupta [9] and Roy [4], we built a tool that automatically generates CF Gct from source code, calculates paths of CF Gct , arranges paths according to the priority order and generates test data automatically For each path in Pe , our tool generates test data including specific values of variables and marks * on statements that are time-dependent The designer can use this data to design the test-cases Fig CF Gct of the case study By calculating the weighted average of the vertices, we calculate and get the result as follows: = (0 + + + + + + 384.62)/7 = 54, 95 Vertex is the ending vertex so we not calculate this vertex’s weight Based on the CF Gct , we calculate the set of paths P P consists of paths: p1 , p2 and p3 p1 = {1, 2, 8} p2 = {1, 3, 4, 8} p3 = {1, 3, 5, 6, 7, 8} We see that only vertex has a higher weight than , so every testable path on CFG must go through vertex There are several paths that are useless in energy testing To remove them, we apply algorithm and the result returned Pe : Pe = {p3 } The total weight of the path p3 is 384.62 and Pe contains only one path, there is no need to re-order If Pe contains more 100on June 19,2021 at 00:32:02 UTC from IEEE Xplore Restrictions apply Authorized licensed use limited to: Univ of Calif Santa Barbara Downloaded 2020 7th NAFOSTED Conference on Information and Computer Science (NICS) than one path, we can sort them in descending order to get priority for the testable paths Next, we consider the path p3 and the method of automatically generating data from this path 1| 3| 5| 6| 7| 8| !(index < 0) !(index >= 100) index++; mediaPlayer.seekTo(index); *mediaPlayer.start(); } In path p3 , there is only one variable, index Based on index’s constraint conditions in statements and 3, applying the method proposed in Sub section IV-C, the test data will be generated automatically TestData: {index = 0, index = 1, , index = 99} In addition, the mediaP layer.start() statement is marked with a star (*), so when designing a test case, the designer should note that the statement is time-dependent Therefore, when selecting songs to test for this function, it is necessary to select songs of different lengths to give more test cases [9] Gupta, Neelam, Aditya P Mathur, and Mary Lou Soffa ”Generating test data for branch coverage” Proceedings ASE 2000 Fifteenth IEEE International Conference on Automated Software Engineering IEEE, 2000 [10] Lide Zhang, Robert P Dick, Z Morley Mao, Zhaoguang Wang, and Ann Arbor ”Accurate Online Power Estimation and Automatic Battery Behavior Based Power Model Generation for Smartphones”, 2010 [11] Frances E Allen ”Control flow analysis” ACM Sigplan Notices, 5(7):1–19, 1970 [12] Tanwer, Sangeeta, and Dharmender Kumar ”Automatic test case generation of C program using CFG” International Journal of Computer Science Issues (IJCSI) 7.4 (2010): 27 [13] Panthi, Vikas, and D P Mohapatra ”Test Scenarios Generation using Path Coverage” International Journal of Computer Science and Informatics 3.2 (2013): 64-68 [14] Mishra, Deepti Bala, et al ”Test case generation and optimization for critical path testing using genetic algorithm” Soft Computing for Problem Solving Springer, Singapore, 2019 67-80 [15] Korel, Bogdan ”Automated software test data generation” IEEE Transactions on software engineering 16.8 (1990): 870-879 [16] Edvardsson, Jon ”A survey on automatic test data generation” Proceedings of the 2nd Conference on Computer Science and Engineering 1999 [17] Android API https://developer.android.com/guide/index.html VI C ONCLUSION The paper have presented an approach to automatically generate data in the power test of Android applications Based on the previous research results, we proposed to build a Control Flow Graph that allows to classify useful and unhelpful paths in energy testing of the graph We also proposed algorithms to select and optimize testable paths The advantage of our approach is the ability to remove most of the useless testing paths on the Control Flow Graph and so that we may save time and effort in power testing of mobile softwares We have developed a tool that automatically generates test data from the source code according to the principle of the approach This tool, actually, can only generate test data from simple code snippets In the future, we plan to improve the approach and the support tool to solve more complex problems and large softwares R EFERENCES [1] Hong-Anh Le, Anh-Tu Bui, and Ninh-Thuan Truong ”An approach to modeling and estimating power consumption of mobile applications” Mobile networks and Applications, 24(1):124–133, 2019 [2] Anh-Tu Bui, Hong-Anh Le, and Ninh-Thuan Truong ”Generation of power state machine for android devices” In ICCASA/ICTCC, 2017 [3] Jabbarvand, Reyhaneh, and Sam Malek ”Advancing Energy Testing of Mobile Applications” 2017 IEEE/ACM 39th International Conference on Software Engineering Companion (ICSE-C) IEEE, 2017 [4] Pargas, Roy P., Mary Jean Harrold, and Robert R Peck ”Testdata generation using genetic algorithms” Software testing, verification and reliability 9.4 (1999): 263-282 [5] Jabbarvand, Reyhaneh, et al ”Energy-aware test-suite minimization for android apps” Proceedings of the 25th International Symposium on Software Testing and Analysis 2016 [6] Aditya P Mathur ”Foundations of software testing 2E” Pearson Education India, 2013 [7] Manish Mishra, Shashi Mishra, and Rabins Porwal ”Basic principle for test case generation automatically” VSRD International Journal of Computer Science & Information Technology, 2(9):772–781, 2012 [8] Flemming Nielson, Hanne R Nielson, and Chris Hankin ”Principles of program analysis” Springer, 2015 101on June 19,2021 at 00:32:02 UTC from IEEE Xplore Restrictions apply Authorized licensed use limited to: Univ of Calif Santa Barbara Downloaded ... to optimize the test data set, including: • Step 1: Finding testable paths • Step 2: Arranging the paths in order of priority A Finding testable paths To find testable paths in the graph, we... Based on index’s constraint conditions in statements and 3, applying the method proposed in Sub section IV-C, the test data will be generated automatically TestData: {index = 0, index = 1, , index... on Information and Computer Science (NICS) Fig Approach overview of generating test data Fig An example CF G, CF Gc and CF Gct IV AUTOMATICALLY GENERATE TEST DATA In order to generate test data