File: pair_with_ui_interactive_uitest.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 (423 lines) | stat: -rw-r--r-- 15,088 bytes parent folder | download | duplicates (3)
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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <string>

#include "ash/ash_element_identifiers.h"
#include "ash/root_window_controller.h"
#include "ash/shelf/shelf.h"
#include "ash/shell.h"
#include "ash/strings/grit/ash_strings.h"
#include "ash/system/status_area_widget.h"
#include "ash/system/toast/system_toast_view.h"
#include "ash/system/unified/unified_system_tray.h"
#include "base/json/string_escape.h"
#include "base/memory/raw_ptr.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
#include "base/test/scoped_feature_list.h"
#include "base/test/test_future.h"
#include "base/time/time.h"
#include "chrome/browser/ui/webui/ash/bluetooth/bluetooth_pairing_dialog.h"
#include "chrome/grit/generated_resources.h"
#include "chrome/test/base/ash/interactive/bluetooth/bluetooth_power_state_observer.h"
#include "chrome/test/base/ash/interactive/bluetooth/bluetooth_util.h"
#include "chrome/test/base/ash/interactive/interactive_ash_test.h"
#include "chrome/test/base/ash/interactive/settings/interactive_uitest_elements.h"
#include "chrome/test/base/ash/interactive/webui/interactive_uitest_elements.h"
#include "device/bluetooth/bluetooth_adapter.h"
#include "device/bluetooth/bluetooth_adapter_factory.h"
#include "device/bluetooth/bluetooth_common.h"
#include "device/bluetooth/bluetooth_device.h"
#include "device/bluetooth/dbus/fake_bluetooth_device_client.h"
#include "device/bluetooth/floss/fake_floss_adapter_client.h"
#include "device/bluetooth/floss/fake_floss_battery_manager_client.h"
#include "device/bluetooth/floss/floss_dbus_manager.h"
#include "device/bluetooth/floss/floss_features.h"
#include "ui/base/interaction/element_identifier.h"
#include "ui/base/interaction/polling_state_observer.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/views/controls/label.h"

DEFINE_LOCAL_ELEMENT_IDENTIFIER_VALUE(kBluetoothPairingDialogElementId);
DEFINE_LOCAL_ELEMENT_IDENTIFIER_VALUE(kOSSettingsId);

namespace ash {
namespace {

// This JavaScript checks that the code for pairing a Bluetooth device is shown
// in the UI. This JavaScript expects `el` to be the root node on the code entry
// page, and checks that there is a <span> element for each digit in the code
// and for the button indicating the user should afterwards press 'enter'.
constexpr char kCheckPairingCodeJs[] = R"(
  (el) => {
    const code = '%s';
    const elements = el.querySelectorAll('div#code > span');
    if (elements.length != code.length + 1) {
      return false;
    }
    for (let i = 0; i < code.length; i++) {
      if (elements[i].innerText !== code[i]) {
        return false;
      }
    }
    return elements[elements.length - 1].innerText.indexOf('%s') != -1;
  })";

class PairWithUiInteractiveUiTest : public InteractiveAshTest {
 public:
  // InteractiveAshTest:
  void SetUpOnMainThread() override {
    InteractiveAshTest::SetUpOnMainThread();

    // Set up context for element tracking for InteractiveBrowserTest.
    SetupContextWidget();
  }

  WebContentsInteractionTestUtil::DeepQuery BluetoothPairingDeviceItem() {
    return WebContentsInteractionTestUtil::DeepQuery(
        {"bluetooth-pairing-device-item", "div#deviceName"});
  }

 protected:
  ui::test::internal::InteractiveTestPrivate::MultiStep
  OpenDialogAndClickDevice(const std::string& name) {
    DEFINE_LOCAL_STATE_IDENTIFIER_VALUE(BluetoothPowerStateObserver,
                                        kBluetoothPowerState);

    return Steps(
        ObserveState(kBluetoothPowerState,
                     BluetoothPowerStateObserver::Create()),

        Log("Waiting for Floss clients to be ready (if Floss is enabled)"),

        WaitForFlossClientsReady(),

        Log("Waiting for Bluetooth to be enabled"),

        WaitForState(kBluetoothPowerState, true),

        Log("Opening the Quick Settings bubble"),

        OpenQuickSettings(),

        Log("Navigating to the bluetooth detailed page"),

        NavigateQuickSettingsToBluetoothPage(),

        Log("Waiting for bluetooth toggle button to be shown and enabled"),

        WaitForShow(kBluetoothDetailedViewToggleElementId),
        WaitForViewProperty(kBluetoothDetailedViewToggleElementId, views::View,
                            Enabled, true),

        Log("Waiting for \"pair new device\" button to be be shown"),

        WaitForShow(kBluetoothDetailedViewPairNewDeviceElementId),

        Log("Clicking the \"pair new device\" button"),

        MoveMouseTo(kBluetoothDetailedViewPairNewDeviceElementId), ClickMouse(),

        Log("Waiting for the Quick Settings bubble to close"),

        WaitForHide(kQuickSettingsViewElementId),

        Log("Waiting for the pairing dialog to be visible"),

        InstrumentNonTabWebView(
            kBluetoothPairingDialogElementId,
            BluetoothPairingDialog::kBluetoothPairingDialogElementId),

        Log("Waiting for the device to be visible in the pairing dialog"),

        WaitForElementExists(kBluetoothPairingDialogElementId,
                             webui::bluetooth::PairingDialog()),

        Log(base::StringPrintf("Waiting for Bluetooth device '%s' to be found",
                               name.c_str())),

        WaitForAnyElementTextContains(
            kBluetoothPairingDialogElementId,
            webui::bluetooth::PairingDialogDeviceSelectionPage(),
            BluetoothPairingDeviceItem(),
            /*text=*/name),

        Log("Checking that the device is not paired"),

        CheckBluetoothDevicePairedState(name, /*paired=*/false),

        Log("Clicking the device"),

        ClickAnyElementTextContains(
            kBluetoothPairingDialogElementId,
            webui::bluetooth::PairingDialogDeviceSelectionPage(),
            BluetoothPairingDeviceItem(),
            /*text=*/name));
  }

  ui::test::internal::InteractiveTestPrivate::MultiStep
  CheckDeviceBecomesPaired(const std::string& name) {
    using Observer = views::test::PollingViewObserver<bool, SystemToastView>;
    DEFINE_LOCAL_STATE_IDENTIFIER_VALUE(Observer, kPollingViewState);

    return Steps(
        Log("Waiting for pairing dialog to close"),

        WaitForHide(kBluetoothPairingDialogElementId),

        Log("Waiting for a system toast to become visible and have the "
            "expected text"),

        PollView(
            kPollingViewState, SystemToastView::kSystemToastViewElementId,
            base::BindRepeating(
                [](const std::string name,
                   const SystemToastView* system_toast_view) -> bool {
                  return system_toast_view->GetText() ==
                         l10n_util::GetStringFUTF16(
                             IDS_ASH_STATUS_TRAY_BLUETOOTH_PAIRED_OR_CONNECTED_TOAST,
                             base::ASCIIToUTF16(name));
                },
                name),
            base::Milliseconds(50)),

        Log("Checking that the device became paired"),

        CheckBluetoothDevicePairedState(name, /*paired=*/true));
  }

  ui::test::internal::InteractiveTestPrivate::MultiStep
  PerformDeviceForgetSteps(const std::string& device_name) {
    return Steps(
        Log("Navigating to the Bluetooth device details page"),

        NavigateToBluetoothDeviceDetailsPage(kOSSettingsId, device_name),
        WaitForElementExists(
            kOSSettingsId,
            ash::settings::bluetooth::BluetoothForgetDeviceButton()),

        Log("Forgetting the Bluetooth device"),

        ClickElement(kOSSettingsId,
                     ash::settings::bluetooth::BluetoothForgetDeviceButton()),
        WaitForElementExists(kOSSettingsId,
                             ash::settings::bluetooth::BluetoothForgetDialog()),
        ClickElement(
            kOSSettingsId,
            ash::settings::bluetooth::BluetoothForgetDialogDoneButton()),

        Log("Verifying we are no longer in Bluetooth device details page"),

        WaitForElementDoesNotExist(
            kOSSettingsId, ash::settings::bluetooth::BluetoothForgetDialog()),
        WaitForElementDoesNotExist(
            kOSSettingsId,
            ash::settings::bluetooth::BluetoothForgetDeviceButton()),
        WaitForElementExists(kOSSettingsId,
                             ash::settings::bluetooth::BluetoothDeviceList()),

        CheckBluetoothDevicePairedState(device_name, /*paired=*/false));
  }

  ui::test::internal::InteractiveTestPrivate::MultiStep
  PerformCheckDeviceBattery(const std::string& device_name) {
    return Steps(
        Log("Navigating to the Bluetooth device details page"),

        NavigateToBluetoothDeviceDetailsPage(kOSSettingsId, device_name),
        WaitForElementExists(
            kOSSettingsId,
            ash::settings::bluetooth::BluetoothForgetDeviceButton()),

        Log("Checking battery percentage"),

        WaitForElementTextContains(
            kOSSettingsId,
            ash::settings::bluetooth::BluetoothBatteryPercentage(),
            base::NumberToString(floss::FakeFlossBatteryManagerClient::
                                     kDefaultBatteryPercentage)));
  }

  ui::test::internal::InteractiveTestPrivate::MultiStep
  PerformOpenPairingDialogSteps() {
    return Steps(
        Log("Navigating to the Bluetooth device details page"),

        NavigateSettingsToBluetoothPage(kOSSettingsId),
        WaitForElementExists(
            kOSSettingsId,
            ash::settings::bluetooth::BluetoothPairNewDeviceButton()),
        ClickElement(kOSSettingsId,
                     ash::settings::bluetooth::BluetoothPairNewDeviceButton()),
        WaitForElementExists(
            kOSSettingsId, ash::settings::bluetooth::BluetoothPairingDialog()));
  }

 protected:
  base::test::ScopedFeatureList feature_list_;

 private:
  ui::test::internal::InteractiveTestPrivate::MultiStep
  CheckBluetoothDevicePairedState(const std::string& name, bool paired) {
    return Steps(Do([name, paired]() {
      for (const auto& device : GetBluetoothAdapter()->GetDevices()) {
        EXPECT_TRUE(device->GetName() != name || device->IsPaired() == paired);
      }
    }));
  }

  // TODO(b/353285322): Remove this logic when we are able to rely solely on the
  // adapter powered state for whether we can continue the dependent tests.
  ui::test::internal::InteractiveTestPrivate::MultiStep
  WaitForFlossClientsReady() {
    DEFINE_LOCAL_STATE_IDENTIFIER_VALUE(ui::test::PollingStateObserver<bool>,
                                        kFlossClientsReady);
    return Steps(
        ObserveState(
            kFlossClientsReady,
            std::make_unique<ui::test::PollingStateObserver<bool>>(
                base::BindRepeating([]() {
                  return !floss::features::IsFlossEnabled() ||
                         (floss::FlossDBusManager::IsInitialized() &&
                          floss::FlossDBusManager::Get()->AreClientsReady());
                }))),
        WaitForState(kFlossClientsReady, true));
  }
};

class FlossPairWithUiInteractiveUiTest : public PairWithUiInteractiveUiTest {
 public:
  FlossPairWithUiInteractiveUiTest() {
    feature_list_.InitWithFeatures(
        /*enabled_features=*/{floss::features::kFlossEnabled},
        /*disabled_features=*/{});
  }
};

class BluezPairWithUiInteractiveUiTest : public PairWithUiInteractiveUiTest {
 public:
  BluezPairWithUiInteractiveUiTest() {
    // Use the legacy BlueZ bluetooth stack.
    feature_list_.InitAndDisableFeature(floss::features::kFlossEnabled);
  }
};

IN_PROC_BROWSER_TEST_F(FlossPairWithUiInteractiveUiTest, PairAndForget) {
  RunTestSequence(
      OpenDialogAndClickDevice(floss::FakeFlossAdapterClient::kJustWorksName),

      CheckDeviceBecomesPaired(floss::FakeFlossAdapterClient::kJustWorksName));

  InstallSystemApps();
  ui::ElementContext context =
      LaunchSystemWebApp(SystemWebAppType::SETTINGS, kOSSettingsId);

  RunTestSequenceInContext(
      context,

      PerformCheckDeviceBattery(floss::FakeFlossAdapterClient::kJustWorksName),

      PerformDeviceForgetSteps(floss::FakeFlossAdapterClient::kJustWorksName),

      Log("Battery percentage should not exist after forgetting devcei"),

      WaitForElementDoesNotExist(
          kOSSettingsId,
          ash::settings::bluetooth::BluetoothBatteryPercentage()),

      Log("Test complete"));
}

IN_PROC_BROWSER_TEST_F(FlossPairWithUiInteractiveUiTest, PairWithPinCode) {
  RunTestSequence(
      OpenDialogAndClickDevice(
          floss::FakeFlossAdapterClient::kPinCodeDisplayName),

      Log("Waiting to be prompted to enter the code"),

      WaitForElementExists(kBluetoothPairingDialogElementId,
                           webui::bluetooth::PairingDialogEnterCodePage()),

      Log("Checking that the expected code is shown"),

      CheckJsResultAt(
          kBluetoothPairingDialogElementId,
          webui::bluetooth::PairingDialogEnterCodePage(),
          base::StringPrintf(
              kCheckPairingCodeJs, floss::FakeFlossAdapterClient::kPinCode,
              l10n_util::GetStringUTF8(IDS_BLUETOOTH_PAIRING_ENTER_KEY)
                  .c_str())),

      Log("Simulating the pairing process"),

      Do([&]() {
        for (const auto& device : GetBluetoothAdapter()->GetDevices()) {
          if (device->GetName() ==
              floss::FakeFlossAdapterClient::kPinCodeDisplayName) {
            device->SetPinCode(floss::FakeFlossAdapterClient::kPinCode);
            break;
          }
        }
      }),

      CheckDeviceBecomesPaired(
          floss::FakeFlossAdapterClient::kPinCodeDisplayName),

      Log("Test complete"));
}

IN_PROC_BROWSER_TEST_F(BluezPairWithUiInteractiveUiTest, SimplePairAndForget) {
  RunTestSequence(OpenDialogAndClickDevice(
                      bluez::FakeBluetoothDeviceClient::kJustWorksName),

                  CheckDeviceBecomesPaired(
                      bluez::FakeBluetoothDeviceClient::kJustWorksName));

  InstallSystemApps();
  ui::ElementContext context =
      LaunchSystemWebApp(SystemWebAppType::SETTINGS, kOSSettingsId);

  RunTestSequenceInContext(
      context,

      PerformDeviceForgetSteps(floss::FakeFlossAdapterClient::kJustWorksName),

      Log("Test complete"));
}

IN_PROC_BROWSER_TEST_F(FlossPairWithUiInteractiveUiTest,
                       OpenPairingDialogFromOsSettings) {
  InstallSystemApps();
  ui::ElementContext context =
      LaunchSystemWebApp(SystemWebAppType::SETTINGS, kOSSettingsId);

  RunTestSequenceInContext(
      context,

      Log("Opening pairing dialog without pairing to a device"),

      PerformOpenPairingDialogSteps(),

      Log("Test complete"));
}

IN_PROC_BROWSER_TEST_F(BluezPairWithUiInteractiveUiTest,
                       OpenPairingDialogFromOsSettings) {
  InstallSystemApps();
  ui::ElementContext context =
      LaunchSystemWebApp(SystemWebAppType::SETTINGS, kOSSettingsId);

  RunTestSequenceInContext(
      context,

      Log("Opening pairing dialog without pairing to a device"),

      PerformOpenPairingDialogSteps(),

      Log("Test complete"));
}

}  // namespace
}  // namespace ash