Monday, March 5, 2018

Using soft hyphens in Swift 3.0

There can be cases when we will need to support hyphens in the iOS app. But it can be crucial thing to detect at which place we should put hyphen and then break the line.

Instead of detecting places manually and creating the wheel again we can use Soft Hyphens. The logic behind it is to use unicode value of soft hyphen and put it at every possible place where the word can be split.

Lets grab the example, the sentence "I love singing" can be split "I lo-ve sing-ing". So instead of hyphens we will use \u{00AD} unicode characters.


let loveSinging = "I lo\u{00AD}ve sing\u{00AD}ing"

After having the correctly formatted string just assign the string to your label. iOS operating system will decide on its own to show/hide the unicode characters which represents hyphen. Do not forget to set line break mode to truncate tail.


label.text = loveSinging

Note for .strings file

When using the resource file, the escaping character is different. So instead of \u{00AD} we should use \U00AD.

Monday, June 12, 2017

Test Case Generation - Model Checker


Introduction
In all software development process the time really matters. This is the aspect which is valued as a high-cost.
When the beginner programmers start to learn programming languages, they try to learn basic syntax, some language related paradigms as well. They are so enthusiastic, doing their best to acquire knowledge and study. Some time passes, and now they can write basic to-do list application, it can be mobile, web-based or desktop. At this moment, they don't care about the tests, since they can't evaluate how important can the test be. This is the first phase of every single programmer. The second phase for them is to find a job or internship in a company, to gain more experience and earn experience and money. So this is the time when they encounter real project structure and processes how the big projects are done. Understanding the different people responsibilities, separation of the business side and development teams. Here they see the values of quality assurance(QA), that QAs are responsible for the quality, testing properly the applications, report bugs etc.

So, to say in different words, the second phase is the time for most of the programmers where they ask the question: Do we really need tests? Why should I spend the time in writing the tests which will test my own code? This question is not answered in a small period of time, the one needs particular knowledge in programming and development process to clearly see how important the tests can be.

The main argument for second phase programmers not to write tests is time consumption. They say they could do more valuable tasks meanwhile, instead of writing the tests. So in that case they are right, writing the tests can be time consuming, which is extra cost for the company. But in reality, testing is essential for achieving high quality and standard software.

This paper is intended to review and analyse how much will we win if we automate tests. The perfect example where the strength of test automation is visible.

test-cases.png


The interesting fact here is the creation time, when automating the tests we need more time to create tests and prepare the system for automation, but then we significantly win in execution time.

Not only above mentioned is the intention but also how sufficiently we can generate test cases and reduce the time consumption. One of the most interesting part is code coverage, how well will our generated tests will cover the code. Is every case of your program flow included or not?

So my main purpose is first to gather significant information about test generation and code coverage, and if possible then find some different ways to fill the failures for test case generation.

The main methodology which I will talk about is model checker. Model checker method will work if the software product (code, specification, requirements model)  can be represented as a finite state transition system.

Taking Test Automation Seriously
Software testers sometimes are in a hurry and they do not have enough time to write tests, so they are avoiding the fact that testing should be implemented. Their distress lead them to look for a "silver bullet" that will help them recover a discerning pace to their work. Test automatisation is frequently their choice. The dream is that it will make their development process less complex and less demanding and help them to cope with unreasonable timetables.

Automated tests for a graphical user interface are quite difficult to implement. Since day by day graphical user interface(GUI) is becoming more and more complex, interfaces are changing daily during the development processes. Fortunately, significant work has been done in previous years to build tools which will automate tests, they are creating the GUI tests for us, the perfect examples are:  QA Partner done by Segue Software and also Xrunner or WinRunner created by Mercury Interactive.

All software testers have really good understanding and they know best practices why the testing can be critical for software development process. They know the consequences, what will be outcome of neglecting code reviews and ignoring bugs. They see better the results of coding before designing. So testers are people who clear the sky, but sometimes they fail in one thing. They do not understand that test automation should be activity of development process,
thusly needs to cling to critical development practices. This graph should be shown to everyone who rejects the above mentioned sentence.

automated-testing-roi-perceived.gif

What are the major challenges for GUI test automation? To start with there are different kinds of GUI test automation, first automation for web-based, which is mainly working with parsing the HTML. Second is desktop automation, which is a bit complicated, since the structure of interface sometimes is unknown. Another type is mobile testing automation, which is as well more complicated than web-based application testing. To answer the question, main challenges are maintainability and reliability.

Sometimes developers and testers underestimate test automation. When they hear the test tool, they are thinking about something really easy and simple to use.

System Model
As mentioned above, we are going to discuss model checker, how it works and what environment should be created in order to fully perform test case generation.
To start with, the system is a subset of different states. The process when the system changes from one state to another is called transition. If we want to test transition between state A and state B when the condition C is true,  we should formulate a condition which will describe the test case testing the transition - the bunch of inputs must take the model to state A; and in state A, C, out condition, must be true. The last is that next state must be B.
I have already mentioned above that the system must contain the finite number of states. Let's say that the system contains n variables {x1, x2, x3, … xn}, where each xi gets its value from domain Di. All this means that reachable state of the space is the subset of all domains like: D = D1 x D2 x D3 … Dn.
What are the initial values of the variables listed above? The predicate p, which is subset of D, is responsible for defining the initial values for variables.
Transition relation for each variable specifies how the variable can be changed while the systems moves from state A to state B. In other words transition for xi can be thought group of three components
  • Set of xi pre-state values
  • Set of xi post-state values
  • Condition
We now have a rough understanding what is system model. It is a tuple of (D, A, p), where D is the state-space of the system, A - transition relation and p - the system initial state.


Test Case Generation

Model checker methodology results have been really promising, it really helps to find all test cases and some leaks(traps) into the coverage. In this chapter I will talk  about the approach itself.

Our aim to generate test cases involves using the model-checker as the core engine. Model-checker has to verify all properties one by one, these properties are generated by model-checker itself and they are called as trap properties. These properties are intended to fail for the given system specification, this leads the model-checker to generate counter-example. The  counter-example shows the sequence of states(guidelines) which should be followed by all conforming implementation. And in the end all this becomes a full and comprehensive test case. After that our task is to generate the trap properties  in such a way that the group of counter-examples generated will be enough to satisfy the coverage criteria that we are dealing with.

Alternatively, counter-example can be generated using mutation. "A mutation describes a syntactic change that may introduce inconsistency, and can be applied to the model or specification , or properties extracted from the model in order to create test-cases - says Gordon Fraser. Here we should also define new expression "Mutation score". This is the number of mutants  which is distinguished by test-suite from the original.

model_check_flow.jpg

Conclusion
The paper gave us brief understanding of test automation and test generation. Despite the fact that the algorithm of generation is pretty difficult to understand, even more difficult to implement, we have a rough idea how it works.

The fact that the test automation is really important and reduces the time, thus the cost, is obvious. The examples which were given above assures me in the importance of test automation.

To conclude model-checker seems one of the most well-known methodology to generate test cases. The main idea how the model checker is working is that first it tries to generate trap properties and then tries to find all failures within those properties. The next step is to generate counter-examples from those failures, counter-examples than lead us to test case, while bunch of test cases gives us test suite which is our outcome. But as mentioned above, it is quite impossible to fully cover all your cases, since the number of tests are finite.

We have also identified the different types of code coverages, those coverages can be used in different kinds of applications, depending on the complexity.

Saturday, January 2, 2016

Finding iBeacons without ProximityUUID Xamarin Android

            The iBeacon is the small Low Energy Bluetooth device which constantly broadcasts the radio signals to phones and tablets using Bluetooth. Mobile devices can listen to these signals and trigger an action. User's mobile device identifies the concrete iBeacon via it’s ProximityUUID  which is the unique hexadecimal number. ProximityUUID is essential to get the radio signals from the iBeacon. 

I was developing my first iBeacon app on iOS, I was unable to connect the iBeacon and finally came to conclusion that, the ProximityUUID which I was using was, unfortunately incorrect. Despite the fact that I was checking the ProximityUUID with various iOS apps such as Locate and eBeacon, the ProximityUUID still seemed incorrect. After a day research and posting the question to stackoverflow, I was suggested to use Android app called Locate to find the correct ProximityUUID of the iBeacon. The android Locate app worked like a charm, it gave the correct UUID and finally I connected. The thing what I have understood is that iOS restricts users to read iBeacons identifiers unless you know the ProximityUUID.

This experience has urged me to search and  write the post to help other developers solve the problem. In this post I will explain how we can find the UUID-s of nearby beacons with Xamarin.Android and monitor or range it.

First of all add the permissions of  Bluetooth and BluetoothAdmin in your manifest file.

<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />    

We are going to use the BluetoothAdapter class for bluetooth connection. First implement the BluetoothAdapter.ILeScanCallback for your activity, create the instance of BluetoothAdapter class in your OnCreate method and start scanning.

    adapter = BluetoothAdapter.DefaultAdapter;
    adapter.StartLeScan (this);

Than implement the OnLeScan method in your activity. Here we will have a list of beacons, which will finally hold the result of the nearby iBeacons.
   
private List<iBeacon> _beacons;
public void OnLeScan (BluetoothDevice device, int rssi, byte[] scanRecord)
          { }

The main part starts here, the parameter scanRecord which is the array of bytes is holding all information about the nearable bluetooth device. The OnLeScan method is called whenever your app receives the signal from any bluetooth device, it may include bluetooth peripherals, iBeacons, eddystones and etc. Here we are trying to find the pattern, whether the device which is sending the signal is iBeacon or not.

Since the OnLeScan is called whenever your app receives the bluetooth signal, we can add a timer which will handle how many seconds will the scanning be running.

timer = new Timer ();
timer.Elapsed += Timer_Elapsed;
             timer.Interval = 1000;

If we want to identify the iBeacon, we are going to skip  2 – 5 bytes since they are providing the information of vendor company. We should check next bytes:
  • 0x02 means iBeacon
  • 0x15 is the data length
All these allows us to be assured that this device is the iBeacon.
Next step is to take 16 byte length part from the source array(scanRecord for our case), this number is the ProximityUUID of the iBeacon.
    The same is for Minor and Major values, but the length of both is 2 bytes, we should multiply first byte by 100 and add the second byte to find the Major value and the same technique is used for the Minor value.

    You can read more about the bytes information here and here.

Let’s have a look on OnLeScan method.
public void OnLeScan (BluetoothDevice device, int rssi, byte[] scanRecord)
       {
//checking the beacon not to be duplicated
if (!_beacons.Any (x => x.MacAddress == device.Address)) {
               int startByte = 2;
               var patternFound = false;
               while (startByte <= 5) {
                   if (((int)scanRecord [startByte + 2] & 0xff) == 0x02 &&
                       ((int)scanRecord [startByte + 3] & 0xff) == 0x15) { //Identifies correct data length
                       patternFound = true;
                   break;
                   }
                   startByte++;
               }

               if (patternFound) {
                   //Convert to hex String
                   byte[] uuidBytes = new byte[16];
                   Array.Copy (scanRecord, startByte + 4, uuidBytes, 0, 16);
                   var hexString = BitConverter.ToString (uuidBytes).Replace ("-", "");

                   //Here is your UUID
                   var uuid = hexString.Substring (0, 8) + "-" +
                              hexString.Substring (8, 4) + "-" +
                              hexString.Substring (12, 4) + "-" +
                              hexString.Substring (16, 4) + "-" +
                              hexString.Substring (20, 12);

                   //Here is your Major value
                   int major = (scanRecord [startByte + 20] & 0xff) * 0x100 + (scanRecord [startByte + 21] & 0xff);

                   //Here is your Minor value
                   int minor = (scanRecord [startByte + 22] & 0xff) * 0x100 + (scanRecord [startByte + 23] & 0xff);

                   if (!_beacons.Any (b => b.UUID == uuid && b.Major == major && b.Minor == minor))
                       _beacons.Add (new iBeacon () {
                           UUID = uuid,
                           Minor = Convert.ToUInt16 (minor),
                           Major = Convert.ToUInt16 (major),
                           Name = device.Name,
                           MacAddress = device.Address
                       });
               }
}

       After 10 seconds the timer finishes, and we are holding the result in the variable called _beacons, all nearby iBeacons with their ProximityUUID, Minor and Major values.

private void Timer_Elapsed (object sender, ElapsedEventArgs e)
       {
           elapsedSeconds++;
           if (elapsedSeconds > 10) {
               timer.Stop ();
               adapter.StopLeScan (this);
               elapsedSeconds = 0;
               var myResult = _beacons;
           }
       }

The sample project can be seen on my github, check it out:

Finding beacons without UUID

Using soft hyphens in Swift 3.0

There can be cases when we will need to support hyphens in the iOS app. But it can be crucial thing to detect at which place we should put h...