9ac1a18931feebe0e3bfd2beb610b7e793cd16af
[platform/upstream/iotivity.git] / service / easy-setup / sdk / mediator / android / EasySetupCore / src / androidTest / java / org / iotivity / service / easysetup / mediator / EasySetupServiceTest.java
1 //******************************************************************\r
2 //\r
3 // Copyright 2015 Samsung Electronics All Rights Reserved.\r
4 //\r
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=\r
6 //\r
7 // Licensed under the Apache License, Version 2.0 (the "License");\r
8 // you may not use this file except in compliance with the License.\r
9 // You may obtain a copy of the License at\r
10 //\r
11 //      http://www.apache.org/licenses/LICENSE-2.0\r
12 //\r
13 // Unless required by applicable law or agreed to in writing, software\r
14 // distributed under the License is distributed on an "AS IS" BASIS,\r
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
16 // See the License for the specific language governing permissions and\r
17 // limitations under the License.\r
18 //\r
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=\r
20 package org.iotivity.service.easysetup.mediator;\r
21 \r
22 import android.net.wifi.WifiConfiguration;\r
23 import android.test.AndroidTestCase;\r
24 import android.util.Log;\r
25 \r
26 import java.io.IOException;\r
27 \r
28 public class EasySetupServiceTest extends AndroidTestCase {\r
29 \r
30 \r
31     public void testStartSetupWithWiFiOnboarding() {\r
32 \r
33         EasySetupService mService;\r
34         EnrolleeDevice mDevice;\r
35         EnrolleeDeviceFactory mFactory;\r
36 \r
37         final Object lock = new Object();\r
38 \r
39 \r
40         /* Create Easy Setup service */\r
41         mService = EasySetupService.getInstance(getContext(), new EasySetupStatus() {\r
42             @Override\r
43             public void onFinished(EnrolleeDevice enrolledevice) {\r
44 \r
45                 //countDownLatch.countDown();\r
46                 Utility.toNotify(lock);\r
47 \r
48                 if (enrolledevice.isSetupSuccessful()) {\r
49 \r
50                     if (enrolledevice.mOnBoardingConfig.getConnType() == WiFiOnBoardingConfig.ConnType.WiFi) {\r
51                         IpOnBoardingConnection conn = (IpOnBoardingConnection) enrolledevice.getConnection();\r
52                         String ip = conn.getIp();\r
53                         if (ip == null || ip.isEmpty()) {\r
54                             assertTrue(false);\r
55                             return;\r
56                         }\r
57                         String mac = conn.getHardwareAddress();\r
58                         if (mac == null || mac.isEmpty()) {\r
59                             assertTrue(false);\r
60                             return;\r
61                         }\r
62                         // Device configured successfully\r
63                         assertTrue(true);\r
64                     }\r
65 \r
66                 } else {\r
67                     assertTrue(false);\r
68                 }\r
69             }\r
70 \r
71             @Override\r
72             public void onProgress(EnrolleeDevice enrolleeDevice) {\r
73                 // Handled in EasySetupStatusTest\r
74             }\r
75         });\r
76 \r
77 \r
78         /* Create On boarding configuration */\r
79         WiFiOnBoardingConfig mWiFiOnBoardingConfig = new WiFiOnBoardingConfig();\r
80         mWiFiOnBoardingConfig.setSSId("EasySetup123");\r
81         mWiFiOnBoardingConfig.setSharedKey("EasySetup123");\r
82         mWiFiOnBoardingConfig.setAuthAlgo(WifiConfiguration.AuthAlgorithm.OPEN);\r
83         mWiFiOnBoardingConfig.setKms(WifiConfiguration.KeyMgmt.WPA_PSK);\r
84 \r
85         /* Create provisioning configuration */\r
86         WiFiProvConfig mWiFiProvConfig = new WiFiProvConfig("hub2.4G", "11112222");\r
87 \r
88         /* Create enrolling device factory instance */\r
89         mFactory = EnrolleeDeviceFactory.newInstance(getContext());\r
90 \r
91         /* Check if the factory created successfully */\r
92         assertTrue(mFactory != null);\r
93 \r
94         /* Create enrolling device */\r
95         mDevice = mFactory.newEnrolleeDevice(mWiFiOnBoardingConfig, mWiFiProvConfig);\r
96 \r
97         /* Check if the the device is created successfully*/\r
98         assertTrue(mDevice != null);\r
99 \r
100         /* Check if the the correct device is created as per the given configuration*/\r
101         assertTrue((mDevice instanceof EnrolleeDeviceWiFiOnboarding));\r
102 \r
103 \r
104         try {\r
105             mService.startSetup(mDevice);\r
106             // If no exception is thrown means setup started successfully.\r
107             assertTrue(true);\r
108 \r
109         } catch (IOException e) {\r
110             assertTrue(false);\r
111         }\r
112 \r
113         try {\r
114 \r
115             Utility.toWait(lock);\r
116 \r
117             Log.i("EasySetupTest", "Lock is released");\r
118 \r
119             if (!mDevice.isSetupSuccessful()) {\r
120                 assertTrue(false);\r
121                 return;\r
122             }\r
123 \r
124             IpOnBoardingConnection conn = (IpOnBoardingConnection) mDevice.getConnection();\r
125             if (conn == null) {\r
126                 assertTrue(false);\r
127                 return;\r
128             }\r
129 \r
130             String ip = conn.getIp();\r
131             if (ip == null || ip.isEmpty()) {\r
132                 assertTrue(false);\r
133                 return;\r
134             }\r
135 \r
136             String mac = conn.getHardwareAddress();\r
137             if (mac == null || mac.isEmpty()) {\r
138                 assertTrue(false);\r
139                 return;\r
140             }\r
141 \r
142             Log.i("EasySetupTest", "Ip" + conn.getIp());\r
143             Log.i("EasySetupTest", "MAC" + conn.getHardwareAddress());\r
144 \r
145             // Device configured successfully\r
146             assertTrue(true);\r
147 \r
148         } catch (Exception e) {\r
149             e.printStackTrace();\r
150             assertTrue(false);\r
151         }\r
152 \r
153     }\r
154 \r
155 }