File: system_geolocation_source_unittest.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (141 lines) | stat: -rw-r--r-- 5,585 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <utility>

#include "ash/constants/ash_pref_names.h"
#include "ash/constants/geolocation_access_level.h"
#include "ash/session/session_controller_impl.h"
#include "ash/shell.h"
#include "ash/system/privacy_hub/privacy_hub_controller.h"
#include "ash/test/ash_test_base.h"
#include "base/test/bind.h"
#include "base/test/scoped_feature_list.h"
#include "base/test/test_future.h"
#include "chrome/browser/ash/geolocation/system_geolocation_source.h"
#include "components/prefs/pref_service.h"
#include "services/device/public/cpp/geolocation/location_system_permission_status.h"

namespace ash {

class SystemGeolocationSourceTests : public AshTestBase {
 protected:
  SystemGeolocationSourceTests()
      : AshTestBase(base::test::TaskEnvironment::TimeSource::MOCK_TIME) {}

  // AshTestBase:
  void SetUp() override { AshTestBase::SetUp(); }

  // Bind the `source` to subscribe to the active user's pref service.
  // NOTE: In production the singleton `SystemGeolocationSource` automatically
  // subscribes to the Primary user's pref service. However `AshTestBase`
  // deviates from this behavior, requiring manual subscription.
  void SubscribeToActiveUserPrefService(SystemGeolocationSource& source) {
    source.OnActiveUserPrefServiceChanged(
        Shell::Get()->session_controller()->GetActivePrefService());
  }

  void SetUserPref(GeolocationAccessLevel access_level) {
    Shell::Get()->session_controller()->GetActivePrefService()->SetInteger(
        prefs::kUserGeolocationAccessLevel, static_cast<int>(access_level));
  }

  GeolocationAccessLevel GetUserPref() {
    return static_cast<GeolocationAccessLevel>(
        Shell::Get()->session_controller()->GetActivePrefService()->GetInteger(
            prefs::kUserGeolocationAccessLevel));
  }

 protected:
  base::test::ScopedFeatureList scoped_feature_list_;
};

class SystemGeolocationSourceTestsGeolocationOn
    : public SystemGeolocationSourceTests {
 protected:
  SystemGeolocationSourceTestsGeolocationOn() {
    scoped_feature_list_.InitWithFeatures({features::kCrosPrivacyHub}, {});
  }
};

class SystemGeolocationSourceTestsGeolocationOff
    : public SystemGeolocationSourceTests {
 protected:
  SystemGeolocationSourceTestsGeolocationOff() {
    // Disables the geolocation part of the PrivacyHub
    scoped_feature_list_.InitWithFeatures({}, {ash::features::kCrosPrivacyHub});
  }
};

TEST_F(SystemGeolocationSourceTestsGeolocationOn, PrefChange) {
  EXPECT_TRUE(ash::features::IsCrosPrivacyHubLocationEnabled());

  // Initialize `SystemGeolocationSource` and start observing the active user's
  // pref changes.
  SystemGeolocationSource source;
  SubscribeToActiveUserPrefService(source);

  base::test::TestFuture<device::LocationSystemPermissionStatus> status;
  source.RegisterPermissionUpdateCallback(status.GetRepeatingCallback());

  // Initial value should be to allow.
  EXPECT_EQ(device::LocationSystemPermissionStatus::kAllowed, status.Take());

  // Change user settings to "Blocked for all" and check that the callback is
  // called.
  SetUserPref(GeolocationAccessLevel::kDisallowed);
  EXPECT_EQ(device::LocationSystemPermissionStatus::kDenied, status.Take());

  // Change user settings to "Only allowed for system services" and check that
  // callback is called.
  SetUserPref(GeolocationAccessLevel::kOnlyAllowedForSystem);
  EXPECT_EQ(device::LocationSystemPermissionStatus::kDenied, status.Take());

  // Change user settings to "Allowed" and check that the callback is called.
  SetUserPref(GeolocationAccessLevel::kAllowed);
  EXPECT_EQ(device::LocationSystemPermissionStatus::kAllowed, status.Take());
}

TEST_F(SystemGeolocationSourceTestsGeolocationOff, DisabledInV0) {
  EXPECT_FALSE(ash::features::IsCrosPrivacyHubLocationEnabled());

  // Initialize `SystemGeolocationSource` and start observing the active user's
  // pref changes.
  SystemGeolocationSource source;
  SubscribeToActiveUserPrefService(source);

  base::test::TestFuture<device::LocationSystemPermissionStatus> status;
  source.RegisterPermissionUpdateCallback(base::BindLambdaForTesting(
      [&status](device::LocationSystemPermissionStatus value) {
        // This drops the extra (duplicated) status changes.
        if (!status.IsReady()) {
          status.GetRepeatingCallback().Run(value);
        } else {
          // This is a duplicated update, let's check that the status is not
          // changed (i.e. it is really duplication, not a real change).
          EXPECT_EQ(status.Get(), value);
        }
      }));
  // Initial value should be allowed and the callback should be called with this
  // value
  EXPECT_EQ(device::LocationSystemPermissionStatus::kAllowed, status.Take());
  EXPECT_EQ(GeolocationAccessLevel::kAllowed, GetUserPref());

  // Change user settings to "Blocked for all" and check that the sent value is
  // still `kAllowed`.
  SetUserPref(GeolocationAccessLevel::kDisallowed);
  EXPECT_EQ(GeolocationAccessLevel::kAllowed, GetUserPref());

  // Change user settings "Only allowed for system services" and check that the
  // sent value is still `kAllowed`.
  SetUserPref(GeolocationAccessLevel::kOnlyAllowedForSystem);
  EXPECT_EQ(GeolocationAccessLevel::kAllowed, GetUserPref());

  // Change user settings to "Allowed" and check that the sent value is still
  // `kAllowed`.
  SetUserPref(GeolocationAccessLevel::kAllowed);
  EXPECT_EQ(GeolocationAccessLevel::kAllowed, GetUserPref());
}

}  // namespace ash