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 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107
|
// 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 "base/time/time.h"
#include "chromeos/ash/components/audio/audio_device_selection_test_base.h"
#include "ash/constants/ash_features.h"
#include "base/test/metrics/user_action_tester.h"
#include "base/test/scoped_feature_list.h"
#include "chromeos/ash/components/audio/audio_device_encoding.h"
#include "chromeos/ash/components/audio/audio_device_metrics_handler.h"
#include "chromeos/ash/components/audio/cras_audio_handler.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace ash {
namespace {
// The bucket sample constant for testing the system switch or not switch
// decision after audio device has changed.
constexpr uint8_t kSystemNotSwitchSample = 0;
constexpr uint8_t kSystemSwitchSample = 1;
// Test the histogram of system switch or not switch decision after audio device
// has changed.
void ExpectSystemDecisionHistogramCount(
const base::HistogramTester& histogram_tester,
uint16_t expected_system_switch_input_count,
uint16_t expected_system_not_switch_input_count,
uint16_t expected_system_switch_output_count,
uint16_t expected_system_not_switch_output_count,
bool is_chrome_restarts) {
histogram_tester.ExpectBucketCount(
AudioDeviceMetricsHandler::kSystemSwitchInputAudio, kSystemSwitchSample,
expected_system_switch_input_count);
histogram_tester.ExpectBucketCount(
AudioDeviceMetricsHandler::kSystemSwitchInputAudio,
kSystemNotSwitchSample, expected_system_not_switch_input_count);
histogram_tester.ExpectBucketCount(
AudioDeviceMetricsHandler::kSystemSwitchOutputAudio, kSystemSwitchSample,
expected_system_switch_output_count);
histogram_tester.ExpectBucketCount(
AudioDeviceMetricsHandler::kSystemSwitchOutputAudio,
kSystemNotSwitchSample, expected_system_not_switch_output_count);
histogram_tester.ExpectBucketCount(
AudioDeviceMetricsHandler::kAudioSelectionPerformance,
AudioDeviceMetricsHandler::AudioSelectionEvents::kSystemSwitchInput,
expected_system_switch_input_count);
histogram_tester.ExpectBucketCount(
AudioDeviceMetricsHandler::kAudioSelectionPerformance,
AudioDeviceMetricsHandler::AudioSelectionEvents::kSystemNotSwitchInput,
expected_system_not_switch_input_count);
histogram_tester.ExpectBucketCount(
AudioDeviceMetricsHandler::kAudioSelectionPerformance,
AudioDeviceMetricsHandler::AudioSelectionEvents::kSystemSwitchOutput,
expected_system_switch_output_count);
histogram_tester.ExpectBucketCount(
AudioDeviceMetricsHandler::kAudioSelectionPerformance,
AudioDeviceMetricsHandler::AudioSelectionEvents::kSystemNotSwitchOutput,
expected_system_not_switch_output_count);
if (is_chrome_restarts) {
histogram_tester.ExpectBucketCount(
AudioDeviceMetricsHandler::kSystemSwitchInputAudioChromeRestarts,
kSystemSwitchSample, expected_system_switch_input_count);
histogram_tester.ExpectBucketCount(
AudioDeviceMetricsHandler::kSystemSwitchInputAudioChromeRestarts,
kSystemNotSwitchSample, expected_system_not_switch_input_count);
histogram_tester.ExpectBucketCount(
AudioDeviceMetricsHandler::kSystemSwitchOutputAudioChromeRestarts,
kSystemSwitchSample, expected_system_switch_output_count);
histogram_tester.ExpectBucketCount(
AudioDeviceMetricsHandler::kSystemSwitchOutputAudioChromeRestarts,
kSystemNotSwitchSample, expected_system_not_switch_output_count);
histogram_tester.ExpectBucketCount(
AudioDeviceMetricsHandler::kAudioSelectionPerformance,
AudioDeviceMetricsHandler::AudioSelectionEvents::
kSystemSwitchInputChromeRestart,
expected_system_switch_input_count);
histogram_tester.ExpectBucketCount(
AudioDeviceMetricsHandler::kAudioSelectionPerformance,
AudioDeviceMetricsHandler::AudioSelectionEvents::
kSystemNotSwitchInputChromeRestart,
expected_system_not_switch_input_count);
histogram_tester.ExpectBucketCount(
AudioDeviceMetricsHandler::kAudioSelectionPerformance,
AudioDeviceMetricsHandler::AudioSelectionEvents::
kSystemSwitchOutputChromeRestart,
expected_system_switch_output_count);
histogram_tester.ExpectBucketCount(
AudioDeviceMetricsHandler::kAudioSelectionPerformance,
AudioDeviceMetricsHandler::AudioSelectionEvents::
kSystemNotSwitchOutputChromeRestart,
expected_system_not_switch_output_count);
} else {
histogram_tester.ExpectBucketCount(
AudioDeviceMetricsHandler::kSystemSwitchInputAudioNonChromeRestarts,
kSystemSwitchSample, expected_system_switch_input_count);
histogram_tester.ExpectBucketCount(
AudioDeviceMetricsHandler::kSystemSwitchInputAudioNonChromeRestarts,
kSystemNotSwitchSample, expected_system_not_switch_input_count);
histogram_tester.ExpectBucketCount(
AudioDeviceMetricsHandler::kSystemSwitchOutputAudioNonChromeRestarts,
kSystemSwitchSample, expected_system_switch_output_count);
histogram_tester.ExpectBucketCount(
AudioDeviceMetricsHandler::kSystemSwitchOutputAudioNonChromeRestarts,
kSystemNotSwitchSample, expected_system_not_switch_output_count);
histogram_tester.ExpectBucketCount(
AudioDeviceMetricsHandler::kAudioSelectionPerformance,
AudioDeviceMetricsHandler::AudioSelectionEvents::
kSystemSwitchInputNonChromeRestart,
expected_system_switch_input_count);
histogram_tester.ExpectBucketCount(
AudioDeviceMetricsHandler::kAudioSelectionPerformance,
AudioDeviceMetricsHandler::AudioSelectionEvents::
kSystemNotSwitchInputNonChromeRestart,
expected_system_not_switch_input_count);
histogram_tester.ExpectBucketCount(
AudioDeviceMetricsHandler::kAudioSelectionPerformance,
AudioDeviceMetricsHandler::AudioSelectionEvents::
kSystemSwitchOutputNonChromeRestart,
expected_system_switch_output_count);
histogram_tester.ExpectBucketCount(
AudioDeviceMetricsHandler::kAudioSelectionPerformance,
AudioDeviceMetricsHandler::AudioSelectionEvents::
kSystemNotSwitchOutputNonChromeRestart,
expected_system_not_switch_output_count);
}
}
// Test the histogram of user override after system switch or not switch.
void ExpectUserOverrideSystemDecisionHistogramCount(
const base::HistogramTester& histogram_tester,
uint16_t expected_user_override_system_switch_input_count,
uint16_t expected_user_override_system_not_switch_input_count,
uint16_t expected_user_override_system_switch_output_count,
uint16_t expected_user_override_system_not_switch_output_count,
bool is_chrome_restarts) {
histogram_tester.ExpectTotalCount(
AudioDeviceMetricsHandler::kUserOverrideSystemSwitchInputAudio,
expected_user_override_system_switch_input_count);
histogram_tester.ExpectTotalCount(
AudioDeviceMetricsHandler::kUserOverrideSystemNotSwitchInputAudio,
expected_user_override_system_not_switch_input_count);
histogram_tester.ExpectTotalCount(
AudioDeviceMetricsHandler::kUserOverrideSystemSwitchOutputAudio,
expected_user_override_system_switch_output_count);
histogram_tester.ExpectTotalCount(
AudioDeviceMetricsHandler::kUserOverrideSystemNotSwitchOutputAudio,
expected_user_override_system_not_switch_output_count);
if (is_chrome_restarts) {
histogram_tester.ExpectTotalCount(
AudioDeviceMetricsHandler::
kUserOverrideSystemSwitchInputAudioChromeRestarts,
expected_user_override_system_switch_input_count);
histogram_tester.ExpectTotalCount(
AudioDeviceMetricsHandler::
kUserOverrideSystemNotSwitchInputAudioChromeRestarts,
expected_user_override_system_not_switch_input_count);
histogram_tester.ExpectTotalCount(
AudioDeviceMetricsHandler::
kUserOverrideSystemSwitchOutputAudioChromeRestarts,
expected_user_override_system_switch_output_count);
histogram_tester.ExpectTotalCount(
AudioDeviceMetricsHandler::
kUserOverrideSystemNotSwitchOutputAudioChromeRestarts,
expected_user_override_system_not_switch_output_count);
} else {
histogram_tester.ExpectTotalCount(
AudioDeviceMetricsHandler::
kUserOverrideSystemSwitchInputAudioNonChromeRestarts,
expected_user_override_system_switch_input_count);
histogram_tester.ExpectTotalCount(
AudioDeviceMetricsHandler::
kUserOverrideSystemNotSwitchInputAudioNonChromeRestarts,
expected_user_override_system_not_switch_input_count);
histogram_tester.ExpectTotalCount(
AudioDeviceMetricsHandler::
kUserOverrideSystemSwitchOutputAudioNonChromeRestarts,
expected_user_override_system_switch_output_count);
histogram_tester.ExpectTotalCount(
AudioDeviceMetricsHandler::
kUserOverrideSystemNotSwitchOutputAudioNonChromeRestarts,
expected_user_override_system_not_switch_output_count);
}
}
// Test the histogram of user override after system switch or not switch, these
// metrics are only fired within limited time frame.
void ExpectUserOverrideSystemDecisionHistogramCountInLimitedTime(
const base::HistogramTester& histogram_tester,
uint16_t expected_user_override_system_switch_input_count_in_limited_time,
uint16_t
expected_user_override_system_not_switch_input_count_in_limited_time,
uint16_t expected_user_override_system_switch_output_count_in_limited_time,
uint16_t
expected_user_override_system_not_switch_output_count_in_limited_time,
bool is_chrome_restarts) {
histogram_tester.ExpectBucketCount(
AudioDeviceMetricsHandler::kAudioSelectionPerformance,
AudioDeviceMetricsHandler::AudioSelectionEvents::
kUserOverrideSystemSwitchInput,
expected_user_override_system_switch_input_count_in_limited_time);
histogram_tester.ExpectBucketCount(
AudioDeviceMetricsHandler::kAudioSelectionPerformance,
AudioDeviceMetricsHandler::AudioSelectionEvents::
kUserOverrideSystemNotSwitchInput,
expected_user_override_system_not_switch_input_count_in_limited_time);
histogram_tester.ExpectBucketCount(
AudioDeviceMetricsHandler::kAudioSelectionPerformance,
AudioDeviceMetricsHandler::AudioSelectionEvents::
kUserOverrideSystemSwitchOutput,
expected_user_override_system_switch_output_count_in_limited_time);
histogram_tester.ExpectBucketCount(
AudioDeviceMetricsHandler::kAudioSelectionPerformance,
AudioDeviceMetricsHandler::AudioSelectionEvents::
kUserOverrideSystemNotSwitchOutput,
expected_user_override_system_not_switch_output_count_in_limited_time);
if (is_chrome_restarts) {
histogram_tester.ExpectBucketCount(
AudioDeviceMetricsHandler::kAudioSelectionPerformance,
AudioDeviceMetricsHandler::AudioSelectionEvents::
kUserOverrideSystemSwitchInputChromeRestart,
expected_user_override_system_switch_input_count_in_limited_time);
histogram_tester.ExpectBucketCount(
AudioDeviceMetricsHandler::kAudioSelectionPerformance,
AudioDeviceMetricsHandler::AudioSelectionEvents::
kUserOverrideSystemNotSwitchInputChromeRestart,
expected_user_override_system_not_switch_input_count_in_limited_time);
histogram_tester.ExpectBucketCount(
AudioDeviceMetricsHandler::kAudioSelectionPerformance,
AudioDeviceMetricsHandler::AudioSelectionEvents::
kUserOverrideSystemSwitchOutputChromeRestart,
expected_user_override_system_switch_output_count_in_limited_time);
histogram_tester.ExpectBucketCount(
AudioDeviceMetricsHandler::kAudioSelectionPerformance,
AudioDeviceMetricsHandler::AudioSelectionEvents::
kUserOverrideSystemNotSwitchOutputChromeRestart,
expected_user_override_system_not_switch_output_count_in_limited_time);
} else {
histogram_tester.ExpectBucketCount(
AudioDeviceMetricsHandler::kAudioSelectionPerformance,
AudioDeviceMetricsHandler::AudioSelectionEvents::
kUserOverrideSystemSwitchInputNonChromeRestart,
expected_user_override_system_switch_input_count_in_limited_time);
histogram_tester.ExpectBucketCount(
AudioDeviceMetricsHandler::kAudioSelectionPerformance,
AudioDeviceMetricsHandler::AudioSelectionEvents::
kUserOverrideSystemNotSwitchInputNonChromeRestart,
expected_user_override_system_not_switch_input_count_in_limited_time);
histogram_tester.ExpectBucketCount(
AudioDeviceMetricsHandler::kAudioSelectionPerformance,
AudioDeviceMetricsHandler::AudioSelectionEvents::
kUserOverrideSystemSwitchOutputNonChromeRestart,
expected_user_override_system_switch_output_count_in_limited_time);
histogram_tester.ExpectBucketCount(
AudioDeviceMetricsHandler::kAudioSelectionPerformance,
AudioDeviceMetricsHandler::AudioSelectionEvents::
kUserOverrideSystemNotSwitchOutputNonChromeRestart,
expected_user_override_system_not_switch_output_count_in_limited_time);
}
}
// Test the time delta histogram of user override after system switch or not
// switch.
void ExpectUserOverrideSystemDecisionTimeDelta(
const base::HistogramTester& histogram_tester,
bool is_input,
bool system_has_switched,
uint16_t delta_in_minute,
bool is_chrome_restarts) {
std::string histogram_name;
if (is_input) {
histogram_name =
system_has_switched
? AudioDeviceMetricsHandler::kUserOverrideSystemSwitchInputAudio
: AudioDeviceMetricsHandler::kUserOverrideSystemNotSwitchInputAudio;
} else {
histogram_name =
system_has_switched
? AudioDeviceMetricsHandler::kUserOverrideSystemSwitchOutputAudio
: AudioDeviceMetricsHandler::
kUserOverrideSystemNotSwitchOutputAudio;
}
histogram_tester.ExpectTimeBucketCount(
histogram_name,
base::Minutes(delta_in_minute) / base::Minutes(1).InMilliseconds(),
/*expected_count=*/1);
std::string histogram_name_separated_by_chrome_restarts;
if (is_chrome_restarts) {
if (is_input) {
histogram_name_separated_by_chrome_restarts =
system_has_switched
? AudioDeviceMetricsHandler::
kUserOverrideSystemSwitchInputAudioChromeRestarts
: AudioDeviceMetricsHandler::
kUserOverrideSystemNotSwitchInputAudioChromeRestarts;
} else {
histogram_name_separated_by_chrome_restarts =
system_has_switched
? AudioDeviceMetricsHandler::
kUserOverrideSystemSwitchOutputAudioChromeRestarts
: AudioDeviceMetricsHandler::
kUserOverrideSystemNotSwitchOutputAudioChromeRestarts;
}
} else {
if (is_input) {
histogram_name_separated_by_chrome_restarts =
system_has_switched
? AudioDeviceMetricsHandler::
kUserOverrideSystemSwitchInputAudioNonChromeRestarts
: AudioDeviceMetricsHandler::
kUserOverrideSystemNotSwitchInputAudioNonChromeRestarts;
} else {
histogram_name_separated_by_chrome_restarts =
system_has_switched
? AudioDeviceMetricsHandler::
kUserOverrideSystemSwitchOutputAudioNonChromeRestarts
: AudioDeviceMetricsHandler::
kUserOverrideSystemNotSwitchOutputAudioNonChromeRestarts;
}
}
histogram_tester.ExpectTimeBucketCount(
histogram_name_separated_by_chrome_restarts,
base::Minutes(delta_in_minute) / base::Minutes(1).InMilliseconds(),
/*expected_count=*/1);
}
// Test the histogram of consecutive decives change.
void ExpectConsecutiveDeviceChangeHistogramCount(
const base::HistogramTester& histogram_tester,
uint16_t expected_consecutive_input_device_changed,
uint16_t expected_consecutive_output_device_changed,
uint16_t expected_consecutive_input_device_added,
uint16_t expected_consecutive_output_device_added) {
histogram_tester.ExpectTotalCount(
AudioDeviceMetricsHandler::kConsecutiveInputDevicsChanged,
expected_consecutive_input_device_changed);
histogram_tester.ExpectTotalCount(
AudioDeviceMetricsHandler::kConsecutiveOutputDevicsChanged,
expected_consecutive_output_device_changed);
histogram_tester.ExpectTotalCount(
AudioDeviceMetricsHandler::kConsecutiveInputDevicsAdded,
expected_consecutive_input_device_added);
histogram_tester.ExpectTotalCount(
AudioDeviceMetricsHandler::kConsecutiveOutputDevicsAdded,
expected_consecutive_output_device_added);
}
class AudioDeviceSelectionTest : public AudioDeviceSelectionTestBase {};
TEST_F(AudioDeviceSelectionTest, PlugUnplugMetricAction) {
AudioNode input1 = NewInputNode("USB");
AudioNode input2 = NewInputNode("USB");
AudioNode output3 = NewOutputNode("USB");
AudioNode output4 = NewOutputNode("USB");
{
base::UserActionTester actions;
Plug(input1);
Plug(output3);
ASSERT_EQ(ActiveInputNodeId(), input1.id);
ASSERT_EQ(ActiveOutputNodeId(), output3.id);
Plug(input2);
Plug(output4);
ASSERT_EQ(ActiveInputNodeId(), input2.id);
ASSERT_EQ(ActiveOutputNodeId(), output4.id);
// Automatic switches should not generate events.
EXPECT_EQ(actions.GetActionCount(
AudioDeviceMetricsHandler::kUserActionSwitchInput),
0);
EXPECT_EQ(actions.GetActionCount(
AudioDeviceMetricsHandler::kUserActionSwitchOutput),
0);
EXPECT_EQ(actions.GetActionCount(
AudioDeviceMetricsHandler::kUserActionSwitchInputOverridden),
0);
EXPECT_EQ(actions.GetActionCount(
AudioDeviceMetricsHandler::kUserActionSwitchOutputOverridden),
0);
}
{
base::UserActionTester actions;
Select(input1);
ASSERT_EQ(ActiveInputNodeId(), input1.id);
ASSERT_EQ(ActiveOutputNodeId(), output4.id);
EXPECT_EQ(actions.GetActionCount(
AudioDeviceMetricsHandler::kUserActionSwitchInput),
1);
EXPECT_EQ(actions.GetActionCount(
AudioDeviceMetricsHandler::kUserActionSwitchOutput),
0);
EXPECT_EQ(actions.GetActionCount(
AudioDeviceMetricsHandler::kUserActionSwitchInputOverridden),
1);
EXPECT_EQ(actions.GetActionCount(
AudioDeviceMetricsHandler::kUserActionSwitchOutputOverridden),
0);
}
{
base::UserActionTester actions;
Select(output3);
ASSERT_EQ(ActiveInputNodeId(), input1.id);
ASSERT_EQ(ActiveOutputNodeId(), output3.id);
EXPECT_EQ(actions.GetActionCount(
AudioDeviceMetricsHandler::kUserActionSwitchInput),
0);
EXPECT_EQ(actions.GetActionCount(
AudioDeviceMetricsHandler::kUserActionSwitchOutput),
1);
EXPECT_EQ(actions.GetActionCount(
AudioDeviceMetricsHandler::kUserActionSwitchInputOverridden),
0);
EXPECT_EQ(actions.GetActionCount(
AudioDeviceMetricsHandler::kUserActionSwitchOutputOverridden),
1);
}
{
base::UserActionTester actions;
Select(input2);
Select(output4);
ASSERT_EQ(ActiveInputNodeId(), input2.id);
ASSERT_EQ(ActiveOutputNodeId(), output4.id);
EXPECT_EQ(actions.GetActionCount(
AudioDeviceMetricsHandler::kUserActionSwitchInput),
1);
EXPECT_EQ(actions.GetActionCount(
AudioDeviceMetricsHandler::kUserActionSwitchOutput),
1);
// Switching back and forth should not be counted.
EXPECT_EQ(actions.GetActionCount(
AudioDeviceMetricsHandler::kUserActionSwitchInputOverridden),
0);
EXPECT_EQ(actions.GetActionCount(
AudioDeviceMetricsHandler::kUserActionSwitchOutputOverridden),
0);
}
{
base::UserActionTester actions;
Unplug(input1);
Plug(input1);
ASSERT_EQ(ActiveInputNodeId(), input2.id);
Select(input1);
EXPECT_EQ(actions.GetActionCount(
AudioDeviceMetricsHandler::kUserActionSwitchInput),
1);
// Switching after the system decides to do nothing, should be counted.
EXPECT_EQ(actions.GetActionCount(
AudioDeviceMetricsHandler::kUserActionSwitchInputOverridden),
1);
}
{
base::UserActionTester actions;
Unplug(output3);
Plug(output3);
ASSERT_EQ(ActiveOutputNodeId(), output4.id);
Select(output3);
EXPECT_EQ(actions.GetActionCount(
AudioDeviceMetricsHandler::kUserActionSwitchOutput),
1);
// Switching after the system decides to do nothing, should be counted.
EXPECT_EQ(actions.GetActionCount(
AudioDeviceMetricsHandler::kUserActionSwitchOutputOverridden),
1);
}
}
TEST_F(AudioDeviceSelectionTest, PlugUnplugHistogramMetrics) {
// Time delta constant for testing the user override system decision.
constexpr uint16_t kTimeDeltaInMinuteA = 2;
constexpr uint16_t kTimeDeltaInMinuteB = 30;
constexpr uint16_t kTimeDeltaInMinuteC = 200;
AudioNode input_internal = NewInputNode("INTERNAL_MIC");
AudioNode input_USB = NewInputNode("USB");
AudioNode input_bluetooth_nb = NewInputNode("BLUETOOTH_NB_MIC");
AudioNode output_internal = NewOutputNode("INTERNAL_SPEAKER");
AudioNode output_USB = NewOutputNode("USB");
uint16_t expected_system_switch_input_count = 0;
uint16_t expected_system_not_switch_input_count = 0;
uint16_t expected_system_switch_output_count = 0;
uint16_t expected_system_not_switch_output_count = 0;
uint16_t expected_user_override_system_switch_input_count = 0;
uint16_t expected_user_override_system_not_switch_input_count = 0;
uint16_t expected_user_override_system_switch_output_count = 0;
uint16_t expected_user_override_system_not_switch_output_count = 0;
uint16_t expected_user_override_system_switch_input_count_in_limited_time = 0;
uint16_t
expected_user_override_system_not_switch_input_count_in_limited_time = 0;
uint16_t expected_user_override_system_switch_output_count_in_limited_time =
0;
uint16_t
expected_user_override_system_not_switch_output_count_in_limited_time = 0;
uint16_t num_of_input_devices = 0;
uint16_t num_of_output_devices = 0;
uint16_t expected_consecutive_input_device_changed = 0;
uint16_t expected_consecutive_output_device_changed = 0;
uint16_t expected_consecutive_input_device_added = 0;
uint16_t expected_consecutive_output_device_added = 0;
// Plug in internal mic and speaker.
// Do not record if there is no alternative device available.
Plug(input_internal);
Plug(output_internal);
num_of_input_devices++;
num_of_output_devices++;
ExpectSystemDecisionHistogramCount(
histogram_tester(), expected_system_switch_input_count,
expected_system_not_switch_input_count,
expected_system_switch_output_count,
expected_system_not_switch_output_count, /*is_chrome_restarts=*/false);
ExpectConsecutiveDeviceChangeHistogramCount(
histogram_tester(), expected_consecutive_input_device_changed,
expected_consecutive_output_device_changed,
expected_consecutive_input_device_added,
expected_consecutive_output_device_added);
// Plug in USB devices with higher priority than current active one.
// Expect to record system has switched both input and output.
Plug(input_USB);
Plug(output_USB);
num_of_input_devices++;
num_of_output_devices++;
ExpectSystemDecisionHistogramCount(
histogram_tester(), ++expected_system_switch_input_count,
expected_system_not_switch_input_count,
++expected_system_switch_output_count,
expected_system_not_switch_output_count, /*is_chrome_restarts=*/false);
histogram_tester().ExpectBucketCount(
AudioDeviceMetricsHandler::kSystemSwitchInputAudioDeviceCount,
num_of_input_devices, /*bucket_count=*/1);
histogram_tester().ExpectBucketCount(
AudioDeviceMetricsHandler::kSystemSwitchOutputAudioDeviceCount,
num_of_output_devices, /*bucket_count=*/1);
histogram_tester().ExpectBucketCount(
AudioDeviceMetricsHandler::
kSystemSwitchInputAudioDeviceCountNonChromeRestarts,
num_of_input_devices, /*bucket_count=*/1);
histogram_tester().ExpectBucketCount(
AudioDeviceMetricsHandler::
kSystemSwitchOutputAudioDeviceCountNonChromeRestarts,
num_of_output_devices, /*bucket_count=*/1);
histogram_tester().ExpectBucketCount(
AudioDeviceMetricsHandler::kSystemSwitchInputAudioDeviceSet,
EncodeAudioDeviceSet(
{AudioDevice(input_internal), AudioDevice(input_USB)}),
/*bucket_count=*/1);
histogram_tester().ExpectBucketCount(
AudioDeviceMetricsHandler::kSystemSwitchOutputAudioDeviceSet,
EncodeAudioDeviceSet(
{AudioDevice(output_internal), AudioDevice(output_USB)}),
/*bucket_count=*/1);
histogram_tester().ExpectBucketCount(
AudioDeviceMetricsHandler::
kSystemSwitchInputAudioDeviceSetNonChromeRestarts,
EncodeAudioDeviceSet(
{AudioDevice(output_internal), AudioDevice(output_USB)}),
/*bucket_count=*/1);
histogram_tester().ExpectBucketCount(
AudioDeviceMetricsHandler::
kSystemSwitchOutputAudioDeviceSetNonChromeRestarts,
EncodeAudioDeviceSet(
{AudioDevice(output_internal), AudioDevice(output_USB)}),
/*bucket_count=*/1);
histogram_tester().ExpectBucketCount(
AudioDeviceMetricsHandler::kSystemSwitchInputBeforeAndAfterAudioDeviceSet,
EncodeBeforeAndAfterAudioDeviceSets(
/*device_set_before=*/{AudioDevice(output_internal)},
/*device_set_after=*/{AudioDevice(output_internal),
AudioDevice(output_USB)}),
/*bucket_count=*/1);
histogram_tester().ExpectBucketCount(
AudioDeviceMetricsHandler::
kSystemSwitchOutputBeforeAndAfterAudioDeviceSet,
EncodeBeforeAndAfterAudioDeviceSets(
/*device_set_before=*/{AudioDevice(output_internal)},
/*device_set_after=*/{AudioDevice(output_internal),
AudioDevice(output_USB)}),
/*bucket_count=*/1);
histogram_tester().ExpectBucketCount(
AudioDeviceMetricsHandler::
kSystemSwitchInputBeforeAndAfterAudioDeviceSetNonChromeRestarts,
EncodeBeforeAndAfterAudioDeviceSets(
/*device_set_before=*/{AudioDevice(output_internal)},
/*device_set_after=*/{AudioDevice(output_internal),
AudioDevice(output_USB)}),
/*bucket_count=*/1);
histogram_tester().ExpectBucketCount(
AudioDeviceMetricsHandler::
kSystemSwitchOutputBeforeAndAfterAudioDeviceSetNonChromeRestarts,
EncodeBeforeAndAfterAudioDeviceSets(
/*device_set_before=*/{AudioDevice(output_internal)},
/*device_set_after=*/{AudioDevice(output_internal),
AudioDevice(output_USB)}),
/*bucket_count=*/1);
ExpectConsecutiveDeviceChangeHistogramCount(
histogram_tester(), ++expected_consecutive_input_device_changed,
++expected_consecutive_output_device_changed,
++expected_consecutive_input_device_added,
++expected_consecutive_output_device_added);
// User switches input device immediately.
// Expect to record user overrides system decision of switching input
// device.
Select(input_internal);
histogram_tester().ExpectBucketCount(
AudioDeviceMetricsHandler::
kUserOverrideSystemSwitchInputBeforeAndAfterAudioDeviceSet,
EncodeBeforeAndAfterAudioDeviceSets(
/*device_set_before=*/{AudioDevice(input_internal)},
/*device_set_after=*/{AudioDevice(input_internal),
AudioDevice(input_USB)}),
/*bucket_count=*/1);
ExpectUserOverrideSystemDecisionHistogramCount(
histogram_tester(), ++expected_user_override_system_switch_input_count,
expected_user_override_system_not_switch_input_count,
expected_user_override_system_switch_output_count,
expected_user_override_system_not_switch_output_count,
/*is_chrome_restarts=*/false);
ExpectUserOverrideSystemDecisionHistogramCountInLimitedTime(
histogram_tester(),
++expected_user_override_system_switch_input_count_in_limited_time,
expected_user_override_system_not_switch_input_count_in_limited_time,
expected_user_override_system_switch_output_count_in_limited_time,
expected_user_override_system_not_switch_output_count_in_limited_time,
/*is_chrome_restarts=*/false);
ExpectUserOverrideSystemDecisionTimeDelta(
histogram_tester(), /*is_input=*/true, /*system_has_switched=*/true,
/*delta_in_minute=*/0, /*is_chrome_restarts=*/false);
// User switches output device after some time.
// Expect to record user overrides system decision of switching output
// device.
FastForwardBy(base::Minutes(kTimeDeltaInMinuteA));
Select(output_internal);
histogram_tester().ExpectBucketCount(
AudioDeviceMetricsHandler::
kUserOverrideSystemSwitchOutputBeforeAndAfterAudioDeviceSet,
EncodeBeforeAndAfterAudioDeviceSets(
/*device_set_before=*/{AudioDevice(output_internal)},
/*device_set_after=*/{AudioDevice(output_internal),
AudioDevice(output_USB)}),
/*bucket_count=*/1);
ExpectUserOverrideSystemDecisionHistogramCount(
histogram_tester(), expected_user_override_system_switch_input_count,
expected_user_override_system_not_switch_input_count,
++expected_user_override_system_switch_output_count,
expected_user_override_system_not_switch_output_count,
/*is_chrome_restarts=*/false);
ExpectUserOverrideSystemDecisionHistogramCountInLimitedTime(
histogram_tester(),
expected_user_override_system_switch_input_count_in_limited_time,
expected_user_override_system_not_switch_input_count_in_limited_time,
++expected_user_override_system_switch_output_count_in_limited_time,
expected_user_override_system_not_switch_output_count_in_limited_time,
/*is_chrome_restarts=*/false);
ExpectUserOverrideSystemDecisionTimeDelta(
histogram_tester(), /*is_input=*/false, /*system_has_switched=*/true,
/*delta_in_minute=*/kTimeDeltaInMinuteA, /*is_chrome_restarts=*/false);
// User switches output device again.
// Do not record since user has just switched output device previously and
// there is no system switch or not switch decision in between.
Select(output_USB);
histogram_tester().ExpectBucketCount(
AudioDeviceMetricsHandler::
kUserOverrideSystemSwitchOutputBeforeAndAfterAudioDeviceSet,
EncodeBeforeAndAfterAudioDeviceSets(
/*device_set_before=*/{AudioDevice(output_internal)},
/*device_set_after=*/{AudioDevice(output_internal),
AudioDevice(output_USB)}),
/*bucket_count=*/1);
ExpectUserOverrideSystemDecisionHistogramCount(
histogram_tester(), expected_user_override_system_switch_input_count,
expected_user_override_system_not_switch_input_count,
expected_user_override_system_switch_output_count,
expected_user_override_system_not_switch_output_count,
/*is_chrome_restarts=*/false);
ExpectUserOverrideSystemDecisionHistogramCountInLimitedTime(
histogram_tester(),
expected_user_override_system_switch_input_count_in_limited_time,
expected_user_override_system_not_switch_input_count_in_limited_time,
expected_user_override_system_switch_output_count_in_limited_time,
expected_user_override_system_not_switch_output_count_in_limited_time,
/*is_chrome_restarts=*/false);
// Plug in a bluetooth nb mic with lower priority than current active one.
// Expect to record system does not switch input.
Plug(input_bluetooth_nb);
num_of_input_devices++;
ExpectSystemDecisionHistogramCount(
histogram_tester(), expected_system_switch_input_count,
++expected_system_not_switch_input_count,
expected_system_switch_output_count,
expected_system_not_switch_output_count, /*is_chrome_restarts=*/false);
histogram_tester().ExpectBucketCount(
AudioDeviceMetricsHandler::kSystemNotSwitchInputAudioDeviceCount,
num_of_input_devices, /*bucket_count=*/1);
histogram_tester().ExpectBucketCount(
AudioDeviceMetricsHandler::
kSystemNotSwitchInputAudioDeviceCountNonChromeRestarts,
num_of_input_devices, /*bucket_count=*/1);
histogram_tester().ExpectBucketCount(
AudioDeviceMetricsHandler::kSystemNotSwitchInputAudioDeviceSet,
EncodeAudioDeviceSet({AudioDevice(input_internal), AudioDevice(input_USB),
AudioDevice(input_bluetooth_nb)}),
/*bucket_count=*/1);
histogram_tester().ExpectBucketCount(
AudioDeviceMetricsHandler::
kSystemNotSwitchInputAudioDeviceSetNonChromeRestarts,
EncodeAudioDeviceSet({AudioDevice(input_internal), AudioDevice(input_USB),
AudioDevice(input_bluetooth_nb)}),
/*bucket_count=*/1);
histogram_tester().ExpectBucketCount(
AudioDeviceMetricsHandler::
kSystemNotSwitchInputBeforeAndAfterAudioDeviceSet,
EncodeBeforeAndAfterAudioDeviceSets(
/*device_set_before=*/{AudioDevice(output_internal),
AudioDevice(output_USB)},
/*device_set_after=*/{AudioDevice(output_internal),
AudioDevice(output_USB),
AudioDevice(input_bluetooth_nb)}),
/*bucket_count=*/1);
histogram_tester().ExpectBucketCount(
AudioDeviceMetricsHandler::
kSystemNotSwitchInputBeforeAndAfterAudioDeviceSetNonChromeRestarts,
EncodeBeforeAndAfterAudioDeviceSets(
/*device_set_before=*/{AudioDevice(output_internal),
AudioDevice(output_USB)},
/*device_set_after=*/{AudioDevice(output_internal),
AudioDevice(output_USB),
AudioDevice(input_bluetooth_nb)}),
/*bucket_count=*/1);
ExpectConsecutiveDeviceChangeHistogramCount(
histogram_tester(), ++expected_consecutive_input_device_changed,
expected_consecutive_output_device_changed,
++expected_consecutive_input_device_added,
expected_consecutive_output_device_added);
// User switches to USB input after some time.
// Expect to record user overrides system decision of not switching input
// device.
FastForwardBy(base::Minutes(kTimeDeltaInMinuteB));
Select(input_USB);
histogram_tester().ExpectBucketCount(
AudioDeviceMetricsHandler::
kUserOverrideSystemNotSwitchInputBeforeAndAfterAudioDeviceSet,
EncodeBeforeAndAfterAudioDeviceSets(
/*device_set_before=*/{AudioDevice(input_internal),
AudioDevice(input_USB)},
/*device_set_after=*/{AudioDevice(input_internal),
AudioDevice(input_USB),
AudioDevice(input_bluetooth_nb)}),
/*bucket_count=*/1);
ExpectUserOverrideSystemDecisionHistogramCount(
histogram_tester(), expected_user_override_system_switch_input_count,
++expected_user_override_system_not_switch_input_count,
expected_user_override_system_switch_output_count,
expected_user_override_system_not_switch_output_count,
/*is_chrome_restarts=*/false);
ExpectUserOverrideSystemDecisionHistogramCountInLimitedTime(
histogram_tester(),
expected_user_override_system_switch_input_count_in_limited_time,
++expected_user_override_system_not_switch_input_count_in_limited_time,
expected_user_override_system_switch_output_count_in_limited_time,
expected_user_override_system_not_switch_output_count_in_limited_time,
/*is_chrome_restarts=*/false);
ExpectUserOverrideSystemDecisionTimeDelta(
histogram_tester(), /*is_input=*/true, /*system_has_switched=*/false,
/*delta_in_minute=*/kTimeDeltaInMinuteB, /*is_chrome_restarts=*/false);
// User unplugs current active device USB input.
// Expect to record system has switched input.
Unplug(input_USB);
num_of_input_devices--;
ExpectSystemDecisionHistogramCount(
histogram_tester(), ++expected_system_switch_input_count,
expected_system_not_switch_input_count,
expected_system_switch_output_count,
expected_system_not_switch_output_count, /*is_chrome_restarts=*/false);
histogram_tester().ExpectBucketCount(
AudioDeviceMetricsHandler::kSystemSwitchInputAudioDeviceCount,
num_of_input_devices, /*bucket_count=*/2);
histogram_tester().ExpectBucketCount(
AudioDeviceMetricsHandler::
kSystemSwitchInputAudioDeviceCountNonChromeRestarts,
num_of_input_devices, /*bucket_count=*/2);
histogram_tester().ExpectBucketCount(
AudioDeviceMetricsHandler::kSystemSwitchInputAudioDeviceSet,
EncodeAudioDeviceSet(
{AudioDevice(input_internal), AudioDevice(input_bluetooth_nb)}),
/*bucket_count=*/1);
histogram_tester().ExpectBucketCount(
AudioDeviceMetricsHandler::
kSystemSwitchInputAudioDeviceSetNonChromeRestarts,
EncodeAudioDeviceSet(
{AudioDevice(input_internal), AudioDevice(input_bluetooth_nb)}),
/*bucket_count=*/1);
histogram_tester().ExpectBucketCount(
AudioDeviceMetricsHandler::kSystemSwitchInputBeforeAndAfterAudioDeviceSet,
EncodeBeforeAndAfterAudioDeviceSets(
/*device_set_before=*/{AudioDevice(input_internal),
AudioDevice(input_bluetooth_nb),
AudioDevice(input_USB)},
/*device_set_after=*/{AudioDevice(input_internal),
AudioDevice(input_bluetooth_nb)}),
/*bucket_count=*/1);
histogram_tester().ExpectBucketCount(
AudioDeviceMetricsHandler::
kSystemSwitchInputBeforeAndAfterAudioDeviceSetNonChromeRestarts,
EncodeBeforeAndAfterAudioDeviceSets(
/*device_set_before=*/{AudioDevice(input_internal),
AudioDevice(input_bluetooth_nb),
AudioDevice(input_USB)},
/*device_set_after=*/{AudioDevice(input_internal),
AudioDevice(input_bluetooth_nb)}),
/*bucket_count=*/1);
ExpectConsecutiveDeviceChangeHistogramCount(
histogram_tester(), ++expected_consecutive_input_device_changed,
expected_consecutive_output_device_changed,
expected_consecutive_input_device_added,
expected_consecutive_output_device_added);
// User switches to input_bluetooth_nb after some time.
// Expect to record user overrides system decision of switching input device.
FastForwardBy(base::Minutes(kTimeDeltaInMinuteC));
Select(input_bluetooth_nb);
histogram_tester().ExpectBucketCount(
AudioDeviceMetricsHandler::
kUserOverrideSystemSwitchInputBeforeAndAfterAudioDeviceSet,
EncodeBeforeAndAfterAudioDeviceSets(
/*device_set_before=*/{AudioDevice(input_internal),
AudioDevice(input_bluetooth_nb),
AudioDevice(input_USB)},
/*device_set_after=*/{AudioDevice(input_internal),
AudioDevice(input_bluetooth_nb)}),
/*bucket_count=*/1);
ExpectUserOverrideSystemDecisionHistogramCount(
histogram_tester(), ++expected_user_override_system_switch_input_count,
expected_user_override_system_not_switch_input_count,
expected_user_override_system_switch_output_count,
expected_user_override_system_not_switch_output_count,
/*is_chrome_restarts=*/false);
ExpectUserOverrideSystemDecisionHistogramCountInLimitedTime(
histogram_tester(),
expected_user_override_system_switch_input_count_in_limited_time,
expected_user_override_system_not_switch_input_count_in_limited_time,
expected_user_override_system_switch_output_count_in_limited_time,
expected_user_override_system_not_switch_output_count_in_limited_time,
/*is_chrome_restarts=*/false);
ExpectUserOverrideSystemDecisionTimeDelta(
histogram_tester(), /*is_input=*/true, /*system_has_switched=*/true,
/*delta_in_minute=*/kTimeDeltaInMinuteC, /*is_chrome_restarts=*/false);
// User unplugs active device input_bluetooth_nb.
// Do not record if there is no alternative device available.
Unplug(input_bluetooth_nb);
ExpectSystemDecisionHistogramCount(
histogram_tester(), expected_system_switch_input_count,
expected_system_not_switch_input_count,
expected_system_switch_output_count,
expected_system_not_switch_output_count, /*is_chrome_restarts=*/false);
ExpectConsecutiveDeviceChangeHistogramCount(
histogram_tester(), ++expected_consecutive_input_device_changed,
expected_consecutive_output_device_changed,
expected_consecutive_input_device_added,
expected_consecutive_output_device_added);
}
TEST_F(AudioDeviceSelectionTest, SystemBootsHistogramMetrics) {
AudioNode input_internal = NewInputNode("INTERNAL_MIC");
AudioNode input_USB = NewInputNode("USB");
AudioNode output_internal = NewOutputNode("INTERNAL_SPEAKER");
AudioNode output_USB = NewOutputNode("USB");
uint16_t expected_system_switch_input_count = 0;
uint16_t expected_system_not_switch_input_count = 0;
uint16_t expected_system_switch_output_count = 0;
uint16_t expected_system_not_switch_output_count = 0;
uint16_t num_of_input_devices = 0;
uint16_t num_of_output_devices = 0;
uint16_t expected_user_override_system_switch_input_count = 0;
uint16_t expected_user_override_system_not_switch_input_count = 0;
uint16_t expected_user_override_system_switch_output_count = 0;
uint16_t expected_user_override_system_not_switch_output_count = 0;
uint16_t expected_user_override_system_switch_input_count_in_limited_time = 0;
uint16_t
expected_user_override_system_not_switch_input_count_in_limited_time = 0;
uint16_t expected_user_override_system_switch_output_count_in_limited_time =
0;
uint16_t
expected_user_override_system_not_switch_output_count_in_limited_time = 0;
// System boots with multiple audio devices.
// Expect to record system has switched both input and output.
SystemBootsWith({input_internal, input_USB, output_internal, output_USB});
num_of_input_devices += 2;
num_of_output_devices += 2;
ExpectSystemDecisionHistogramCount(
histogram_tester(), ++expected_system_switch_input_count,
expected_system_not_switch_input_count,
++expected_system_switch_output_count,
expected_system_not_switch_output_count, /*is_chrome_restarts=*/true);
histogram_tester().ExpectBucketCount(
AudioDeviceMetricsHandler::kSystemSwitchInputAudioDeviceCount,
num_of_input_devices, /*bucket_count=*/1);
histogram_tester().ExpectBucketCount(
AudioDeviceMetricsHandler::kSystemSwitchOutputAudioDeviceCount,
num_of_output_devices, /*bucket_count=*/1);
histogram_tester().ExpectBucketCount(
AudioDeviceMetricsHandler::
kSystemSwitchInputAudioDeviceCountChromeRestarts,
num_of_input_devices, /*bucket_count=*/1);
histogram_tester().ExpectBucketCount(
AudioDeviceMetricsHandler::
kSystemSwitchOutputAudioDeviceCountChromeRestarts,
num_of_output_devices, /*bucket_count=*/1);
histogram_tester().ExpectBucketCount(
AudioDeviceMetricsHandler::kSystemSwitchInputAudioDeviceSet,
EncodeAudioDeviceSet(
{AudioDevice(input_internal), AudioDevice(input_USB)}),
/*bucket_count=*/1);
histogram_tester().ExpectBucketCount(
AudioDeviceMetricsHandler::kSystemSwitchOutputAudioDeviceSet,
EncodeAudioDeviceSet(
{AudioDevice(input_internal), AudioDevice(input_USB)}),
/*bucket_count=*/1);
histogram_tester().ExpectBucketCount(
AudioDeviceMetricsHandler::kSystemSwitchInputAudioDeviceSetChromeRestarts,
EncodeAudioDeviceSet(
{AudioDevice(output_internal), AudioDevice(output_USB)}),
/*bucket_count=*/1);
histogram_tester().ExpectBucketCount(
AudioDeviceMetricsHandler::
kSystemSwitchOutputAudioDeviceSetChromeRestarts,
EncodeAudioDeviceSet(
{AudioDevice(output_internal), AudioDevice(output_USB)}),
/*bucket_count=*/1);
histogram_tester().ExpectBucketCount(
AudioDeviceMetricsHandler::kSystemSwitchInputBeforeAndAfterAudioDeviceSet,
EncodeBeforeAndAfterAudioDeviceSets(
/*device_set_before=*/{},
/*device_set_after=*/{AudioDevice(input_internal),
AudioDevice(input_USB)}),
/*bucket_count=*/1);
histogram_tester().ExpectBucketCount(
AudioDeviceMetricsHandler::
kSystemSwitchOutputBeforeAndAfterAudioDeviceSet,
EncodeBeforeAndAfterAudioDeviceSets(
/*device_set_before=*/{},
/*device_set_after=*/{AudioDevice(input_internal),
AudioDevice(input_USB)}),
/*bucket_count=*/1);
histogram_tester().ExpectBucketCount(
AudioDeviceMetricsHandler::
kSystemSwitchInputBeforeAndAfterAudioDeviceSetChromeRestarts,
EncodeBeforeAndAfterAudioDeviceSets(
/*device_set_before=*/{},
/*device_set_after=*/{AudioDevice(input_internal),
AudioDevice(input_USB)}),
/*bucket_count=*/1);
histogram_tester().ExpectBucketCount(
AudioDeviceMetricsHandler::
kSystemSwitchOutputBeforeAndAfterAudioDeviceSetChromeRestarts,
EncodeBeforeAndAfterAudioDeviceSets(
/*device_set_before=*/{},
/*device_set_after=*/{AudioDevice(input_internal),
AudioDevice(input_USB)}),
/*bucket_count=*/1);
// Mock user switching to internal mic.
Select(input_internal);
histogram_tester().ExpectBucketCount(
AudioDeviceMetricsHandler::
kUserOverrideSystemSwitchInputBeforeAndAfterAudioDeviceSet,
EncodeBeforeAndAfterAudioDeviceSets(
/*device_set_before=*/{},
/*device_set_after=*/{AudioDevice(input_internal),
AudioDevice(input_USB)}),
/*bucket_count=*/1);
ExpectUserOverrideSystemDecisionHistogramCount(
histogram_tester(), ++expected_user_override_system_switch_input_count,
expected_user_override_system_not_switch_input_count,
expected_user_override_system_switch_output_count,
expected_user_override_system_not_switch_output_count,
/*is_chrome_restarts=*/true);
ExpectUserOverrideSystemDecisionHistogramCountInLimitedTime(
histogram_tester(),
++expected_user_override_system_switch_input_count_in_limited_time,
expected_user_override_system_not_switch_input_count_in_limited_time,
expected_user_override_system_switch_output_count_in_limited_time,
expected_user_override_system_not_switch_output_count_in_limited_time,
/*is_chrome_restarts=*/true);
ExpectUserOverrideSystemDecisionTimeDelta(
histogram_tester(), /*is_input=*/true, /*system_has_switched=*/true,
/*delta_in_minute=*/0, /*is_chrome_restarts=*/true);
}
TEST_F(AudioDeviceSelectionTest, DevicePrefEviction) {
std::vector<AudioNode> nodes;
for (int i = 0; i < 101; i++) {
nodes.push_back(NewInputNode("USB"));
}
Plug(nodes[0]);
for (int i = 1; i < 101; i++) {
FastForwardBy(base::Seconds(1));
Plug(nodes[i]);
ASSERT_EQ(ActiveInputNodeId(), nodes[i].id) << " i = " << i;
ASSERT_NE(audio_pref_handler_->GetUserPriority(AudioDevice(nodes[i])),
kUserPriorityNone)
<< " i = " << i;
FastForwardBy(base::Seconds(1));
Unplug(nodes[i]);
}
// We store at most 100 devices in prefs.
EXPECT_NE(audio_pref_handler_->GetUserPriority(AudioDevice(nodes[0])),
kUserPriorityNone)
<< "nodes[0] should be kept because it is connected";
EXPECT_EQ(audio_pref_handler_->GetUserPriority(AudioDevice(nodes[1])),
kUserPriorityNone)
<< "nodes[1] should be evicted because it is unplugged and the least "
"recently seen";
for (int i = 2; i < 101; i++) {
EXPECT_NE(audio_pref_handler_->GetUserPriority(AudioDevice(nodes[i])),
kUserPriorityNone)
<< "nodes[" << i
<< "] should be kept because it is within the recent 100 seen devices";
}
}
} // namespace
} // namespace ash
|