Category Archives: TDD

Rebuilding a legacy app from scratch: MadridBus revamp (I)

MadridBus was the second application that we released in Quoders to the app store. It is an adaptation from the original BilbaoBus, an app to get public transport information like routes, stops info, timetable, etc. It was born just as a learning side project but we got good traction from the beginning  and it grew up having more public transport agencies, improved UI, etc.

screen-shot-2016-10-01-at-09-27-38On the other hand MadridBus wasn’t so popular, probably because there were already a few decent applications in the market for the city of Madrid, so we didn’t focus so much on it. As a result, now days the app looks and feel really outdated, the UI is clunky and the plumbing of the app are really ugly and not maintainable.

So for this reason I have decided to rebuild the app from the scratch as one of my side projects. It will be open source published in Github. I will be explaining here in the blog all the design and implementation decisions, the different techniques, libraries, etc. The goal, apart from having a new and better application, is to have a project that could be a reference for new Android developers and hopefully help them to have a better understanding in how to implement from zero an Android app following good practices, patterns and explain the usage of Android concepts and most used libraries.

In this post I am going to give a quick summary of the different features, patterns, design and other elements that I am going to use following the best practices and modern Android development techniques. In following post I will be describing in more detail each of these elements and explaining the implementation and the source code will be available on Github.

Minimum Viable Product scope and features

So first thing first, let’s define the MVP (Minimum Viable Product) features and requisites.

The current published version of MadridBus has five main features:

    • Go To: This feature allows the user to search a route between point A to B, either pointing in the map or searching by address. This functionality, despite being really useful when you don’t know the city or the best route, interestingly it’s not very used as our Google Analytics data reveals.
    • Line List: Display the list of all the lines (day and night) available and allows the user to see the stops for a specific line and then the next bus time.
    • Stops List: Display a list of all the bus stops in the network. This functionality again is not really very useful, unless you know the name of the stop and want to look up.
    • Favourites: This is really useful as the user can save here to have quick access to the favourites stops, like work, home, etc.
    • News: Display a list of news fro the rss service of the bus agency. Useful when there are disruptions, changes, etc.

So for the MVP of the new app, taking into account what features the users are really using in the published app,  I’m going to go for the Line List, Favourites and News. Also we will be releasing an Android Wear app that will displayFor the V2, we will be adding the Go To feature and maybe the Stops list.

App architecture: Data access and storage

A public transport application needs to work with an extensive and complex source of data containing lines information, time tables, real time, geolocation, etc.

For MadridBus we are going to need two different set of data that we will retrieve from the Madrid public transport agency web servers:

The first one are static data that needs to be downloaded periodically and stored in the device for quick access offline.  This set of data contains the bus lines info, route s, stops position, etc.

The second type of data are the real time information, this will be accessed when we needs to display the real time info of the next bus for a specific stop.

To accomplish this network access we are going to use the well known library Retrofit from Square.  There are other options like Volley from Google but in my personal opinion and as you can see in some reviews like this, Retrofit is much better option. For more information you can check my blog post about Retrofit.

For the data storage on the device we are going to use a local data base managed with the library Realm. Databases in Android usually is a pain to implement using the SQLite API, but with real we simplify and make more robust and less error prone our data base implementation. It supports multithreading, encryption, reactive extensions, etc.

UI Layer: Material design

So now let’s talk about the UI layer. As we mentioned before, for the version 1 we are going to implement just the Line list, Favourites and News functionalities.

We can see clearly three different features that will need three direct access from the home screen. So following the Material Design guidelines we are going to use the recently added Bottom Navigation widget that will give us access to the three different features of the app. As we can see in the documentation,  this component is recommended for between three and five sections. In case we needed more is recommended to use a Tab view component or maybe using the left navigation drawer.

So the first option is the Home view and it will display a Google Maps showing the location of the user and the nearest bus stops. The second option will be the Line List, using a Recycler view to display the lines info, and the third option will be News screen, again using a Recycler view. We will post designs and wireframes in a future post when we start implementing this.

UI Layer: The MVP pattern

The MVP is now days one of the most used patterns in the Android community  when it comes to organise the UI layer. There are multiple benefits like much more modular, reusable and testable code. For a complete description and explanation of the pattern take a look to one of my previous blog post.

As a quick summary, the Activities, Fragments and custom views will be the View layer of the pattern and they will contain only pure UI functionalities.

The Presenter will be a man in the middle layer that will work as intermediary between the Views and the Model. They will respond to events and actions from the user on the View and it will perform any action required using the Model layer.

Modules wiring and communication

So as we are going to have different layers in the app following we need some mechanism to communicate with each other, passing data, etc.

There are different ways to accomplish this, we have just the traditional way using listeners, but we agree that this comes with many problems and we end up with the infamous hell of listeners.

Another better solution would be using an event bus, but this again has its own downside. The code end up being less readable it grows up having multiple events dispatching from everywhere with unexpected results.

There is a better solution and will be using RxJava. As we know it’s one of the library and techniques that has focused the attention of the Android community for a while now.

We will see in a future post and in the code of the app how to use RxJava and how to architect the app using it and all the benefits that it has. In the mean time you can have a look to my blog post about it.

Testing and Continuous Integration

Testing is often one of the most neglected aspects of the applications development process. For our application we are going to implement two main type of test: Integration Test using Espresso (UI test) and Unit Test when relevant with JUnit.

For more information about a good testing strategy take a look to my blog post about it.

Continuous Integration is another key piece of our app development as it allows us to have a build of our app everytime we submit changes to the code repository. This way we can launch our tests suite every time we make a change in the code validating it’s correctness.

For this functionality we are going to use Travis, a CI platform totally free for open source projects. Once again we will be explaining the configuration in next posts.


So that’s it for today, as mentioned before we are going to be publishing blog posts to comment and explain the implementation of the different parts of the new app as we progress coding it.

Stay tuned!


Test Driven Development (TDD) with Google C++ Testing Framework and Visual Studio (III)

And first of all, why should I use Google Test Framework?  There are several reasons to choose Google Framework between other options and you can find out more about it on their website, but I’ll give you two of my reasons:

  • It’s designed to be portable. You can use it on Windows, Mac and Linux and I can use it on Visual Studio and Borland Builder C++. That’s cool.
  • Automatically detects your tests and you don’t have to set specifically wich tests must run.

Ok, let’s start downloading the framework from here.

Unzip the gtest file and put it in the Visual Studio Projects folder or in any other folder where you have your projects. You’ll see several folders, each one belongs to a different platform. In our case we’re going to use the Visual Studio’s framework, so go inside the “msvc” folder, open the gtest solution and compile and then follow the steps:

  1. Create a new console test project with Visual Studio and C++ language.
  2. Add to this solution the projects “\msvc\gtest” and “\msvc\gtest_main” of the “gtest” solution that we compiled before.
  3. Go to the project Properties and in the Configuration Properties > C/C++ > Code Generation set “Basic Runtime Checks” to “Both (/RTC1, equiv. to /RTCsu) (/RTC1)” and set “Runtime Library” to “Multi-threaded (/MT)”.
  4. In this Properties page, set the “Optimization” option to “Disabled”.
  5. Also in  Configuration Properties > C/C++ >General > Additional Include Directories add this line “$(InputDir)\gtest-1.3.0\include” for All Configurations (Relese/Debug).

Ok, now add a new .cpp to our Test Project, for example “TestBattery1.cpp” where we are going to write our first Test using an example that you can find in the Google Framework website, which defines three steps:

  1. Include necessary header files.
  2. Use the TEST macro to define your tests.
  3. Call RUN_ALL_TESTS() in main().

In this code example, we test the function Factorial. You can find the function here and the tests code in this link:

// Tests factorial of 0.
TEST(FactorialTest, Zero) {
EXPECT_EQ(1, Factorial(0));
// Tests factorial of positive numbers.
TEST(FactorialTest, Positive) {
EXPECT_EQ(1, Factorial(1));
EXPECT_EQ(2, Factorial(2));
EXPECT_EQ(6, Factorial(3));
EXPECT_EQ(40320, Factorial(8));

And that’s all for today.Finally, I leave here  a very good book about TDD but in this case with C#:

Test Driven Development (TDD) with Google C++ Testing Framework and Visual Studio (II)

Probably, like every developers, what you want is to start coding and see how is that of Google Testing Framework and how does it works. But not yet, my friend. Today we are going to have a look to the Unit Testing concept.

A “Unit Test” is a piece of code implemented to test a unit of work, which means that we are going to check the correct implementation of a requirement. For me the more important characteristics of a unit test are:

  • Repeatable and predictable.  It has to be designed to be executed  every time you needed and the result always must be consistent. Imagine that you’re testing an algorithm and you launch the test and it’s returns different results to the same parameters. Not good….
  • Isolated. You have to test only what is inside that function. Nothing else. That means that other modules, functionality or parts of your application shouldn’t be tested. For example, if you’re testing a function that reads a XML tag, don’t test in the same unit test a XML write routine.

Now, the process of executing unit testing is:

  1. Prepare the test performing some operation, initializing variables, etc…
  2. Launch the test.
  3. Check results
Ok, enough, it’s too much theory. We love coding and everything is much clear with examples, so in the next post we will start creating our first Unit Test with Google C++ Testing Framework!
Stay tuned!


Test Driven Development (TDD) with Google C++ Testing Framework and Visual Studio (I)

Test Driven Development has become one of the most important and known paradigms in software development.  We could say that is part of the Agile methodologies that are changing the traditional Waterfall way of work.  But, what exactly TDD is?

If we summarize in a few words, we could say that TDD is just write first the test, and then write the code. That’s it.  At the beginning it might seem a bit strange and you have to change the way you think when you are about to write a routine or a new class, but it has a lot of benefits that worth it:

  • Ensures quality from the start. If you have any bug, the test will fail at first attempt.
  • Maintains clean your modules of unused code.
  • Provides regression testing. If you change a piece of code, you can be sure that it will not affect to any other functionality.
  • Produces simpler and more focused APIs, classes and libraries. When you are developing a method that has to pass a specific test, you avoid the temptation of mess up your method with a different functionality.
  • It’s much easier to match the code and business requirements.

There are a lot of different frameworks to implement Unit Testing and start to practice with TDD. Here you can find a huge list.

In this series of post for C++ language, I’m going to focus on the Google C++ Testing Framework. It’s been developed by Google and I find it quite simple and useful.  In the next post we will explain how to use it and in subsequent posts I’ll try to explain other frameworks for .NET.

Stay tuned!