File: device_info_manager_unittest.cc

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (220 lines) | stat: -rw-r--r-- 7,832 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/apps/almanac_api_client/device_info_manager.h"

#include <memory>
#include <optional>

#include "base/test/scoped_chromeos_version_info.h"
#include "base/test/test_future.h"
#include "chrome/browser/apps/almanac_api_client/proto/client_context.pb.h"
#include "chrome/browser/browser_process.h"
#include "chrome/common/channel_info.h"
#include "chrome/test/base/testing_profile.h"
#include "chromeos/ash/components/system/fake_statistics_provider.h"
#include "chromeos/ash/components/system/statistics_provider.h"
#include "components/language/core/browser/pref_names.h"
#include "components/prefs/pref_service.h"
#include "components/version_info/channel.h"
#include "content/public/test/browser_task_environment.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace apps {

class DeviceInfoManagerTest : public testing::Test {
 public:
  void SetUp() override {
    device_info_manager_ = base::WrapUnique(new DeviceInfoManager(profile()));
  }

  Profile* profile() { return &profile_; }
  DeviceInfoManager* device_info_manager() {
    return device_info_manager_.get();
  }
  ash::system::FakeStatisticsProvider* statistics_provider() {
    return &fake_statistics_provider_;
  }

 private:
  content::BrowserTaskEnvironment task_environment_;
  TestingProfile profile_;

  std::unique_ptr<DeviceInfoManager> device_info_manager_;
  ash::system::ScopedFakeStatisticsProvider fake_statistics_provider_;
};

TEST_F(DeviceInfoManagerTest, CheckDeviceInfo) {
  const char kLsbRelease[] = R"(
  CHROMEOS_RELEASE_VERSION=123.4.5
  CHROMEOS_RELEASE_BOARD=puff-signed-mp-v11keys
  CHROMEOS_ARC_ANDROID_SDK_VERSION=33
  )";
  base::test::ScopedChromeOSVersionInfo version(kLsbRelease, base::Time());

  statistics_provider()->SetMachineStatistic(ash::system::kHardwareClassKey,
                                             "FOOBAR D0G-F4N-C1UB");

  static constexpr char kTestLocale[] = "test_locale";
  profile()->GetPrefs()->SetString(language::prefs::kApplicationLocale,
                                   kTestLocale);

  base::test::TestFuture<DeviceInfo> info_future;
  device_info_manager()->GetDeviceInfo(info_future.GetCallback());

  DeviceInfo device_info = info_future.Take();

  ASSERT_EQ(device_info.board, "puff");
  ASSERT_FALSE(device_info.model.empty());
  ASSERT_EQ(device_info.user_type, "unmanaged");
  ASSERT_FALSE(device_info.version_info.ash_chrome.empty());
  ASSERT_EQ(device_info.version_info.platform, "123.4.5");
  ASSERT_EQ(device_info.version_info.channel, chrome::GetChannel());
  ASSERT_EQ(device_info.version_info.arc_sdk, 0);
  ASSERT_EQ(device_info.hardware_id, "FOOBAR D0G-F4N-C1UB");
  ASSERT_EQ(device_info.locale, kTestLocale);
  ASSERT_EQ(device_info.custom_label_tag, std::nullopt);
}

TEST_F(DeviceInfoManagerTest, CheckDeviceInfoNoLanguagePreference) {
  base::test::TestFuture<DeviceInfo> info_future;
  device_info_manager()->GetDeviceInfo(info_future.GetCallback());

  DeviceInfo device_info = info_future.Take();

  // If there's no preferred locale set in prefs, locale should fall back to the
  // current UI language.
  ASSERT_EQ(device_info.locale, g_browser_process->GetApplicationLocale());
}

TEST_F(DeviceInfoManagerTest, GetDeviceInfoMultipleTimes) {
  statistics_provider()->SetMachineStatistic(ash::system::kHardwareClassKey,
                                             "FOOBAR D0G-F4N-C1UB");

  base::test::TestFuture<DeviceInfo> info_future_1;
  base::test::TestFuture<DeviceInfo> info_future_2;
  base::test::TestFuture<DeviceInfo> info_future_3;

  device_info_manager()->GetDeviceInfo(info_future_1.GetCallback());
  device_info_manager()->GetDeviceInfo(info_future_2.GetCallback());

  DeviceInfo device_info_1 = info_future_1.Take();
  DeviceInfo device_info_2 = info_future_2.Take();

  device_info_manager()->GetDeviceInfo(info_future_3.GetCallback());

  DeviceInfo device_info_3 = info_future_3.Take();

  // Check that we obtained the same result for all three DeviceInfos.
  ASSERT_EQ(device_info_1.hardware_id, device_info_2.hardware_id);
  ASSERT_EQ(device_info_1.hardware_id, device_info_3.hardware_id);
}

TEST_F(DeviceInfoManagerTest, DeviceInfoToProto) {
  DeviceInfo device_info;
  device_info.board = "brya";
  device_info.model = "taniks";
  device_info.hardware_id = "FOOBAR D0G-F4N-C1UB";
  device_info.user_type = "unmanaged";
  device_info.version_info.ash_chrome = "10.10.10";
  device_info.version_info.platform = "12345.0.0";
  device_info.version_info.channel = version_info::Channel::STABLE;
  device_info.version_info.arc_sdk = 33;
  device_info.version_info.steam_client = "TRUE";
  device_info.locale = "en-US";
  device_info.custom_label_tag = "COOL-OEM";

  proto::ClientDeviceContext device_context = device_info.ToDeviceContext();

  EXPECT_EQ(device_context.board(), "brya");
  EXPECT_EQ(device_context.model(), "taniks");
  EXPECT_EQ(device_context.channel(),
            apps::proto::ClientDeviceContext::CHANNEL_STABLE);
  EXPECT_EQ(device_context.versions().chrome_ash(), "10.10.10");
  EXPECT_EQ(device_context.versions().chrome_os_platform(), "12345.0.0");
  EXPECT_EQ(device_context.versions().arc_sdk(), 33);
  EXPECT_EQ(device_context.versions().steam_client(), "TRUE");
  EXPECT_EQ(device_context.hardware_id(), "FOOBAR D0G-F4N-C1UB");
  EXPECT_EQ(device_context.custom_label_tag(), "COOL-OEM");

  proto::ClientUserContext user_context = device_info.ToUserContext();

  EXPECT_EQ(user_context.language(), "en-US");
  EXPECT_EQ(user_context.user_type(),
            apps::proto::ClientUserContext::USERTYPE_UNMANAGED);
}

TEST_F(DeviceInfoManagerTest, UserTypeToProto) {
  {
    DeviceInfo info;
    info.user_type = "unmanaged";
    EXPECT_EQ(info.ToUserContext().user_type(),
              proto::ClientUserContext::USERTYPE_UNMANAGED);
  }
  {
    DeviceInfo info;
    info.user_type = "managed";
    EXPECT_EQ(info.ToUserContext().user_type(),
              proto::ClientUserContext::USERTYPE_MANAGED);
  }
  {
    DeviceInfo info;
    info.user_type = "child";
    EXPECT_EQ(info.ToUserContext().user_type(),
              proto::ClientUserContext::USERTYPE_CHILD);
  }
  {
    DeviceInfo info;
    info.user_type = "guest";
    EXPECT_EQ(info.ToUserContext().user_type(),
              proto::ClientUserContext::USERTYPE_GUEST);
  }
  {
    DeviceInfo info;
    info.user_type = "managed_guest";
    EXPECT_EQ(info.ToUserContext().user_type(),
              proto::ClientUserContext::USERTYPE_MANAGED_GUEST);
  }
  {
    DeviceInfo info;
    info.user_type = "dog";
    EXPECT_EQ(info.ToUserContext().user_type(),
              proto::ClientUserContext::USERTYPE_UNKNOWN);
  }
}

TEST_F(DeviceInfoManagerTest, ChannelTypeToProto) {
  {
    DeviceInfo info;
    info.version_info.channel = version_info::Channel::CANARY;
    EXPECT_EQ(info.ToDeviceContext().channel(),
              proto::ClientDeviceContext::CHANNEL_CANARY);
  }
  {
    DeviceInfo info;
    info.version_info.channel = version_info::Channel::DEV;
    EXPECT_EQ(info.ToDeviceContext().channel(),
              proto::ClientDeviceContext::CHANNEL_DEV);
  }
  {
    DeviceInfo info;
    info.version_info.channel = version_info::Channel::BETA;
    EXPECT_EQ(info.ToDeviceContext().channel(),
              proto::ClientDeviceContext::CHANNEL_BETA);
  }
  {
    DeviceInfo info;
    info.version_info.channel = version_info::Channel::STABLE;
    EXPECT_EQ(info.ToDeviceContext().channel(),
              proto::ClientDeviceContext::CHANNEL_STABLE);
  }
  {
    DeviceInfo info;
    info.version_info.channel = version_info::Channel::UNKNOWN;
    EXPECT_EQ(info.ToDeviceContext().channel(),
              proto::ClientDeviceContext::CHANNEL_DEFAULT);
  }
}

}  // namespace apps