1 /*
2  * Copyright (C) 2019 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 
17 package android.net;
18 
19 import static org.junit.Assert.assertEquals;
20 
21 import android.os.Build;
22 import android.os.RemoteException;
23 
24 import androidx.test.filters.SmallTest;
25 import androidx.test.runner.AndroidJUnit4;
26 
27 import com.android.testutils.DevSdkIgnoreRule;
28 import com.android.testutils.DevSdkIgnoreRule.IgnoreAfter;
29 import com.android.testutils.DevSdkIgnoreRule.IgnoreUpTo;
30 
31 import org.junit.Rule;
32 import org.junit.Test;
33 import org.junit.runner.RunWith;
34 
35 @RunWith(AndroidJUnit4.class)
36 @SmallTest
37 public class CaptivePortalTest {
38     @Rule
39     public final DevSdkIgnoreRule ignoreRule = new DevSdkIgnoreRule();
40 
41     private static final int DEFAULT_TIMEOUT_MS = 5000;
42     private static final String TEST_PACKAGE_NAME = "com.google.android.test";
43 
44     private final class MyCaptivePortalImpl extends ICaptivePortal.Stub {
45         int mCode = -1;
46         String mPackageName = null;
47 
48         @Override
appResponse(final int response)49         public void appResponse(final int response) throws RemoteException {
50             mCode = response;
51         }
52 
53         @Override
appRequest(final int request)54         public void appRequest(final int request) throws RemoteException {
55             mCode = request;
56         }
57 
58         // This is only @Override on R-
logEvent(int eventId, String packageName)59         public void logEvent(int eventId, String packageName) throws RemoteException {
60             mCode = eventId;
61             mPackageName = packageName;
62         }
63     }
64 
65     private interface TestFunctor {
useCaptivePortal(CaptivePortal o)66         void useCaptivePortal(CaptivePortal o);
67     }
68 
runCaptivePortalTest(TestFunctor f)69     private MyCaptivePortalImpl runCaptivePortalTest(TestFunctor f) {
70         final MyCaptivePortalImpl cp = new MyCaptivePortalImpl();
71         f.useCaptivePortal(new CaptivePortal(cp.asBinder()));
72         return cp;
73     }
74 
75     @Test
testReportCaptivePortalDismissed()76     public void testReportCaptivePortalDismissed() {
77         final MyCaptivePortalImpl result =
78                 runCaptivePortalTest(c -> c.reportCaptivePortalDismissed());
79         assertEquals(result.mCode, CaptivePortal.APP_RETURN_DISMISSED);
80     }
81 
82     @Test
testIgnoreNetwork()83     public void testIgnoreNetwork() {
84         final MyCaptivePortalImpl result = runCaptivePortalTest(c -> c.ignoreNetwork());
85         assertEquals(result.mCode, CaptivePortal.APP_RETURN_UNWANTED);
86     }
87 
88     @Test
testUseNetwork()89     public void testUseNetwork() {
90         final MyCaptivePortalImpl result = runCaptivePortalTest(c -> c.useNetwork());
91         assertEquals(result.mCode, CaptivePortal.APP_RETURN_WANTED_AS_IS);
92     }
93 
94     @IgnoreUpTo(Build.VERSION_CODES.Q)
95     @Test
testReevaluateNetwork()96     public void testReevaluateNetwork() {
97         final MyCaptivePortalImpl result = runCaptivePortalTest(c -> c.reevaluateNetwork());
98         assertEquals(result.mCode, CaptivePortal.APP_REQUEST_REEVALUATION_REQUIRED);
99     }
100 
101     @IgnoreUpTo(Build.VERSION_CODES.R)
102     @Test
testLogEvent()103     public void testLogEvent() {
104         /**
105         * From S testLogEvent is expected to do nothing but shouldn't crash (the API
106         * logEvent has been deprecated).
107         */
108         final MyCaptivePortalImpl result = runCaptivePortalTest(c -> c.logEvent(
109                 0,
110                 TEST_PACKAGE_NAME));
111     }
112 
113     @IgnoreAfter(Build.VERSION_CODES.R)
114     @Test
testLogEvent_UntilR()115     public void testLogEvent_UntilR() {
116         final MyCaptivePortalImpl result = runCaptivePortalTest(c -> c.logEvent(
117                 42, TEST_PACKAGE_NAME));
118         assertEquals(result.mCode, 42);
119         assertEquals(result.mPackageName, TEST_PACKAGE_NAME);
120     }
121 }
122