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
|
// 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/android/toolbar/adaptive_toolbar_bridge.h"
#include "base/run_loop.h"
#include "chrome/browser/segmentation_platform/segmentation_platform_service_factory.h"
#include "chrome/test/base/chrome_test_utils.h"
#include "chrome/test/base/testing_profile.h"
#include "components/segmentation_platform/public/constants.h"
#include "components/segmentation_platform/public/result.h"
#include "components/segmentation_platform/public/testing/mock_segmentation_platform_service.h"
#include "content/public/test/browser_task_environment.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
using segmentation_platform::MockSegmentationPlatformService;
using testing::_;
namespace adaptive_toolbar {
class AdaptiveToolbarBridgeTest : public ::testing::Test {
public:
AdaptiveToolbarBridgeTest(const AdaptiveToolbarBridgeTest&) = delete;
AdaptiveToolbarBridgeTest& operator=(const AdaptiveToolbarBridgeTest&) =
delete;
~AdaptiveToolbarBridgeTest() override = default;
void SetUp() override {
TestingProfile::Builder builder;
builder.AddTestingFactory(
segmentation_platform::SegmentationPlatformServiceFactory::
GetInstance(),
base::BindRepeating([](content::BrowserContext* context)
-> std::unique_ptr<KeyedService> {
return std::make_unique<MockSegmentationPlatformService>();
}));
profile_ = builder.Build();
}
void TearDown() override {
// Clear default actions for safe teardown.
testing::Mock::VerifyAndClear(&GetSegmentationPlatformService());
}
AdaptiveToolbarBridgeTest() = default;
MockSegmentationPlatformService& GetSegmentationPlatformService() {
return *static_cast<MockSegmentationPlatformService*>(
segmentation_platform::SegmentationPlatformServiceFactory::
GetForProfile(profile_.get()));
}
void BridgeCallback(bool is_ready, std::vector<int> ranked_buttons) {
callback_buttons_ = ranked_buttons;
callback_is_ready_ = is_ready;
run_loop_.Quit();
}
protected:
// Needed for TestingProfile::Builder.
content::BrowserTaskEnvironment task_environment_;
std::unique_ptr<TestingProfile> profile_;
std::vector<int> callback_buttons_;
bool callback_is_ready_;
base::RunLoop run_loop_;
};
TEST_F(AdaptiveToolbarBridgeTest, GetRankedButtons) {
Profile* profile = profile_.get();
ON_CALL(GetSegmentationPlatformService(), GetClassificationResult(_, _, _, _))
.WillByDefault(testing::WithArg<3>(testing::Invoke(
[](segmentation_platform::ClassificationResultCallback callback) {
auto result = segmentation_platform::ClassificationResult(
segmentation_platform::PredictionStatus::kSucceeded);
// Set segmentation to return a sorted list of labels.
result.ordered_labels = {
segmentation_platform::kAdaptiveToolbarModelLabelShare,
segmentation_platform::kAdaptiveToolbarModelLabelAddToBookmarks,
segmentation_platform::kAdaptiveToolbarModelLabelTranslate};
base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
FROM_HERE, base::BindOnce(std::move(callback), result));
})));
adaptive_toolbar::GetRankedSessionVariantButtons(
profile, /* use_raw_results= */ false,
base::BindOnce(&AdaptiveToolbarBridgeTest::BridgeCallback,
base::Unretained(this)));
run_loop_.Run();
EXPECT_TRUE(callback_is_ready_);
// The returned enum values should match the order of the segmentation result.
std::vector<int> expected_buttons = {
static_cast<int>(AdaptiveToolbarButtonVariant::kShare),
static_cast<int>(AdaptiveToolbarButtonVariant::kAddToBookmarks),
static_cast<int>(AdaptiveToolbarButtonVariant::kTranslate)};
EXPECT_EQ(callback_buttons_, expected_buttons);
}
TEST_F(AdaptiveToolbarBridgeTest, GetRankedButtons_NotReady) {
Profile* profile = profile_.get();
ON_CALL(GetSegmentationPlatformService(), GetClassificationResult(_, _, _, _))
.WillByDefault(testing::WithArg<3>(testing::Invoke(
[](segmentation_platform::ClassificationResultCallback callback) {
// Set segmentation to return kNotReady.
auto result = segmentation_platform::ClassificationResult(
segmentation_platform::PredictionStatus::kNotReady);
base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
FROM_HERE, base::BindOnce(std::move(callback), result));
})));
adaptive_toolbar::GetRankedSessionVariantButtons(
profile, /* use_raw_results= */ false,
base::BindOnce(&AdaptiveToolbarBridgeTest::BridgeCallback,
base::Unretained(this)));
run_loop_.Run();
EXPECT_FALSE(callback_is_ready_);
std::vector<int> expected_buttons = {
static_cast<int>(AdaptiveToolbarButtonVariant::kUnknown),
};
EXPECT_EQ(callback_buttons_, expected_buttons);
}
TEST_F(AdaptiveToolbarBridgeTest, GetRankedButtons_RawResults) {
Profile* profile = profile_.get();
ON_CALL(GetSegmentationPlatformService(),
GetAnnotatedNumericResult(_, _, _, _))
.WillByDefault(testing::WithArg<3>(testing::Invoke(
[](segmentation_platform::AnnotatedNumericResultCallback callback) {
segmentation_platform::AnnotatedNumericResult result(
segmentation_platform::PredictionStatus::kSucceeded);
// Set segmentation to result an annotated numeric result, this
// includes a list of labels on the model's config and a list of
// scores. Both lists are parallel, the first score belongs to the
// first label.
auto* result_classifier = result.result.mutable_output_config()
->mutable_predictor()
->mutable_multi_class_classifier();
result_classifier->add_class_labels(
segmentation_platform::kAdaptiveToolbarModelLabelNewTab);
result_classifier->add_class_labels(
segmentation_platform::kAdaptiveToolbarModelLabelVoice);
result_classifier->add_class_labels(
segmentation_platform::kAdaptiveToolbarModelLabelShare);
result_classifier->add_class_labels(
segmentation_platform::kAdaptiveToolbarModelLabelTranslate);
result_classifier->add_class_labels(
segmentation_platform::
kAdaptiveToolbarModelLabelAddToBookmarks);
result.result.add_result(0.2);
result.result.add_result(0.3);
result.result.add_result(0.7);
result.result.add_result(0.9);
result.result.add_result(0.4);
base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
FROM_HERE, base::BindOnce(std::move(callback), result));
})));
adaptive_toolbar::GetRankedSessionVariantButtons(
profile, /* use_raw_results= */ true,
base::BindOnce(&AdaptiveToolbarBridgeTest::BridgeCallback,
base::Unretained(this)));
run_loop_.Run();
EXPECT_TRUE(callback_is_ready_);
// The returned enum values should be sorted by score.
std::vector<int> expected_buttons = {
static_cast<int>(AdaptiveToolbarButtonVariant::kTranslate),
static_cast<int>(AdaptiveToolbarButtonVariant::kShare),
static_cast<int>(AdaptiveToolbarButtonVariant::kAddToBookmarks),
static_cast<int>(AdaptiveToolbarButtonVariant::kVoice),
static_cast<int>(AdaptiveToolbarButtonVariant::kNewTab),
};
EXPECT_EQ(callback_buttons_, expected_buttons);
}
TEST_F(AdaptiveToolbarBridgeTest, GetRankedButtons_RawResults_NotReady) {
Profile* profile = profile_.get();
ON_CALL(GetSegmentationPlatformService(),
GetAnnotatedNumericResult(_, _, _, _))
.WillByDefault(testing::WithArg<3>(testing::Invoke(
[](segmentation_platform::AnnotatedNumericResultCallback callback) {
segmentation_platform::AnnotatedNumericResult result(
segmentation_platform::PredictionStatus::kNotReady);
base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
FROM_HERE, base::BindOnce(std::move(callback), result));
})));
adaptive_toolbar::GetRankedSessionVariantButtons(
profile, /* use_raw_results= */ true,
base::BindOnce(&AdaptiveToolbarBridgeTest::BridgeCallback,
base::Unretained(this)));
run_loop_.Run();
EXPECT_FALSE(callback_is_ready_);
std::vector<int> expected_buttons = {
static_cast<int>(AdaptiveToolbarButtonVariant::kUnknown),
};
EXPECT_EQ(callback_buttons_, expected_buttons);
}
} // namespace adaptive_toolbar
|