EMMA Coverage Report (generated Fri Aug 23 16:39:17 PDT 2013)
[all classes][org.chromium.net]

COVERAGE SUMMARY FOR SOURCE FILE [NetworkChangeNotifierTest.java]

nameclass, %method, %block, %line, %
NetworkChangeNotifierTest.java75%  (3/4)100% (15/15)99%  (154/156)100% (46.8/47)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class NetworkChangeNotifierTest100% (1/1)100% (2/2)99%  (100/101)100% (30/30)
testNetworkChangeNotifierJavaObservers (): void 100% (1/1)99%  (97/98)100% (28/28)
NetworkChangeNotifierTest (): void 100% (1/1)100% (3/3)100% (2/2)
     
class NetworkChangeNotifierTest$10%   (0/1)100% (0/0)100% (0/0)100% (0/0)
     
class NetworkChangeNotifierTest$MockConnectivityManagerDelegate100% (1/1)100% (8/8)97%  (34/35)99%  (10.9/11)
isConnected (): boolean 100% (1/1)88%  (7/8)87%  (0.9/1)
NetworkChangeNotifierTest$MockConnectivityManagerDelegate (NetworkChangeNotif... 100% (1/1)100% (6/6)100% (1/1)
activeNetworkExists (): boolean 100% (1/1)100% (3/3)100% (1/1)
getNetworkSubtype (): int 100% (1/1)100% (3/3)100% (1/1)
getNetworkType (): int 100% (1/1)100% (3/3)100% (1/1)
setActiveNetworkExists (boolean): void 100% (1/1)100% (4/4)100% (2/2)
setNetworkSubtype (int): void 100% (1/1)100% (4/4)100% (2/2)
setNetworkType (int): void 100% (1/1)100% (4/4)100% (2/2)
     
class NetworkChangeNotifierTest$NetworkChangeNotifierTestObserver100% (1/1)100% (5/5)100% (20/20)100% (7/7)
NetworkChangeNotifierTest$NetworkChangeNotifierTestObserver (): void 100% (1/1)100% (6/6)100% (2/2)
NetworkChangeNotifierTest$NetworkChangeNotifierTestObserver (NetworkChangeNot... 100% (1/1)100% (3/3)100% (1/1)
hasReceivedNotification (): boolean 100% (1/1)100% (3/3)100% (1/1)
onConnectionTypeChanged (int): void 100% (1/1)100% (4/4)100% (2/2)
resetHasReceivedNotification (): void 100% (1/1)100% (4/4)100% (2/2)

1// Copyright (c) 2012 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4 
5package org.chromium.net;
6 
7import android.content.Context;
8import android.content.Intent;
9import android.net.ConnectivityManager;
10import android.telephony.TelephonyManager;
11import android.test.InstrumentationTestCase;
12import android.test.UiThreadTest;
13import android.test.suitebuilder.annotation.MediumTest;
14 
15import org.chromium.base.ActivityStatus;
16import org.chromium.base.test.util.Feature;
17 
18public class NetworkChangeNotifierTest extends InstrumentationTestCase {
19    /**
20     * Listens for alerts fired by the NetworkChangeNotifier when network status changes.
21     */
22    private static class NetworkChangeNotifierTestObserver
23            implements NetworkChangeNotifier.ConnectionTypeObserver {
24        private boolean mReceivedNotification = false;
25 
26        @Override
27        public void onConnectionTypeChanged(int connectionType) {
28            mReceivedNotification = true;
29        }
30 
31        public boolean hasReceivedNotification() {
32            return mReceivedNotification;
33        }
34 
35        public void resetHasReceivedNotification() {
36            mReceivedNotification = false;
37        }
38    }
39 
40    /**
41     * Mocks out calls to the ConnectivityManager.
42     */
43    class MockConnectivityManagerDelegate
44            extends NetworkChangeNotifierAutoDetect.ConnectivityManagerDelegate {
45        private boolean mActiveNetworkExists;
46        private int mNetworkType;
47        private int mNetworkSubtype;
48 
49        @Override
50        boolean activeNetworkExists() {
51            return mActiveNetworkExists;
52        }
53 
54        @Override
55        boolean isConnected() {
56            return getNetworkType() != NetworkChangeNotifier.CONNECTION_NONE;
57        }
58 
59        void setActiveNetworkExists(boolean networkExists) {
60            mActiveNetworkExists = networkExists;
61        }
62 
63        @Override
64        int getNetworkType() {
65            return mNetworkType;
66        }
67 
68        void setNetworkType(int networkType) {
69            mNetworkType = networkType;
70        }
71 
72        @Override
73        int getNetworkSubtype() {
74            return mNetworkSubtype;
75        }
76 
77        void setNetworkSubtype(int networkSubtype) {
78            mNetworkSubtype = networkSubtype;
79        }
80    }
81 
82    /**
83     * Tests that when Chrome gets an intent indicating a change in network connectivity, it sends a
84     * notification to Java observers.
85     */
86    @UiThreadTest
87    @MediumTest
88    @Feature({"Android-AppBase"})
89    public void testNetworkChangeNotifierJavaObservers() throws InterruptedException {
90        // Create a new notifier that doesn't have a native-side counterpart.
91        Context context = getInstrumentation().getTargetContext();
92        NetworkChangeNotifier.resetInstanceForTests(context);
93 
94        NetworkChangeNotifier.setAutoDetectConnectivityState(true);
95        NetworkChangeNotifierAutoDetect receiver = NetworkChangeNotifier.getAutoDetectorForTest();
96        assertTrue(receiver != null);
97 
98        MockConnectivityManagerDelegate connectivityDelegate =
99                new MockConnectivityManagerDelegate();
100        connectivityDelegate.setActiveNetworkExists(true);
101        connectivityDelegate.setNetworkType(NetworkChangeNotifier.CONNECTION_UNKNOWN);
102        connectivityDelegate.setNetworkSubtype(TelephonyManager.NETWORK_TYPE_UNKNOWN);
103        receiver.setConnectivityManagerDelegateForTests(connectivityDelegate);
104 
105        // Initialize the NetworkChangeNotifier with a connection.
106        Intent connectivityIntent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
107        receiver.onReceive(getInstrumentation().getTargetContext(), connectivityIntent);
108 
109        // We shouldn't be re-notified if the connection hasn't actually changed.
110        NetworkChangeNotifierTestObserver observer = new NetworkChangeNotifierTestObserver();
111        NetworkChangeNotifier.addConnectionTypeObserver(observer);
112        receiver.onReceive(getInstrumentation().getTargetContext(), connectivityIntent);
113        assertFalse(observer.hasReceivedNotification());
114 
115        // Mimic that connectivity has been lost and ensure that Chrome notifies our observer.
116        connectivityDelegate.setActiveNetworkExists(false);
117        connectivityDelegate.setNetworkType(NetworkChangeNotifier.CONNECTION_NONE);
118        Intent noConnectivityIntent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
119        receiver.onReceive(getInstrumentation().getTargetContext(), noConnectivityIntent);
120        assertTrue(observer.hasReceivedNotification());
121 
122        observer.resetHasReceivedNotification();
123        // Pretend we got moved to the background.
124        receiver.onActivityStateChange(ActivityStatus.PAUSED);
125        // Change the state.
126        connectivityDelegate.setActiveNetworkExists(true);
127        connectivityDelegate.setNetworkType(NetworkChangeNotifier.CONNECTION_WIFI);
128        // The NetworkChangeNotifierAutoDetect doesn't receive any notification while we are in the
129        // background, but when we get back to the foreground the state changed should be detected
130        // and a notification sent.
131        receiver.onActivityStateChange(ActivityStatus.RESUMED);
132        assertTrue(observer.hasReceivedNotification());
133    }
134}

[all classes][org.chromium.net]
EMMA 2.0.5312 (C) Vladimir Roubtsov