1package com.xtremelabs.robolectric.shadows;
2
3import android.telephony.PhoneStateListener;
4import android.telephony.TelephonyManager;
5import com.xtremelabs.robolectric.WithTestDefaultsRunner;
6import org.junit.Before;
7import org.junit.Test;
8import org.junit.runner.RunWith;
9
10import static android.content.Context.TELEPHONY_SERVICE;
11import static com.xtremelabs.robolectric.Robolectric.application;
12import static com.xtremelabs.robolectric.Robolectric.newInstanceOf;
13import static com.xtremelabs.robolectric.Robolectric.shadowOf;
14import static org.hamcrest.CoreMatchers.equalTo;
15import static org.hamcrest.CoreMatchers.notNullValue;
16import static org.hamcrest.CoreMatchers.sameInstance;
17import static org.junit.Assert.assertEquals;
18import static org.junit.Assert.assertThat;
19
20@RunWith(WithTestDefaultsRunner.class)
21public class TelephonyManagerTest {
22
23	private TelephonyManager manager;
24	private ShadowTelephonyManager shadowManager;
25	private MyPhoneStateListener listener;
26
27	@Before
28	public void setUp() throws Exception {
29		manager = newInstanceOf(TelephonyManager.class);
30		shadowManager = shadowOf(manager);
31
32		listener = new MyPhoneStateListener();
33	}
34
35	@Test
36	public void testListen() {
37		manager.listen(listener, PhoneStateListener.LISTEN_CALL_STATE);
38		assertThat(shadowManager.getListener(), notNullValue());
39		assertThat((MyPhoneStateListener) shadowManager.getListener(), sameInstance(listener));
40		assertThat(shadowManager.getEventFlags(), equalTo(PhoneStateListener.LISTEN_CALL_STATE));
41	}
42
43    @Test
44    public void shouldGiveDeviceId() {
45        String testId = "TESTING123";
46        TelephonyManager telephonyManager = (TelephonyManager) application.getSystemService(TELEPHONY_SERVICE);
47        shadowOf(telephonyManager).setDeviceId(testId);
48        assertEquals(testId, telephonyManager.getDeviceId());
49    }
50
51    @Test
52    public void shouldGiveNetworkOperatorName() {
53        TelephonyManager telephonyManager = (TelephonyManager) application.getSystemService(TELEPHONY_SERVICE);
54        ShadowTelephonyManager shadowTelephonyManager = shadowOf(telephonyManager);
55        shadowTelephonyManager.setNetworkOperatorName("SomeOperatorName");
56        assertEquals("SomeOperatorName", telephonyManager.getNetworkOperatorName());
57    }
58
59    @Test
60    public void shouldGiveNetworkCountryIso() {
61        TelephonyManager telephonyManager = (TelephonyManager) application.getSystemService(TELEPHONY_SERVICE);
62        ShadowTelephonyManager shadowTelephonyManager = shadowOf(telephonyManager);
63        shadowTelephonyManager.setNetworkCountryIso("SomeIso");
64        assertEquals("SomeIso", telephonyManager.getNetworkCountryIso());
65    }
66
67    @Test
68    public void shouldGiveNetworkOperator() {
69        TelephonyManager telephonyManager = (TelephonyManager) application.getSystemService(TELEPHONY_SERVICE);
70        ShadowTelephonyManager shadowTelephonyManager = shadowOf(telephonyManager);
71        shadowTelephonyManager.setNetworkOperator("SomeOperator");
72        assertEquals("SomeOperator", telephonyManager.getNetworkOperator());
73    }
74
75    @Test(expected = SecurityException.class)
76    public void getDeviceId_shouldThrowSecurityExceptionWhenReadPhoneStatePermissionNotGranted() throws Exception {
77        shadowManager.setReadPhoneStatePermission(false);
78        manager.getDeviceId();
79    }
80
81    @Test
82    public void shouldGivePhoneType() {
83        TelephonyManager telephonyManager = (TelephonyManager) application.getSystemService(TELEPHONY_SERVICE);
84        ShadowTelephonyManager shadowTelephonyManager = shadowOf(telephonyManager);
85        shadowTelephonyManager.setPhoneType( TelephonyManager.PHONE_TYPE_CDMA );
86        assertEquals(TelephonyManager.PHONE_TYPE_CDMA, telephonyManager.getPhoneType());
87        shadowTelephonyManager.setPhoneType( TelephonyManager.PHONE_TYPE_GSM );
88        assertEquals(TelephonyManager.PHONE_TYPE_GSM, telephonyManager.getPhoneType());
89    }
90
91	private class MyPhoneStateListener extends PhoneStateListener {
92
93	}
94}
95