Imported Upstream version 1.1.1
[platform/upstream/iotivity.git] / service / easy-setup / mediator / richsdk / android / EasySetupCore / src / androidTest / java / org / iotivity / service / easysetup / mediator / EasySetupStatusTest.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 EasySetupStatusTest 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             EnrolleeState old_state = null;
43
44             @Override
45             public void onFinished(EnrolleeDevice enrolledevice) {
46
47                 //countDownLatch.countDown();
48                 Utility.toNotify(lock);
49
50                 if (enrolledevice.isSetupSuccessful()) {
51
52                     if (enrolledevice.mOnBoardingConfig.getConnType() == WiFiOnBoardingConfig.ConnType.WiFi) {
53                         IpOnBoardingConnection conn = (IpOnBoardingConnection) enrolledevice.getConnection();
54                         String ip = conn.getIp();
55                         if (ip == null || ip.isEmpty()) {
56                             assertTrue(false);
57                             return;
58                         }
59                         String mac = conn.getHardwareAddress();
60                         if (mac == null || mac.isEmpty()) {
61                             assertTrue(false);
62                             return;
63                         }
64                         // Device configured successfully
65                         assertTrue(true);
66                     }
67
68                 } else {
69                     assertTrue(false);
70                 }
71             }
72
73             @Override
74             public void onProgress(EnrolleeDevice enrolleeDevice) {
75                 EnrolleeState state = enrolleeDevice.mState;
76                 // TODO
77                 switch (state) {
78                     case DEVICE_INIT_STATE:
79                         Log.d("enrollee state", "DEVICE_INIT_STATE");
80                         assertTrue(false);
81                         break;
82                     case DEVICE_ON_BOARDING_STATE:
83                         if (old_state == null)
84                             assertTrue(true);
85                         else assertTrue(false);
86                         old_state = EnrolleeState.DEVICE_ON_BOARDING_STATE;
87                         Log.d("enrollee state", "DEVICE_ON_BOARDING_STATE");
88                         break;
89
90                     case DEVICE_ON_BOARDED_STATE:
91                         if (old_state == EnrolleeState.DEVICE_ON_BOARDING_STATE)
92                             assertTrue(true);
93                         else assertTrue(false);
94                         old_state = EnrolleeState.DEVICE_ON_BOARDED_STATE;
95                         Log.d("enrollee state", "DEVICE_ON_BOARDED_STATE");
96                         break;
97
98                     case DEVICE_PROVISIONING_STATE:
99                         if (old_state == EnrolleeState.DEVICE_ON_BOARDED_STATE)
100                             assertTrue(true);
101                         else assertTrue(false);
102                         old_state = EnrolleeState.DEVICE_PROVISIONING_STATE;
103                         Log.d("enrollee state", "DEVICE_PROVISIONING_STATE");
104                         break;
105
106                     case DEVICE_PROVISIONED_STATE:
107                         if (old_state == EnrolleeState.DEVICE_PROVISIONING_STATE)
108                             assertTrue(true);
109                         else assertTrue(false);
110                         Log.d("enrollee state", "DEVICE_PROVISIONING_SUCCESS_STATE");
111                         break;
112
113                     default:
114                         Log.d("enrollee state", "unknown state");
115                         assertTrue(false);
116                         break;
117                 }
118
119             }
120         });
121
122
123         /* Create On boarding configuration */
124         WiFiOnBoardingConfig mWiFiOnBoardingConfig = new WiFiOnBoardingConfig();
125         mWiFiOnBoardingConfig.setSSId("EasySetup123");
126         mWiFiOnBoardingConfig.setSharedKey("EasySetup123");
127         mWiFiOnBoardingConfig.setAuthAlgo(WifiConfiguration.AuthAlgorithm.OPEN);
128         mWiFiOnBoardingConfig.setKms(WifiConfiguration.KeyMgmt.WPA_PSK);
129
130         /* Create provisioning configuration */
131         WiFiProvConfig mWiFiProvConfig = new WiFiProvConfig("hub2.4G", "11112222");
132
133         /* Create enrolling device factory instance */
134         mFactory = EnrolleeDeviceFactory.newInstance(getContext());
135
136         /* Create enrolling device */
137         mDevice = mFactory.newEnrolleeDevice(mWiFiProvConfig, mWiFiOnBoardingConfig);
138
139         try {
140             mService.startSetup(mDevice);
141         } catch (ESException e) {
142         }
143         catch (IOException e) {
144         }
145
146         try {
147
148             Utility.toWait(lock);
149
150             Log.i("EasySetupTest", "Lock is released");
151
152             IpOnBoardingConnection conn = (IpOnBoardingConnection) mDevice.getConnection();
153
154             Log.i("EasySetupTest", "Ip" + conn.getIp());
155             Log.i("EasySetupTest", "MAC" + conn.getHardwareAddress());
156
157             // Device configured successfully
158
159         } catch (Exception e) {
160             e.printStackTrace();
161         }
162
163     }
164
165 }