#include "components/optimization_guide/core/model_execution/on_device_model_service_controller.h"
#include <memory>
#include <optional>
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/functional/callback_helpers.h"
#include "base/memory/scoped_refptr.h"
#include "base/memory/weak_ptr.h"
#include "base/strings/string_util.h"
#include "base/test/bind.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/test/scoped_feature_list.h"
#include "base/test/task_environment.h"
#include "base/test/test_future.h"
#include "base/time/time.h"
#include "base/types/cxx23_to_underlying.h"
#include "base/uuid.h"
#include "components/optimization_guide/core/model_execution/feature_keys.h"
#include "components/optimization_guide/core/model_execution/model_execution_features.h"
#include "components/optimization_guide/core/model_execution/model_execution_prefs.h"
#include "components/optimization_guide/core/model_execution/on_device_model_access_controller.h"
#include "components/optimization_guide/core/model_execution/on_device_model_adaptation_loader.h"
#include "components/optimization_guide/core/model_execution/on_device_model_metadata.h"
#include "components/optimization_guide/core/model_execution/on_device_model_service_controller.h"
#include "components/optimization_guide/core/model_execution/test/fake_model_assets.h"
#include "components/optimization_guide/core/model_execution/test/fake_on_device_model_service_controller.h"
#include "components/optimization_guide/core/model_execution/test/feature_config_builder.h"
#include "components/optimization_guide/core/model_execution/test/request_builder.h"
#include "components/optimization_guide/core/model_execution/test/response_holder.h"
#include "components/optimization_guide/core/model_execution/test/test_on_device_model_component_state_manager.h"
#include "components/optimization_guide/core/model_info.h"
#include "components/optimization_guide/core/optimization_guide_constants.h"
#include "components/optimization_guide/core/optimization_guide_features.h"
#include "components/optimization_guide/core/optimization_guide_logger.h"
#include "components/optimization_guide/core/optimization_guide_model_executor.h"
#include "components/optimization_guide/core/optimization_guide_test_util.h"
#include "components/optimization_guide/core/optimization_guide_util.h"
#include "components/optimization_guide/core/test_model_info_builder.h"
#include "components/optimization_guide/proto/features/compose.pb.h"
#include "components/optimization_guide/proto/model_execution.pb.h"
#include "components/optimization_guide/proto/redaction.pb.h"
#include "components/optimization_guide/proto/substitution.pb.h"
#include "components/optimization_guide/proto/text_safety_model_metadata.pb.h"
#include "components/prefs/testing_pref_service.h"
#include "services/on_device_model/public/cpp/test_support/fake_service.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace optimization_guide {
ElementsAre;
ElementsAreArray;
LoadModelResult;
ExecuteModelResult;
namespace {
class FakeOnDeviceModelAvailabilityObserver
: public OnDeviceModelAvailabilityObserver { … };
}
std::vector<std::string> ConcatResponses(
const std::vector<std::string>& responses) { … }
constexpr auto kFeature = …;
class OnDeviceModelServiceControllerTest : public testing::Test { … };
TEST_F(OnDeviceModelServiceControllerTest, ScoreNullBeforeContext) { … }
TEST_F(OnDeviceModelServiceControllerTest, ScorePresentAfterContext) { … }
TEST_F(OnDeviceModelServiceControllerTest, ScoreNullAfterExecute) { … }
TEST_F(OnDeviceModelServiceControllerTest, ModelExecutionSuccess) { … }
TEST_F(OnDeviceModelServiceControllerTest,
MultipleModelAdaptationExecutionSuccess) { … }
TEST_F(OnDeviceModelServiceControllerTest, ModelAdaptationAndBaseModelSuccess) { … }
TEST_F(OnDeviceModelServiceControllerTest,
ModelAdaptationEmptyWeightsUsesBaseModel) { … }
TEST_F(OnDeviceModelServiceControllerTest,
ModelExecutionFeatureExecutionNotEnabled) { … }
TEST_F(OnDeviceModelServiceControllerTest, ModelExecutionWithContext) { … }
TEST_F(OnDeviceModelServiceControllerTest,
ModelExecutionLoadsSingleContextChunk) { … }
TEST_F(OnDeviceModelServiceControllerTest,
ModelExecutionLoadsLongContextInChunks) { … }
TEST_F(OnDeviceModelServiceControllerTest,
ModelExecutionCancelsOptionalContext) { … }
TEST_F(OnDeviceModelServiceControllerTest, ModelExecutionModelToBeInstalled) { … }
TEST_F(OnDeviceModelServiceControllerTest, ModelAvailableAfterInit) { … }
TEST_F(OnDeviceModelServiceControllerTest, MidSessionModelUpdate) { … }
TEST_F(OnDeviceModelServiceControllerTest, SessionBeforeAndAfterModelUpdate) { … }
TEST_F(OnDeviceModelServiceControllerTest, SessionFailsForInvalidFeature) { … }
TEST_F(OnDeviceModelServiceControllerTest, UpdateSafetyModel) { … }
TEST_F(OnDeviceModelServiceControllerTest, UpdatingSafetyModelResetsSession) { … }
TEST_F(OnDeviceModelServiceControllerTest, SessionRequiresSafetyModel) { … }
TEST(SafetyConfigTest, MissingScoreIsUnsafe) { … }
TEST(SafetyConfigTest, SafeWithRequiredScores) { … }
TEST_F(OnDeviceModelServiceControllerTest, DefaultOutputSafetyPasses) { … }
TEST_F(OnDeviceModelServiceControllerTest, DefaultOutputSafetyFails) { … }
TEST_F(OnDeviceModelServiceControllerTest, SafetyModelUsedButNoRetract) { … }
TEST_F(OnDeviceModelServiceControllerTest, RequestCheckPassesWithSafeUrl) { … }
TEST_F(OnDeviceModelServiceControllerTest, RequestCheckFailsWithUnsafeUrl) { … }
TEST_F(OnDeviceModelServiceControllerTest, RequestCheckIgnoredInDarkMode) { … }
TEST_F(OnDeviceModelServiceControllerTest,
RequestCheckFailsWithSafeUrlWithFallbackThreshold) { … }
TEST_F(OnDeviceModelServiceControllerTest,
RequestCheckFailsWithUnmetRequiredLanguage) { … }
TEST_F(OnDeviceModelServiceControllerTest,
RequestCheckFailsWithUnmetRequiredLanguageButIgnored) { … }
TEST_F(OnDeviceModelServiceControllerTest,
RequestCheckPassesWithMetRequiredLanguage) { … }
TEST_F(OnDeviceModelServiceControllerTest,
RequestCheckPassesWithLanguageOnlyFilter) { … }
TEST_F(OnDeviceModelServiceControllerTest,
RequestCheckFailsWithLanguageOnlyFilter) { … }
TEST_F(OnDeviceModelServiceControllerTest,
RawOutputCheckPassesWithMetRequiredLanguage) { … }
TEST_F(OnDeviceModelServiceControllerTest, RawOutputCheckFailsWithUnsafeText) { … }
TEST_F(OnDeviceModelServiceControllerTest,
RawOutputCheckFailsWithSafeTextInUndeterminedLanguage) { … }
TEST_F(OnDeviceModelServiceControllerTest, SafetyModelDarkMode) { … }
TEST_F(OnDeviceModelServiceControllerTest, SafetyModelDarkModeNoFeatureConfig) { … }
TEST_F(OnDeviceModelServiceControllerTest, ModelExecutionNoMinContext) { … }
TEST_F(OnDeviceModelServiceControllerTest, ReturnsErrorOnServiceDisconnect) { … }
TEST_F(OnDeviceModelServiceControllerTest, CancelsExecuteOnAddContext) { … }
TEST_F(OnDeviceModelServiceControllerTest, CancelsExecuteOnExecute) { … }
TEST_F(OnDeviceModelServiceControllerTest, WontStartSessionAfterGpuBlocked) { … }
TEST_F(OnDeviceModelServiceControllerTest, DontRecreateSessionIfGpuBlocked) { … }
TEST_F(OnDeviceModelServiceControllerTest, StopsConnectingAfterMultipleDrops) { … }
TEST_F(OnDeviceModelServiceControllerTest, AlternatingDisconnectSucceeds) { … }
TEST_F(OnDeviceModelServiceControllerTest,
MultipleDisconnectsThenVersionChangeRetries) { … }
TEST_F(OnDeviceModelServiceControllerTest, AddContextDisconnectExecute) { … }
TEST_F(OnDeviceModelServiceControllerTest, AddContextExecuteDisconnect) { … }
TEST_F(OnDeviceModelServiceControllerTest, ExecuteDisconnectedSession) { … }
TEST_F(OnDeviceModelServiceControllerTest, CallsRemoteExecute) { … }
TEST_F(OnDeviceModelServiceControllerTest, AddContextInvalidConfig) { … }
TEST_F(OnDeviceModelServiceControllerTest, ExecuteInvalidConfig) { … }
TEST_F(OnDeviceModelServiceControllerTest, FallbackToServerAfterDelay) { … }
TEST_F(OnDeviceModelServiceControllerTest,
FallbackToServerOnDisconnectWhileWaitingForExecute) { … }
TEST_F(OnDeviceModelServiceControllerTest,
DestroySessionWhileWaitingForResponse) { … }
TEST_F(OnDeviceModelServiceControllerTest, DisconnectsWhenIdle) { … }
TEST_F(OnDeviceModelServiceControllerTest,
ShutsDownServiceAfterPerformanceCheck) { … }
TEST_F(OnDeviceModelServiceControllerTest, UseServerWithRepeatedDelays) { … }
TEST_F(OnDeviceModelServiceControllerTest, RedactedField) { … }
TEST_F(OnDeviceModelServiceControllerTest, RejectedField) { … }
TEST_F(OnDeviceModelServiceControllerTest, UsePreviousResponseForRewrite) { … }
TEST_F(OnDeviceModelServiceControllerTest, ReplacementText) { … }
TEST_F(OnDeviceModelServiceControllerTest, DetectsRepeats) { … }
TEST_F(OnDeviceModelServiceControllerTest, DetectsRepeatsAndCancelsResponse) { … }
TEST_F(OnDeviceModelServiceControllerTest, DetectsRepeatsAcrossResponses) { … }
TEST_F(OnDeviceModelServiceControllerTest, IgnoresNonRepeatingText) { … }
TEST_F(OnDeviceModelServiceControllerTest,
UseRemoteTextSafetyFallbackButNoSafetyFallbackConfig) { … }
TEST_F(OnDeviceModelServiceControllerTest, UseRemoteTextSafetyFallback) { … }
TEST_F(OnDeviceModelServiceControllerTest,
UseRemoteTextSafetyFallbackFiltered) { … }
TEST_F(OnDeviceModelServiceControllerTest,
UseRemoteTextSafetyFallbackOtherError) { … }
TEST_F(OnDeviceModelServiceControllerTest,
UseRemoteTextSafetyFallbackNewRequestBeforeCallbackComesBack) { … }
TEST_F(OnDeviceModelServiceControllerTest,
InitWithNoOnDeviceComponentStateManager) { … }
TEST_F(OnDeviceModelServiceControllerTest, UsesAdapterTopKAndTemperature) { … }
TEST_F(OnDeviceModelServiceControllerTest, UsesSessionTopKAndTemperature) { … }
TEST_F(OnDeviceModelServiceControllerTest, TsInterval0) { … }
TEST_F(OnDeviceModelServiceControllerTest, TsInterval1) { … }
TEST_F(OnDeviceModelServiceControllerTest, TsInterval3) { … }
TEST_F(OnDeviceModelServiceControllerTest, TestAvailabilityObserver) { … }
class OnDeviceModelServiceControllerTsIntervalTest
: public OnDeviceModelServiceControllerTest,
public ::testing::WithParamInterface<int> { … };
TEST_P(OnDeviceModelServiceControllerTsIntervalTest,
DetectsRepeatsWithSafetyModel) { … }
INSTANTIATE_TEST_SUITE_P(…);
TEST_F(OnDeviceModelServiceControllerTest, ModelValidationSucceeds) { … }
TEST_F(OnDeviceModelServiceControllerTest,
ModelValidationSucceedsImmediatelyWithNoPrompts) { … }
TEST_F(OnDeviceModelServiceControllerTest, ModelValidationBlocksSession) { … }
TEST_F(OnDeviceModelServiceControllerTest,
ModelValidationBlocksSessionPendingCheck) { … }
TEST_F(OnDeviceModelServiceControllerTest, ModelValidationNewModelVersion) { … }
TEST_F(OnDeviceModelServiceControllerTest,
ModelValidationNewModelVersionCancelsPreviousValidation) { … }
TEST_F(OnDeviceModelServiceControllerTest, ModelValidationDoesNotRepeat) { … }
TEST_F(OnDeviceModelServiceControllerTest, ModelValidationRepeatsOnFailure) { … }
TEST_F(OnDeviceModelServiceControllerTest, ModelValidationMaximumRetry) { … }
TEST_F(OnDeviceModelServiceControllerTest, ModelValidationDisabled) { … }
TEST_F(OnDeviceModelServiceControllerTest, ModelValidationDelayed) { … }
TEST_F(OnDeviceModelServiceControllerTest, ModelValidationInterrupted) { … }
TEST_F(OnDeviceModelServiceControllerTest, ModelValidationFails) { … }
TEST_F(OnDeviceModelServiceControllerTest, ModelValidationFailsOnCrash) { … }
TEST_F(OnDeviceModelServiceControllerTest,
PerformanceCheckDoesNotInterruptModelValidation) { … }
TEST_F(OnDeviceModelServiceControllerTest,
ModelValidationDoesNotInterruptPerformanceCheck) { … }
}