File: emoji_page_handler_unittest.cc

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; 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,811; 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 (131 lines) | stat: -rw-r--r-- 5,078 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
// 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 "chrome/browser/ui/webui/ash/emoji/emoji_page_handler.h"

#include "ash/constants/ash_pref_names.h"
#include "base/json/values_util.h"
#include "base/test/test_future.h"
#include "base/time/time.h"
#include "chrome/test/base/testing_profile.h"
#include "components/prefs/pref_service.h"
#include "content/public/test/browser_task_environment.h"
#include "content/public/test/test_web_contents_factory.h"
#include "content/public/test/test_web_ui.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace ash {
namespace {

using ::emoji_picker::mojom::EmojiVariant;
using ::emoji_picker::mojom::EmojiVariantPtr;
using ::emoji_picker::mojom::HistoryItem;
using ::emoji_picker::mojom::HistoryItemPtr;
using ::emoji_picker::mojom::Category::kEmojis;
using ::testing::AllOf;
using ::testing::ElementsAre;
using ::testing::Field;
using ::testing::IsEmpty;
using ::testing::Pointee;

base::Time TimeFromSeconds(int64_t seconds) {
  return base::Time::FromDeltaSinceWindowsEpoch(base::Seconds(seconds));
}

class EmojiPageHandlerTest : public ::testing::Test {
 public:
  void SetUp() override {
    profile_ = std::make_unique<TestingProfile>();
    web_ui_.set_web_contents(
        web_contents_factory_.CreateWebContents(profile_.get()));
  }

 protected:
  content::BrowserTaskEnvironment task_environment_;
  std::unique_ptr<TestingProfile> profile_;
  content::TestWebUI web_ui_;
  content::TestWebContentsFactory web_contents_factory_;
};

TEST_F(EmojiPageHandlerTest, UpdatesEmojiHistoryInPrefs) {
  mojo::PendingReceiver<emoji_picker::mojom::PageHandler> receiver;
  EmojiPageHandler handler(std::move(receiver), &web_ui_, nullptr, false, false,
                           kEmojis, "");

  std::vector<HistoryItemPtr> history;
  history.push_back(HistoryItem::New("abc", TimeFromSeconds(10)));
  history.push_back(HistoryItem::New("xyz", TimeFromSeconds(5)));
  handler.UpdateHistoryInPrefs(kEmojis, std::move(history));

  const base::Value::List* emoji_history =
      profile_->GetPrefs()
          ->GetDict(prefs::kEmojiPickerHistory)
          .FindList("emoji");
  EXPECT_EQ(emoji_history->size(), 2u);

  auto& item0 = (*emoji_history)[0].GetDict();
  EXPECT_EQ(item0.Find("text")->GetString(), "abc");
  EXPECT_EQ(base::ValueToTime(item0.Find("timestamp")), TimeFromSeconds(10));

  auto& item1 = (*emoji_history)[1].GetDict();
  EXPECT_EQ(item1.Find("text")->GetString(), "xyz");
  EXPECT_EQ(base::ValueToTime(item1.Find("timestamp")), TimeFromSeconds(5));
}

TEST_F(EmojiPageHandlerTest, UpdatesPerferredVariantsInPrefs) {
  mojo::PendingReceiver<emoji_picker::mojom::PageHandler> receiver;
  EmojiPageHandler handler(std::move(receiver), &web_ui_, nullptr, false, false,
                           kEmojis, "");

  std::vector<EmojiVariantPtr> variants;
  variants.push_back(EmojiVariant::New("abc", "123"));
  variants.push_back(EmojiVariant::New("xyz", "456"));
  handler.UpdatePreferredVariantsInPrefs(std::move(variants));

  const base::Value::Dict& preference =
      profile_->GetPrefs()->GetDict(prefs::kEmojiPickerPreferences);
  const base::Value::Dict* preferred_variants =
      preference.FindDict("preferred_variants");
  EXPECT_EQ(preferred_variants->Find("abc")->GetString(), "123");
  EXPECT_EQ(preferred_variants->Find("xyz")->GetString(), "456");
}

TEST_F(EmojiPageHandlerTest, GetsHistoryFromPrefs) {
  mojo::PendingReceiver<emoji_picker::mojom::PageHandler> receiver;
  EmojiPageHandler handler(std::move(receiver), &web_ui_, nullptr, false, false,
                           kEmojis, "");
  std::vector<HistoryItemPtr> history;
  history.push_back(HistoryItem::New("abc", TimeFromSeconds(10)));
  history.push_back(HistoryItem::New("xyz", TimeFromSeconds(5)));
  handler.UpdateHistoryInPrefs(kEmojis, std::move(history));

  base::test::TestFuture<std::vector<HistoryItemPtr>> future;
  handler.GetHistoryFromPrefs(kEmojis, future.GetCallback());

  EXPECT_TRUE(future.IsReady());
  EXPECT_THAT(
      future.Get(),
      ElementsAre(Pointee(AllOf(Field("text", &HistoryItem::emoji, "abc"),
                                Field("timestamp", &HistoryItem::timestamp,
                                      TimeFromSeconds(10)))),
                  Pointee(AllOf(Field("text", &HistoryItem::emoji, "xyz"),
                                Field("timestamp", &HistoryItem::timestamp,
                                      TimeFromSeconds(5))))));
}

TEST_F(EmojiPageHandlerTest, GetsEmptyHistoryFromEmptyPrefs) {
  mojo::PendingReceiver<emoji_picker::mojom::PageHandler> receiver;
  EmojiPageHandler handler(std::move(receiver), &web_ui_, nullptr, false, false,
                           kEmojis, "");

  base::test::TestFuture<std::vector<HistoryItemPtr>> future;
  handler.GetHistoryFromPrefs(kEmojis, future.GetCallback());

  EXPECT_TRUE(future.IsReady());
  EXPECT_THAT(future.Get(), IsEmpty());
}

}  // namespace
}  // namespace ash