• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2022 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.impl;
6 
7 import static org.junit.Assert.assertFalse;
8 import static org.junit.Assert.assertTrue;
9 
10 import static org.chromium.net.CronetTestRule.getContext;
11 
12 import android.content.Context;
13 import android.content.ContextWrapper;
14 import android.content.pm.ApplicationInfo;
15 import android.content.pm.PackageManager;
16 import android.os.Bundle;
17 
18 import androidx.test.ext.junit.runners.AndroidJUnit4;
19 import androidx.test.filters.SmallTest;
20 
21 import org.junit.Before;
22 import org.junit.Rule;
23 import org.junit.Test;
24 import org.junit.runner.RunWith;
25 
26 import org.chromium.base.test.util.PackageManagerWrapper;
27 import org.chromium.net.CronetTestRule;
28 import org.chromium.net.CronetTestRule.OnlyRunNativeCronet;
29 import org.chromium.net.impl.CronetLogger.CronetSource;
30 
31 /**
32  * Tests {@link CronetManifest}
33  */
34 @RunWith(AndroidJUnit4.class)
35 public class CronetManifestTest {
36     @Rule
37     public final CronetTestRule mTestRule = new CronetTestRule();
38 
39     private Context mMockContext;
40     private Bundle mMetadata;
41     private ApplicationInfo mAppInfo;
42 
43     @Before
setUp()44     public void setUp() throws Exception {
45         mAppInfo = new ApplicationInfo();
46         mMockContext = new MockContext(getContext());
47         mMetadata = new Bundle();
48     }
49 
50     @Test
51     @SmallTest
52     @OnlyRunNativeCronet
testTelemetryOptIn_whenNoMetadata()53     public void testTelemetryOptIn_whenNoMetadata() throws Exception {
54         assertFalse(CronetManifest.isAppOptedInForTelemetry(
55                 mMockContext, CronetSource.CRONET_SOURCE_STATICALLY_LINKED));
56         assertFalse(CronetManifest.isAppOptedInForTelemetry(
57                 mMockContext, CronetSource.CRONET_SOURCE_PLAY_SERVICES));
58         assertFalse(CronetManifest.isAppOptedInForTelemetry(
59                 mMockContext, CronetSource.CRONET_SOURCE_FALLBACK));
60     }
61 
62     @Test
63     @SmallTest
64     @OnlyRunNativeCronet
testTelemetryOptIn_whenMetadataIsTrue()65     public void testTelemetryOptIn_whenMetadataIsTrue() throws Exception {
66         mMetadata.putBoolean(CronetManifest.TELEMETRY_OPT_IN_META_DATA_STR, true);
67         mAppInfo.metaData = mMetadata;
68 
69         assertTrue(CronetManifest.isAppOptedInForTelemetry(
70                 mMockContext, CronetSource.CRONET_SOURCE_STATICALLY_LINKED));
71         assertTrue(CronetManifest.isAppOptedInForTelemetry(
72                 mMockContext, CronetSource.CRONET_SOURCE_PLAY_SERVICES));
73         assertTrue(CronetManifest.isAppOptedInForTelemetry(
74                 mMockContext, CronetSource.CRONET_SOURCE_FALLBACK));
75     }
76 
77     @Test
78     @SmallTest
79     @OnlyRunNativeCronet
testTelemetryOptIn_whenMetadataIsFalse()80     public void testTelemetryOptIn_whenMetadataIsFalse() throws Exception {
81         mMetadata.putBoolean(CronetManifest.TELEMETRY_OPT_IN_META_DATA_STR, false);
82         mAppInfo.metaData = mMetadata;
83 
84         assertFalse(CronetManifest.isAppOptedInForTelemetry(
85                 mMockContext, CronetSource.CRONET_SOURCE_STATICALLY_LINKED));
86         assertFalse(CronetManifest.isAppOptedInForTelemetry(
87                 mMockContext, CronetSource.CRONET_SOURCE_PLAY_SERVICES));
88         assertFalse(CronetManifest.isAppOptedInForTelemetry(
89                 mMockContext, CronetSource.CRONET_SOURCE_FALLBACK));
90     }
91 
92     private class MockContext extends ContextWrapper {
MockContext(Context base)93         public MockContext(Context base) {
94             super(base);
95         }
96 
97         @Override
getPackageManager()98         public PackageManager getPackageManager() {
99             return new PackageManagerWrapper(super.getPackageManager()) {
100                 @Override
101                 public ApplicationInfo getApplicationInfo(String packageName, int flags)
102                         throws PackageManager.NameNotFoundException {
103                     return mAppInfo;
104                 }
105             };
106         }
107     }
108 }
109