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

ash / quick_insert / model / quick_insert_emoji_history_model_unittest.cc [blame]

// Copyright 2024 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/quick_insert/model/quick_insert_emoji_history_model.h"

#include "ash/constants/ash_pref_names.h"
#include "base/json/values_util.h"
#include "base/test/simple_test_clock.h"
#include "base/time/time.h"
#include "base/values.h"
#include "components/prefs/pref_service.h"
#include "components/prefs/scoped_user_pref_update.h"
#include "components/sync_preferences/testing_pref_service_syncable.h"
#include "testing/gmock/include/gmock/gmock-matchers.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/base/emoji/emoji_panel_helper.h"

namespace ash {
namespace {

using ::testing::ElementsAre;
using HistoryItem = QuickInsertEmojiHistoryModel::EmojiHistoryItem;

base::Time TimeFromMicroSeconds(int64_t microseconds) {
  return base::Time::FromDeltaSinceWindowsEpoch(
      base::Microseconds(microseconds));
}

class QuickInsertEmojiHistoryModelTest : public testing::Test {
 public:
  QuickInsertEmojiHistoryModelTest() {
    prefs_.registry()->RegisterDictionaryPref(prefs::kEmojiPickerHistory);
  }

  PrefService* pref_service() { return &prefs_; }

 private:
  sync_preferences::TestingPrefServiceSyncable prefs_;
};

TEST_F(QuickInsertEmojiHistoryModelTest, ReturnsRecentEmojisFromPrefs) {
  ScopedDictPrefUpdate update(pref_service(), prefs::kEmojiPickerHistory);
  update->Set(
      "emoji",
      base::Value::List()
          .Append(base::Value::Dict().Set("text", "abc").Set("timestamp", "10"))
          .Append(
              base::Value::Dict().Set("text", "xyz").Set("timestamp", "5")));
  QuickInsertEmojiHistoryModel model(pref_service());

  EXPECT_THAT(
      model.GetRecentEmojis(ui::EmojiPickerCategory::kEmojis),
      ElementsAre(HistoryItem{.text = "abc",
                              .category = ui::EmojiPickerCategory::kEmojis,
                              .timestamp = TimeFromMicroSeconds(10)},
                  HistoryItem{.text = "xyz",
                              .category = ui::EmojiPickerCategory::kEmojis,
                              .timestamp = TimeFromMicroSeconds(5)}));
}

TEST_F(QuickInsertEmojiHistoryModelTest, AddsNewRecentEmoji) {
  ScopedDictPrefUpdate update(pref_service(), prefs::kEmojiPickerHistory);
  update->Set(
      "emoji",
      base::Value::List()
          .Append(base::Value::Dict().Set("text", "abc").Set("timestamp", "10"))
          .Append(
              base::Value::Dict().Set("text", "xyz").Set("timestamp", "5")));
  base::SimpleTestClock clock;
  QuickInsertEmojiHistoryModel model(pref_service(), &clock);
  clock.SetNow(TimeFromMicroSeconds(20));

  model.UpdateRecentEmoji(ui::EmojiPickerCategory::kEmojis, "def");

  EXPECT_THAT(
      model.GetRecentEmojis(ui::EmojiPickerCategory::kEmojis),
      ElementsAre(HistoryItem{.text = "def",
                              .category = ui::EmojiPickerCategory::kEmojis,
                              .timestamp = TimeFromMicroSeconds(20)},
                  HistoryItem{.text = "abc",
                              .category = ui::EmojiPickerCategory::kEmojis,
                              .timestamp = TimeFromMicroSeconds(10)},
                  HistoryItem{.text = "xyz",
                              .category = ui::EmojiPickerCategory::kEmojis,
                              .timestamp = TimeFromMicroSeconds(5)}));
}

TEST_F(QuickInsertEmojiHistoryModelTest, AddsExistingRecentEmoji) {
  ScopedDictPrefUpdate update(pref_service(), prefs::kEmojiPickerHistory);
  update->Set(
      "emoji",
      base::Value::List()
          .Append(base::Value::Dict().Set("text", "abc").Set("timestamp", "10"))
          .Append(
              base::Value::Dict().Set("text", "xyz").Set("timestamp", "5")));
  base::SimpleTestClock clock;
  QuickInsertEmojiHistoryModel model(pref_service(), &clock);
  clock.SetNow(TimeFromMicroSeconds(20));

  model.UpdateRecentEmoji(ui::EmojiPickerCategory::kEmojis, "xyz");

  EXPECT_THAT(
      model.GetRecentEmojis(ui::EmojiPickerCategory::kEmojis),
      ElementsAre(HistoryItem{.text = "xyz",
                              .category = ui::EmojiPickerCategory::kEmojis,
                              .timestamp = TimeFromMicroSeconds(20)},
                  HistoryItem{.text = "abc",
                              .category = ui::EmojiPickerCategory::kEmojis,
                              .timestamp = TimeFromMicroSeconds(10)}));
}

TEST_F(QuickInsertEmojiHistoryModelTest, AddsRecentEmojiEmptyHistory) {
  base::SimpleTestClock clock;
  QuickInsertEmojiHistoryModel model(pref_service(), &clock);
  clock.SetNow(TimeFromMicroSeconds(5));

  model.UpdateRecentEmoji(ui::EmojiPickerCategory::kEmojis, "abc");

  EXPECT_THAT(
      model.GetRecentEmojis(ui::EmojiPickerCategory::kEmojis),
      ElementsAre(HistoryItem{.text = "abc",
                              .category = ui::EmojiPickerCategory::kEmojis,
                              .timestamp = TimeFromMicroSeconds(5)}));
}

}  // namespace
}  // namespace ash