[Title] Added TargetDevicesSelector class UNIT TEST.
authorgyeongseok.seo <gyeongseok.seo@samsung.com>
Tue, 10 Jul 2012 09:16:25 +0000 (18:16 +0900)
committergyeongseok.seo <gyeongseok.seo@samsung.com>
Wed, 11 Jul 2012 08:10:44 +0000 (17:10 +0900)
[Type] Enhancement
[Module] webapp-eplugin
[Priority] Major
[Jira#]
[Redmine#] 5363
[Problem]
[Cause]
[Solution]
[TestCase]

Change-Id: Ic49df8cd95ce3da9ed97a6f3d467b90179b0f65f

org.tizen.cli/src/org/tizen/cli/exec/TargetDeviceSelector.java
org.tizen.cli/test/src/org/tizen/cli/exec/TargetDeviceSelectorTest.java [new file with mode: 0644]

index 572e2c1..37f721d 100644 (file)
 package org.tizen.cli.exec;
 
 import java.text.MessageFormat;
-import java.util.ArrayList;
-import java.util.Iterator;
-import java.util.List;
 
 import org.tizen.common.core.command.Prompter;
 import org.tizen.common.core.command.prompter.Option;
-import org.tizen.common.core.command.sdb.DevicesSdbCommand;
 import org.tizen.common.core.command.sdb.SmartDevelopmentBridgeManager;
 import org.tizen.common.util.ArrayUtil;
-import org.tizen.common.util.StringUtil;
 import org.tizen.sdblib.IDevice;
-import org.tizen.sdblib.SmartDevelopmentBridge;
 
+
+/**
+ * common class for WRT command
+ * 
+ * @author GyeongSeok Seo{@literal <gyeongseok.seo@samsung.com>} (S-Core)
+ * @author BonYong Lee{@literal <bonyong.lee@samsung.com>} (S-Core)
+ */
 public class
 TargetDeviceSelector
 {
@@ -66,41 +67,25 @@ TargetDeviceSelector
     }
 
     public boolean isDeviceConnected() {
-        boolean isConnected = false;
-        IDevice[] devices = getConnectDevices();
-        if ( devices != null ) {
-            isConnected = true;
-        }
-
-        return isConnected;
+        return null != ArrayUtil.pickupFirst( getConnectDevices() );
     }
 
     public boolean isDeviceConnected( String name ) {
-        boolean isConnected = false;
-        IDevice[] devices = getConnectDevices();
-
-        for( int i = 0; i < devices.length; i++ ) {
-            if ( name.equals( devices[i].toString() ) ) {
-                isConnected = true;
-                break;
-            }
-        }
-
-        return isConnected;
+        return null != getDevice( name );
     }
 
     public boolean isDeviceConnected( IDevice device ) {
-        boolean isConnected = false;
-        IDevice[] devices = getConnectDevices();
-
-        if ( ArrayUtil.contains( devices, device) ) {
-                isConnected = true;
+        if ( device == null ) {
+            return false;
         }
-        return isConnected;
+        return null != getDevice( device.toString() );
     }
 
     public IDevice getDevice( String name ) {
         IDevice device = null;
+        if ( name == null ) {
+            return device;
+        }
         IDevice[] devices = getConnectDevices();
         for( int i = 0; i < devices.length; i++ ) {
             if ( name.equals( devices[i].toString() ) ) {
diff --git a/org.tizen.cli/test/src/org/tizen/cli/exec/TargetDeviceSelectorTest.java b/org.tizen.cli/test/src/org/tizen/cli/exec/TargetDeviceSelectorTest.java
new file mode 100644 (file)
index 0000000..e616a19
--- /dev/null
@@ -0,0 +1,238 @@
+/*
+* Web IDE - Command Line Interface
+*
+* Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
+*
+* Contact:
+* GyeongSeok Seo <gyeongseok.seo@samsung.com>
+* BonYong Lee <bonyong.lee@samsung.com>
+* 
+* Licensed under the Apache License, Version 2.0 (the "License");
+* you may not use this file except in compliance with the License.
+* You may obtain a copy of the License at
+*
+* http://www.apache.org/licenses/LICENSE-2.0
+*
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.
+*
+* Contributors:
+* - S-Core Co., Ltd
+*
+*/
+package org.tizen.cli.exec;
+
+import static org.junit.Assert.*;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.tizen.common.core.command.Prompter;
+import org.tizen.common.core.command.prompter.Option;
+import org.tizen.sdblib.IDevice;
+
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+/**
+ * TargetDeviceSelectorTest
+ *
+ * Test case for {@link TargetDeviceSelector}
+ * 
+ * @author GyeongSeok Seo{@literal <gyeongseok.seo@samsung.com>} (S-Core)
+ * @author BonYong Lee{@literal <bonyong.lee@samsung.com>} (S-Core)
+ * 
+ * @see TargetDeviceSelector
+ */
+public class
+TargetDeviceSelectorTest
+{
+    /**
+     * mock Prompter
+     */
+    protected Prompter mockPropter;
+
+    /**
+     * mock IDevice
+     */
+    protected IDevice mockDevice1;
+
+    /**
+     * mock IDevice
+     */
+    protected IDevice mockDevice2;
+
+    /**
+     * mock IDevices
+     */
+    IDevice mockDevices[];
+
+    /**
+     * test class
+     */
+    protected TargetDeviceSelector testClass;
+
+    /**
+     * Prepare test target
+     */
+    @Before
+    public
+    void
+    setUp()
+    {
+        mockPropter = mock( Prompter.class );
+        mockDevice1 = mock( IDevice.class );
+        mockDevice2 = mock( IDevice.class );
+        mockDevices = new IDevice[] { mockDevice1, mockDevice2 }; 
+        when( mockDevice1.toString() ).thenReturn( "test1" );
+        when( mockDevice2.toString() ).thenReturn( "test2" );
+
+        testClass = new TargetDeviceSelector( mockPropter ) {
+            @Override
+            public IDevice[] getConnectDevices() {
+                return mockDevices;
+            }
+        };
+    }
+
+    /**
+     * Test {@link TargetDeviceSelector#printDeviceList(org.tizen.sdblib.IDevice[])()}
+     * 
+     * @throws Exception in case of failure in test
+     * 
+     * @see TargetDeviceSelector#printDeviceList(org.tizen.sdblib.IDevice[])()
+     */
+    @Test
+    public
+    void
+    test_printDeviceList()
+    throws Exception
+    {
+        try {
+            testClass.printDeviceList( mockDevices );
+            verify( mockPropter ).notify( "-----------------------------\n" );
+            verify( mockPropter ).notify( "currnet connected target List\n" );
+            verify( mockPropter ).notify( "1) test1\n" );
+            verify( mockPropter ).notify( "2) test2\n" );
+        } catch (Exception e) {
+            fail( "devices is not null then Not throw exception" );
+        }
+
+        try {
+            testClass.printDeviceList( null );
+            verify( mockPropter ).notify( "-----------------------------\n" );
+            verify( mockPropter ).notify( "currnet connected target List\n" );
+            fail( "devices is null then throw exception" );
+        } catch (Exception e) {}
+    }
+
+    /**
+     * Test {@link TargetDeviceSelector#getConnectDevices()}
+     * 
+     * @throws Exception in case of failure in test
+     * 
+     * @see TargetDeviceSelector#getConnectDevices()
+     */
+    @Test
+    public
+    void
+    test_getConnectDevices()
+    throws Exception
+    {
+        assertArrayEquals( mockDevices, testClass.getConnectDevices() );
+    }
+
+    /**
+     * Test {@link TargetDeviceSelector#isDeviceConnected()}
+     * Test {@link TargetDeviceSelector#isDeviceConnected(IDevice)}
+     * Test {@link TargetDeviceSelector#isDeviceConnected(String)}
+     * 
+     * @throws Exception in case of failure in test
+     * 
+     * @see TargetDeviceSelector#isDeviceConnected()
+     * @see TargetDeviceSelector#isDeviceConnected(IDevice)
+     * @see TargetDeviceSelector#isDeviceConnected(String)
+     */
+    @Test
+    public
+    void
+    test_isDeviceConnected()
+    throws Exception
+    {
+        assertTrue( testClass.isDeviceConnected() );
+        assertTrue( testClass.isDeviceConnected( "test1" ) );
+        assertTrue( testClass.isDeviceConnected( "test2" ) );
+        assertTrue( testClass.isDeviceConnected( mockDevice1 ) );
+        assertTrue( testClass.isDeviceConnected( mockDevice2 ) );
+
+        assertFalse( testClass.isDeviceConnected( (String)null ) );
+        assertFalse( testClass.isDeviceConnected( "" ) );
+        assertFalse( testClass.isDeviceConnected( (IDevice)null ) );
+    }
+
+    /**
+     * Test {@link TargetDeviceSelector#getDevice(String)}
+     * 
+     * @throws Exception in case of failure in test
+     * 
+     * @see TargetDeviceSelector#getConnectDevices()
+     */
+    @Test
+    public
+    void
+    test_getDevice()
+    throws Exception
+    {
+        assertEquals( null, testClass.getDevice( (String)null ) );
+        assertEquals( null, testClass.getDevice( "" ) );
+        assertEquals( mockDevice1, testClass.getDevice( "test1" ) );
+        assertEquals( mockDevice2, testClass.getDevice( "test2" ) );
+    }
+
+    /**
+     * Test {@link TargetDeviceSelector#selectDevice()}
+     * 
+     * @throws Exception in case of failure in test
+     * 
+     * @see TargetDeviceSelector#selectDevice()
+     */
+    @Test
+    public
+    void
+    test_selectDevice()
+    throws Exception
+    {
+        assertEquals( null, testClass.selectDevice() );
+
+        Option yes = new Option( "Yes" );
+        Option no = new Option( "No", true );
+        when( mockPropter.interact( "device is not selected\n" +
+                                    "[test1] device select and\n" +
+                                    "Do you want to be continue?",
+                                    yes,
+                                    no
+                                    )
+            ).thenReturn( yes );
+        assertEquals( mockDevice1, testClass.selectDevice() );
+
+        when( mockPropter.interact( "device is not selected\n" +
+                "[test1] device select and\n" +
+                "Do you want to be continue?",
+                yes,
+                no
+                )
+            ).thenReturn( no );
+        Option num = new Option( "Num" );
+
+        when( mockPropter.interact( "select device or cancel process\n",
+                num,
+                no
+                )
+            ).thenReturn( no );
+        assertEquals( null, testClass.selectDevice() );
+        verify( mockPropter ).notify( "Process is canceled, please selected device" );
+    }
+}