1 package org.robolectric.shadows;
2 
3 import static com.google.common.truth.Truth.assertThat;
4 import static org.robolectric.Shadows.shadowOf;
5 
6 import android.app.Application;
7 import android.content.Context;
8 import android.net.wifi.p2p.WifiP2pGroup;
9 import android.net.wifi.p2p.WifiP2pManager;
10 import androidx.test.core.app.ApplicationProvider;
11 import androidx.test.ext.junit.runners.AndroidJUnit4;
12 import org.junit.Before;
13 import org.junit.Test;
14 import org.junit.runner.RunWith;
15 import org.mockito.Mock;
16 import org.mockito.MockitoAnnotations;
17 import org.robolectric.RuntimeEnvironment;
18 
19 @RunWith(AndroidJUnit4.class)
20 public class ShadowWifiP2pManagerTest {
21 
22   private WifiP2pManager manager;
23   private ShadowWifiP2pManager shadowManager;
24   @Mock private WifiP2pManager.ChannelListener mockListener;
25   private WifiP2pManager.Channel channel;
26 
27   @Before
setUp()28   public void setUp() {
29     MockitoAnnotations.initMocks(this);
30     Application context = ApplicationProvider.getApplicationContext();
31     manager = (WifiP2pManager) context.getSystemService(Context.WIFI_P2P_SERVICE);
32     shadowManager = shadowOf(manager);
33     channel = manager.initialize(context, context.getMainLooper(), mockListener);
34     assertThat(channel).isNotNull();
35   }
36 
37   @Test
createGroup_success()38   public void createGroup_success() {
39     TestActionListener testListener = new TestActionListener();
40     manager.createGroup(channel, testListener);
41     assertThat(testListener.success).isTrue();
42   }
43 
44   @Test
createGroup_nullListener()45   public void createGroup_nullListener() {
46     manager.createGroup(channel, null);
47 
48     // Should not fail with a null listener
49   }
50 
51   @Test
createGroup_fail()52   public void createGroup_fail() {
53     TestActionListener testListener = new TestActionListener();
54 
55     RuntimeEnvironment.getMasterScheduler().pause();
56 
57     manager.createGroup(channel, testListener);
58     shadowManager.setNextActionFailure(WifiP2pManager.BUSY);
59 
60     RuntimeEnvironment.getMasterScheduler().unPause();
61 
62     assertThat(testListener.success).isFalse();
63     assertThat(testListener.reason).isEqualTo(WifiP2pManager.BUSY);
64   }
65 
66   @Test
clearActionFailure()67   public void clearActionFailure() {
68     shadowManager.setNextActionFailure(WifiP2pManager.ERROR);
69 
70     TestActionListener testListener = new TestActionListener();
71     manager.createGroup(channel, testListener);
72     assertThat(testListener.success).isFalse();
73 
74     manager.createGroup(channel, testListener);
75     assertThat(testListener.success).isTrue();
76   }
77 
78   @Test
removeGroup_success()79   public void removeGroup_success() {
80     TestActionListener testListener = new TestActionListener();
81     manager.removeGroup(channel, testListener);
82     assertThat(testListener.success).isTrue();
83   }
84 
85   @Test
removeGroup_nullListener()86   public void removeGroup_nullListener() {
87     manager.removeGroup(channel, null);
88 
89     // Should not fail with a null listener
90   }
91 
92   @Test
removeGroup_failure()93   public void removeGroup_failure() {
94     TestActionListener testListener = new TestActionListener();
95 
96     RuntimeEnvironment.getMasterScheduler().pause();
97     manager.removeGroup(channel, testListener);
98 
99     shadowManager.setNextActionFailure(WifiP2pManager.BUSY);
100     RuntimeEnvironment.getMasterScheduler().unPause();
101 
102     assertThat(testListener.success).isFalse();
103     assertThat(testListener.reason).isEqualTo(WifiP2pManager.BUSY);
104   }
105 
106   @Test
requestGroupInfo()107   public void requestGroupInfo() {
108     TestGroupInfoListener listener = new TestGroupInfoListener();
109 
110     WifiP2pGroup wifiP2pGroup = new WifiP2pGroup();
111     shadowOf(wifiP2pGroup).setInterface("ssid");
112     shadowOf(wifiP2pGroup).setPassphrase("passphrase");
113     shadowOf(wifiP2pGroup).setNetworkName("networkname");
114 
115     shadowManager.setGroupInfo(channel, wifiP2pGroup);
116 
117     manager.requestGroupInfo(channel, listener);
118 
119     assertThat(listener.group.getNetworkName()).isEqualTo(wifiP2pGroup.getNetworkName());
120     assertThat(listener.group.getInterface()).isEqualTo(wifiP2pGroup.getInterface());
121     assertThat(listener.group.getPassphrase()).isEqualTo(wifiP2pGroup.getPassphrase());
122   }
123 
124   @Test
requestGroupInfo_nullListener()125   public void requestGroupInfo_nullListener() {
126     WifiP2pGroup wifiP2pGroup = new WifiP2pGroup();
127     shadowManager.setGroupInfo(channel, wifiP2pGroup);
128 
129     manager.requestGroupInfo(channel, null);
130 
131     // Should not fail with a null listener
132   }
133 
134   private static class TestActionListener implements WifiP2pManager.ActionListener {
135     private int reason;
136     private boolean success;
137 
138     @Override
onSuccess()139     public void onSuccess() {
140       success = true;
141     }
142 
143     @Override
onFailure(int reason)144     public void onFailure(int reason) {
145       success = false;
146       this.reason = reason;
147     }
148   }
149 
150   private static class TestGroupInfoListener implements WifiP2pManager.GroupInfoListener {
151     private WifiP2pGroup group;
152 
153     @Override
onGroupInfoAvailable(WifiP2pGroup group)154     public void onGroupInfoAvailable(WifiP2pGroup group) {
155       this.group = group;
156     }
157   }
158 }
159