• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2017 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 package org.chromium.net;
6 
7 import android.annotation.SuppressLint;
8 import android.content.Context;
9 import android.net.ConnectivityManager;
10 import android.net.ConnectivityManager.NetworkCallback;
11 import android.os.Build;
12 import android.os.Looper;
13 
14 import androidx.test.InstrumentationRegistry;
15 import androidx.test.filters.MediumTest;
16 
17 import org.junit.After;
18 import org.junit.Assert;
19 import org.junit.Test;
20 import org.junit.runner.RunWith;
21 
22 import org.chromium.base.test.BaseJUnit4ClassRunner;
23 import org.chromium.base.test.util.MinAndroidSdkLevel;
24 
25 /**
26  * Tests for org.chromium.net.NetworkChangeNotifier without native code. This class specifically
27  * does not have a setUp() method that loads native libraries.
28  */
29 @RunWith(BaseJUnit4ClassRunner.class)
30 @SuppressLint("NewApi")
31 public class NetworkChangeNotifierNoNativeTest {
32     @After
tearDown()33     public void tearDown() {
34         // Destroy NetworkChangeNotifierAutoDetect
35         NetworkChangeNotifier.setAutoDetectConnectivityState(false);
36     }
37 
38     /**
39      * Verify NetworkChangeNotifier can initialize without calling into native code. This test
40      * will crash if any native calls are made during NetworkChangeNotifier initialization.
41      */
42     @Test
43     @MediumTest
testNoNativeDependence()44     public void testNoNativeDependence() {
45         Looper.prepare();
46         NetworkChangeNotifier.init();
47         NetworkChangeNotifier.registerToReceiveNotificationsAlways();
48     }
49 
50     /**
51      * Verify NetworkChangeNotifier.registerNetworkCallbackFailed() returns false under normal
52      * circumstances.
53      */
54     @Test
55     @MediumTest
testDefaultState()56     public void testDefaultState() {
57         Looper.prepare();
58         NetworkChangeNotifier ncn = NetworkChangeNotifier.init();
59         Assert.assertFalse(ncn.registerNetworkCallbackFailed());
60         NetworkChangeNotifier.registerToReceiveNotificationsAlways();
61         Assert.assertFalse(ncn.registerNetworkCallbackFailed());
62     }
63 
64     /** Verify NetworkChangeNotifier.registerNetworkCallbackFailed() catches exception properly. */
65     @Test
66     @MediumTest
67     @MinAndroidSdkLevel(Build.VERSION_CODES.N)
testRegisterNetworkCallbackFail()68     public void testRegisterNetworkCallbackFail() {
69         ConnectivityManager connectivityManager =
70                 (ConnectivityManager)
71                         InstrumentationRegistry.getTargetContext()
72                                 .getSystemService(Context.CONNECTIVITY_SERVICE);
73         Looper.prepare();
74         NetworkChangeNotifier ncn = NetworkChangeNotifier.init();
75         Assert.assertFalse(ncn.registerNetworkCallbackFailed());
76 
77         // Use up all NetworkRequests
78         for (int i = 0; i < 99; i++) {
79             connectivityManager.registerDefaultNetworkCallback(new NetworkCallback());
80         }
81 
82         NetworkChangeNotifier.registerToReceiveNotificationsAlways();
83         Assert.assertTrue(ncn.registerNetworkCallbackFailed());
84     }
85 }
86