File: ambient_topic_queue_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 (718 lines) | stat: -rw-r--r-- 31,644 bytes parent folder | download | duplicates (7)
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
// 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 "ash/ambient/model/ambient_topic_queue.h"

#include <memory>
#include <string>

#include "ash/ambient/test/ambient_ash_test_base.h"
#include "ash/ambient/test/ambient_topic_queue_test_delegate.h"
#include "ash/public/cpp/ambient/ambient_backend_controller.h"
#include "ash/public/cpp/ambient/fake_ambient_backend_controller_impl.h"
#include "ash/public/cpp/ambient/proto/photo_cache_entry.pb.h"
#include "base/containers/contains.h"
#include "base/run_loop.h"
#include "base/strings/stringprintf.h"
#include "base/test/bind.h"
#include "base/test/scoped_run_loop_timeout.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace ash {
namespace {

using ::testing::Each;
using ::testing::ElementsAre;
using ::testing::Eq;
using ::testing::Field;
using ::testing::IsEmpty;
using ::testing::SizeIs;

constexpr base::TimeDelta kDefaultTopicFetchInterval = base::Seconds(10);

AmbientTopicQueue::WaitResult WaitForTopicsAvailable(AmbientTopicQueue& queue) {
  static constexpr base::TimeDelta kTimeout = base::Seconds(3);
  base::test::ScopedRunLoopTimeout loop_timeout(FROM_HERE, kTimeout);
  AmbientTopicQueue::WaitResult result_out;
  base::RunLoop run_loop;
  base::RepeatingClosure quit_closure = run_loop.QuitClosure();
  queue.WaitForTopicsAvailable(base::BindLambdaForTesting(
      [&result_out, quit_closure](AmbientTopicQueue::WaitResult result_in) {
        result_out = result_in;
        quit_closure.Run();
      }));
  run_loop.Run();
  return result_out;
}

std::vector<AmbientModeTopic> PopUntilEmpty(AmbientTopicQueue& queue) {
  std::vector<AmbientModeTopic> fetched_topics;
  while (!queue.IsEmpty()) {
    fetched_topics.push_back(queue.Pop());
  }
  return fetched_topics;
}

AmbientModeTopic CreateTopic(const std::string& url,
                             const std::string& details,
                             bool is_portrait,
                             const std::string& related_url,
                             const std::string& related_details,
                             ::ambient::TopicType topic_type) {
  AmbientModeTopic topic;
  topic.url = url;
  topic.details = details;
  topic.is_portrait = is_portrait;
  topic.topic_type = topic_type;

  topic.related_image_url = related_url;
  topic.related_details = related_details;
  return topic;
}

MATCHER_P(TopicUrlContainsSize, size, "") {
  return base::Contains(arg.url, size.ToString());
}

class AmbientTopicQueueTest : public AmbientAshTestBase {
 protected:
  static constexpr ::ambient::TopicType kDefaultTopicType =
      ::ambient::TopicType::kGeo;

  void SetUp() override {
    AmbientAshTestBase::SetUp();
    // Disable any sort of pairing on the client by default. Makes it easier to
    // reason about how many topics are in the queue.
    backend_controller()->SetPhotoTopicType(kDefaultTopicType);
  }

  AmbientTopicQueueTestDelegate delegate_;
};

// By default, FakeAmbientBackendControllerImpl returns paired topics.
class AmbientTopicQueuePairedTopicTest : public AmbientAshTestBase {
 protected:
  AmbientTopicQueueTestDelegate delegate_;
};

TEST_F(AmbientTopicQueueTest, WaitForTopicsAvailable) {
  AmbientTopicQueue queue(/*topic_fetch_limit=*/10, /*topic_fetch_size=*/5,
                          kDefaultTopicFetchInterval,
                          /*should_split_topics=*/false, &delegate_,
                          backend_controller());

  ASSERT_THAT(WaitForTopicsAvailable(queue),
              Eq(AmbientTopicQueue::WaitResult::kTopicsAvailable));
  ASSERT_FALSE(queue.IsEmpty());
  AmbientModeTopic topic = queue.Pop();
  EXPECT_THAT(topic.topic_type, Eq(kDefaultTopicType));

  ASSERT_THAT(WaitForTopicsAvailable(queue),
              Eq(AmbientTopicQueue::WaitResult::kTopicsAvailable));
  ASSERT_FALSE(queue.IsEmpty());
  topic = queue.Pop();
  EXPECT_THAT(topic.topic_type, Eq(kDefaultTopicType));
}

TEST_F(AmbientTopicQueueTest, RefillsWhenEmpty) {
  AmbientTopicQueue queue(/*topic_fetch_limit=*/10, /*topic_fetch_size=*/1,
                          kDefaultTopicFetchInterval,
                          /*should_split_topics=*/false, &delegate_,
                          backend_controller());

  ASSERT_THAT(WaitForTopicsAvailable(queue),
              Eq(AmbientTopicQueue::WaitResult::kTopicsAvailable));
  ASSERT_FALSE(queue.IsEmpty());
  queue.Pop();
  ASSERT_TRUE(queue.IsEmpty());

  ASSERT_THAT(WaitForTopicsAvailable(queue),
              Eq(AmbientTopicQueue::WaitResult::kTopicsAvailable));
  ASSERT_FALSE(queue.IsEmpty());
}

TEST_F(AmbientTopicQueueTest, RefillsOnSchedule) {
  AmbientTopicQueue queue(/*topic_fetch_limit=*/10, /*topic_fetch_size=*/1,
                          kDefaultTopicFetchInterval,
                          /*should_split_topics=*/false, &delegate_,
                          backend_controller());

  // Wait for first topic to be pushed.
  ASSERT_THAT(WaitForTopicsAvailable(queue),
              Eq(AmbientTopicQueue::WaitResult::kTopicsAvailable));
  // Second topic should be pushed.
  task_environment()->FastForwardBy(kDefaultTopicFetchInterval);
  ASSERT_THAT(WaitForTopicsAvailable(queue),
              Eq(AmbientTopicQueue::WaitResult::kTopicsAvailable));
  EXPECT_THAT(PopUntilEmpty(queue), SizeIs(2u));
}

TEST_F(AmbientTopicQueueTest, StopsRefillingAtLimit) {
  AmbientTopicQueue queue(/*topic_fetch_limit=*/2, /*topic_fetch_size=*/1,
                          kDefaultTopicFetchInterval,
                          /*should_split_topics=*/false, &delegate_,
                          backend_controller());

  ASSERT_THAT(WaitForTopicsAvailable(queue),
              Eq(AmbientTopicQueue::WaitResult::kTopicsAvailable));
  ASSERT_FALSE(queue.IsEmpty());
  queue.Pop();

  ASSERT_THAT(WaitForTopicsAvailable(queue),
              Eq(AmbientTopicQueue::WaitResult::kTopicsAvailable));
  ASSERT_FALSE(queue.IsEmpty());
  queue.Pop();

  EXPECT_TRUE(queue.IsEmpty());
  EXPECT_THAT(WaitForTopicsAvailable(queue),
              Eq(AmbientTopicQueue::WaitResult::kTopicFetchLimitReached));
}

TEST_F(AmbientTopicQueueTest, StopsRefillingAtLimitForScheduledRefills) {
  AmbientTopicQueue queue(/*topic_fetch_limit=*/2, /*topic_fetch_size=*/1,
                          kDefaultTopicFetchInterval,
                          /*should_split_topics=*/false, &delegate_,
                          backend_controller());

  // Fast forward some huge amount so that the topic limit should be reached
  // via scheduled refills.
  task_environment()->FastForwardBy(10 * kDefaultTopicFetchInterval);

  EXPECT_THAT(PopUntilEmpty(queue), SizeIs(2u));
  EXPECT_THAT(WaitForTopicsAvailable(queue),
              Eq(AmbientTopicQueue::WaitResult::kTopicFetchLimitReached));
}

TEST_F(AmbientTopicQueueTest, NotifiesWhenFetchFailed) {
  backend_controller()->SetFetchScreenUpdateInfoResponseSize(0);
  AmbientTopicQueue queue(/*topic_fetch_limit=*/2, /*topic_fetch_size=*/1,
                          kDefaultTopicFetchInterval,
                          /*should_split_topics=*/false, &delegate_,
                          backend_controller());

  EXPECT_THAT(WaitForTopicsAvailable(queue),
              Eq(AmbientTopicQueue::WaitResult::kTopicFetchBackingOff));
}

TEST_F(AmbientTopicQueueTest, NotifiesWhenBackingOff) {
  backend_controller()->SetFetchScreenUpdateInfoResponseSize(0);
  AmbientTopicQueue queue(/*topic_fetch_limit=*/2, /*topic_fetch_size=*/1,
                          kDefaultTopicFetchInterval,
                          /*should_split_topics=*/false, &delegate_,
                          backend_controller());

  // Fast forward some huge amount to enter backoff state.
  task_environment()->FastForwardBy(10 * kDefaultTopicFetchInterval);
  EXPECT_THAT(WaitForTopicsAvailable(queue),
              Eq(AmbientTopicQueue::WaitResult::kTopicFetchBackingOff));
}

TEST_F(AmbientTopicQueueTest, RetriesAfterFailedFetch) {
  backend_controller()->SetFetchScreenUpdateInfoResponseSize(0);
  AmbientTopicQueue queue(/*topic_fetch_limit=*/2, /*topic_fetch_size=*/1,
                          kDefaultTopicFetchInterval,
                          /*should_split_topics=*/false, &delegate_,
                          backend_controller());

  // Fast forward by some huge amount of time to simulate a series of failures
  // with backoff.
  task_environment()->FastForwardBy(10 * kDefaultTopicFetchInterval);

  // Now it should start succeeding again.
  backend_controller()->SetFetchScreenUpdateInfoResponseSize(1);
  // Fast forward by some huge amount of time to fill the queue.
  task_environment()->FastForwardBy(10 * kDefaultTopicFetchInterval);
  EXPECT_THAT(PopUntilEmpty(queue), SizeIs(2u));
  EXPECT_THAT(WaitForTopicsAvailable(queue),
              Eq(AmbientTopicQueue::WaitResult::kTopicFetchLimitReached));
}

TEST_F(AmbientTopicQueueTest, DoesNotPairTopicsWhenSplitIsSet) {
  backend_controller()->SetPhotoTopicType(
      ::ambient::TopicType::kCulturalInstitute);
  AmbientTopicQueue queue(/*topic_fetch_limit=*/10, /*topic_fetch_size=*/2,
                          kDefaultTopicFetchInterval,
                          /*should_split_topics=*/true, &delegate_,
                          backend_controller());

  ASSERT_THAT(WaitForTopicsAvailable(queue),
              Eq(AmbientTopicQueue::WaitResult::kTopicsAvailable));

  auto fetched_topics = PopUntilEmpty(queue);
  ASSERT_THAT(fetched_topics, SizeIs(2));
  EXPECT_THAT(fetched_topics[0].related_image_url, IsEmpty());
  EXPECT_THAT(fetched_topics[1].related_image_url, IsEmpty());
}

TEST_F(AmbientTopicQueuePairedTopicTest, SplitsIncomingTopics) {
  AmbientTopicQueue queue(/*topic_fetch_limit=*/10, /*topic_fetch_size=*/2,
                          kDefaultTopicFetchInterval,
                          /*should_split_topics=*/true, &delegate_,
                          backend_controller());

  ASSERT_THAT(WaitForTopicsAvailable(queue),
              Eq(AmbientTopicQueue::WaitResult::kTopicsAvailable));

  auto fetched_topics = PopUntilEmpty(queue);
  ASSERT_THAT(fetched_topics, SizeIs(4));
  EXPECT_THAT(fetched_topics,
              Each(Field(&AmbientModeTopic::related_image_url, IsEmpty())));
}

TEST_F(AmbientTopicQueueTest, RequestsPairedPersonalPortraits) {
  backend_controller()->SetPhotoTopicType(::ambient::TopicType::kPersonal);
  backend_controller()->SetPhotoOrientation(/*portrait=*/true);
  AmbientTopicQueue queue(/*topic_fetch_limit=*/10, /*topic_fetch_size=*/1,
                          kDefaultTopicFetchInterval,
                          /*should_split_topics=*/false, &delegate_,
                          backend_controller());

  ASSERT_THAT(WaitForTopicsAvailable(queue),
              Eq(AmbientTopicQueue::WaitResult::kTopicsAvailable));
  ASSERT_FALSE(queue.IsEmpty());
  AmbientModeTopic topic = queue.Pop();
  EXPECT_THAT(topic.url, Not(IsEmpty()));
  EXPECT_THAT(topic.related_image_url, Not(IsEmpty()));
}

TEST_F(AmbientTopicQueueTest, DoesNotRequestPairedPersonalPortraits) {
  backend_controller()->SetPhotoTopicType(::ambient::TopicType::kPersonal);
  backend_controller()->SetPhotoOrientation(/*portrait=*/true);
  AmbientTopicQueue queue(/*topic_fetch_limit=*/10, /*topic_fetch_size=*/1,
                          kDefaultTopicFetchInterval,
                          /*should_split_topics=*/true, &delegate_,
                          backend_controller());

  ASSERT_THAT(WaitForTopicsAvailable(queue),
              Eq(AmbientTopicQueue::WaitResult::kTopicsAvailable));
  ASSERT_FALSE(queue.IsEmpty());
  AmbientModeTopic topic = queue.Pop();
  EXPECT_THAT(topic.url, Not(IsEmpty()));
  EXPECT_THAT(topic.related_image_url, IsEmpty());
}

TEST_F(AmbientTopicQueueTest, ShouldPairLandscapeImages) {
  backend_controller()->set_custom_topic_generator(
      base::BindLambdaForTesting([](int, const gfx::Size&) {
        // Set up 3 featured landscape photos and 3 personal landscape photos.
        // Will output 2 paired topics, having one in featured and personal
        // category.
        std::vector<AmbientModeTopic> topics;
        topics.emplace_back(CreateTopic(
            /*url=*/"topic1_url", /*details=*/"topic1_details",
            /*is_portrait=*/false,
            /*related_url=*/"",
            /*related_details=*/"", ::ambient::TopicType::kPersonal));
        topics.emplace_back(CreateTopic(
            /*url=*/"topic2_url", /*details=*/"topic2_details",
            /*is_portrait=*/false,
            /*related_url=*/"",
            /*related_details=*/"", ::ambient::TopicType::kPersonal));
        topics.emplace_back(CreateTopic(
            /*url=*/"topic3_url", /*details=*/"topic3_details",
            /*is_portrait=*/false,
            /*related_url=*/"",
            /*related_details=*/"topic3_related_details",
            ::ambient::TopicType::kPersonal));

        topics.emplace_back(CreateTopic(
            /*url=*/"topic4_url", /*details=*/"topic4_details",
            /*is_portrait=*/false,
            /*related_url=*/"",
            /*related_details=*/"", ::ambient::TopicType::kFeatured));
        topics.emplace_back(CreateTopic(
            /*url=*/"topic5_url", /*details=*/"topic5_details",
            /*is_portrait=*/false,
            /*related_url=*/"",
            /*related_details=*/"", ::ambient::TopicType::kFeatured));
        topics.emplace_back(CreateTopic(
            /*url=*/"topic6_url", /*details=*/"topic6_details",
            /*is_portrait=*/false,
            /*related_url=*/"",
            /*related_details=*/"", ::ambient::TopicType::kFeatured));
        return topics;
      }));

  AmbientTopicQueue queue(/*topic_fetch_limit=*/10, /*topic_fetch_size=*/10,
                          kDefaultTopicFetchInterval,
                          /*should_split_topics=*/false, &delegate_,
                          backend_controller());

  ASSERT_THAT(WaitForTopicsAvailable(queue),
              Eq(AmbientTopicQueue::WaitResult::kTopicsAvailable));
  std::vector<AmbientModeTopic> fetched_topics = PopUntilEmpty(queue);
  ASSERT_THAT(fetched_topics, SizeIs(2u));

  const int index =
      (fetched_topics[0].topic_type == ::ambient::TopicType::kPersonal) ? 0 : 1;
  EXPECT_EQ(fetched_topics[index].url, "topic1_url");
  EXPECT_EQ(fetched_topics[index].details, "topic1_details");
  EXPECT_FALSE(fetched_topics[index].is_portrait);
  EXPECT_EQ(fetched_topics[index].topic_type, ::ambient::TopicType::kPersonal);
  EXPECT_EQ(fetched_topics[index].related_image_url, "topic2_url");
  EXPECT_EQ(fetched_topics[index].related_details, "topic2_details");

  EXPECT_EQ(fetched_topics[1 - index].url, "topic4_url");
  EXPECT_EQ(fetched_topics[1 - index].details, "topic4_details");
  EXPECT_FALSE(fetched_topics[1 - index].is_portrait);
  EXPECT_EQ(fetched_topics[1 - index].topic_type,
            ::ambient::TopicType::kFeatured);
  EXPECT_EQ(fetched_topics[1 - index].related_image_url, "topic5_url");
  EXPECT_EQ(fetched_topics[1 - index].related_details, "topic5_details");
}

TEST_F(AmbientTopicQueueTest, ShouldNotPairPortraitImages) {
  backend_controller()->set_custom_topic_generator(
      base::BindLambdaForTesting([](int, const gfx::Size&) {
        // Test that topics with portrait photos will not be re-paired. Only
        // topics with landscape photos will be paired. Set up 3 landscape
        // topics and 3 portrait topics. Will output 4 topics, having one paired
        // landscape photo. The 3 portrait topics will not change.
        std::vector<AmbientModeTopic> topics;
        topics.emplace_back(CreateTopic(
            /*url=*/"topic1_url", /*details=*/"topic1_details",
            /*is_portrait=*/true,
            /*related_url=*/"topic1_related_url",
            /*related_details=*/"topic1_related_details",
            ::ambient::TopicType::kPersonal));
        topics.emplace_back(CreateTopic(
            /*url=*/"topic2_url", /*details=*/"topic2_details",
            /*is_portrait=*/true,
            /*related_url=*/"topic2_related_url",
            /*related_details=*/"topic2_related_details",
            ::ambient::TopicType::kPersonal));
        topics.emplace_back(CreateTopic(
            /*url=*/"topic3_url", /*details=*/"topic3_details",
            /*is_portrait=*/true,
            /*related_url=*/"topic3_related_url",
            /*related_details=*/"topic3_related_details",
            ::ambient::TopicType::kPersonal));

        topics.emplace_back(CreateTopic(
            /*url=*/"topic4_url", /*details=*/"topic4_details",
            /*is_portrait=*/false,
            /*related_url=*/"",
            /*related_details=*/"", ::ambient::TopicType::kPersonal));
        topics.emplace_back(CreateTopic(
            /*url=*/"topic5_url", /*details=*/"topic5_details",
            /*is_portrait=*/false,
            /*related_url=*/"",
            /*related_details=*/"", ::ambient::TopicType::kPersonal));
        topics.emplace_back(CreateTopic(
            /*url=*/"topic6_url", /*details=*/"topic6_details",
            /*is_portrait=*/false,
            /*related_url=*/"",
            /*related_details=*/"", ::ambient::TopicType::kPersonal));
        return topics;
      }));

  AmbientTopicQueue queue(/*topic_fetch_limit=*/10, /*topic_fetch_size=*/10,
                          kDefaultTopicFetchInterval,
                          /*should_split_topics=*/false, &delegate_,
                          backend_controller());

  ASSERT_THAT(WaitForTopicsAvailable(queue),
              Eq(AmbientTopicQueue::WaitResult::kTopicsAvailable));
  std::vector<AmbientModeTopic> fetched_topics = PopUntilEmpty(queue);
  ASSERT_THAT(fetched_topics, SizeIs(4u));

  for (size_t index = 0; index < fetched_topics.size(); ++index) {
    if (fetched_topics[index].url == "topic1_url") {
      EXPECT_EQ(fetched_topics[index].url, "topic1_url");
      EXPECT_EQ(fetched_topics[index].details, "topic1_details");
      EXPECT_TRUE(fetched_topics[index].is_portrait);
      EXPECT_EQ(fetched_topics[index].topic_type,
                ::ambient::TopicType::kPersonal);
      EXPECT_EQ(fetched_topics[index].related_image_url, "topic1_related_url");
      EXPECT_EQ(fetched_topics[index].related_details,
                "topic1_related_details");
    } else if (fetched_topics[index].url == "topic2_url") {
      EXPECT_EQ(fetched_topics[index].url, "topic2_url");
      EXPECT_EQ(fetched_topics[index].details, "topic2_details");
      EXPECT_TRUE(fetched_topics[index].is_portrait);
      EXPECT_EQ(fetched_topics[index].topic_type,
                ::ambient::TopicType::kPersonal);
      EXPECT_EQ(fetched_topics[index].related_image_url, "topic2_related_url");
      EXPECT_EQ(fetched_topics[index].related_details,
                "topic2_related_details");
    } else if (fetched_topics[index].url == "topic3_url") {
      EXPECT_EQ(fetched_topics[index].url, "topic3_url");
      EXPECT_EQ(fetched_topics[index].details, "topic3_details");
      EXPECT_TRUE(fetched_topics[index].is_portrait);
      EXPECT_EQ(fetched_topics[index].topic_type,
                ::ambient::TopicType::kPersonal);
      EXPECT_EQ(fetched_topics[index].related_image_url, "topic3_related_url");
      EXPECT_EQ(fetched_topics[index].related_details,
                "topic3_related_details");
    } else if (fetched_topics[index].url == "topic4_url") {
      EXPECT_EQ(fetched_topics[index].url, "topic4_url");
      EXPECT_EQ(fetched_topics[index].details, "topic4_details");
      EXPECT_FALSE(fetched_topics[index].is_portrait);
      EXPECT_EQ(fetched_topics[index].topic_type,
                ::ambient::TopicType::kPersonal);
      EXPECT_EQ(fetched_topics[index].related_image_url, "topic5_url");
      EXPECT_EQ(fetched_topics[index].related_details, "topic5_details");
    } else {
      // Not reached.
      EXPECT_FALSE(true);
    }
  }
}

TEST_F(AmbientTopicQueueTest,
       ShouldNotPairIfNoTwoLandscapeImagesInOneCategory) {
  backend_controller()->set_custom_topic_generator(
      base::BindLambdaForTesting([](int, const gfx::Size&) {
        // Set up 1 personal landscape photo, 1 personal portrait photo, and 1
        // featured landscape photos. Will output 1 topic of 1 personal portrait
        // photo.
        std::vector<AmbientModeTopic> topics;
        topics.emplace_back(CreateTopic(
            /*url=*/"topic1_url", /*details=*/"topic1_details",
            /*is_portrait=*/false,
            /*related_url=*/"",
            /*related_details=*/"", ::ambient::TopicType::kPersonal));
        topics.emplace_back(CreateTopic(
            /*url=*/"topic2_url", /*details=*/"topic2_details",
            /*is_portrait=*/true,
            /*related_url=*/"topic2_related_url",
            /*related_details=*/"topic2_related_details",
            ::ambient::TopicType::kPersonal));
        topics.emplace_back(CreateTopic(
            /*url=*/"topic3_url", /*details=*/"topic3_details",
            /*is_portrait=*/false,
            /*related_url=*/"",
            /*related_details=*/"", ::ambient::TopicType::kFeatured));
        return topics;
      }));

  AmbientTopicQueue queue(/*topic_fetch_limit=*/10, /*topic_fetch_size=*/10,
                          kDefaultTopicFetchInterval,
                          /*should_split_topics=*/false, &delegate_,
                          backend_controller());

  ASSERT_THAT(WaitForTopicsAvailable(queue),
              Eq(AmbientTopicQueue::WaitResult::kTopicsAvailable));
  std::vector<AmbientModeTopic> fetched_topics = PopUntilEmpty(queue);
  ASSERT_THAT(fetched_topics, SizeIs(1u));

  EXPECT_EQ(fetched_topics.size(), 1u);
  EXPECT_EQ(fetched_topics[0].url, "topic2_url");
  EXPECT_EQ(fetched_topics[0].details, "topic2_details");
  EXPECT_TRUE(fetched_topics[0].is_portrait);
  EXPECT_EQ(fetched_topics[0].topic_type, ::ambient::TopicType::kPersonal);
  EXPECT_EQ(fetched_topics[0].related_image_url, "topic2_related_url");
  EXPECT_EQ(fetched_topics[0].related_details, "topic2_related_details");
}

TEST_F(AmbientTopicQueueTest, ShouldNotPairTwoLandscapeImagesInGeoCategory) {
  backend_controller()->set_custom_topic_generator(
      base::BindLambdaForTesting([](int, const gfx::Size&) {
        // Set up 2 Geo landscape photos. Will output 2 topics of Geo photos.
        std::vector<AmbientModeTopic> topics;
        topics.emplace_back(CreateTopic(
            /*url=*/"topic1_url", /*details=*/"topic1_details",
            /*is_portrait=*/false,
            /*related_url=*/"",
            /*related_details=*/"", ::ambient::TopicType::kGeo));
        topics.emplace_back(CreateTopic(
            /*url=*/"topic2_url", /*details=*/"topic2_details",
            /*is_portrait=*/false,
            /*related_url=*/"",
            /*related_details=*/"", ::ambient::TopicType::kGeo));
        return topics;
      }));

  AmbientTopicQueue queue(/*topic_fetch_limit=*/10, /*topic_fetch_size=*/10,
                          kDefaultTopicFetchInterval,
                          /*should_split_topics=*/false, &delegate_,
                          backend_controller());

  ASSERT_THAT(WaitForTopicsAvailable(queue),
              Eq(AmbientTopicQueue::WaitResult::kTopicsAvailable));
  std::vector<AmbientModeTopic> fetched_topics = PopUntilEmpty(queue);
  ASSERT_THAT(fetched_topics, SizeIs(2u));

  const int index = (fetched_topics[0].url == "topic1_url") ? 0 : 1;
  EXPECT_EQ(fetched_topics[index].url, "topic1_url");
  EXPECT_EQ(fetched_topics[index].details, "topic1_details");
  EXPECT_FALSE(fetched_topics[index].is_portrait);
  EXPECT_EQ(fetched_topics[index].topic_type, ::ambient::TopicType::kGeo);
  EXPECT_EQ(fetched_topics[index].related_image_url, "");
  EXPECT_EQ(fetched_topics[index].related_details, "");

  EXPECT_EQ(fetched_topics[1 - index].url, "topic2_url");
  EXPECT_EQ(fetched_topics[1 - index].details, "topic2_details");
  EXPECT_FALSE(fetched_topics[1 - index].is_portrait);
  EXPECT_EQ(fetched_topics[1 - index].topic_type, ::ambient::TopicType::kGeo);
  EXPECT_EQ(fetched_topics[1 - index].related_image_url, "");
  EXPECT_EQ(fetched_topics[1 - index].related_details, "");
}

TEST_F(AmbientTopicQueueTest, UniformTopicSizeDistribution) {
  static constexpr gfx::Size kSize1 = gfx::Size(100, 200);
  static constexpr gfx::Size kSize2 = gfx::Size(200, 100);

  backend_controller()->set_custom_topic_generator(base::BindLambdaForTesting(
      [](int num_topics_requested, const gfx::Size& topic_size) {
        std::vector<AmbientModeTopic> topics;
        for (int i = 0; i < num_topics_requested; ++i) {
          topics.push_back(CreateTopic(
              /*url=*/base::StringPrintf("http://test-url-%s.com",
                                         topic_size.ToString().c_str()),
              /*details=*/"",
              /*is_portrait=*/false,
              /*related_url=*/"",
              /*related_details=*/"", kDefaultTopicType));
        }
        return topics;
      }));

  delegate_.SetTopicSizes({kSize1, kSize2});
  AmbientTopicQueue queue(/*topic_fetch_limit=*/10, /*topic_fetch_size=*/4,
                          kDefaultTopicFetchInterval,
                          /*should_split_topics=*/false, &delegate_,
                          backend_controller());

  // Fast forward some huge amount so that the topic limit should be reached
  // via scheduled refills.
  task_environment()->FastForwardBy(10 * kDefaultTopicFetchInterval);
  std::vector<AmbientModeTopic> fetched_topics = PopUntilEmpty(queue);
  EXPECT_THAT(
      fetched_topics,
      ElementsAre(TopicUrlContainsSize(kSize1), TopicUrlContainsSize(kSize2),
                  TopicUrlContainsSize(kSize1), TopicUrlContainsSize(kSize2),
                  TopicUrlContainsSize(kSize1), TopicUrlContainsSize(kSize2),
                  TopicUrlContainsSize(kSize1), TopicUrlContainsSize(kSize2),
                  TopicUrlContainsSize(kSize1), TopicUrlContainsSize(kSize2)));
}

TEST_F(AmbientTopicQueueTest,
       TrimsIMAXResponseForUniformTopicSizeDistribution) {
  static constexpr gfx::Size kSize1 = gfx::Size(100, 200);
  static constexpr gfx::Size kSize2 = gfx::Size(200, 100);

  backend_controller()->set_custom_topic_generator(base::BindLambdaForTesting(
      [](int num_topics_requested, const gfx::Size& topic_size) {
        int num_topics_to_return = topic_size == kSize1
                                       ? num_topics_requested / 2
                                       : num_topics_requested;
        std::vector<AmbientModeTopic> topics;
        for (int i = 0; i < num_topics_to_return; ++i) {
          topics.push_back(CreateTopic(
              /*url=*/base::StringPrintf("http://test-url-%s.com",
                                         topic_size.ToString().c_str()),
              /*details=*/"",
              /*is_portrait=*/false,
              /*related_url=*/"",
              /*related_details=*/"", kDefaultTopicType));
        }
        return topics;
      }));

  delegate_.SetTopicSizes({kSize1, kSize2});
  AmbientTopicQueue queue(/*topic_fetch_limit=*/10, /*topic_fetch_size=*/4,
                          kDefaultTopicFetchInterval,
                          /*should_split_topics=*/false, &delegate_,
                          backend_controller());

  ASSERT_THAT(WaitForTopicsAvailable(queue),
              Eq(AmbientTopicQueue::WaitResult::kTopicsAvailable));
  std::vector<AmbientModeTopic> fetched_topics = PopUntilEmpty(queue);
  EXPECT_THAT(fetched_topics, ElementsAre(TopicUrlContainsSize(kSize1),
                                          TopicUrlContainsSize(kSize2)));
}

TEST_F(AmbientTopicQueueTest, HandlesEmptyResponseForSingleTopicSize) {
  static constexpr gfx::Size kSize1 = gfx::Size(100, 200);
  static constexpr gfx::Size kSize2 = gfx::Size(200, 100);

  backend_controller()->set_custom_topic_generator(base::BindLambdaForTesting(
      [](int num_topics_requested, const gfx::Size& topic_size) {
        std::vector<AmbientModeTopic> topics;
        if (topic_size == kSize1)
          return topics;

        for (int i = 0; i < num_topics_requested; ++i) {
          topics.push_back(CreateTopic(
              /*url=*/base::StringPrintf("http://test-url-%s.com",
                                         topic_size.ToString().c_str()),
              /*details=*/"",
              /*is_portrait=*/false,
              /*related_url=*/"",
              /*related_details=*/"", kDefaultTopicType));
        }
        return topics;
      }));

  delegate_.SetTopicSizes({kSize1, kSize2});
  AmbientTopicQueue queue(/*topic_fetch_limit=*/10, /*topic_fetch_size=*/4,
                          kDefaultTopicFetchInterval,
                          /*should_split_topics=*/false, &delegate_,
                          backend_controller());

  ASSERT_THAT(WaitForTopicsAvailable(queue),
              Eq(AmbientTopicQueue::WaitResult::kTopicsAvailable));
  std::vector<AmbientModeTopic> fetched_topics = PopUntilEmpty(queue);
  EXPECT_THAT(fetched_topics, ElementsAre(TopicUrlContainsSize(kSize2)));
}

TEST_F(AmbientTopicQueueTest, HandlesManyRequestedTopicSizes) {
  static constexpr gfx::Size kSize1 = gfx::Size(100, 200);
  static constexpr gfx::Size kSize2 = gfx::Size(200, 100);
  static constexpr gfx::Size kSize3 = gfx::Size(300, 400);
  static constexpr gfx::Size kSize4 = gfx::Size(400, 300);

  backend_controller()->set_custom_topic_generator(base::BindLambdaForTesting(
      [](int num_topics_requested, const gfx::Size& topic_size) {
        std::vector<AmbientModeTopic> topics;
        for (int i = 0; i < num_topics_requested; ++i) {
          topics.push_back(CreateTopic(
              /*url=*/base::StringPrintf("http://test-url-%s.com",
                                         topic_size.ToString().c_str()),
              /*details=*/"",
              /*is_portrait=*/false,
              /*related_url=*/"",
              /*related_details=*/"", kDefaultTopicType));
        }
        return topics;
      }));

  delegate_.SetTopicSizes({kSize1, kSize2, kSize3, kSize4});
  AmbientTopicQueue queue(/*topic_fetch_limit=*/10, /*topic_fetch_size=*/2,
                          kDefaultTopicFetchInterval,
                          /*should_split_topics=*/false, &delegate_,
                          backend_controller());

  // Fast forward some huge amount so that the topic limit should be reached
  // via scheduled refills.
  task_environment()->FastForwardBy(10 * kDefaultTopicFetchInterval);
  std::vector<AmbientModeTopic> fetched_topics = PopUntilEmpty(queue);
  EXPECT_THAT(
      fetched_topics,
      ElementsAre(TopicUrlContainsSize(kSize1), TopicUrlContainsSize(kSize2),
                  TopicUrlContainsSize(kSize3), TopicUrlContainsSize(kSize4),
                  TopicUrlContainsSize(kSize1), TopicUrlContainsSize(kSize2),
                  TopicUrlContainsSize(kSize3), TopicUrlContainsSize(kSize4),
                  TopicUrlContainsSize(kSize1), TopicUrlContainsSize(kSize2)));
}

TEST_F(AmbientTopicQueueTest, ZeroTopicFetchLimit) {
  AmbientTopicQueue queue(/*topic_fetch_limit=*/0, /*topic_fetch_size=*/5,
                          kDefaultTopicFetchInterval,
                          /*should_split_topics=*/false, &delegate_,
                          backend_controller());

  EXPECT_THAT(WaitForTopicsAvailable(queue),
              Eq(AmbientTopicQueue::WaitResult::kTopicFetchLimitReached));
  EXPECT_TRUE(queue.IsEmpty());
  task_environment()->FastForwardBy(10 * kDefaultTopicFetchInterval);
  EXPECT_THAT(WaitForTopicsAvailable(queue),
              Eq(AmbientTopicQueue::WaitResult::kTopicFetchLimitReached));
  EXPECT_TRUE(queue.IsEmpty());
}

}  // namespace
}  // namespace ash