File: companion_app_broker_impl_unittest.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; 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,806; 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 (358 lines) | stat: -rw-r--r-- 14,955 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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
// 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 "ash/quick_pair/companion_app/companion_app_broker_impl.h"

#include <map>

#include "ash/constants/ash_features.h"
#include "ash/quick_pair/common/device.h"
#include "ash/quick_pair/common/fake_quick_pair_browser_delegate.h"
#include "ash/test/ash_test_base.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/scoped_refptr.h"
#include "base/test/scoped_feature_list.h"
#include "components/signin/public/identity_manager/identity_test_environment.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace {

const std::string kUserEmail = "test@test.test";
constexpr char kTestDeviceAddress[] = "11:12:13:14:15:16";
constexpr char kValidModelId[] = "6EDAF7";
constexpr char kInvalidModelId[] = "000000";
constexpr char kFeatureParamModelId[] = "AAAAAA";
constexpr char kValidCompanionBrowserUri[] = "https://photos.google.com/";
constexpr char kEmptyCompanionBrowserUri[] = "";
constexpr char kCompanionAppId[] = "ncmjhecbjeaamljdfahankockkkdmedg";
constexpr char kValidCompanionPlayStoreUri[] =
    "https://play.google.com/store/apps/"
    "details?id=com.google.android.apps.photos";
constexpr char kEmptyCompanionPlayStoreUri[] = "";
constexpr char kFeatureParamDeviceIds[] = "111111,AAAAAA,BBBBBB,CCCCCC,DDDDDD";

}  // namespace

namespace ash {
namespace quick_pair {

class CompanionAppBrokerImplUnitTest : public AshTestBase,
                                       public CompanionAppBroker::Observer {
 public:
  void SetUp() override {
    AshTestBase::SetUp();

    companion_app_broker_ = std::make_unique<CompanionAppBrokerImpl>();
    companion_app_broker_->AddObserver(this);

    test_device_ = base::MakeRefCounted<Device>(
        kValidModelId, kTestDeviceAddress, Protocol::kFastPairInitial);

    identity_test_environment_ =
        std::make_unique<signin::IdentityTestEnvironment>();
    identity_manager_ = identity_test_environment_->identity_manager();
  }

  void TearDown() override {
    ClearLogin();
    AshTestBase::TearDown();
  }

  void Login(user_manager::UserType user_type) {
    SimulateUserLogin({kUserEmail, user_type});
  }

  void SetIdentityManager(signin::IdentityManager* identity_manager) {
    FakeQuickPairBrowserDelegate* delegate =
        FakeQuickPairBrowserDelegate::Get();
    delegate->SetIdentityManager(identity_manager);
  }

  void SetCompanionAppInstalled(const std::string& app_id, bool installed) {
    FakeQuickPairBrowserDelegate* delegate =
        FakeQuickPairBrowserDelegate::Get();
    delegate->SetCompanionAppInstalled(app_id, installed);
  }

  // CompanionAppBroker::Observer
  void ShowInstallCompanionApp(scoped_refptr<Device> device) override {
    install_companion_app_notification_shown_ = true;
  }

  void ShowLaunchCompanionApp(scoped_refptr<Device> device) override {
    launch_companion_app_notification_shown_ = true;
  }

  void OnCompanionAppInstalled(scoped_refptr<Device> device) override {}

 protected:
  std::unique_ptr<CompanionAppBrokerImpl> companion_app_broker_;
  scoped_refptr<Device> test_device_;
  std::unique_ptr<signin::IdentityTestEnvironment> identity_test_environment_;
  raw_ptr<signin::IdentityManager> identity_manager_;

  bool install_companion_app_notification_shown_ = false;
  bool launch_companion_app_notification_shown_ = false;
};

TEST_F(CompanionAppBrokerImplUnitTest, MaybeShowCompanionAppActions_Disabled) {
  base::test::ScopedFeatureList feature_list;
  feature_list.InitWithFeatures(
      /*enabled_features=*/{},
      /*disabled_features=*/{ash::features::kFastPairPwaCompanion});

  EXPECT_DEATH_IF_SUPPORTED(
      { companion_app_broker_->MaybeShowCompanionAppActions(test_device_); },
      "");
}

// The app is not installed and there is a Play store link, so regular
// logged-in users would be directed to the Play store via "install"
// notification. However, guests are only allowed to access the app through
// the browser link.
TEST_F(CompanionAppBrokerImplUnitTest,
       ShowLaunchCompanionApp_Guest_BrowserOnly) {
  base::test::ScopedFeatureList feature_list;
  feature_list.InitAndEnableFeatureWithParameters(
      ash::features::kFastPairPwaCompanion,
      {{ash::features::kFastPairPwaCompanionInstallUri.name,
        kValidCompanionBrowserUri},
       {ash::features::kFastPairPwaCompanionPlayStoreUri.name,
        kValidCompanionPlayStoreUri},
       {ash::features::kFastPairPwaCompanionAppId.name, kCompanionAppId}});

  SetIdentityManager(identity_manager_);
  SetCompanionAppInstalled(kCompanionAppId, false);
  Login(user_manager::UserType::kGuest);

  EXPECT_FALSE(install_companion_app_notification_shown_);
  EXPECT_FALSE(launch_companion_app_notification_shown_);
  companion_app_broker_->MaybeShowCompanionAppActions(test_device_);
  EXPECT_FALSE(install_companion_app_notification_shown_);
  EXPECT_TRUE(launch_companion_app_notification_shown_);
}

// The companion app is installed, but no browser link is supplied and guests
// can only access browser link.
TEST_F(CompanionAppBrokerImplUnitTest, NoCompanionAppNotification_Guest) {
  base::test::ScopedFeatureList feature_list;
  feature_list.InitAndEnableFeatureWithParameters(
      ash::features::kFastPairPwaCompanion,
      {{ash::features::kFastPairPwaCompanionInstallUri.name,
        kEmptyCompanionBrowserUri},
       {ash::features::kFastPairPwaCompanionPlayStoreUri.name,
        kValidCompanionPlayStoreUri},
       {ash::features::kFastPairPwaCompanionAppId.name, kCompanionAppId}});

  SetIdentityManager(identity_manager_);
  SetCompanionAppInstalled(kCompanionAppId, true);
  Login(user_manager::UserType::kGuest);

  EXPECT_FALSE(install_companion_app_notification_shown_);
  EXPECT_FALSE(launch_companion_app_notification_shown_);
  companion_app_broker_->MaybeShowCompanionAppActions(test_device_);
  EXPECT_FALSE(install_companion_app_notification_shown_);
  EXPECT_FALSE(launch_companion_app_notification_shown_);
}

// If the app is installed, the user should be pointed directly toward it via
// the "Launch" notification
TEST_F(CompanionAppBrokerImplUnitTest, ShowLaunchCompanionApp_Installed) {
  base::test::ScopedFeatureList feature_list;
  feature_list.InitAndEnableFeatureWithParameters(
      ash::features::kFastPairPwaCompanion,
      {{ash::features::kFastPairPwaCompanionInstallUri.name,
        kValidCompanionBrowserUri},
       {ash::features::kFastPairPwaCompanionPlayStoreUri.name,
        kValidCompanionPlayStoreUri},
       {ash::features::kFastPairPwaCompanionAppId.name, kCompanionAppId}});

  SetIdentityManager(identity_manager_);
  SetCompanionAppInstalled(kCompanionAppId, true);
  Login(user_manager::UserType::kRegular);

  EXPECT_FALSE(install_companion_app_notification_shown_);
  EXPECT_FALSE(launch_companion_app_notification_shown_);
  companion_app_broker_->MaybeShowCompanionAppActions(test_device_);
  EXPECT_FALSE(install_companion_app_notification_shown_);
  EXPECT_TRUE(launch_companion_app_notification_shown_);
}

// When the app is not yet installed and there is no Play store link, users
// should be directed to the browser via "Launch" notification.
TEST_F(CompanionAppBrokerImplUnitTest, ShowLaunchCompanionApp_NoPlayStoreLink) {
  base::test::ScopedFeatureList feature_list;
  feature_list.InitAndEnableFeatureWithParameters(
      ash::features::kFastPairPwaCompanion,
      {{ash::features::kFastPairPwaCompanionInstallUri.name,
        kValidCompanionBrowserUri},
       {ash::features::kFastPairPwaCompanionPlayStoreUri.name,
        kEmptyCompanionPlayStoreUri},
       {ash::features::kFastPairPwaCompanionAppId.name, kCompanionAppId}});

  SetIdentityManager(identity_manager_);
  SetCompanionAppInstalled(kCompanionAppId, false);
  Login(user_manager::UserType::kRegular);

  EXPECT_FALSE(install_companion_app_notification_shown_);
  EXPECT_FALSE(launch_companion_app_notification_shown_);
  companion_app_broker_->MaybeShowCompanionAppActions(test_device_);
  EXPECT_FALSE(install_companion_app_notification_shown_);
  EXPECT_TRUE(launch_companion_app_notification_shown_);
}

// If no companion app information is provided for this device, the user cannot
// be directed to the app, so no notification will be shown.
TEST_F(CompanionAppBrokerImplUnitTest, NoCompanionAppNotification_NoAppInfo) {
  base::test::ScopedFeatureList feature_list;
  feature_list.InitAndEnableFeatureWithParameters(
      ash::features::kFastPairPwaCompanion,
      {{ash::features::kFastPairPwaCompanionInstallUri.name,
        kEmptyCompanionBrowserUri},
       {ash::features::kFastPairPwaCompanionPlayStoreUri.name,
        kEmptyCompanionPlayStoreUri},
       {ash::features::kFastPairPwaCompanionAppId.name, kCompanionAppId}});

  SetIdentityManager(identity_manager_);
  SetCompanionAppInstalled(kCompanionAppId, false);
  Login(user_manager::UserType::kRegular);

  EXPECT_FALSE(install_companion_app_notification_shown_);
  EXPECT_FALSE(launch_companion_app_notification_shown_);
  companion_app_broker_->MaybeShowCompanionAppActions(test_device_);
  EXPECT_FALSE(install_companion_app_notification_shown_);
  EXPECT_FALSE(launch_companion_app_notification_shown_);
}

// If the app is not yet installed, the Play store link takes precedence over
// the browser link.
TEST_F(CompanionAppBrokerImplUnitTest, ShowInstallCompanionApp_PlayStoreLink) {
  base::test::ScopedFeatureList feature_list;
  feature_list.InitAndEnableFeatureWithParameters(
      ash::features::kFastPairPwaCompanion,
      {{ash::features::kFastPairPwaCompanionInstallUri.name,
        kValidCompanionBrowserUri},
       {ash::features::kFastPairPwaCompanionPlayStoreUri.name,
        kValidCompanionPlayStoreUri},
       {ash::features::kFastPairPwaCompanionAppId.name, kCompanionAppId}});

  SetIdentityManager(identity_manager_);
  SetCompanionAppInstalled(kCompanionAppId, false);
  Login(user_manager::UserType::kRegular);

  EXPECT_FALSE(install_companion_app_notification_shown_);
  EXPECT_FALSE(launch_companion_app_notification_shown_);
  companion_app_broker_->MaybeShowCompanionAppActions(test_device_);
  EXPECT_TRUE(install_companion_app_notification_shown_);
  EXPECT_FALSE(launch_companion_app_notification_shown_);
}

TEST_F(CompanionAppBrokerImplUnitTest, InstallCompanionApp_Disabled) {
  base::test::ScopedFeatureList feature_list;
  feature_list.InitWithFeatures(
      /*enabled_features=*/{},
      /*disabled_features=*/{ash::features::kFastPairPwaCompanion});

  EXPECT_DEATH_IF_SUPPORTED(
      { companion_app_broker_->InstallCompanionApp(test_device_); }, "");
}

// TODO(b/290816916): Update with new logic to check device metadata.
// Ensures calling InstallCompanionApp with feature enabled does not crash.
TEST_F(CompanionAppBrokerImplUnitTest, InstallCompanionApp_Enabled) {
  base::test::ScopedFeatureList feature_list;
  feature_list.InitAndEnableFeatureWithParameters(
      ash::features::kFastPairPwaCompanion,
      {{ash::features::kFastPairPwaCompanionInstallUri.name,
        kValidCompanionBrowserUri},
       {ash::features::kFastPairPwaCompanionPlayStoreUri.name,
        kValidCompanionPlayStoreUri},
       {ash::features::kFastPairPwaCompanionAppId.name, kCompanionAppId}});

  companion_app_broker_->InstallCompanionApp(test_device_);
}

TEST_F(CompanionAppBrokerImplUnitTest, LaunchCompanionApp_Disabled) {
  base::test::ScopedFeatureList feature_list;
  feature_list.InitWithFeatures(
      /*enabled_features=*/{},
      /*disabled_features=*/{ash::features::kFastPairPwaCompanion});

  EXPECT_DEATH_IF_SUPPORTED(
      { companion_app_broker_->LaunchCompanionApp(test_device_); }, "");
}

// TODO(b/290816916): Update with new logic to check device metadata.
// Ensures calling LaunchCompanionApp with feature enabled does not crash.
TEST_F(CompanionAppBrokerImplUnitTest, LaunchCompanionApp_Enabled) {
  base::test::ScopedFeatureList feature_list;
  feature_list.InitAndEnableFeatureWithParameters(
      ash::features::kFastPairPwaCompanion,
      {{ash::features::kFastPairPwaCompanionInstallUri.name,
        kValidCompanionBrowserUri},
       {ash::features::kFastPairPwaCompanionPlayStoreUri.name,
        kValidCompanionPlayStoreUri},
       {ash::features::kFastPairPwaCompanionAppId.name, kCompanionAppId}});

  companion_app_broker_->LaunchCompanionApp(test_device_);
}

// If the app is not yet installed, the install playstore app prompt will be
// shown
TEST_F(CompanionAppBrokerImplUnitTest,
       ShowsInstallCompanionApp_UsingFeatureParamDeviceId) {
  base::test::ScopedFeatureList feature_list;
  feature_list.InitAndEnableFeatureWithParameters(
      ash::features::kFastPairPwaCompanion,
      {{ash::features::kFastPairPwaCompanionInstallUri.name,
        kValidCompanionBrowserUri},
       {ash::features::kFastPairPwaCompanionPlayStoreUri.name,
        kValidCompanionPlayStoreUri},
       {ash::features::kFastPairPwaCompanionAppId.name, kCompanionAppId},
       {ash::features::kFastPairPwaCompanionDeviceIds.name,
        kFeatureParamDeviceIds}});
  test_device_ = base::MakeRefCounted<Device>(
      kFeatureParamModelId, kTestDeviceAddress, Protocol::kFastPairInitial);

  SetIdentityManager(identity_manager_);
  SetCompanionAppInstalled(kCompanionAppId, false);
  Login(user_manager::UserType::kRegular);

  EXPECT_FALSE(install_companion_app_notification_shown_);
  EXPECT_FALSE(launch_companion_app_notification_shown_);
  companion_app_broker_->MaybeShowCompanionAppActions(test_device_);
  EXPECT_TRUE(install_companion_app_notification_shown_);
  EXPECT_FALSE(launch_companion_app_notification_shown_);
}

// If the device ID isn't supported the install app prompt will not be shown
TEST_F(CompanionAppBrokerImplUnitTest,
       SkipsInstallCompanionApp_UsingInvalidDeviceId) {
  base::test::ScopedFeatureList feature_list;
  feature_list.InitAndEnableFeatureWithParameters(
      ash::features::kFastPairPwaCompanion,
      {{ash::features::kFastPairPwaCompanionInstallUri.name,
        kValidCompanionBrowserUri},
       {ash::features::kFastPairPwaCompanionPlayStoreUri.name,
        kValidCompanionPlayStoreUri},
       {ash::features::kFastPairPwaCompanionAppId.name, kCompanionAppId},
       {ash::features::kFastPairPwaCompanionDeviceIds.name,
        kFeatureParamDeviceIds}});
  test_device_ = base::MakeRefCounted<Device>(
      kInvalidModelId, kTestDeviceAddress, Protocol::kFastPairInitial);

  SetIdentityManager(identity_manager_);
  SetCompanionAppInstalled(kCompanionAppId, false);
  Login(user_manager::UserType::kRegular);

  EXPECT_FALSE(install_companion_app_notification_shown_);
  EXPECT_FALSE(launch_companion_app_notification_shown_);
  companion_app_broker_->MaybeShowCompanionAppActions(test_device_);
  EXPECT_FALSE(install_companion_app_notification_shown_);
  EXPECT_FALSE(launch_companion_app_notification_shown_);
}

}  // namespace quick_pair
}  // namespace ash