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
ash / quick_insert / quick_insert_pixeltest.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 <memory>
#include <string>
#include "ash/constants/ash_pref_names.h"
#include "ash/public/cpp/ash_prefs.h"
#include "ash/quick_insert/metrics/quick_insert_session_metrics.h"
#include "ash/quick_insert/mock_quick_insert_client.h"
#include "ash/quick_insert/quick_insert_controller.h"
#include "ash/quick_insert/views/quick_insert_feature_tour.h"
#include "ash/session/session_controller_impl.h"
#include "ash/shell.h"
#include "ash/test/ash_test_base.h"
#include "ash/test/pixel/ash_pixel_differ.h"
#include "base/files/scoped_temp_dir.h"
#include "base/i18n/rtl.h"
#include "base/strings/strcat.h"
#include "components/history/core/browser/history_service.h"
#include "components/history/core/test/history_service_test_util.h"
#include "components/sync_preferences/testing_pref_service_syncable.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/base/ime/ash/fake_ime_keyboard.h"
#include "ui/base/ime/ash/ime_keyboard.h"
#include "ui/base/ime/ash/input_method_manager.h"
#include "ui/base/ime/ash/mock_input_method_manager.h"
#include "ui/base/ime/fake_text_input_client.h"
#include "ui/base/ime/input_method.h"
#include "ui/views/test/widget_test.h"
#include "ui/views/widget/widget.h"
namespace ash {
namespace {
using ::testing::NiceMock;
using ::testing::Return;
class MockInputMethodManagerWithKeyboard
: public input_method::MockInputMethodManager {
public:
input_method::ImeKeyboard* GetImeKeyboard() override { return &keyboard_; }
private:
input_method::FakeImeKeyboard keyboard_;
};
class QuickInsertPixelTest : public AshTestBase,
public testing::WithParamInterface</*RTL*/ bool> {
public:
QuickInsertPixelTest() {
input_method::InputMethodManager::Initialize(
new MockInputMethodManagerWithKeyboard);
CHECK(history_dir_.CreateUniqueTempDir());
history_service_ =
history::CreateHistoryService(history_dir_.GetPath(), true);
QuickInsertController::DisableFeatureTourForTesting();
}
~QuickInsertPixelTest() { input_method::InputMethodManager::Shutdown(); }
void SetUp() override {
AshTestBase::SetUp();
ON_CALL(client_, GetHistoryService)
.WillByDefault(Return(history_service_.get()));
controller_ = std::make_unique<QuickInsertController>();
controller_->SetClient(&client_);
}
void TearDown() override {
controller_ = nullptr;
AshTestBase::TearDown();
}
// AshTestBase:
std::optional<pixel_test::InitParams> CreatePixelTestInitParams()
const override {
pixel_test::InitParams params;
params.under_rtl = GetParam();
return params;
}
std::string GetTestName(std::string_view scenario_name) const {
if (GetParam()) {
return base::StrCat({scenario_name, "_rtl"});
} else {
return std::string(scenario_name);
}
}
QuickInsertController& controller() { return *controller_; }
private:
NiceMock<MockQuickInsertClient> client_;
std::unique_ptr<QuickInsertController> controller_;
base::ScopedTempDir history_dir_;
std::unique_ptr<history::HistoryService> history_service_;
};
ui::InputMethod* GetInputMethod() {
return Shell::GetPrimaryRootWindow()->GetHost()->GetInputMethod();
}
INSTANTIATE_TEST_SUITE_P(, QuickInsertPixelTest, testing::Bool());
TEST_P(QuickInsertPixelTest, UnfocusedMode) {
controller().ToggleWidget();
views::test::WidgetVisibleWaiter(controller().widget_for_testing()).Wait();
EXPECT_TRUE(GetPixelDiffer()->CompareUiComponentsOnPrimaryScreen(
"quick_insert_unfocused",
/*revision_number=*/0, controller().widget_for_testing()));
}
TEST_P(QuickInsertPixelTest, FocusedModeNoSelection) {
ui::FakeTextInputClient input_field(GetInputMethod(),
{.type = ui::TEXT_INPUT_TYPE_TEXT});
input_field.Focus();
controller().ToggleWidget();
views::test::WidgetVisibleWaiter(controller().widget_for_testing()).Wait();
EXPECT_TRUE(GetPixelDiffer()->CompareUiComponentsOnPrimaryScreen(
"quick_insert_focused_no_selection",
/*revision_number=*/0, controller().widget_for_testing()));
}
TEST_P(QuickInsertPixelTest, FocusedModeHasSelection) {
ui::FakeTextInputClient input_field(GetInputMethod(),
{.type = ui::TEXT_INPUT_TYPE_TEXT});
input_field.SetTextAndSelection(u"abcd", gfx::Range(0, 4));
input_field.Focus();
controller().ToggleWidget();
views::test::WidgetVisibleWaiter(controller().widget_for_testing()).Wait();
EXPECT_TRUE(GetPixelDiffer()->CompareUiComponentsOnPrimaryScreen(
"quick_insert_focused_no_selection",
/*revision_number=*/0, controller().widget_for_testing()));
}
} // namespace
} // namespace ash