File: message_receiver_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 (703 lines) | stat: -rw-r--r-- 27,077 bytes parent folder | download | duplicates (6)
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
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <netinet/in.h>

#include <memory>

#include "ash/constants/ash_features.h"
#include "base/strings/strcat.h"
#include "base/test/scoped_feature_list.h"
#include "base/test/task_environment.h"
#include "chromeos/ash/components/phonehub/message_receiver_impl.h"
#include "chromeos/ash/components/phonehub/phone_hub_structured_metrics_logger.h"
#include "chromeos/ash/components/phonehub/proto/phonehub_api.pb.h"
#include "chromeos/ash/services/secure_channel/public/cpp/client/fake_connection_manager.h"
#include "components/prefs/testing_pref_service.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace ash::phonehub {

namespace {

class FakeObserver : public MessageReceiver::Observer {
 public:
  FakeObserver() = default;
  ~FakeObserver() override = default;

  size_t snapshot_num_calls() const {
    return phone_status_snapshot_updated_num_calls_;
  }

  size_t status_updated_num_calls() const {
    return phone_status_updated_num_calls_;
  }

  size_t feature_setup_response_num_calls() const {
    return feature_setup_response_num_calls_;
  }

  size_t fetch_camera_roll_items_response_calls() const {
    return fetch_camera_roll_items_response_calls_;
  }

  size_t fetch_camera_roll_item_data_response_calls() const {
    return fetch_camera_roll_item_data_response_calls_;
  }

  size_t ping_response_num_calls() const { return ping_response_num_calls_; }

  size_t app_stream_update_calls() const { return app_stream_update_calls_; }

  size_t app_list_update_calls() const { return app_list_update_calls_; }

  size_t app_list_incremental_update_calls() const {
    return app_list_incremental_update_calls_;
  }

  proto::PhoneStatusSnapshot last_snapshot() const { return last_snapshot_; }

  proto::PhoneStatusUpdate last_status_update() const {
    return last_status_update_;
  }

  proto::FeatureSetupResponse last_feature_setup_response() const {
    return last_feature_setup_response_;
  }

  proto::AppStreamUpdate last_app_stream_update() const {
    return last_app_stream_update_;
  }

  proto::AppListUpdate last_app_list_update() const {
    return last_app_list_update_;
  }

  proto::AppListIncrementalUpdate last_app_list_incremental_update() const {
    return last_app_list_incremental_update_;
  }

  proto::FetchCameraRollItemsResponse last_fetch_camera_roll_items_response()
      const {
    return last_fetch_camera_roll_items_response_;
  }

  proto::FetchCameraRollItemDataResponse
  last_fetch_camera_roll_item_data_response() const {
    return last_fetch_camera_roll_item_data_response_;
  }

  // MessageReceiver::Observer:
  void OnPhoneStatusSnapshotReceived(
      proto::PhoneStatusSnapshot phone_status_snapshot) override {
    last_snapshot_ = phone_status_snapshot;
    ++phone_status_snapshot_updated_num_calls_;
  }

  void OnPhoneStatusUpdateReceived(
      proto::PhoneStatusUpdate phone_status_update) override {
    last_status_update_ = phone_status_update;
    ++phone_status_updated_num_calls_;
  }

  void OnFeatureSetupResponseReceived(
      proto::FeatureSetupResponse feature_setup_response) override {
    last_feature_setup_response_ = feature_setup_response;
    ++feature_setup_response_num_calls_;
  }

  void OnFetchCameraRollItemsResponseReceived(
      const proto::FetchCameraRollItemsResponse& response) override {
    last_fetch_camera_roll_items_response_ = response;
    ++fetch_camera_roll_items_response_calls_;
  }

  void OnFetchCameraRollItemDataResponseReceived(
      const proto::FetchCameraRollItemDataResponse& response) override {
    last_fetch_camera_roll_item_data_response_ = response;
    ++fetch_camera_roll_item_data_response_calls_;
  }

  void OnAppStreamUpdateReceived(
      proto::AppStreamUpdate app_stream_update) override {
    last_app_stream_update_ = app_stream_update;
    ++app_stream_update_calls_;
  }

  void OnPingResponseReceived() override { ++ping_response_num_calls_; }

  void OnAppListUpdateReceived(proto::AppListUpdate app_list_update) override {
    last_app_list_update_ = app_list_update;
    ++app_list_update_calls_;
  }

  void OnAppListIncrementalUpdateReceived(
      proto::AppListIncrementalUpdate app_list_incremental_update) override {
    last_app_list_incremental_update_ = app_list_incremental_update;
    ++app_list_incremental_update_calls_;
  }

 private:
  size_t phone_status_snapshot_updated_num_calls_ = 0;
  size_t phone_status_updated_num_calls_ = 0;
  size_t feature_setup_response_num_calls_ = 0;
  size_t fetch_camera_roll_items_response_calls_ = 0;
  size_t fetch_camera_roll_item_data_response_calls_ = 0;
  size_t ping_response_num_calls_ = 0;
  size_t app_stream_update_calls_ = 0;
  size_t app_list_update_calls_ = 0;
  size_t app_list_incremental_update_calls_ = 0;
  proto::PhoneStatusSnapshot last_snapshot_;
  proto::PhoneStatusUpdate last_status_update_;
  proto::FeatureSetupResponse last_feature_setup_response_;
  proto::AppStreamUpdate last_app_stream_update_;
  proto::AppListUpdate last_app_list_update_;
  proto::AppListIncrementalUpdate last_app_list_incremental_update_;
  proto::FetchCameraRollItemsResponse last_fetch_camera_roll_items_response_;
  proto::FetchCameraRollItemDataResponse
      last_fetch_camera_roll_item_data_response_;
};

std::string SerializeMessage(proto::MessageType message_type,
                             const google::protobuf::MessageLite* request) {
  // Add two space characters, followed by the serialized proto.
  std::string message = base::StrCat({"  ", request->SerializeAsString()});

  // Replace the first two characters with |message_type| as a 16-bit int.
  uint16_t* ptr =
      reinterpret_cast<uint16_t*>(const_cast<char*>(message.data()));
  *ptr = htons(static_cast<uint16_t>(message_type));
  return message;
}

std::string SerializeMessageIncorrectly(
    proto::MessageType message_type,
    const google::protobuf::MessageLite* request) {
  // Add two space characters, followed by the serialized proto with junk.
  std::string message =
      base::StrCat({"  ", request->SerializeAsString(), "junk"});

  // Replace the first two characters with |message_type| as a 16-bit int.
  uint16_t* ptr =
      reinterpret_cast<uint16_t*>(const_cast<char*>(message.data()));
  *ptr = htons(static_cast<uint16_t>(message_type));

  // Overwrite the last byte to 0xFF results in malformed wire.
  message[message.size() - 1] = 0xFF;
  return message;
}

}  // namespace

class MessageReceiverImplTest : public testing::Test {
 protected:
  MessageReceiverImplTest()
      : fake_connection_manager_(
            std::make_unique<secure_channel::FakeConnectionManager>()) {}
  MessageReceiverImplTest(const MessageReceiverImplTest&) = delete;
  MessageReceiverImplTest& operator=(const MessageReceiverImplTest&) = delete;
  ~MessageReceiverImplTest() override = default;

  void SetUp() override {
    PhoneHubStructuredMetricsLogger::RegisterPrefs(pref_service_.registry());
    phone_hub_structured_metrics_logger_ =
        std::make_unique<PhoneHubStructuredMetricsLogger>(&pref_service_);
    message_receiver_ = std::make_unique<MessageReceiverImpl>(
        fake_connection_manager_.get(),
        phone_hub_structured_metrics_logger_.get());
    message_receiver_->AddObserver(&fake_observer_);
  }

  void TearDown() override {
    message_receiver_->RemoveObserver(&fake_observer_);
  }

  size_t GetNumPhoneStatusSnapshotCalls() const {
    return fake_observer_.snapshot_num_calls();
  }

  size_t GetNumPhoneStatusUpdatedCalls() const {
    return fake_observer_.status_updated_num_calls();
  }

  size_t GetNumFeatureSetupResponseCalls() const {
    return fake_observer_.feature_setup_response_num_calls();
  }

  size_t GetNumFetchCameraRollItemsResponseCalls() const {
    return fake_observer_.fetch_camera_roll_items_response_calls();
  }

  size_t GetNumFetchCameraRollItemDataResponseCalls() const {
    return fake_observer_.fetch_camera_roll_item_data_response_calls();
  }

  size_t GetNumPingResponseCalls() const {
    return fake_observer_.ping_response_num_calls();
  }

  size_t GetNumAppStreamUpdateCalls() const {
    return fake_observer_.app_stream_update_calls();
  }

  size_t GetNumAppListUpdateCalls() const {
    return fake_observer_.app_list_update_calls();
  }

  size_t GetNumAppListIncrementalUpdateCalls() const {
    return fake_observer_.app_list_incremental_update_calls();
  }

  proto::PhoneStatusSnapshot GetLastSnapshot() const {
    return fake_observer_.last_snapshot();
  }

  proto::PhoneStatusUpdate GetLastStatusUpdate() const {
    return fake_observer_.last_status_update();
  }

  proto::FeatureSetupResponse GetLastFeatureSetupResponse() const {
    return fake_observer_.last_feature_setup_response();
  }

  proto::FetchCameraRollItemsResponse GetLastFetchCameraRollItemsResponse()
      const {
    return fake_observer_.last_fetch_camera_roll_items_response();
  }

  proto::FetchCameraRollItemDataResponse
  GetLastFetchCameraRollItemDataResponse() const {
    return fake_observer_.last_fetch_camera_roll_item_data_response();
  }

  proto::AppStreamUpdate GetLastAppStreamUpdate() const {
    return fake_observer_.last_app_stream_update();
  }

  proto::AppListUpdate GetLastAppListUpdate() const {
    return fake_observer_.last_app_list_update();
  }

  proto::AppListIncrementalUpdate GetLastAppListIncrementalUpdate() const {
    return fake_observer_.last_app_list_incremental_update();
  }

  base::test::TaskEnvironment task_environment_;
  TestingPrefServiceSimple pref_service_;
  FakeObserver fake_observer_;
  std::unique_ptr<secure_channel::FakeConnectionManager>
      fake_connection_manager_;
  std::unique_ptr<PhoneHubStructuredMetricsLogger>
      phone_hub_structured_metrics_logger_;
  std::unique_ptr<MessageReceiverImpl> message_receiver_;
};

TEST_F(MessageReceiverImplTest, OnPhoneStatusSnapshotReceived) {
  const int32_t expected_battery_percentage = 15;
  auto expected_phone_properties = std::make_unique<proto::PhoneProperties>();
  expected_phone_properties->set_battery_percentage(
      expected_battery_percentage);

  proto::PhoneStatusSnapshot expected_snapshot;
  expected_snapshot.set_allocated_properties(
      expected_phone_properties.release());
  expected_snapshot.add_notifications();

  // Simulate receiving a message.
  const std::string expected_message =
      SerializeMessage(proto::PHONE_STATUS_SNAPSHOT, &expected_snapshot);
  fake_connection_manager_->NotifyMessageReceived(expected_message);

  proto::PhoneStatusSnapshot actual_snapshot = GetLastSnapshot();

  EXPECT_EQ(1u, GetNumPhoneStatusSnapshotCalls());
  EXPECT_EQ(0u, GetNumPhoneStatusUpdatedCalls());
  EXPECT_EQ(0u, GetNumFetchCameraRollItemsResponseCalls());
  EXPECT_EQ(expected_battery_percentage,
            actual_snapshot.properties().battery_percentage());
  EXPECT_EQ(1, actual_snapshot.notifications_size());
}

TEST_F(MessageReceiverImplTest, OnPhoneStatusUpdated) {
  const int32_t expected_battery_percentage = 15u;
  auto expected_phone_properties = std::make_unique<proto::PhoneProperties>();
  expected_phone_properties->set_battery_percentage(
      expected_battery_percentage);

  proto::PhoneStatusUpdate expected_update;
  expected_update.set_allocated_properties(expected_phone_properties.release());
  expected_update.add_updated_notifications();

  const int64_t expected_removed_id = 24u;
  expected_update.add_removed_notification_ids(expected_removed_id);

  // Simulate receiving a message.
  const std::string expected_message =
      SerializeMessage(proto::PHONE_STATUS_UPDATE, &expected_update);
  fake_connection_manager_->NotifyMessageReceived(expected_message);

  proto::PhoneStatusUpdate actual_update = GetLastStatusUpdate();

  EXPECT_EQ(0u, GetNumPhoneStatusSnapshotCalls());
  EXPECT_EQ(1u, GetNumPhoneStatusUpdatedCalls());
  EXPECT_EQ(0u, GetNumFetchCameraRollItemsResponseCalls());
  EXPECT_EQ(expected_battery_percentage,
            actual_update.properties().battery_percentage());
  EXPECT_EQ(1, actual_update.updated_notifications_size());
  EXPECT_EQ(expected_removed_id, actual_update.removed_notification_ids()[0]);
}

TEST_F(MessageReceiverImplTest, OnFeatrueSetupResponseReceived) {
  proto::FeatureSetupResponse expected_response;
  expected_response.set_camera_roll_setup_result(
      proto::FeatureSetupResult::RESULT_PERMISSION_GRANTED);
  expected_response.set_notification_setup_result(
      proto::FeatureSetupResult::RESULT_PERMISSION_GRANTED);

  const std::string expected_message =
      SerializeMessage(proto::FEATURE_SETUP_RESPONSE, &expected_response);
  fake_connection_manager_->NotifyMessageReceived(expected_message);

  proto::FeatureSetupResponse actual_response = GetLastFeatureSetupResponse();

  EXPECT_EQ(0u, GetNumPhoneStatusSnapshotCalls());
  EXPECT_EQ(0u, GetNumPhoneStatusUpdatedCalls());
  EXPECT_EQ(1u, GetNumFeatureSetupResponseCalls());
  EXPECT_EQ(proto::FeatureSetupResult::RESULT_PERMISSION_GRANTED,
            actual_response.camera_roll_setup_result());
  EXPECT_EQ(proto::FeatureSetupResult::RESULT_PERMISSION_GRANTED,
            actual_response.notification_setup_result());
}

TEST_F(MessageReceiverImplTest,
       OnFetchCameraRollItemsResponseReceivedWthFeatureEnabled) {
  base::test::ScopedFeatureList feature_list;
  feature_list.InitAndEnableFeature(features::kPhoneHubCameraRoll);

  proto::FetchCameraRollItemsResponse expected_response;
  proto::CameraRollItem* item_proto = expected_response.add_items();
  proto::CameraRollItemMetadata* metadata = item_proto->mutable_metadata();
  metadata->set_key("key");
  proto::CameraRollItemThumbnail* thumbnail = item_proto->mutable_thumbnail();
  thumbnail->set_data("encoded_thumbnail_data");

  // Simulate receiving a message.
  const std::string expected_message = SerializeMessage(
      proto::FETCH_CAMERA_ROLL_ITEMS_RESPONSE, &expected_response);
  fake_connection_manager_->NotifyMessageReceived(expected_message);

  proto::FetchCameraRollItemsResponse actual_response =
      GetLastFetchCameraRollItemsResponse();

  EXPECT_EQ(0u, GetNumPhoneStatusSnapshotCalls());
  EXPECT_EQ(0u, GetNumPhoneStatusUpdatedCalls());
  EXPECT_EQ(0u, GetNumFeatureSetupResponseCalls());
  EXPECT_EQ(1u, GetNumFetchCameraRollItemsResponseCalls());
  EXPECT_EQ(1, actual_response.items_size());
  EXPECT_EQ("key", actual_response.items(0).metadata().key());
  EXPECT_EQ("encoded_thumbnail_data",
            actual_response.items(0).thumbnail().data());
}

TEST_F(MessageReceiverImplTest,
       OnFetchCameraRollItemsResponseReceivedWithFeatureDisabled) {
  base::test::ScopedFeatureList feature_list;
  feature_list.InitAndDisableFeature(features::kPhoneHubCameraRoll);

  proto::FetchCameraRollItemsResponse expected_response;
  proto::CameraRollItem* item_proto = expected_response.add_items();
  proto::CameraRollItemMetadata* metadata = item_proto->mutable_metadata();
  metadata->set_key("key");
  proto::CameraRollItemThumbnail* thumbnail = item_proto->mutable_thumbnail();
  thumbnail->set_data("encoded_thumbnail_data");

  // Simulate receiving a message.
  const std::string expected_message = SerializeMessage(
      proto::FETCH_CAMERA_ROLL_ITEMS_RESPONSE, &expected_response);
  fake_connection_manager_->NotifyMessageReceived(expected_message);

  EXPECT_EQ(0u, GetNumPhoneStatusSnapshotCalls());
  EXPECT_EQ(0u, GetNumPhoneStatusUpdatedCalls());
  EXPECT_EQ(0u, GetNumFeatureSetupResponseCalls());
  EXPECT_EQ(0u, GetNumFetchCameraRollItemsResponseCalls());
}

TEST_F(MessageReceiverImplTest,
       OnFetchCameraRollItemDataResponseReceivedWthFeatureEnabled) {
  base::test::ScopedFeatureList feature_list;
  feature_list.InitAndEnableFeature(features::kPhoneHubCameraRoll);

  proto::FetchCameraRollItemDataResponse expected_response;
  expected_response.mutable_metadata()->set_key("key");
  expected_response.set_file_availability(
      proto::FetchCameraRollItemDataResponse::AVAILABLE);
  expected_response.set_payload_id(1234);

  // Simulate receiving a message.
  const std::string expected_message = SerializeMessage(
      proto::FETCH_CAMERA_ROLL_ITEM_DATA_RESPONSE, &expected_response);
  fake_connection_manager_->NotifyMessageReceived(expected_message);

  proto::FetchCameraRollItemDataResponse actual_response =
      GetLastFetchCameraRollItemDataResponse();

  EXPECT_EQ(0u, GetNumPhoneStatusSnapshotCalls());
  EXPECT_EQ(0u, GetNumPhoneStatusUpdatedCalls());
  EXPECT_EQ(0u, GetNumFeatureSetupResponseCalls());
  EXPECT_EQ(0u, GetNumFetchCameraRollItemsResponseCalls());
  EXPECT_EQ(1u, GetNumFetchCameraRollItemDataResponseCalls());
  EXPECT_EQ("key", actual_response.metadata().key());
  EXPECT_EQ(proto::FetchCameraRollItemDataResponse::AVAILABLE,
            actual_response.file_availability());
  EXPECT_EQ(1234, actual_response.payload_id());
}

TEST_F(MessageReceiverImplTest,
       OnFetchCameraRollItemDataResponseReceivedWithFeatureDisabled) {
  base::test::ScopedFeatureList feature_list;
  feature_list.InitAndDisableFeature(features::kPhoneHubCameraRoll);

  proto::FetchCameraRollItemDataResponse expected_response;
  expected_response.mutable_metadata()->set_key("key");
  expected_response.set_file_availability(
      proto::FetchCameraRollItemDataResponse::AVAILABLE);
  expected_response.set_payload_id(1234);

  // Simulate receiving a message.
  const std::string expected_message = SerializeMessage(
      proto::FETCH_CAMERA_ROLL_ITEM_DATA_RESPONSE, &expected_response);
  fake_connection_manager_->NotifyMessageReceived(expected_message);

  EXPECT_EQ(0u, GetNumPhoneStatusSnapshotCalls());
  EXPECT_EQ(0u, GetNumPhoneStatusUpdatedCalls());
  EXPECT_EQ(0u, GetNumFeatureSetupResponseCalls());
  EXPECT_EQ(0u, GetNumFetchCameraRollItemsResponseCalls());
  EXPECT_EQ(0u, GetNumFetchCameraRollItemDataResponseCalls());
}

TEST_F(MessageReceiverImplTest, OnPingResponseReceivedFeatureEnabled) {
  base::test::ScopedFeatureList feature_list;
  feature_list.InitAndEnableFeature(features::kPhoneHubPingOnBubbleOpen);

  proto::PingResponse expected_response;

  // Simulate receiving a message
  const std::string expected_message =
      SerializeMessage(proto::PING_RESPONSE, &expected_response);
  fake_connection_manager_->NotifyMessageReceived(expected_message);

  EXPECT_EQ(1u, GetNumPingResponseCalls());
}

TEST_F(MessageReceiverImplTest, OnPingResponseReceivedFeatureDisabled) {
  base::test::ScopedFeatureList feature_list;
  feature_list.InitAndDisableFeature(features::kPhoneHubPingOnBubbleOpen);

  proto::PingResponse expected_response;

  // Simulate receiving a message
  const std::string expected_message =
      SerializeMessage(proto::PING_RESPONSE, &expected_response);
  fake_connection_manager_->NotifyMessageReceived(expected_message);

  EXPECT_EQ(0u, GetNumPingResponseCalls());
}

TEST_F(MessageReceiverImplTest, OnAppStreamUpdateReceived) {
  base::test::ScopedFeatureList feature_list;
  feature_list.InitAndEnableFeature(features::kEcheSWA);

  proto::AppStreamUpdate expected_app_stream_update;
  auto* app = expected_app_stream_update.mutable_foreground_app();
  app->set_user_id(12);
  app->set_package_name("package1");
  app->set_visible_name("visible1");

  // Simulate receiving a message.
  const std::string expected_message =
      SerializeMessage(proto::APP_STREAM_UPDATE, &expected_app_stream_update);
  fake_connection_manager_->NotifyMessageReceived(expected_message);

  proto::AppStreamUpdate actual_app_stream_update = GetLastAppStreamUpdate();

  EXPECT_EQ(1u, GetNumAppStreamUpdateCalls());
  EXPECT_EQ(12, actual_app_stream_update.foreground_app().user_id());
  EXPECT_EQ("package1",
            actual_app_stream_update.foreground_app().package_name());
  EXPECT_EQ("visible1",
            actual_app_stream_update.foreground_app().visible_name());
}

TEST_F(MessageReceiverImplTest, OnAppListUpdateReceived) {
  base::test::ScopedFeatureList feature_list;
  feature_list.InitAndEnableFeature(features::kEcheSWA);

  proto::AppListUpdate expected_app_list_update;
  auto* streamable_apps = expected_app_list_update.mutable_all_apps();
  streamable_apps->add_apps();

  // Simulate receiving a message.
  const std::string expected_message =
      SerializeMessage(proto::APP_LIST_UPDATE, &expected_app_list_update);
  fake_connection_manager_->NotifyMessageReceived(expected_message);

  proto::AppListUpdate actual_app_list_update = GetLastAppListUpdate();

  EXPECT_EQ(1u, GetNumAppListUpdateCalls());
  EXPECT_TRUE(actual_app_list_update.has_all_apps());
  EXPECT_EQ(1, actual_app_list_update.all_apps().apps_size());
}

TEST_F(MessageReceiverImplTest, OnAppListUpdateReceivedNoStreamableApps) {
  base::test::ScopedFeatureList feature_list;
  feature_list.InitAndEnableFeature(features::kEcheSWA);

  proto::AppListUpdate expected_app_list_update;

  // Simulate receiving a message.
  const std::string expected_message =
      SerializeMessage(proto::APP_LIST_UPDATE, &expected_app_list_update);
  fake_connection_manager_->NotifyMessageReceived(expected_message);

  proto::AppListUpdate actual_app_list_update = GetLastAppListUpdate();

  EXPECT_EQ(1u, GetNumAppListUpdateCalls());
  EXPECT_FALSE(actual_app_list_update.has_all_apps());
}

TEST_F(MessageReceiverImplTest, OnAppListUpdateReceivedFlagDisabled) {
  base::test::ScopedFeatureList feature_list;
  feature_list.InitAndDisableFeature(features::kEcheSWA);

  proto::AppListUpdate expected_app_list_update;
  auto* streamable_apps = expected_app_list_update.mutable_all_apps();
  streamable_apps->add_apps();

  // Simulate receiving a message.
  const std::string expected_message =
      SerializeMessage(proto::APP_LIST_UPDATE, &expected_app_list_update);
  fake_connection_manager_->NotifyMessageReceived(expected_message);

  EXPECT_EQ(0u, GetNumAppListUpdateCalls());
}

TEST_F(MessageReceiverImplTest, OnAppListIncremenatlUpdateReceived) {
  base::test::ScopedFeatureList feature_list;
  feature_list.InitAndEnableFeature(features::kEcheSWA);

  proto::AppListIncrementalUpdate expected_app_list_incremental_update;
  auto* installed_app =
      expected_app_list_incremental_update.mutable_installed_apps();
  installed_app->add_apps();

  // Simulate receiving a message.
  const std::string expected_message =
      SerializeMessage(proto::APP_LIST_INCREMENTAL_UPDATE,
                       &expected_app_list_incremental_update);
  fake_connection_manager_->NotifyMessageReceived(expected_message);

  proto::AppListIncrementalUpdate actual_app_list_incremental_update =
      GetLastAppListIncrementalUpdate();

  EXPECT_EQ(1u, GetNumAppListIncrementalUpdateCalls());
  EXPECT_TRUE(expected_app_list_incremental_update.has_installed_apps());
  EXPECT_FALSE(expected_app_list_incremental_update.has_removed_apps());
  EXPECT_EQ(1,
            expected_app_list_incremental_update.installed_apps().apps_size());
}

TEST_F(MessageReceiverImplTest,
       OnAppListIncremenatlUpdateReceivedFlagDisabled) {
  base::test::ScopedFeatureList feature_list;
  feature_list.InitAndDisableFeature(features::kEcheSWA);

  proto::AppListIncrementalUpdate expected_app_list_incremental_update;
  auto* installed_app =
      expected_app_list_incremental_update.mutable_installed_apps();
  installed_app->add_apps();

  // Simulate receiving a message.
  const std::string expected_message =
      SerializeMessage(proto::APP_LIST_INCREMENTAL_UPDATE,
                       &expected_app_list_incremental_update);
  fake_connection_manager_->NotifyMessageReceived(expected_message);

  EXPECT_EQ(0u, GetNumAppListIncrementalUpdateCalls());
}

TEST_F(MessageReceiverImplTest, OnMessageReceivedParseFailureStates) {
  base::test::ScopedFeatureList feature_list;
  feature_list.InitWithFeatures(
      /*enabled_features=*/{features::kEcheSWA, features::kPhoneHubCameraRoll,
                            features::kPhoneHubPingOnBubbleOpen},
      /*disabled_features=*/{});

  std::string expected_message;

  proto::PhoneStatusSnapshot expected_snapshot;
  // Simulate receiving an incorrect message by malforming the last byte.
  expected_message = SerializeMessageIncorrectly(proto::PHONE_STATUS_SNAPSHOT,
                                                 &expected_snapshot);
  fake_connection_manager_->NotifyMessageReceived(expected_message);

  EXPECT_EQ(0u, GetNumPhoneStatusSnapshotCalls());

  proto::PhoneStatusUpdate expected_update;
  // Simulate receiving an incorrect message by malforming the last byte.
  expected_message =
      SerializeMessageIncorrectly(proto::PHONE_STATUS_UPDATE, &expected_update);
  fake_connection_manager_->NotifyMessageReceived(expected_message);

  EXPECT_EQ(0u, GetNumPhoneStatusUpdatedCalls());

  proto::FeatureSetupResponse expected_response;
  // Simulate receiving an incorrect message by malforming the last byte.
  expected_message = SerializeMessageIncorrectly(proto::FEATURE_SETUP_RESPONSE,
                                                 &expected_response);
  fake_connection_manager_->NotifyMessageReceived(expected_message);

  EXPECT_EQ(0u, GetNumFeatureSetupResponseCalls());

  proto::FetchCameraRollItemsResponse expected_items;
  // Simulate receiving an incorrect message by malforming the last byte.
  expected_message = SerializeMessageIncorrectly(
      proto::FETCH_CAMERA_ROLL_ITEMS_RESPONSE, &expected_items);
  fake_connection_manager_->NotifyMessageReceived(expected_message);

  EXPECT_EQ(0u, GetNumFetchCameraRollItemsResponseCalls());

  proto::FetchCameraRollItemDataResponse expected_data;
  // Simulate receiving an incorrect message by malforming the last byte.
  expected_message = SerializeMessageIncorrectly(
      proto::FETCH_CAMERA_ROLL_ITEM_DATA_RESPONSE, &expected_data);
  fake_connection_manager_->NotifyMessageReceived(expected_message);

  EXPECT_EQ(0u, GetNumFetchCameraRollItemDataResponseCalls());

  proto::AppStreamUpdate expected_app_stream_update;
  // Simulate receiving an incorrect message by malforming the last byte.
  expected_message = SerializeMessageIncorrectly(proto::APP_STREAM_UPDATE,
                                                 &expected_app_stream_update);
  fake_connection_manager_->NotifyMessageReceived(expected_message);

  EXPECT_EQ(0u, GetNumAppStreamUpdateCalls());

  proto::AppListUpdate expected_app_list_update;
  // Simulate receiving an incorrect message by malforming the last byte.
  expected_message = SerializeMessageIncorrectly(proto::APP_LIST_UPDATE,
                                                 &expected_app_list_update);
  fake_connection_manager_->NotifyMessageReceived(expected_message);

  EXPECT_EQ(0u, GetNumAppListUpdateCalls());

  proto::AppListIncrementalUpdate expected_incremental_update;
  // Simulate receiving an incorrect message by malforming the last byte.
  expected_message = SerializeMessageIncorrectly(
      proto::APP_LIST_INCREMENTAL_UPDATE, &expected_incremental_update);
  fake_connection_manager_->NotifyMessageReceived(expected_message);

  EXPECT_EQ(0u, GetNumAppListIncrementalUpdateCalls());
}

}  // namespace ash::phonehub