File: versioning_message_controller_impl_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 (571 lines) | stat: -rw-r--r-- 24,545 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
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
// Copyright 2025 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/saved_tab_groups/internal/versioning_message_controller_impl.h"

#include <memory>
#include <vector>

#include "base/functional/callback.h"
#include "base/run_loop.h"
#include "base/test/scoped_feature_list.h"
#include "base/test/task_environment.h"
#include "components/data_sharing/public/features.h"
#include "components/prefs/pref_registry_simple.h"
#include "components/prefs/testing_pref_service.h"
#include "components/saved_tab_groups/public/pref_names.h"
#include "components/saved_tab_groups/public/versioning_message_controller.h"
#include "components/saved_tab_groups/test_support/mock_tab_group_sync_service.h"
#include "components/saved_tab_groups/test_support/saved_tab_group_test_utils.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace tab_groups {
using MessageType = VersioningMessageController::MessageType;

namespace {

class VersioningMessageControllerImplTest : public testing::Test {
 public:
  VersioningMessageControllerImplTest()
      : tab_group1_(test::CreateTestSavedTabGroup()) {
    pref_service_.registry()->RegisterBooleanPref(
        prefs::kEligibleForVersionOutOfDateInstantMessage, false);
    pref_service_.registry()->RegisterBooleanPref(
        prefs::kEligibleForVersionOutOfDatePersistentMessage, false);
    pref_service_.registry()->RegisterBooleanPref(
        prefs::kEligibleForVersionUpdatedMessage, false);
    pref_service_.registry()->RegisterBooleanPref(
        prefs::kHasShownAnyVersionOutOfDateMessage, false);

    SetTabGroupSyncServiceExpectation(/*had_shared_tab_groups=*/false,
                                      /*had_open_shared_tab_groups=*/false);
  }

  void SetPref(const std::string& pref_name, bool value) {
    pref_service_.SetBoolean(pref_name, value);
  }

  bool GetPref(const std::string& pref_name) {
    return pref_service_.GetBoolean(pref_name);
  }

  void SetTabGroupSyncServiceExpectation(bool had_shared_tab_groups,
                                         bool had_open_shared_tab_groups) {
    EXPECT_CALL(mock_tab_group_sync_service_,
                HadSharedTabGroupsLastSession(/*open_shared_tab_groups=*/false))
        .WillRepeatedly(testing::Return(had_shared_tab_groups));
    EXPECT_CALL(mock_tab_group_sync_service_,
                HadSharedTabGroupsLastSession(/*open_shared_tab_groups=*/true))
        .WillRepeatedly(testing::Return(had_open_shared_tab_groups));
  }

  void SetTabGroupSyncServiceCurrentExpectation(bool has_shared_tab_groups) {
    if (has_shared_tab_groups) {
      tab_group1_.SetCollaborationId(CollaborationId("collaboration_id"));
    }
    EXPECT_CALL(mock_tab_group_sync_service_, ReadAllGroups())
        .WillRepeatedly(
            testing::Return(std::vector<const SavedTabGroup*>({&tab_group1_})));
  }

  void InitializeController() {
    controller_ = std::make_unique<VersioningMessageControllerImpl>(
        &pref_service_, &mock_tab_group_sync_service_);
    controller_->OnInitialized();
  }

  bool RunShouldShowMessageUiAsync(MessageType message_type) {
    base::RunLoop run_loop;
    bool actual_result = false;

    controller_->ShouldShowMessageUiAsync(
        message_type, base::BindOnce(
                          [](base::RunLoop* run_loop, bool* out_actual_value,
                             bool received_value) {
                            *out_actual_value = received_value;
                            run_loop->Quit();
                          },
                          &run_loop, &actual_result));
    run_loop.Run();

    return actual_result;
  }

  bool ShouldShowMessageUi(MessageType message_type) {
    bool sync_result = controller_->ShouldShowMessageUi(message_type);
    bool async_result = RunShouldShowMessageUiAsync(message_type);
    EXPECT_EQ(sync_result, async_result);
    return sync_result;
  }

  void SetupFeatureList(bool version_up_to_date,
                        bool data_sharing_enabled = true) {
    SetupFeatureListWithUiFlag(/*shared_data_types_enabled=*/version_up_to_date,
                               /*version_ui_flag_enabled=*/!version_up_to_date,
                               /*data_sharing_enabled=*/data_sharing_enabled);
  }

  void SetupFeatureListWithUiFlag(bool shared_data_types_enabled,
                                  bool version_ui_flag_enabled,
                                  bool data_sharing_enabled = true) {
    scoped_feature_list_.Reset();
    std::vector<base::test::FeatureRef> enabled_features;
    std::vector<base::test::FeatureRef> disabled_features;
    if (shared_data_types_enabled) {
      disabled_features.emplace_back(
          data_sharing::features::kSharedDataTypesKillSwitch);
    } else {
      enabled_features.emplace_back(
          data_sharing::features::kSharedDataTypesKillSwitch);
    }
    if (version_ui_flag_enabled) {
      enabled_features.emplace_back(
          data_sharing::features::kDataSharingEnableUpdateChromeUI);
    } else {
      disabled_features.emplace_back(
          data_sharing::features::kDataSharingEnableUpdateChromeUI);
    }
    if (data_sharing_enabled) {
      enabled_features.emplace_back(
          data_sharing::features::kDataSharingFeature);
    } else {
      disabled_features.emplace_back(
          data_sharing::features::kDataSharingFeature);
    }

    scoped_feature_list_.InitWithFeatures(enabled_features, disabled_features);
  }

 protected:
  base::test::TaskEnvironment task_environment_;
  TestingPrefServiceSimple pref_service_;
  MockTabGroupSyncService mock_tab_group_sync_service_;
  std::unique_ptr<VersioningMessageControllerImpl> controller_;
  base::test::ScopedFeatureList scoped_feature_list_;
  SavedTabGroup tab_group1_;
};

TEST_F(VersioningMessageControllerImplTest,
       ShouldShowMessageUiAsync_VersionOutOfDate_HasSharedGroups) {
  SetupFeatureList(/*version_up_to_date=*/false);
  SetTabGroupSyncServiceExpectation(/*had_shared_tab_groups=*/true,
                                    /*had_open_shared_tab_groups=*/true);
  InitializeController();
  EXPECT_TRUE(
      ShouldShowMessageUi(MessageType::VERSION_OUT_OF_DATE_INSTANT_MESSAGE));
  EXPECT_TRUE(
      ShouldShowMessageUi(MessageType::VERSION_OUT_OF_DATE_PERSISTENT_MESSAGE));
  EXPECT_FALSE(ShouldShowMessageUi(MessageType::VERSION_UPDATED_MESSAGE));
}

TEST_F(VersioningMessageControllerImplTest,
       ShouldShowMessageUiAsync_VersionOutOfDate_HasSharedGroups_NoUiFlag) {
  SetupFeatureListWithUiFlag(/*shared_data_types_enabled=*/false,
                             /*version_ui_flag_enabled=*/false);
  SetTabGroupSyncServiceExpectation(/*had_shared_tab_groups=*/true,
                                    /*had_open_shared_tab_groups=*/true);
  InitializeController();
  EXPECT_FALSE(
      ShouldShowMessageUi(MessageType::VERSION_OUT_OF_DATE_INSTANT_MESSAGE));
  EXPECT_FALSE(
      ShouldShowMessageUi(MessageType::VERSION_OUT_OF_DATE_PERSISTENT_MESSAGE));
  EXPECT_FALSE(ShouldShowMessageUi(MessageType::VERSION_UPDATED_MESSAGE));

  // Verify prefs.
  EXPECT_FALSE(GetPref(prefs::kEligibleForVersionOutOfDateInstantMessage));
  EXPECT_FALSE(GetPref(prefs::kEligibleForVersionOutOfDatePersistentMessage));
  EXPECT_FALSE(GetPref(prefs::kEligibleForVersionUpdatedMessage));
  EXPECT_FALSE(GetPref(prefs::kHasShownAnyVersionOutOfDateMessage));
}

TEST_F(
    VersioningMessageControllerImplTest,
    ShouldShowMessageUiAsync_VersionOutOfDate_HasSharedGroups_InvalidStateOfFlags) {
  // This is a test for invalid combinations of flags where data type is enabled
  // but update chrome UI flag is also incorrectly enabled.
  SetupFeatureListWithUiFlag(/*shared_data_types_enabled=*/true,
                             /*version_ui_flag_enabled=*/true);
  SetTabGroupSyncServiceExpectation(/*had_shared_tab_groups=*/true,
                                    /*had_open_shared_tab_groups=*/true);
  InitializeController();
  EXPECT_FALSE(
      ShouldShowMessageUi(MessageType::VERSION_OUT_OF_DATE_INSTANT_MESSAGE));
  EXPECT_FALSE(
      ShouldShowMessageUi(MessageType::VERSION_OUT_OF_DATE_PERSISTENT_MESSAGE));
  EXPECT_FALSE(ShouldShowMessageUi(MessageType::VERSION_UPDATED_MESSAGE));

  // Verify prefs.
  EXPECT_FALSE(GetPref(prefs::kEligibleForVersionOutOfDateInstantMessage));
  EXPECT_FALSE(GetPref(prefs::kEligibleForVersionOutOfDatePersistentMessage));
  EXPECT_FALSE(GetPref(prefs::kEligibleForVersionUpdatedMessage));
  EXPECT_FALSE(GetPref(prefs::kHasShownAnyVersionOutOfDateMessage));
}

TEST_F(VersioningMessageControllerImplTest,
       ShouldShowMessageUiAsync_VersionOutOfDate_NoSharedGroups) {
  SetupFeatureList(/*version_up_to_date=*/false);
  SetTabGroupSyncServiceExpectation(/*had_shared_tab_groups=*/false,
                                    /*had_open_shared_tab_groups=*/false);
  InitializeController();
  EXPECT_FALSE(
      ShouldShowMessageUi(MessageType::VERSION_OUT_OF_DATE_INSTANT_MESSAGE));
  EXPECT_FALSE(
      ShouldShowMessageUi(MessageType::VERSION_OUT_OF_DATE_PERSISTENT_MESSAGE));
  EXPECT_FALSE(ShouldShowMessageUi(MessageType::VERSION_UPDATED_MESSAGE));
}

TEST_F(VersioningMessageControllerImplTest,
       ShouldShowMessageUiAsync_VersionOutOfDate_NoOpenSharedGroups) {
  SetupFeatureList(/*version_up_to_date=*/false);
  SetTabGroupSyncServiceExpectation(/*had_shared_tab_groups=*/true,
                                    /*had_open_shared_tab_groups=*/false);
  InitializeController();
  EXPECT_FALSE(
      ShouldShowMessageUi(MessageType::VERSION_OUT_OF_DATE_INSTANT_MESSAGE));
  EXPECT_TRUE(
      ShouldShowMessageUi(MessageType::VERSION_OUT_OF_DATE_PERSISTENT_MESSAGE));
  EXPECT_FALSE(ShouldShowMessageUi(MessageType::VERSION_UPDATED_MESSAGE));
}

TEST_F(
    VersioningMessageControllerImplTest,
    ShouldShowMessageUiAsync_VersionOutOfDate_HasShownButNotDismissedBefore) {
  SetupFeatureList(/*version_up_to_date=*/false);
  SetPref(prefs::kHasShownAnyVersionOutOfDateMessage, true);
  SetPref(prefs::kEligibleForVersionOutOfDateInstantMessage, false);
  SetPref(prefs::kEligibleForVersionOutOfDatePersistentMessage, true);

  InitializeController();
  EXPECT_FALSE(
      ShouldShowMessageUi(MessageType::VERSION_OUT_OF_DATE_INSTANT_MESSAGE));
  EXPECT_TRUE(
      ShouldShowMessageUi(MessageType::VERSION_OUT_OF_DATE_PERSISTENT_MESSAGE));
  EXPECT_FALSE(ShouldShowMessageUi(MessageType::VERSION_UPDATED_MESSAGE));
}

TEST_F(
    VersioningMessageControllerImplTest,
    ShouldShowMessageUiAsync_VersionUpdatedMessage_VersionUpdated_NotShownIfOutOfDateMessageNeverShown) {
  SetupFeatureList(/*version_up_to_date=*/true);
  SetPref(prefs::kHasShownAnyVersionOutOfDateMessage, false);
  SetTabGroupSyncServiceCurrentExpectation(/*has_shared_tab_groups*/ true);
  InitializeController();

  EXPECT_FALSE(
      ShouldShowMessageUi(MessageType::VERSION_OUT_OF_DATE_INSTANT_MESSAGE));
  EXPECT_FALSE(
      ShouldShowMessageUi(MessageType::VERSION_OUT_OF_DATE_PERSISTENT_MESSAGE));
  EXPECT_FALSE(ShouldShowMessageUi(MessageType::VERSION_UPDATED_MESSAGE));
}

TEST_F(
    VersioningMessageControllerImplTest,
    ShouldShowMessageUiAsync_VersionUpdatedMessage_VersionUpdated_ShownIfOutOfDateMessageShownBefore) {
  SetupFeatureList(/*version_up_to_date=*/true);
  SetPref(prefs::kHasShownAnyVersionOutOfDateMessage, true);
  SetTabGroupSyncServiceCurrentExpectation(/*has_shared_tab_groups*/ true);
  InitializeController();

  EXPECT_FALSE(
      ShouldShowMessageUi(MessageType::VERSION_OUT_OF_DATE_INSTANT_MESSAGE));
  EXPECT_FALSE(
      ShouldShowMessageUi(MessageType::VERSION_OUT_OF_DATE_PERSISTENT_MESSAGE));
  EXPECT_TRUE(ShouldShowMessageUi(MessageType::VERSION_UPDATED_MESSAGE));
}

TEST_F(VersioningMessageControllerImplTest, OnMessageUiShown_InstantMessage) {
  SetupFeatureList(/*version_up_to_date=*/false);
  SetPref(prefs::kEligibleForVersionOutOfDateInstantMessage, true);
  SetPref(prefs::kHasShownAnyVersionOutOfDateMessage, false);
  InitializeController();
  controller_->OnMessageUiShown(
      MessageType::VERSION_OUT_OF_DATE_INSTANT_MESSAGE);
  EXPECT_FALSE(GetPref(prefs::kEligibleForVersionOutOfDateInstantMessage));
  EXPECT_TRUE(GetPref(prefs::kHasShownAnyVersionOutOfDateMessage));
}

TEST_F(VersioningMessageControllerImplTest,
       OnMessageUiShown_PersistentMessage) {
  SetupFeatureList(/*version_up_to_date=*/false);
  SetPref(prefs::kEligibleForVersionOutOfDatePersistentMessage, true);
  SetPref(prefs::kHasShownAnyVersionOutOfDateMessage, true);
  InitializeController();
  controller_->OnMessageUiShown(
      MessageType::VERSION_OUT_OF_DATE_PERSISTENT_MESSAGE);
  EXPECT_TRUE(GetPref(prefs::kEligibleForVersionOutOfDatePersistentMessage));
  EXPECT_TRUE(GetPref(prefs::kHasShownAnyVersionOutOfDateMessage));
}

TEST_F(VersioningMessageControllerImplTest,
       OnMessageUiShown_VersionUpdatedMessage) {
  SetupFeatureList(/*version_up_to_date=*/true);
  SetPref(prefs::kEligibleForVersionUpdatedMessage, true);
  InitializeController();
  controller_->OnMessageUiShown(MessageType::VERSION_UPDATED_MESSAGE);
  EXPECT_FALSE(GetPref(prefs::kEligibleForVersionUpdatedMessage));
}

TEST_F(VersioningMessageControllerImplTest,
       OnMessageUiDismissed_PersistentMessage) {
  SetupFeatureList(/*version_up_to_date=*/true);
  SetPref(prefs::kEligibleForVersionOutOfDatePersistentMessage, true);
  InitializeController();
  controller_->OnMessageUiDismissed(
      MessageType::VERSION_OUT_OF_DATE_PERSISTENT_MESSAGE);
  EXPECT_FALSE(GetPref(prefs::kEligibleForVersionOutOfDatePersistentMessage));
}

TEST_F(VersioningMessageControllerImplTest,
       Initialization_QueuedCallbacksFlushed) {
  SetupFeatureList(/*version_up_to_date=*/true);
  controller_ = std::make_unique<VersioningMessageControllerImpl>(
      &pref_service_, &mock_tab_group_sync_service_);
  bool callback_called = false;
  controller_->ShouldShowMessageUiAsync(
      MessageType::VERSION_OUT_OF_DATE_INSTANT_MESSAGE,
      base::BindOnce(
          [](bool* callback_called, bool result) { *callback_called = true; },
          &callback_called));
  EXPECT_FALSE(callback_called);
  controller_->OnInitialized();
  EXPECT_TRUE(callback_called);
}

TEST_F(VersioningMessageControllerImplTest, MultipleRestarts) {
  {
    // Start with version up-to-date.
    SetupFeatureList(/*version_up_to_date=*/true);
    SetTabGroupSyncServiceExpectation(/*had_shared_tab_groups=*/true,
                                      /*had_open_shared_tab_groups=*/true);
    SetTabGroupSyncServiceCurrentExpectation(/*has_shared_tab_groups*/ true);
    InitializeController();

    EXPECT_FALSE(
        ShouldShowMessageUi(MessageType::VERSION_OUT_OF_DATE_INSTANT_MESSAGE));
    EXPECT_FALSE(ShouldShowMessageUi(
        MessageType::VERSION_OUT_OF_DATE_PERSISTENT_MESSAGE));
    EXPECT_FALSE(ShouldShowMessageUi(MessageType::VERSION_UPDATED_MESSAGE));
  }

  {
    // Restart with version out-of-date.
    SetupFeatureList(/*version_up_to_date=*/false);
    SetTabGroupSyncServiceExpectation(/*had_shared_tab_groups=*/true,
                                      /*had_open_shared_tab_groups=*/true);
    SetTabGroupSyncServiceCurrentExpectation(/*has_shared_tab_groups*/ false);
    InitializeController();

    EXPECT_TRUE(
        ShouldShowMessageUi(MessageType::VERSION_OUT_OF_DATE_INSTANT_MESSAGE));
    EXPECT_TRUE(ShouldShowMessageUi(
        MessageType::VERSION_OUT_OF_DATE_PERSISTENT_MESSAGE));
    EXPECT_FALSE(ShouldShowMessageUi(MessageType::VERSION_UPDATED_MESSAGE));

    // Don't show the messages yet.
  }

  {
    // Restart again with version out-of-date. Since no message was actually
    // shown last session, they should still be eligible to be shown.
    SetupFeatureList(/*version_up_to_date=*/false);
    SetTabGroupSyncServiceExpectation(/*had_shared_tab_groups=*/false,
                                      /*had_open_shared_tab_groups=*/false);
    SetTabGroupSyncServiceCurrentExpectation(/*has_shared_tab_groups*/ false);
    InitializeController();

    EXPECT_TRUE(
        ShouldShowMessageUi(MessageType::VERSION_OUT_OF_DATE_INSTANT_MESSAGE));
    EXPECT_TRUE(ShouldShowMessageUi(
        MessageType::VERSION_OUT_OF_DATE_PERSISTENT_MESSAGE));
    EXPECT_FALSE(ShouldShowMessageUi(MessageType::VERSION_UPDATED_MESSAGE));

    controller_->OnMessageUiShown(
        MessageType::VERSION_OUT_OF_DATE_INSTANT_MESSAGE);
    controller_->OnMessageUiShown(
        MessageType::VERSION_OUT_OF_DATE_PERSISTENT_MESSAGE);
  }

  {
    // Restart with version out-of-date. Only persistent message can be shown
    // here since it isn't dismissed yet.
    SetupFeatureList(/*version_up_to_date=*/false);
    SetTabGroupSyncServiceExpectation(/*had_shared_tab_groups=*/false,
                                      /*had_open_shared_tab_groups=*/false);
    SetTabGroupSyncServiceCurrentExpectation(/*has_shared_tab_groups*/ false);
    InitializeController();

    EXPECT_FALSE(
        ShouldShowMessageUi(MessageType::VERSION_OUT_OF_DATE_INSTANT_MESSAGE));
    EXPECT_TRUE(ShouldShowMessageUi(
        MessageType::VERSION_OUT_OF_DATE_PERSISTENT_MESSAGE));
    EXPECT_FALSE(ShouldShowMessageUi(MessageType::VERSION_UPDATED_MESSAGE));

    controller_->OnMessageUiShown(
        MessageType::VERSION_OUT_OF_DATE_PERSISTENT_MESSAGE);
    controller_->OnMessageUiDismissed(
        MessageType::VERSION_OUT_OF_DATE_PERSISTENT_MESSAGE);
  }

  {
    // Restart with version out-of-date. No message can be shown since all the
    // message have been already shown and dismissed.
    SetupFeatureList(/*version_up_to_date=*/false);
    SetTabGroupSyncServiceExpectation(/*had_shared_tab_groups=*/false,
                                      /*had_open_shared_tab_groups=*/false);
    SetTabGroupSyncServiceCurrentExpectation(/*has_shared_tab_groups*/ false);
    InitializeController();

    EXPECT_FALSE(
        ShouldShowMessageUi(MessageType::VERSION_OUT_OF_DATE_INSTANT_MESSAGE));
    EXPECT_FALSE(ShouldShowMessageUi(
        MessageType::VERSION_OUT_OF_DATE_PERSISTENT_MESSAGE));
    EXPECT_FALSE(ShouldShowMessageUi(MessageType::VERSION_UPDATED_MESSAGE));
  }

  {
    // Restart with version up-to-date. Expect IPH to be eligible to be shown.
    SetupFeatureList(/*version_up_to_date=*/true);
    SetTabGroupSyncServiceExpectation(/*had_shared_tab_groups=*/false,
                                      /*had_open_shared_tab_groups=*/false);
    SetTabGroupSyncServiceCurrentExpectation(/*has_shared_tab_groups*/ true);
    InitializeController();

    EXPECT_FALSE(
        ShouldShowMessageUi(MessageType::VERSION_OUT_OF_DATE_INSTANT_MESSAGE));
    EXPECT_FALSE(ShouldShowMessageUi(
        MessageType::VERSION_OUT_OF_DATE_PERSISTENT_MESSAGE));
    EXPECT_TRUE(ShouldShowMessageUi(MessageType::VERSION_UPDATED_MESSAGE));

    // Don't show the IPH yet. Show it in next session.
  }

  {
    // Restart with version up-to-date. IPH wasn't shown in last session, so it
    // should still be eligible to be shown.
    SetupFeatureList(/*version_up_to_date=*/true);
    SetTabGroupSyncServiceExpectation(/*had_shared_tab_groups=*/false,
                                      /*had_open_shared_tab_groups=*/false);
    SetTabGroupSyncServiceCurrentExpectation(/*has_shared_tab_groups*/ true);
    InitializeController();

    EXPECT_FALSE(
        ShouldShowMessageUi(MessageType::VERSION_OUT_OF_DATE_INSTANT_MESSAGE));
    EXPECT_FALSE(ShouldShowMessageUi(
        MessageType::VERSION_OUT_OF_DATE_PERSISTENT_MESSAGE));
    EXPECT_TRUE(ShouldShowMessageUi(MessageType::VERSION_UPDATED_MESSAGE));

    controller_->OnMessageUiShown(MessageType::VERSION_UPDATED_MESSAGE);
  }

  {
    // Restart with version up-to-date. IPH shouldn't be shown as it was shown
    // before.
    SetupFeatureList(/*version_up_to_date=*/true);
    SetTabGroupSyncServiceExpectation(/*had_shared_tab_groups=*/false,
                                      /*had_open_shared_tab_groups=*/false);
    SetTabGroupSyncServiceCurrentExpectation(/*has_shared_tab_groups*/ true);
    InitializeController();

    EXPECT_FALSE(
        ShouldShowMessageUi(MessageType::VERSION_OUT_OF_DATE_INSTANT_MESSAGE));
    EXPECT_FALSE(ShouldShowMessageUi(
        MessageType::VERSION_OUT_OF_DATE_PERSISTENT_MESSAGE));
    EXPECT_FALSE(ShouldShowMessageUi(MessageType::VERSION_UPDATED_MESSAGE));
  }
}

TEST_F(VersioningMessageControllerImplTest, MultipleRestarts_ButWithoutUiFlag) {
  {
    // Start test with version out-of-date, but UI flag enabled.
    SetupFeatureListWithUiFlag(/*shared_data_types_enabled=*/false,
                               /*version_ui_flag_enabled=*/true);
    SetTabGroupSyncServiceExpectation(/*had_shared_tab_groups=*/true,
                                      /*had_open_shared_tab_groups=*/true);
    SetTabGroupSyncServiceCurrentExpectation(/*has_shared_tab_groups*/ false);
    InitializeController();

    EXPECT_TRUE(
        ShouldShowMessageUi(MessageType::VERSION_OUT_OF_DATE_INSTANT_MESSAGE));
    EXPECT_TRUE(ShouldShowMessageUi(
        MessageType::VERSION_OUT_OF_DATE_PERSISTENT_MESSAGE));
    EXPECT_FALSE(ShouldShowMessageUi(MessageType::VERSION_UPDATED_MESSAGE));

    controller_->OnMessageUiShown(
        MessageType::VERSION_OUT_OF_DATE_INSTANT_MESSAGE);
    controller_->OnMessageUiShown(
        MessageType::VERSION_OUT_OF_DATE_PERSISTENT_MESSAGE);
  }

  {
    // Restart with version out-of-date, UI flag disabled.
    SetupFeatureListWithUiFlag(/*shared_data_types_enabled=*/false,
                               /*version_ui_flag_enabled=*/false);
    SetTabGroupSyncServiceExpectation(/*had_shared_tab_groups=*/false,
                                      /*had_open_shared_tab_groups=*/false);
    SetTabGroupSyncServiceCurrentExpectation(/*has_shared_tab_groups*/ false);
    InitializeController();

    EXPECT_FALSE(
        ShouldShowMessageUi(MessageType::VERSION_OUT_OF_DATE_INSTANT_MESSAGE));
    EXPECT_FALSE(ShouldShowMessageUi(
        MessageType::VERSION_OUT_OF_DATE_PERSISTENT_MESSAGE));
    EXPECT_FALSE(ShouldShowMessageUi(MessageType::VERSION_UPDATED_MESSAGE));
  }

  {
    // Restart with version out-of-date, UI flag enabled again.
    SetupFeatureListWithUiFlag(/*shared_data_types_enabled=*/false,
                               /*version_ui_flag_enabled=*/true);
    SetTabGroupSyncServiceExpectation(/*had_shared_tab_groups=*/false,
                                      /*had_open_shared_tab_groups=*/false);
    SetTabGroupSyncServiceCurrentExpectation(/*has_shared_tab_groups*/ false);
    InitializeController();

    EXPECT_FALSE(
        ShouldShowMessageUi(MessageType::VERSION_OUT_OF_DATE_INSTANT_MESSAGE));
    EXPECT_TRUE(ShouldShowMessageUi(
        MessageType::VERSION_OUT_OF_DATE_PERSISTENT_MESSAGE));
    EXPECT_FALSE(ShouldShowMessageUi(MessageType::VERSION_UPDATED_MESSAGE));
  }

  {
    // Restart with version up-to-date. IPH wasn't shown in last session, so it
    // should still be eligible to be shown.
    SetupFeatureList(/*version_up_to_date=*/true);
    SetTabGroupSyncServiceExpectation(/*had_shared_tab_groups=*/false,
                                      /*had_open_shared_tab_groups=*/false);
    SetTabGroupSyncServiceCurrentExpectation(/*has_shared_tab_groups*/ true);
    InitializeController();

    EXPECT_FALSE(
        ShouldShowMessageUi(MessageType::VERSION_OUT_OF_DATE_INSTANT_MESSAGE));
    EXPECT_FALSE(ShouldShowMessageUi(
        MessageType::VERSION_OUT_OF_DATE_PERSISTENT_MESSAGE));
    EXPECT_TRUE(ShouldShowMessageUi(MessageType::VERSION_UPDATED_MESSAGE));
  }
}

TEST_F(VersioningMessageControllerImplTest,
       TestShouldShowMessageUi_DataSharingDisabled) {
  SetupFeatureList(/*version_up_to_date=*/false,
                   /*data_sharing_enabled=*/false);
  SetTabGroupSyncServiceExpectation(/*had_shared_tab_groups=*/true,
                                    /*had_open_shared_tab_groups=*/true);
  InitializeController();

  EXPECT_FALSE(
      ShouldShowMessageUi(MessageType::VERSION_OUT_OF_DATE_INSTANT_MESSAGE));
  EXPECT_FALSE(
      ShouldShowMessageUi(MessageType::VERSION_OUT_OF_DATE_PERSISTENT_MESSAGE));
  EXPECT_FALSE(ShouldShowMessageUi(MessageType::VERSION_UPDATED_MESSAGE));

  // Verify prefs are not changed on startup in this state.
  EXPECT_FALSE(GetPref(prefs::kEligibleForVersionOutOfDateInstantMessage));
  EXPECT_FALSE(GetPref(prefs::kEligibleForVersionOutOfDatePersistentMessage));
  EXPECT_FALSE(GetPref(prefs::kEligibleForVersionUpdatedMessage));
  EXPECT_FALSE(GetPref(prefs::kHasShownAnyVersionOutOfDateMessage));
}

}  // namespace
}  // namespace tab_groups