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