blob: 8e28fae624cd36b4a54ebcaaa2c61c0c32ac1c0d [file] [log] [blame]
Christopher Wiley08725a82016-05-18 16:32:44 -07001/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server.connectivity.tethering;
18
19import static org.mockito.Mockito.verifyNoMoreInteractions;
20
21import android.net.INetworkStatsService;
22import android.os.INetworkManagementService;
23import android.os.test.TestLooper;
24
25import org.junit.Before;
26import org.junit.Test;
27import org.mockito.Mock;
28import org.mockito.MockitoAnnotations;
29
30
31public class TetherInterfaceSMTest {
32 private static final String IFACE_NAME = "testnet1";
33
34 @Mock private INetworkManagementService mNMService;
35 @Mock private INetworkStatsService mStatsService;
36 @Mock private IControlsTethering mTetherHelper;
37
38 private final TestLooper mLooper = new TestLooper();
39 private final Object mMutex = new Object();
40 private TetherInterfaceSM mTestedSm;
41
42 @Before
43 public void setUp() throws Exception {
44 MockitoAnnotations.initMocks(this);
45 mTestedSm = new TetherInterfaceSM(IFACE_NAME, mLooper.getLooper(), false, mMutex,
46 mNMService, mStatsService, mTetherHelper);
47 mTestedSm.start();
48 }
49
50 @Test
51 public void shouldDoNothingUntilRequested() {
52 final int [] NOOP_COMMANDS = {
53 TetherInterfaceSM.CMD_TETHER_MODE_DEAD,
54 TetherInterfaceSM.CMD_TETHER_UNREQUESTED,
55 TetherInterfaceSM.CMD_INTERFACE_UP,
56 TetherInterfaceSM.CMD_CELL_DUN_ERROR,
57 TetherInterfaceSM.CMD_IP_FORWARDING_ENABLE_ERROR,
58 TetherInterfaceSM.CMD_IP_FORWARDING_DISABLE_ERROR,
59 TetherInterfaceSM.CMD_START_TETHERING_ERROR,
60 TetherInterfaceSM.CMD_STOP_TETHERING_ERROR,
61 TetherInterfaceSM.CMD_SET_DNS_FORWARDERS_ERROR,
62 TetherInterfaceSM.CMD_TETHER_CONNECTION_CHANGED
63 };
64 for (int command : NOOP_COMMANDS) {
65 mTestedSm.sendMessage(command);
66 dispatchUntilIdle();
67 // None of those commands should trigger us to request action from
68 // the rest of the system.
69 verifyNoMoreInteractions(mNMService);
70 verifyNoMoreInteractions(mStatsService);
71 verifyNoMoreInteractions(mTetherHelper);
72 }
73 }
74
75 private void dispatchUntilIdle() {
76 for (int i = 0; i < 100; i++) {
77 if (mLooper.isIdle()) {
78 return;
79 }
80 mLooper.dispatchAll();
81 }
82 throw new RuntimeException("Failed to clear message loop.");
83 }
84}