File: notification_processor_unittest.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (673 lines) | stat: -rw-r--r-- 29,174 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
// 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 "chromeos/ash/components/phonehub/notification_processor.h"
#include <string>

#include "ash/constants/ash_features.h"
#include "base/memory/ptr_util.h"
#include "base/test/scoped_feature_list.h"
#include "base/test/task_environment.h"
#include "chromeos/ash/components/phonehub/fake_notification_manager.h"
#include "chromeos/ash/components/phonehub/phone_model_test_util.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/gfx/image/image_skia.h"
#include "ui/gfx/image/image_unittest_util.h"

namespace ash::phonehub {

namespace {

constexpr int64_t kNotificationIdA = 1;
constexpr int64_t kNotificationIdB = 2;
constexpr int64_t kNotificationIdC = 3;

constexpr int64_t kInlineReplyIdA = 3;
constexpr int64_t kInlineReplyIdB = 4;

constexpr int64_t kOpenableActionId = -2;
constexpr int64_t kAnswerActionId = 1;
constexpr int64_t kDeclineActionId = 2;
constexpr int64_t kHangupActionId = 3;

const char kIconDataA[] = "icon_a";
const char kIconDataB[] = "icon_b";

const char kSharedImageA[] = "shared_image_a";
const char kSharedImageB[] = "shared_image_b";

const char kContactImageA[] = "contact_image_a";
const char kContactImageB[] = "contact_image_b";

// Garbage color for the purpose of verification in these tests.
const SkColor kIconColor = SkColorSetRGB(0x12, 0x34, 0x56);

SkBitmap TestBitmap() {
  SkBitmap bitmap;
  bitmap.allocN32Pixels(1, 1);
  return bitmap;
}

gfx::Image TestImage() {
  gfx::ImageSkia image_skia = gfx::ImageSkia::CreateFrom1xBitmap(TestBitmap());
  image_skia.MakeThreadSafe();
  return gfx::Image(image_skia);
}

}  // namespace

class NotificationProcessorTest : public testing::Test {
 public:
  friend class NotificationProcessor;

  NotificationProcessorTest() {
    scoped_feature_list_.InitWithFeatures(
        /*enabled_features=*/{features::kEcheSWA,
                              features::kPhoneHubCallNotification},
        /*disabled_features=*/{});
  }
  NotificationProcessorTest(const NotificationProcessorTest&) = delete;
  NotificationProcessorTest& operator=(const NotificationProcessorTest&) =
      delete;
  ~NotificationProcessorTest() override = default;

  class FakeImageDecoderDelegate
      : public NotificationProcessor::ImageDecoderDelegate {
   public:
    using DecodeImageCallback = NotificationProcessor::DecodeImageCallback;

    FakeImageDecoderDelegate() = default;
    ~FakeImageDecoderDelegate() override = default;

    void PerformImageDecode(
        const std::string& data,
        DecodeImageCallback single_image_decoded_closure) override {
      decode_image_callbacks_.push(std::move(single_image_decoded_closure));
    }

    size_t NumberOfDecodeImageCallbacks() {
      return decode_image_callbacks_.size();
    }

    void RunNextCallback(SkBitmap bitmap) {
      std::move(decode_image_callbacks_.front()).Run(bitmap);
      decode_image_callbacks_.pop();
    }

    void RunAllCallbacks() {
      while (!decode_image_callbacks_.empty())
        RunNextCallback(TestBitmap());
    }

    void RunAllCallbacksWithEmpty() {
      while (!decode_image_callbacks_.empty())
        RunNextCallback(SkBitmap());
    }

    std::queue<DecodeImageCallback> decode_image_callbacks_;
  };

  // testing::Test:
  void SetUp() override {
    fake_notification_manager_ = std::make_unique<FakeNotificationManager>();

    notification_processor_ = base::WrapUnique(new NotificationProcessor(
        fake_notification_manager_.get(),
        std::make_unique<FakeImageDecoderDelegate>()));
  }

  FakeImageDecoderDelegate* image_decoder_delegate() {
    return static_cast<FakeImageDecoderDelegate*>(
        notification_processor_->delegate_.get());
  }

  NotificationProcessor* notification_processor() {
    return notification_processor_.get();
  }

  FakeNotificationManager* fake_notification_manager() {
    return fake_notification_manager_.get();
  }

  size_t NumPendingRequests() {
    return notification_processor()->pending_notification_requests_.size();
  }

  proto::Notification CreateNewInlineReplyableOpenableNotification(
      int64_t notification_id,
      int64_t inline_reply_id,
      Notification::InteractionBehavior behavior) {
    return CreateNewInlineReplyableNotification(
        notification_id, inline_reply_id,
        /* icon= */ std::string(),
        /* shared_image= */ std::string(),
        /* contact_image= */ std::string(), behavior);
  }

  proto::Notification CreateNewInlineReplyableNotification(
      int64_t notification_id,
      int64_t inline_reply_id,
      std::string icon = std::string(),
      std::string shared_image = std::string(),
      std::string contact_image = std::string(),
      Notification::InteractionBehavior behavior =
          Notification::InteractionBehavior::kNone,
      proto::AppStreamabilityStatus app_streamability_status =
          proto::AppStreamabilityStatus::STREAMABLE) {
    auto origin_app = std::make_unique<proto::App>();
    origin_app->set_icon(icon);
    origin_app->set_app_streamability_status(app_streamability_status);

    proto::Notification notification;
    notification.set_id(notification_id);
    notification.set_allocated_origin_app(origin_app.release());
    notification.set_contact_image(contact_image);
    notification.set_shared_image(shared_image);
    notification.set_category(
        proto::Notification::Category::Notification_Category_CONVERSATION);

    notification.add_actions();
    proto::Action* mutable_action = notification.mutable_actions(0);
    mutable_action->set_id(inline_reply_id);
    mutable_action->set_type(proto::Action_InputType::Action_InputType_TEXT);

    if (behavior == Notification::InteractionBehavior::kOpenable) {
      notification.add_actions();
      proto::Action* open_action = notification.mutable_actions(1);
      open_action->set_id(kOpenableActionId);
      open_action->set_type(proto::Action_InputType::Action_InputType_OPEN);
    }
    return notification;
  }

  proto::Notification CreateNewInlineReplyableMonochromeIconNotification(
      int64_t notification_id,
      int64_t inline_reply_id,
      std::optional<SkColor> icon_color = std::nullopt,
      std::string icon = std::string()) {
    proto::Notification notification = CreateNewInlineReplyableNotification(
        notification_id, inline_reply_id, icon);
    proto::App* origin_app = notification.mutable_origin_app();
    origin_app->set_monochrome_icon_mask(icon);
    if (icon_color != std::nullopt) {
      auto color_rgb = std::make_unique<proto::ColorRgb>();
      color_rgb->set_red(SkColorGetR(*icon_color));
      color_rgb->set_green(SkColorGetG(*icon_color));
      color_rgb->set_blue(SkColorGetB(*icon_color));
      origin_app->set_allocated_monochrome_icon_color(color_rgb.release());
    }
    return notification;
  }

  proto::Notification CreateNonTextTypeNotification(
      int64_t notification_id,
      int64_t inline_reply_id,
      std::string icon = std::string()) {
    auto origin_app = std::make_unique<proto::App>();
    origin_app->set_icon(icon);

    proto::Notification notification;
    notification.set_id(notification_id);
    notification.set_allocated_origin_app(origin_app.release());

    notification.add_actions();
    proto::Action* open_action = notification.mutable_actions(0);
    open_action->set_id(kOpenableActionId);
    open_action->set_type(proto::Action_InputType::Action_InputType_OPEN);

    return notification;
  }

  proto::Notification CreateIncomingCallNotification(int64_t notification_id) {
    auto origin_app = std::make_unique<proto::App>();
    origin_app->set_icon(std::string());

    proto::Notification notification;
    notification.set_id(notification_id);
    notification.set_allocated_origin_app(origin_app.release());
    notification.set_contact_image(std::string());
    notification.set_shared_image(std::string());
    notification.set_category(
        proto::Notification::Category::Notification_Category_INCOMING_CALL);

    notification.add_actions();
    proto::Action* answer_action = notification.mutable_actions(0);
    answer_action->set_id(kAnswerActionId);
    answer_action->set_call_action(
        proto::Action_CallAction::Action_CallAction_ANSWER);

    notification.add_actions();
    proto::Action* decline_action = notification.mutable_actions(1);
    decline_action->set_id(kDeclineActionId);
    decline_action->set_call_action(
        proto::Action_CallAction::Action_CallAction_DECLINE);

    return notification;
  }

  proto::Notification CreateOngoingCallNotification(int64_t notification_id) {
    auto origin_app = std::make_unique<proto::App>();
    origin_app->set_icon(std::string());

    proto::Notification notification;
    notification.set_id(notification_id);
    notification.set_allocated_origin_app(origin_app.release());
    notification.set_contact_image(std::string());
    notification.set_shared_image(std::string());
    notification.set_category(
        proto::Notification::Category::Notification_Category_ONGOING_CALL);

    notification.add_actions();
    proto::Action* hangup_action = notification.mutable_actions(0);
    hangup_action->set_id(kHangupActionId);
    hangup_action->set_call_action(
        proto::Action_CallAction::Action_CallAction_HANGUP);

    notification.add_actions();
    proto::Action* open_action = notification.mutable_actions(1);
    open_action->set_id(kOpenableActionId);
    open_action->set_type(proto::Action_InputType::Action_InputType_OPEN);

    return notification;
  }

 private:
  std::unique_ptr<FakeNotificationManager> fake_notification_manager_;
  std::unique_ptr<NotificationProcessor> notification_processor_;

  base::test::ScopedFeatureList scoped_feature_list_;
  base::test::SingleThreadTaskEnvironment task_environment_;
};

TEST_F(NotificationProcessorTest, FailedToDecodeImage) {
  std::vector<proto::Notification> first_set_of_notifications;

  // The icon should be an empty image as the decoder failed to decode the
  // image to be used as both the color_icon and monochrome_icon_mask.
  first_set_of_notifications.emplace_back(CreateNewInlineReplyableNotification(
      kNotificationIdA, kInlineReplyIdA, kIconDataA));
  notification_processor()->AddNotifications(first_set_of_notifications);
  image_decoder_delegate()->RunNextCallback(SkBitmap());
  image_decoder_delegate()->RunNextCallback(SkBitmap());

  const Notification* notification =
      fake_notification_manager()->GetNotification(kNotificationIdA);
  EXPECT_TRUE(notification->app_metadata().color_icon.IsEmpty());
  EXPECT_TRUE(notification->app_metadata().monochrome_icon_mask.has_value());
  EXPECT_FALSE(notification->shared_image().has_value());
  EXPECT_FALSE(notification->contact_image().has_value());
}

TEST_F(NotificationProcessorTest, ShouldSkipDecodeImageIfNotAvailable) {
  std::vector<proto::Notification> first_set_of_notifications;

  first_set_of_notifications.emplace_back(CreateNonTextTypeNotification(
      kNotificationIdA, kInlineReplyIdA, kIconDataA));
  notification_processor()->AddNotifications(first_set_of_notifications);

  EXPECT_EQ(0u, image_decoder_delegate()->NumberOfDecodeImageCallbacks());
  image_decoder_delegate()->RunAllCallbacks();

  EXPECT_EQ(0u, fake_notification_manager()->num_notifications());
}

TEST_F(NotificationProcessorTest, MonochromeIconFieldsPopulatedCorrectly) {
  std::vector<proto::Notification> first_set_of_notifications;

  // Legacy notifications don't supply color and should not be filled in.
  first_set_of_notifications.emplace_back(CreateNewInlineReplyableNotification(
      kNotificationIdA, kInlineReplyIdA, kIconDataA));
  notification_processor()->AddNotifications(first_set_of_notifications);
  image_decoder_delegate()->RunAllCallbacks();

  const Notification* notification =
      fake_notification_manager()->GetNotification(kNotificationIdA);
  EXPECT_TRUE(gfx::test::AreImagesEqual(notification->app_metadata().color_icon,
                                        TestImage()));
  EXPECT_FALSE(notification->app_metadata().icon_color.has_value());

  // Monochrome notifications without a color should not have icon_color filled.
  first_set_of_notifications.clear();
  first_set_of_notifications.emplace_back(
      CreateNewInlineReplyableMonochromeIconNotification(
          kNotificationIdA, kInlineReplyIdA, std::nullopt, kIconDataA));
  notification_processor()->AddNotifications(first_set_of_notifications);
  image_decoder_delegate()->RunAllCallbacks();

  notification = fake_notification_manager()->GetNotification(kNotificationIdA);
  EXPECT_TRUE(notification->app_metadata().icon_is_monochrome);
  EXPECT_TRUE(gfx::test::AreImagesEqual(notification->app_metadata().color_icon,
                                        TestImage()));
  EXPECT_FALSE(notification->app_metadata().icon_color.has_value());

  // Monochrome notifications with a color should have icon_color filled.
  first_set_of_notifications.clear();
  first_set_of_notifications.emplace_back(
      CreateNewInlineReplyableMonochromeIconNotification(
          kNotificationIdA, kInlineReplyIdA, kIconColor, kIconDataA));
  notification_processor()->AddNotifications(first_set_of_notifications);
  image_decoder_delegate()->RunAllCallbacks();

  notification = fake_notification_manager()->GetNotification(kNotificationIdA);
  EXPECT_TRUE(notification->app_metadata().icon_is_monochrome);
  EXPECT_TRUE(gfx::test::AreImagesEqual(notification->app_metadata().color_icon,
                                        TestImage()));
  EXPECT_TRUE(notification->app_metadata().icon_color.has_value());
  EXPECT_TRUE(*notification->app_metadata().icon_color == kIconColor);

  // Monochrome notifications without an icon should not fill in color.
  first_set_of_notifications.clear();
  first_set_of_notifications.emplace_back(
      CreateNewInlineReplyableMonochromeIconNotification(
          kNotificationIdA, kInlineReplyIdA, kIconColor,
          /*icon=*/std::string()));
  notification_processor()->AddNotifications(first_set_of_notifications);
  image_decoder_delegate()->RunAllCallbacksWithEmpty();

  notification = fake_notification_manager()->GetNotification(kNotificationIdA);
  EXPECT_TRUE(notification->app_metadata().icon_is_monochrome);
  EXPECT_TRUE(notification->app_metadata().color_icon.IsEmpty());
  EXPECT_FALSE(notification->app_metadata().icon_color.has_value());
}

TEST_F(NotificationProcessorTest, ImageFieldPopulatedCorrectly) {
  std::vector<proto::Notification> first_set_of_notifications;

  // The icon should be populated. The shared and contact image should be null.
  first_set_of_notifications.emplace_back(CreateNewInlineReplyableNotification(
      kNotificationIdA, kInlineReplyIdA, kIconDataA));
  notification_processor()->AddNotifications(first_set_of_notifications);
  image_decoder_delegate()->RunAllCallbacks();

  const Notification* notification =
      fake_notification_manager()->GetNotification(kNotificationIdA);
  EXPECT_TRUE(gfx::test::AreImagesEqual(notification->app_metadata().color_icon,
                                        TestImage()));
  EXPECT_FALSE(notification->shared_image().has_value());
  EXPECT_FALSE(notification->contact_image().has_value());

  // The icon and shared image should be populated. The contact image should be
  // null.
  first_set_of_notifications.clear();
  first_set_of_notifications.emplace_back(CreateNewInlineReplyableNotification(
      kNotificationIdA, kInlineReplyIdA, kIconDataA, kSharedImageA));
  notification_processor()->AddNotifications(first_set_of_notifications);
  image_decoder_delegate()->RunAllCallbacks();

  notification = fake_notification_manager()->GetNotification(kNotificationIdA);
  EXPECT_TRUE(gfx::test::AreImagesEqual(notification->app_metadata().color_icon,
                                        TestImage()));
  EXPECT_TRUE(
      gfx::test::AreImagesEqual(*notification->shared_image(), TestImage()));
  EXPECT_FALSE(notification->contact_image().has_value());

  // The icon and contact image should be populated. The shared image should be
  // null.
  first_set_of_notifications.clear();
  first_set_of_notifications.emplace_back(CreateNewInlineReplyableNotification(
      kNotificationIdA, kInlineReplyIdA, kIconDataA, std::string(),
      kContactImageA));
  notification_processor()->AddNotifications(first_set_of_notifications);
  image_decoder_delegate()->RunAllCallbacks();

  notification = fake_notification_manager()->GetNotification(kNotificationIdA);
  EXPECT_TRUE(gfx::test::AreImagesEqual(notification->app_metadata().color_icon,
                                        TestImage()));
  EXPECT_FALSE(notification->shared_image().has_value());
  EXPECT_TRUE(
      gfx::test::AreImagesEqual(*notification->contact_image(), TestImage()));

  // All images should be should be populated.
  first_set_of_notifications.clear();
  first_set_of_notifications.emplace_back(CreateNewInlineReplyableNotification(
      kNotificationIdA, kInlineReplyIdA, kIconDataA, kSharedImageA,
      kContactImageA));
  notification_processor()->AddNotifications(first_set_of_notifications);
  image_decoder_delegate()->RunAllCallbacks();
  EXPECT_TRUE(gfx::test::AreImagesEqual(notification->app_metadata().color_icon,
                                        TestImage()));
  EXPECT_TRUE(
      gfx::test::AreImagesEqual(*notification->shared_image(), TestImage()));
  EXPECT_TRUE(
      gfx::test::AreImagesEqual(*notification->contact_image(), TestImage()));
}

TEST_F(NotificationProcessorTest, StreamabilityStatus) {
  std::vector<proto::Notification> first_set_of_notifications;

  first_set_of_notifications.emplace_back(CreateNewInlineReplyableNotification(
      kNotificationIdA, kInlineReplyIdA, kIconDataA, std::string(),
      std::string(), Notification::InteractionBehavior::kNone,
      proto::AppStreamabilityStatus::BLOCK_LISTED));
  notification_processor()->AddNotifications(first_set_of_notifications);
  image_decoder_delegate()->RunAllCallbacks();

  const Notification* notification =
      fake_notification_manager()->GetNotification(kNotificationIdA);
  EXPECT_EQ(proto::AppStreamabilityStatus::BLOCK_LISTED,
            notification->app_metadata().app_streamability_status);
}

TEST_F(NotificationProcessorTest, AddRemoveClearWithoutRace) {
  // Add 2 notifications with all images populated.
  std::vector<proto::Notification> first_set_of_notifications;
  first_set_of_notifications.emplace_back(CreateNewInlineReplyableNotification(
      kNotificationIdA, kInlineReplyIdA, kIconDataA, kSharedImageA,
      kContactImageA));
  first_set_of_notifications.emplace_back(CreateNewInlineReplyableNotification(
      kNotificationIdB, kInlineReplyIdB, kIconDataB, kSharedImageB,
      kContactImageB));

  notification_processor()->AddNotifications(first_set_of_notifications);

  // 8 image decode callbacks will occur for kIconDataA, data for icons twice
  // for each notification, kSharedImageA, kContactImageA, kIconDataB,
  // kSharedImageB, and kContactImageB.
  EXPECT_EQ(8u, image_decoder_delegate()->NumberOfDecodeImageCallbacks());
  image_decoder_delegate()->RunAllCallbacks();

  EXPECT_EQ(2u, fake_notification_manager()->num_notifications());
  EXPECT_TRUE(fake_notification_manager()->GetNotification(kNotificationIdA));
  EXPECT_TRUE(fake_notification_manager()->GetNotification(kNotificationIdB));

  // Remove notification with id kNotificationIdA.
  base::flat_set<int64_t> ids_of_notifications_to_remove;
  ids_of_notifications_to_remove.emplace(kNotificationIdA);
  notification_processor()->RemoveNotifications(ids_of_notifications_to_remove);
  EXPECT_EQ(1u, fake_notification_manager()->num_notifications());
  EXPECT_FALSE(fake_notification_manager()->GetNotification(kNotificationIdA));
  EXPECT_TRUE(fake_notification_manager()->GetNotification(kNotificationIdB));

  // Clear all notifications.
  notification_processor()->ClearNotificationsAndPendingUpdates();
  EXPECT_EQ(0u, fake_notification_manager()->num_notifications());
}

TEST_F(NotificationProcessorTest, AddRemoveWithRace) {
  // Add 2 notifications.
  std::vector<proto::Notification> first_set_of_notifications;
  first_set_of_notifications.emplace_back(CreateNewInlineReplyableNotification(
      kNotificationIdA, kInlineReplyIdA, kIconDataA, kSharedImageA));
  first_set_of_notifications.emplace_back(CreateNewInlineReplyableNotification(
      kNotificationIdB, kInlineReplyIdB, kIconDataB));

  notification_processor()->AddNotifications(first_set_of_notifications);

  // One pending requests because |first_set_of_notifications| processing
  // occurred immediately.
  EXPECT_EQ(1u, NumPendingRequests());

  // Remove notification with id kNotificationIdA while
  // |first_set_of_notifications| is still being processed.
  base::flat_set<int64_t> ids_of_notifications_to_remove;
  ids_of_notifications_to_remove.emplace(kNotificationIdA);
  notification_processor()->RemoveNotifications(ids_of_notifications_to_remove);

  // Pending delete request, first in the queue.
  EXPECT_EQ(2u, NumPendingRequests());
  EXPECT_EQ(0u, fake_notification_manager()->num_notifications());

  // Add a set of notifications such that only one image needs to be decoded,
  // when neither the first set has completed processing more the remove request
  // has been fully processed.
  std::vector<proto::Notification> second_set_of_notifications;
  second_set_of_notifications.emplace_back(CreateNewInlineReplyableNotification(
      kNotificationIdA, kInlineReplyIdA, kIconDataA));
  notification_processor()->AddNotifications(second_set_of_notifications);

  // Pending add request, second in the queue.
  EXPECT_EQ(3u, NumPendingRequests());
  EXPECT_EQ(0u, fake_notification_manager()->num_notifications());

  // 5 image decode callbacks will occur (two icons, two monochrome icons, and a
  // shared image). When the last image decode callback is finished running,
  // which in this case is original icon2, it will cause the next notification
  // edit request to be executed.
  EXPECT_EQ(5u, image_decoder_delegate()->NumberOfDecodeImageCallbacks());
  EXPECT_EQ(3u, NumPendingRequests());
  image_decoder_delegate()->RunNextCallback(TestBitmap());

  EXPECT_EQ(4u, image_decoder_delegate()->NumberOfDecodeImageCallbacks());
  EXPECT_EQ(3u, NumPendingRequests());
  image_decoder_delegate()->RunNextCallback(TestBitmap());

  EXPECT_EQ(3u, image_decoder_delegate()->NumberOfDecodeImageCallbacks());
  EXPECT_EQ(3u, NumPendingRequests());

  // The scheduled remove callback will occur, then subsequently the add
  // notification with 1 image.
  image_decoder_delegate()->RunNextCallback(TestBitmap());
  image_decoder_delegate()->RunNextCallback(TestBitmap());
  image_decoder_delegate()->RunNextCallback(TestBitmap());
  EXPECT_EQ(1u, NumPendingRequests());
  EXPECT_EQ(2u, image_decoder_delegate()->NumberOfDecodeImageCallbacks());

  EXPECT_EQ(1u, fake_notification_manager()->num_notifications());
  EXPECT_FALSE(fake_notification_manager()->GetNotification(kNotificationIdA));
  EXPECT_TRUE(fake_notification_manager()->GetNotification(kNotificationIdB));

  // 1 image decode callback will occur.
  image_decoder_delegate()->RunAllCallbacks();
  EXPECT_EQ(0u, NumPendingRequests());
  EXPECT_EQ(2u, fake_notification_manager()->num_notifications());
  EXPECT_TRUE(fake_notification_manager()->GetNotification(kNotificationIdA));
  EXPECT_TRUE(fake_notification_manager()->GetNotification(kNotificationIdB));
}

TEST_F(NotificationProcessorTest, AddClearAllWithRace) {
  std::vector<proto::Notification> first_set_of_notifications;
  first_set_of_notifications.emplace_back(CreateNewInlineReplyableNotification(
      kNotificationIdA, kInlineReplyIdA, kIconDataA, kSharedImageA));
  first_set_of_notifications.emplace_back(CreateNewInlineReplyableNotification(
      kNotificationIdB, kInlineReplyIdB, kIconDataA));

  notification_processor()->AddNotifications(first_set_of_notifications);

  // Clearing Notifications will invalidate all callbacks in process and
  // immediately clear all pointers.
  notification_processor()->ClearNotificationsAndPendingUpdates();
  EXPECT_EQ(0u, fake_notification_manager()->num_notifications());
  image_decoder_delegate()->RunAllCallbacks();
  EXPECT_EQ(0u, NumPendingRequests());
  EXPECT_EQ(0u, fake_notification_manager()->num_notifications());
  EXPECT_FALSE(fake_notification_manager()->GetNotification(kNotificationIdA));
  EXPECT_FALSE(fake_notification_manager()->GetNotification(kNotificationIdB));
}

TEST_F(NotificationProcessorTest, InteractionBehaviorPopulatedCorrectly) {
  std::vector<proto::Notification> first_set_of_notifications;

  // The notification should be openable if a OPEN action is specified.
  first_set_of_notifications.emplace_back(
      CreateNewInlineReplyableOpenableNotification(
          kNotificationIdA, kInlineReplyIdA,
          Notification::InteractionBehavior::kOpenable));
  notification_processor()->AddNotifications(first_set_of_notifications);
  image_decoder_delegate()->RunAllCallbacks();

  const Notification* notification =
      fake_notification_manager()->GetNotification(kNotificationIdA);
  EXPECT_EQ(Notification::InteractionBehavior::kOpenable,
            notification->interaction_behavior());
  EXPECT_EQ(Notification::Category::kConversation, notification->category());

  // The notification should not specify interaction behaviors if none are
  // available.
  first_set_of_notifications.clear();
  first_set_of_notifications.emplace_back(
      CreateNewInlineReplyableNotification(kNotificationIdA, kInlineReplyIdA));
  notification_processor()->AddNotifications(first_set_of_notifications);
  image_decoder_delegate()->RunAllCallbacks();

  notification = fake_notification_manager()->GetNotification(kNotificationIdA);
  EXPECT_EQ(Notification::InteractionBehavior::kNone,
            notification->interaction_behavior());
  EXPECT_EQ(Notification::Category::kConversation, notification->category());

  // The notification has IncomingCall category if answer action is
  // found.
  first_set_of_notifications.clear();
  first_set_of_notifications.emplace_back(
      CreateIncomingCallNotification(kNotificationIdA));
  notification_processor()->AddNotifications(first_set_of_notifications);
  image_decoder_delegate()->RunAllCallbacks();

  notification = fake_notification_manager()->GetNotification(kNotificationIdA);
  EXPECT_EQ(Notification::Category::kIncomingCall, notification->category());

  // The notification has OngoingCall category if hangup action is
  // found.
  first_set_of_notifications.clear();
  first_set_of_notifications.emplace_back(
      CreateOngoingCallNotification(kNotificationIdC));
  notification_processor()->AddNotifications(first_set_of_notifications);
  image_decoder_delegate()->RunAllCallbacks();

  notification = fake_notification_manager()->GetNotification(kNotificationIdC);
  EXPECT_EQ(Notification::Category::kOngoingCall, notification->category());
}

TEST_F(NotificationProcessorTest, ActionIdMapPopulatedCorrectly) {
  std::vector<proto::Notification> first_set_of_notifications;

  // The inline reply notification should have reply action id.
  first_set_of_notifications.emplace_back(
      CreateNewInlineReplyableNotification(kNotificationIdA, kInlineReplyIdA));
  notification_processor()->AddNotifications(first_set_of_notifications);
  image_decoder_delegate()->RunAllCallbacks();

  const Notification* notification =
      fake_notification_manager()->GetNotification(kNotificationIdA);
  EXPECT_EQ(1u, notification->action_id_map().size());
  EXPECT_EQ(kInlineReplyIdA, notification->action_id_map().at(
                                 Notification::ActionType::kInlineReply));

  // The incoming call notification should have answer and decline action ids.
  first_set_of_notifications.clear();
  first_set_of_notifications.emplace_back(
      CreateIncomingCallNotification(kNotificationIdA));
  notification_processor()->AddNotifications(first_set_of_notifications);
  image_decoder_delegate()->RunAllCallbacks();

  notification = fake_notification_manager()->GetNotification(kNotificationIdA);
  EXPECT_EQ(2u, notification->action_id_map().size());
  EXPECT_EQ(kAnswerActionId, notification->action_id_map().at(
                                 Notification::ActionType::kAnswer));
  EXPECT_EQ(kDeclineActionId, notification->action_id_map().at(
                                  Notification::ActionType::kDecline));

  // The ongoing call notification should have hangup action id.
  first_set_of_notifications.clear();
  first_set_of_notifications.emplace_back(
      CreateOngoingCallNotification(kNotificationIdC));
  notification_processor()->AddNotifications(first_set_of_notifications);
  image_decoder_delegate()->RunAllCallbacks();

  notification = fake_notification_manager()->GetNotification(kNotificationIdC);
  EXPECT_EQ(1u, notification->action_id_map().size());
  EXPECT_EQ(kHangupActionId, notification->action_id_map().at(
                                 Notification::ActionType::kHangup));
}

}  // namespace ash::phonehub