Wednesday 10 October 2012

PhoneGap Pinch Zoom


These code will allow pinch zoom in PhoneGap application.

1. index.html.


<meta name="viewport" content="width=device-width, height=device-height, initial-scale=0.8, user-scalable=1" />


2. in java file.


    @Override

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.loadUrl("file:///android_asset/www/index.html");
        WebSettings settings = appView.getSettings();
        settings.setBuiltInZoomControls(true);
        settings.setSupportZoom(true);
        settings.setDefaultZoom(ZoomDensity.FAR);
       
    }

Wednesday 15 August 2012

Unit Testing in Python

I want to do automated unit testing for my class that I had written in Python.
To do automated unit testing I need three items:
1. The class I want to test.
2. The unit testing class.
3. The test runner.

Step 1:
-------
I created my class and named it as controllerTesting.py.
I added functions inside the file as below:


class Test():
    def returnZero(self):
        return 0
    
    def returnNonZero(self):
        return 12

    def returnSumNumber(self, number1, number2):
        return number1 + number2


Step 2:
-------
I created the file (test.py) and add the functions to test the functions in controllerTesting.py
I added code as below:


import unittest

from controllers.controllerTesting import Test

class TestControllerTest(unittest.TestCase):
    
    def testZeroReturn(self):
        val = 0
        t = Test()
        self.assertEqual(val, t.returnZero())
        
    def testNonZeroReturn(self):
        val = 0
        t = Test()
        self.assertNotEqual(val, t.returnNonZero())
        
    def testSumNumber(self):
        total = 4
        t = Test()
        r = t.returnSumNumber(2, 2)
        self.assertEqual(total, r, 'incorrect default size')



Step 3:
-------
I created the test runner (test_runner.py) and add these code to the file.


import unittest

from test import TestControllerTest

if __name__ == '__main__':
    suite = unittest.TestLoader().loadTestsFromTestCase(TestControllerTest)
    suite = unittest.TestSuite()
    suite.addTest(unittest.makeSuite(TestControllerTest))

    
    log_file = 'unit_testing_log.txt'
    f = open(log_file, "w")
    runner = unittest.TextTestRunner(f, verbosity=2).run(suite)    
    unittest.TextTestRunner(verbosity=2).run(suite)


Step 4:
-------
I Ran the unit test using the command.

python test_runner.py

The output of the testing were printed in unit_testing_log.txt located in the same directory of the unit testing file.

Output example:


testNonZeroReturn (test.TestControllerTest) ... ok
testSumNumber (test.TestControllerTest) ... FAIL
testZeroReturn (test.TestControllerTest) ... ok
testNonZeroReturn (test1.TestControllerTest2) ... ok
testSumNumber (test1.TestControllerTest2) ... ok
testZeroReturn (test1.TestControllerTest2) ... ok

======================================================================
FAIL: testSumNumber (test.TestControllerTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "test.py", line 33, in testSumNumber
    self.assertEqual(total, r, 'incorrect default size')
AssertionError: 4 != 0 : incorrect default size

----------------------------------------------------------------------
Ran 6 tests in 0.001s

FAILED (failures=1)

Thursday 21 June 2012

How to import module from different folder in Python

Let say we have a folder structure like this:

Project
-- controllers
   -- controllerA.py
-- unit_testing
   -- test_controllerA.py

We want to import controllerA to be used in test_controllerA. So in test_controllerA.py we should add these code:

/**  test_controllerA .py **/
import sys
import os.path
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), os.path.pardir)))

/**Now we can import the class **/
from controllers.controllerA import controllerA




Thursday 31 May 2012

PHP TDD

We can use SimpleTest to write program  in TDD style.
0. Download SimpleTest.
1. Add the unit testing and report file.
2. Subclass the UnitTestCase
3. Write the function to do the unit testing.
4. Run the test.


Example:
<?php
require_once('simpletest/unit_tester.php');
require_once('simpletest/reporter.php');

require_once "Location.php"; // class to test

class MyTestingClass extends UnitTestCase
{
    /*
     * Test distance calculation function in Location class.
     */
    function TestCalculateDistance()
    {
      $point1 = array('x'=>0, 'y'=>0);
      $point2 = array('x'=>10, 'y'=>10);
   
      $distance = number_format(14.142135623731, 2, '.', '');
      $location = new Location();
      $retDistance = $location->calculateDistance($point1, $point2); // Function in Location class
      $retDistance = number_format($retDistance, 2, '.', '');
      $this->assertEqual($distance, $retDistance); // return true if distance is calculated correctly
    }
}
$test = new TestLocation();
$test->run(new HtmlReporter()); // run the test
?>



Monday 14 May 2012

Python TDD tool and tutorial

This site has video tutorial on how to start Test Driven Development (TDD) using python and PYTDDMON. It's a good tutorial to start TDD.

Friday 27 April 2012

PhoneGap Android orientation.

My PhoneGap application exited when device orientation changed.
These code will solve the problem. Update AndroidManifest with the code as highlighted in the example. 


    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".EStoryBooksActivity"
            android:label="@string/app_name"
            android:configChanges="orientation|keyboardHidden" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity 
            android:name="org.apache.cordova.DroidGap" 
            android:label="@string/app_name"
            android:configChanges="orientation|keyboardHidden"> 
            
            <intent-filter> 
            </intent-filter> 
        </activity>
    </application>

For Android SDK 3.1 need to add screen size to the config.

android:configChanges="orientation|screenSize|keyboardHidden

Monday 23 April 2012

Web Apps

Another book to read. It give a good advice on how to create a web apps instead of website.

Wednesday 11 April 2012

DIVE INTO HTML5

This book "Dive into HTML5" have good explanations of HTML5 features. Highly recommended to read.

Sunday 11 March 2012

Tuesday 6 March 2012

Turbogears2

I just start my exploration on turbogears2 framework. Few works need to be done before I can play with it.

1. Setting up virtual machine
2. Install Ubuntu.
3. Install Eclipse.
4. Install SVN.
5. Install Turbogears. (Step by step)


All done. Let's start working!