Commit 06fb90ed authored by alanlxl's avatar alanlxl Committed by Commit Bot

Clean old smart dim model and ml_service_client code

Now that new ml agent is enabled by default, we can remove the old model
and ml_service_client code of smart dim.

Also change the unit test accordingly.

Bug: 1067048, 1136331
Test: Unit test passed & tested on DUT
Change-Id: Ife97ecd54d0dec0ddcd6c816517c4c71629ce0eb
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2462989Reviewed-by: default avatarAndrew Moylan <amoylan@chromium.org>
Reviewed-by: default avatarXinglong Luan <alanlxl@chromium.org>
Commit-Queue: Xinglong Luan <alanlxl@chromium.org>
Cr-Commit-Position: refs/heads/master@{#816439}
parent 3dd79e0c
...@@ -3612,7 +3612,6 @@ source_set("unit_tests") { ...@@ -3612,7 +3612,6 @@ source_set("unit_tests") {
"power/ml/recent_events_counter_unittest.cc", "power/ml/recent_events_counter_unittest.cc",
"power/ml/smart_dim/ml_agent_unittest.cc", "power/ml/smart_dim/ml_agent_unittest.cc",
"power/ml/smart_dim/ml_agent_util_unittest.cc", "power/ml/smart_dim/ml_agent_util_unittest.cc",
"power/ml/smart_dim/model_unittest.cc",
"power/ml/user_activity_manager_unittest.cc", "power/ml/user_activity_manager_unittest.cc",
"power/ml/user_activity_ukm_logger_helpers_unittest.cc", "power/ml/user_activity_ukm_logger_helpers_unittest.cc",
"power/ml/user_activity_ukm_logger_unittest.cc", "power/ml/user_activity_ukm_logger_unittest.cc",
......
...@@ -6,8 +6,6 @@ source_set("smart_dim") { ...@@ -6,8 +6,6 @@ source_set("smart_dim") {
public = [ public = [
"metrics.h", "metrics.h",
"ml_agent.h", "ml_agent.h",
"model.h",
"model_impl.h",
] ]
sources = [ sources = [
...@@ -20,11 +18,6 @@ source_set("smart_dim") { ...@@ -20,11 +18,6 @@ source_set("smart_dim") {
"ml_agent.h", "ml_agent.h",
"ml_agent_util.cc", "ml_agent_util.cc",
"ml_agent_util.h", "ml_agent_util.h",
"ml_service_client.cc",
"ml_service_client.h",
"model.h",
"model_impl.cc",
"model_impl.h",
"smart_dim_worker.cc", "smart_dim_worker.cc",
"smart_dim_worker.h", "smart_dim_worker.h",
] ]
......
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROME_BROWSER_CHROMEOS_POWER_ML_SMART_DIM_MODEL_H_
#define CHROME_BROWSER_CHROMEOS_POWER_ML_SMART_DIM_MODEL_H_
#include "base/callback.h"
#include "chrome/browser/chromeos/power/ml/user_activity_event.pb.h"
namespace chromeos {
namespace power {
namespace ml {
// Interface to indicate whether an upcoming screen dim should go ahead based on
// whether user will remain inactive if screen is dimmed now.
class SmartDimModel {
public:
using DimDecisionCallback =
base::OnceCallback<void(UserActivityEvent::ModelPrediction)>;
virtual ~SmartDimModel() = default;
// Post a request to determine whether an upcoming dim should go ahead based
// on input |features|. When a decision is arrived at, run the callback
// specified by |dim_callback|. If this method is called again before it
// calls the previous callback, the previous callback will be canceled.
virtual void RequestDimDecision(const UserActivityEvent::Features& features,
DimDecisionCallback dim_callback) = 0;
// Cancel a previous dim decision request, if one is pending. If no dim
// decision request is pending, this function has no effect.
virtual void CancelPreviousRequest() = 0;
};
} // namespace ml
} // namespace power
} // namespace chromeos
#endif // CHROME_BROWSER_CHROMEOS_POWER_ML_SMART_DIM_MODEL_H_
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/chromeos/power/ml/smart_dim/model_impl.h"
#include <cmath>
#include <map>
#include <string>
#include "base/bind.h"
#include "base/logging.h"
#include "base/memory/ref_counted_memory.h"
#include "base/memory/scoped_refptr.h"
#include "base/metrics/field_trial_params.h"
#include "base/optional.h"
#include "base/strings/string_number_conversions.h"
#include "base/task/post_task.h"
#include "base/task_runner_util.h"
#include "chrome/browser/chromeos/power/ml/user_activity_ukm_logger_helpers.h"
#include "chrome/grit/browser_resources.h"
#include "chromeos/constants/chromeos_features.h"
#include "components/assist_ranker/example_preprocessing.h"
#include "components/assist_ranker/proto/example_preprocessor.pb.h"
#include "components/assist_ranker/proto/ranker_example.pb.h"
#include "ui/base/resource/resource_bundle.h"
namespace chromeos {
namespace power {
namespace ml {
namespace {
constexpr size_t k20181115ModelInputVectorSize = 343;
constexpr size_t k20190521ModelInputVectorSize = 592;
constexpr double k20181115ModelDefaultDimThreshold = -1.0;
constexpr double k20190521ModelDefaultDimThreshold = -0.6;
// Loads the preprocessor config protobuf, which will be used later to convert a
// RankerExample to a vectorized float for inactivity score calculation. Returns
// nullptr if cannot load or parse the config.
std::unique_ptr<assist_ranker::ExamplePreprocessorConfig>
LoadExamplePreprocessorConfig() {
auto config = std::make_unique<assist_ranker::ExamplePreprocessorConfig>();
const int res_id =
base::FeatureList::IsEnabled(features::kSmartDimModelV3)
? IDR_SMART_DIM_20190521_EXAMPLE_PREPROCESSOR_CONFIG_PB
: IDR_SMART_DIM_20181115_EXAMPLE_PREPROCESSOR_CONFIG_PB;
scoped_refptr<base::RefCountedMemory> raw_config =
ui::ResourceBundle::GetSharedInstance().LoadDataResourceBytes(res_id);
if (!raw_config || !raw_config->front()) {
LOG(ERROR) << "Failed to load SmartDimModel example preprocessor config.";
return nullptr;
}
if (!config->ParseFromArray(raw_config->front(), raw_config->size())) {
LOG(ERROR) << "Failed to parse SmartDimModel example preprocessor config.";
return nullptr;
}
return config;
}
// Populates |example| using |features|. Returns true if no error occurred.
bool PopulateRankerExample(const UserActivityEvent::Features& features,
assist_ranker::RankerExample* example) {
CHECK(example);
// Some features are bucketized before being logged to UKM. Hence training
// examples use bucketized values. We need to bucketize them here to ensure
// consistency.
// It's ok if a feature is missing from |features|, and we will not return
// false. But if a feature exists in |features|, then we expect it to have
// a bucketized version in |buckets|. If its bucketized version is missing
// from |buckets| then we return false.
const std::map<std::string, int> buckets =
UserActivityUkmLoggerBucketizer::BucketizeUserActivityEventFeatures(
features);
auto& ranker_example_features = *example->mutable_features();
if (features.has_battery_percent()) {
const auto it = buckets.find(kBatteryPercent);
if (it == buckets.end())
return false;
ranker_example_features[kBatteryPercent].set_int32_value(it->second);
}
if (features.has_device_management()) {
ranker_example_features["DeviceManagement"].set_int32_value(
features.device_management());
}
if (features.has_device_mode()) {
ranker_example_features["DeviceMode"].set_int32_value(
features.device_mode());
}
if (features.has_device_type()) {
ranker_example_features["DeviceType"].set_int32_value(
features.device_type());
}
if (features.has_key_events_in_last_hour()) {
const auto it = buckets.find(kKeyEventsInLastHour);
if (it == buckets.end())
return false;
ranker_example_features[kKeyEventsInLastHour].set_int32_value(it->second);
}
if (features.has_last_activity_day()) {
ranker_example_features["LastActivityDay"].set_int32_value(
features.last_activity_day());
}
if (features.has_last_activity_time_sec()) {
const auto it = buckets.find(kLastActivityTime);
if (it == buckets.end())
return false;
ranker_example_features[kLastActivityTime].set_int32_value(it->second);
}
if (features.has_last_user_activity_time_sec()) {
const auto it = buckets.find(kLastUserActivityTime);
if (it == buckets.end())
return false;
ranker_example_features[kLastUserActivityTime].set_int32_value(it->second);
}
if (features.has_mouse_events_in_last_hour()) {
const auto it = buckets.find(kMouseEventsInLastHour);
if (it == buckets.end())
return false;
ranker_example_features[kMouseEventsInLastHour].set_int32_value(it->second);
}
if (features.has_on_battery()) {
// This is an int value in the model.
ranker_example_features["OnBattery"].set_int32_value(features.on_battery());
}
ranker_example_features["PreviousNegativeActionsCount"].set_int32_value(
features.previous_negative_actions_count());
ranker_example_features["PreviousPositiveActionsCount"].set_int32_value(
features.previous_positive_actions_count());
ranker_example_features["RecentTimeActive"].set_int32_value(
features.recent_time_active_sec());
if (features.has_video_playing_time_sec()) {
const auto it = buckets.find(kRecentVideoPlayingTime);
if (it == buckets.end())
return false;
ranker_example_features[kRecentVideoPlayingTime].set_int32_value(
it->second);
}
if (features.has_on_to_dim_sec()) {
ranker_example_features["ScreenDimDelay"].set_int32_value(
features.on_to_dim_sec());
}
if (features.has_dim_to_screen_off_sec()) {
ranker_example_features["ScreenDimToOffDelay"].set_int32_value(
features.dim_to_screen_off_sec());
}
if (features.has_time_since_last_key_sec()) {
ranker_example_features["TimeSinceLastKey"].set_int32_value(
features.time_since_last_key_sec());
}
if (features.has_time_since_last_mouse_sec()) {
ranker_example_features["TimeSinceLastMouse"].set_int32_value(
features.time_since_last_mouse_sec());
}
if (features.has_time_since_video_ended_sec()) {
const auto it = buckets.find(kTimeSinceLastVideoEnded);
if (it == buckets.end())
return false;
ranker_example_features[kTimeSinceLastVideoEnded].set_int32_value(
it->second);
}
if (features.has_engagement_score()) {
ranker_example_features["SiteEngagementScore"].set_int32_value(
features.engagement_score());
}
if (features.has_has_form_entry()) {
ranker_example_features["HasFormEntry"].set_bool_value(
features.has_form_entry());
}
if (features.has_tab_domain()) {
ranker_example_features["TabDomain"].set_string_value(
features.tab_domain());
ranker_example_features["HasTabs"].set_bool_value(true);
} else {
ranker_example_features["HasTabs"].set_bool_value(false);
}
return true;
}
// Squashes |score| into range [0, 1] using sigmoid, and scales it to [0,100]
// so that values can be easily logged.
int ScoreToProbability(float score) {
const float sigmoid = 1.0f / (1.0f + exp(-score));
const int prob = floor(sigmoid * 100);
return prob;
}
// Returns "dim_threshold" from experiment parameter. Also logs status to UMA.
float GetDimThreshold() {
const double default_threshold =
base::FeatureList::IsEnabled(features::kSmartDimModelV3)
? k20190521ModelDefaultDimThreshold
: k20181115ModelDefaultDimThreshold;
const double dim_threshold = base::GetFieldTrialParamByFeatureAsDouble(
features::kUserActivityPrediction, "dim_threshold", default_threshold);
if (std::abs(dim_threshold - default_threshold) < 1e-10) {
LogPowerMLSmartDimParameterResult(
SmartDimParameterResult::kUseDefaultValue);
} else {
LogPowerMLSmartDimParameterResult(SmartDimParameterResult::kSuccess);
}
return dim_threshold;
}
} // namespace
SmartDimModelImpl::SmartDimModelImpl() = default;
SmartDimModelImpl::~SmartDimModelImpl() = default;
SmartDimModelResult SmartDimModelImpl::PreprocessInput(
const UserActivityEvent::Features& features,
std::vector<float>* vectorized_features) {
DCHECK(vectorized_features);
LazyInitialize();
if (!preprocessor_config_) {
return SmartDimModelResult::kPreprocessorInitializationFailed;
}
assist_ranker::RankerExample ranker_example;
if (!PopulateRankerExample(features, &ranker_example)) {
return SmartDimModelResult::kOtherError;
}
int preprocessor_error = assist_ranker::ExamplePreprocessor::Process(
*preprocessor_config_, &ranker_example, true);
// kNoFeatureIndexFound can occur normally (e.g., when the domain name
// isn't known to the model or a rarely seen enum value is used).
if (preprocessor_error != assist_ranker::ExamplePreprocessor::kSuccess &&
preprocessor_error !=
assist_ranker::ExamplePreprocessor::kNoFeatureIndexFound) {
return SmartDimModelResult::kPreprocessorOtherError;
}
const auto& extracted_features =
ranker_example.features()
.at(assist_ranker::ExamplePreprocessor::kVectorizedFeatureDefaultName)
.float_list()
.float_value();
vectorized_features->assign(extracted_features.begin(),
extracted_features.end());
return SmartDimModelResult::kSuccess;
}
UserActivityEvent::ModelPrediction
SmartDimModelImpl::CreatePredictionFromInactivityScore(float inactivity_score) {
UserActivityEvent::ModelPrediction prediction;
const float dim_threshold = GetDimThreshold();
prediction.set_decision_threshold(ScoreToProbability(dim_threshold));
prediction.set_inactivity_score(ScoreToProbability(inactivity_score));
if (inactivity_score >= dim_threshold) {
prediction.set_response(UserActivityEvent::ModelPrediction::DIM);
} else {
prediction.set_response(UserActivityEvent::ModelPrediction::NO_DIM);
}
return prediction;
}
void SmartDimModelImpl::ShouldDim(
const UserActivityEvent::Features& input_features,
DimDecisionCallback callback) {
UserActivityEvent::ModelPrediction prediction;
prediction.set_response(UserActivityEvent::ModelPrediction::MODEL_ERROR);
std::vector<float> vectorized_features;
auto preprocess_result =
PreprocessInput(input_features, &vectorized_features);
if (preprocess_result != SmartDimModelResult::kSuccess) {
LogPowerMLSmartDimModelResult(preprocess_result);
std::move(callback).Run(prediction);
return;
}
const size_t expected_size =
base::FeatureList::IsEnabled(features::kSmartDimModelV3)
? k20190521ModelInputVectorSize
: k20181115ModelInputVectorSize;
if (vectorized_features.size() != expected_size) {
LOG(ERROR) << "Smart Dim vectorized features not of correct size.";
LogPowerMLSmartDimModelResult(
SmartDimModelResult::kMismatchedFeatureSizeError);
std::move(callback).Run(prediction);
return;
}
if (!ml_service_client_) {
LOG(ERROR) << "ML service Mojo client not initialized correctly";
LogPowerMLSmartDimModelResult(
SmartDimModelResult::kMlServiceInitializationFailedError);
std::move(callback).Run(prediction);
return;
}
ml_service_client_->DoInference(
vectorized_features,
base::Bind(&SmartDimModelImpl::CreatePredictionFromInactivityScore,
base::Unretained(this)),
std::move(callback));
}
void SmartDimModelImpl::RequestDimDecision(
const UserActivityEvent::Features& input_features,
DimDecisionCallback dim_callback) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
// Cancel previously assigned callbacks and set it to the new callback.
cancelable_callback_.Reset(std::move(dim_callback));
ShouldDim(input_features, cancelable_callback_.callback());
}
void SmartDimModelImpl::CancelPreviousRequest() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
cancelable_callback_.Cancel();
}
void SmartDimModelImpl::SetMlServiceClientForTesting(
std::unique_ptr<MlServiceClient> client) {
DCHECK(!ml_service_client_);
ml_service_client_ = std::move(client);
}
void SmartDimModelImpl::LazyInitialize() {
if (!ml_service_client_) {
ml_service_client_ = CreateMlServiceClient();
}
if (!preprocessor_config_) {
preprocessor_config_ = LoadExamplePreprocessorConfig();
}
}
} // namespace ml
} // namespace power
} // namespace chromeos
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROME_BROWSER_CHROMEOS_POWER_ML_SMART_DIM_MODEL_IMPL_H_
#define CHROME_BROWSER_CHROMEOS_POWER_ML_SMART_DIM_MODEL_IMPL_H_
#include <memory>
#include "base/cancelable_callback.h"
#include "base/macros.h"
#include "base/sequenced_task_runner.h"
#include "base/time/time.h"
#include "chrome/browser/chromeos/power/ml/smart_dim/metrics.h"
#include "chrome/browser/chromeos/power/ml/smart_dim/ml_service_client.h"
#include "chrome/browser/chromeos/power/ml/smart_dim/model.h"
namespace assist_ranker {
class ExamplePreprocessorConfig;
} // namespace assist_ranker
namespace chromeos {
namespace power {
namespace ml {
// Real implementation of SmartDimModel that predicts whether an upcoming screen
// dim should go ahead based on user activity/inactivity following dim.
class SmartDimModelImpl : public SmartDimModel {
public:
SmartDimModelImpl();
~SmartDimModelImpl() override;
// chromeos::power::ml::SmartDimModel overrides:
void RequestDimDecision(const UserActivityEvent::Features& features,
DimDecisionCallback dim_callback) override;
void CancelPreviousRequest() override;
// Override MlServiceClient in a unit test environment where there is no real
// ML Service daemon to connect to.
void SetMlServiceClientForTesting(std::unique_ptr<MlServiceClient> client);
private:
// Loads the preprocessor config if not already loaded. Also initializes the
// MlServiceClient object.
void LazyInitialize();
// Pre-processes the input features into a vector, placed in
// |*vectorized_features|, which is consumable by the ML model.
//
// Returns SmartDimModelResult::kSuccess on success, and the appropriate
// error on failure.
SmartDimModelResult PreprocessInput(
const UserActivityEvent::Features& features,
std::vector<float>* vectorized_features);
// Takes an |inactivity_score| returned from the ML model and, using that,
// returns a ModelPrediction.
UserActivityEvent::ModelPrediction CreatePredictionFromInactivityScore(
float inactivity_score);
// Calls the ML service Mojo API to perform an Smart Dim inference call,
// given |input_features|. The |callback| is supplied to the Mojo API,
// which in turn will call it to provide the result (a ModelPrediction), once
// the inference is complete.
void ShouldDim(const UserActivityEvent::Features& input_features,
DimDecisionCallback callback);
std::unique_ptr<assist_ranker::ExamplePreprocessorConfig>
preprocessor_config_;
// Cancelable wrapper for the DimDecisionCallback passed by the client to
// RequestDimDecision().
base::CancelableOnceCallback<void(UserActivityEvent::ModelPrediction)>
cancelable_callback_;
// Pointer to the object that handles the ML service calls.
std::unique_ptr<MlServiceClient> ml_service_client_;
SEQUENCE_CHECKER(sequence_checker_);
DISALLOW_COPY_AND_ASSIGN(SmartDimModelImpl);
};
} // namespace ml
} // namespace power
} // namespace chromeos
#endif // CHROME_BROWSER_CHROMEOS_POWER_ML_SMART_DIM_MODEL_IMPL_H_
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/chromeos/power/ml/smart_dim/model_impl.h"
#include <memory>
#include "base/bind.h"
#include "base/macros.h"
#include "base/run_loop.h"
#include "base/strings/string_number_conversions.h"
#include "base/test/scoped_feature_list.h"
#include "base/test/task_environment.h"
#include "base/threading/sequenced_task_runner_handle.h"
#include "base/time/time.h"
#include "chrome/browser/chromeos/power/ml/user_activity_event.pb.h"
#include "chromeos/constants/chromeos_features.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/base/page_transition_types.h"
namespace chromeos {
namespace power {
namespace ml {
namespace {
// Arbitrary inactivity score for the fake ML Service client to return:
constexpr float kTestInactivityScore = -3.7;
// Quantized into the range [0, 100] via sigmoid transform:
constexpr int kQuantizedTestInactivityScore = 2;
// Arbitrary dim thresholds lower/higher than kTestInactivityScore, implying
// no/yes dim decisions respectively.
constexpr double kLowDimThreshold = -10.0;
constexpr double kHighDimThreshold = -0.1;
// Same values quantized into the range [0, 100] via sigmoid transform:
constexpr int kQuantizedLowDimThreshold = 0;
constexpr int kQuantizedHighDimThreshold = 47;
UserActivityEvent::Features DefaultFeatures() {
UserActivityEvent::Features features;
// Bucketize to 95.
features.set_battery_percent(96.0);
features.set_device_management(UserActivityEvent::Features::UNMANAGED);
features.set_device_mode(UserActivityEvent::Features::CLAMSHELL);
features.set_device_type(UserActivityEvent::Features::CHROMEBOOK);
// Bucketize to 200.
features.set_key_events_in_last_hour(290);
features.set_last_activity_day(UserActivityEvent::Features::THU);
// Bucketize to 7.
features.set_last_activity_time_sec(25920);
// Bucketize to 7.
features.set_last_user_activity_time_sec(25920);
// Bucketize to 2000.
features.set_mouse_events_in_last_hour(2600);
features.set_on_battery(false);
features.set_previous_negative_actions_count(3);
features.set_previous_positive_actions_count(0);
features.set_recent_time_active_sec(190);
features.set_video_playing_time_sec(0);
features.set_on_to_dim_sec(30);
features.set_dim_to_screen_off_sec(10);
features.set_time_since_last_key_sec(30);
features.set_time_since_last_mouse_sec(688);
// Bucketize to 900.
features.set_time_since_video_ended_sec(1100);
features.set_has_form_entry(false);
features.set_source_id(123); // not used.
features.set_engagement_score(40);
features.set_tab_domain("//mail.google.com");
return features;
}
// Class to hold scoped local modifications to Smart-dim related feature flags.
class SmartDimFeatureFlags {
public:
SmartDimFeatureFlags() = default;
void Initialize(const double dim_threshold) {
const std::map<std::string, std::string> params = {
{"dim_threshold", base::NumberToString(dim_threshold)}};
smart_dim_feature_override_.InitAndEnableFeatureWithParameters(
features::kUserActivityPrediction, params);
}
private:
base::test::ScopedFeatureList smart_dim_feature_override_;
DISALLOW_COPY_AND_ASSIGN(SmartDimFeatureFlags);
};
class FakeMlServiceClient : public MlServiceClient {
public:
FakeMlServiceClient() = default;
~FakeMlServiceClient() override {}
// MlServiceClient:
void DoInference(
const std::vector<float>& features,
base::RepeatingCallback<UserActivityEvent::ModelPrediction(float)>
get_prediction_callback,
SmartDimModel::DimDecisionCallback decision_callback) override {
const float inactivity_score = kTestInactivityScore;
UserActivityEvent::ModelPrediction model_prediction =
get_prediction_callback.Run(inactivity_score);
base::SequencedTaskRunnerHandle::Get()->PostTask(
FROM_HERE,
base::BindOnce(std::move(decision_callback), model_prediction));
}
private:
DISALLOW_COPY_AND_ASSIGN(FakeMlServiceClient);
};
} // namespace
class SmartDimModelImplTest : public testing::Test {
public:
SmartDimModelImplTest()
: task_environment_(
base::test::TaskEnvironment::MainThreadType::IO,
base::test::TaskEnvironment::ThreadPoolExecutionMode::QUEUED) {}
~SmartDimModelImplTest() override = default;
protected:
// Sets a fake MlServiceClient into |impl|.
void InjectFakeMlServiceClient(SmartDimModelImpl* const impl) {
impl->SetMlServiceClientForTesting(std::make_unique<FakeMlServiceClient>());
}
base::test::TaskEnvironment task_environment_;
private:
DISALLOW_COPY_AND_ASSIGN(SmartDimModelImplTest);
};
// With a high dim threshold, NO_DIM decision is expected.
TEST_F(SmartDimModelImplTest, ShouldNotDim) {
SmartDimFeatureFlags flags;
flags.Initialize(kHighDimThreshold);
SmartDimModelImpl smart_dim_model;
InjectFakeMlServiceClient(&smart_dim_model);
bool callback_done = false;
smart_dim_model.RequestDimDecision(
DefaultFeatures(), base::BindOnce(
[](bool* callback_done,
UserActivityEvent::ModelPrediction prediction) {
EXPECT_EQ(
UserActivityEvent::ModelPrediction::NO_DIM,
prediction.response());
EXPECT_EQ(kQuantizedHighDimThreshold,
prediction.decision_threshold());
EXPECT_EQ(kQuantizedTestInactivityScore,
prediction.inactivity_score());
*callback_done = true;
},
&callback_done));
task_environment_.RunUntilIdle();
EXPECT_TRUE(callback_done);
}
// With a low dim threshold, DIM decision is expected.
TEST_F(SmartDimModelImplTest, ShouldDim) {
SmartDimFeatureFlags flags;
flags.Initialize(kLowDimThreshold);
SmartDimModelImpl smart_dim_model;
InjectFakeMlServiceClient(&smart_dim_model);
bool callback_done = false;
smart_dim_model.RequestDimDecision(
DefaultFeatures(), base::BindOnce(
[](bool* callback_done,
UserActivityEvent::ModelPrediction prediction) {
EXPECT_EQ(
UserActivityEvent::ModelPrediction::DIM,
prediction.response());
EXPECT_EQ(kQuantizedLowDimThreshold,
prediction.decision_threshold());
EXPECT_EQ(kQuantizedTestInactivityScore,
prediction.inactivity_score());
*callback_done = true;
},
&callback_done));
task_environment_.RunUntilIdle();
EXPECT_TRUE(callback_done);
}
// Check that CancelableCallback ensures a callback doesn't execute twice, in
// case two RequestDimDecision() calls were made before any callback ran.
TEST_F(SmartDimModelImplTest, CheckCancelableCallback) {
SmartDimFeatureFlags flags;
flags.Initialize(kLowDimThreshold);
SmartDimModelImpl smart_dim_model;
InjectFakeMlServiceClient(&smart_dim_model);
bool callback_done = false;
int num_callbacks_run = 0;
for (int i = 0; i < 2; i++) {
smart_dim_model.RequestDimDecision(
DefaultFeatures(),
base::BindOnce(
[](bool* callback_done, int* num_callbacks_run,
UserActivityEvent::ModelPrediction prediction) {
EXPECT_EQ(UserActivityEvent::ModelPrediction::DIM,
prediction.response());
EXPECT_EQ(kQuantizedLowDimThreshold,
prediction.decision_threshold());
EXPECT_EQ(kQuantizedTestInactivityScore,
prediction.inactivity_score());
*callback_done = true;
(*num_callbacks_run)++;
},
&callback_done, &num_callbacks_run));
}
task_environment_.RunUntilIdle();
EXPECT_TRUE(callback_done);
EXPECT_EQ(1, num_callbacks_run);
}
// Check that CancelPreviousRequest() can successfully prevent a previous
// requested dim decision request from running.
TEST_F(SmartDimModelImplTest, CheckCanceledRequest) {
SmartDimFeatureFlags flags;
flags.Initialize(kLowDimThreshold);
SmartDimModelImpl smart_dim_model;
InjectFakeMlServiceClient(&smart_dim_model);
bool callback_done = false;
smart_dim_model.RequestDimDecision(
DefaultFeatures(), base::BindOnce(
[](bool* callback_done,
UserActivityEvent::ModelPrediction prediction) {
*callback_done = true;
},
&callback_done));
smart_dim_model.CancelPreviousRequest();
task_environment_.RunUntilIdle();
EXPECT_FALSE(callback_done);
}
} // namespace ml
} // namespace power
} // namespace chromeos
...@@ -51,7 +51,7 @@ UserActivityController::UserActivityController() { ...@@ -51,7 +51,7 @@ UserActivityController::UserActivityController() {
&user_activity_ukm_logger_, detector, power_manager_client, &user_activity_ukm_logger_, detector, power_manager_client,
session_manager, session_manager,
video_observer_user_logger.InitWithNewPipeAndPassReceiver(), video_observer_user_logger.InitWithNewPipeAndPassReceiver(),
chromeos::ChromeUserManager::Get(), &smart_dim_model_); chromeos::ChromeUserManager::Get());
aura::Env::GetInstance() aura::Env::GetInstance()
->context_factory() ->context_factory()
->GetHostFrameSinkManager() ->GetHostFrameSinkManager()
......
...@@ -9,7 +9,6 @@ ...@@ -9,7 +9,6 @@
#include "base/callback.h" #include "base/callback.h"
#include "chrome/browser/chromeos/power/ml/idle_event_notifier.h" #include "chrome/browser/chromeos/power/ml/idle_event_notifier.h"
#include "chrome/browser/chromeos/power/ml/smart_dim/model_impl.h"
#include "chrome/browser/chromeos/power/ml/user_activity_manager.h" #include "chrome/browser/chromeos/power/ml/user_activity_manager.h"
#include "chrome/browser/chromeos/power/ml/user_activity_ukm_logger_impl.h" #include "chrome/browser/chromeos/power/ml/user_activity_ukm_logger_impl.h"
...@@ -33,7 +32,6 @@ class UserActivityController { ...@@ -33,7 +32,6 @@ class UserActivityController {
std::unique_ptr<IdleEventNotifier> idle_event_notifier_; std::unique_ptr<IdleEventNotifier> idle_event_notifier_;
UserActivityUkmLoggerImpl user_activity_ukm_logger_; UserActivityUkmLoggerImpl user_activity_ukm_logger_;
std::unique_ptr<UserActivityManager> user_activity_manager_; std::unique_ptr<UserActivityManager> user_activity_manager_;
SmartDimModelImpl smart_dim_model_;
DISALLOW_COPY_AND_ASSIGN(UserActivityController); DISALLOW_COPY_AND_ASSIGN(UserActivityController);
}; };
......
...@@ -105,10 +105,8 @@ UserActivityManager::UserActivityManager( ...@@ -105,10 +105,8 @@ UserActivityManager::UserActivityManager(
chromeos::PowerManagerClient* power_manager_client, chromeos::PowerManagerClient* power_manager_client,
session_manager::SessionManager* session_manager, session_manager::SessionManager* session_manager,
mojo::PendingReceiver<viz::mojom::VideoDetectorObserver> receiver, mojo::PendingReceiver<viz::mojom::VideoDetectorObserver> receiver,
const chromeos::ChromeUserManager* user_manager, const chromeos::ChromeUserManager* user_manager)
SmartDimModel* smart_dim_model)
: ukm_logger_(ukm_logger), : ukm_logger_(ukm_logger),
smart_dim_model_(smart_dim_model),
user_activity_observer_(this), user_activity_observer_(this),
power_manager_client_observer_(this), power_manager_client_observer_(this),
session_manager_observer_(this), session_manager_observer_(this),
...@@ -263,19 +261,14 @@ void UserActivityManager::UpdateAndGetSmartDimDecision( ...@@ -263,19 +261,14 @@ void UserActivityManager::UpdateAndGetSmartDimDecision(
profile->GetPrefs()->GetBoolean(ash::prefs::kPowerSmartDimEnabled); profile->GetPrefs()->GetBoolean(ash::prefs::kPowerSmartDimEnabled);
} }
if (smart_dim_enabled && if (smart_dim_enabled &&
base::FeatureList::IsEnabled(features::kUserActivityPrediction) && base::FeatureList::IsEnabled(features::kUserActivityPrediction)) {
SmartDimModelReady()) {
waiting_for_model_decision_ = true; waiting_for_model_decision_ = true;
time_dim_decision_requested_ = base::TimeTicks::Now(); time_dim_decision_requested_ = base::TimeTicks::Now();
auto request_callback = auto request_callback =
base::BindOnce(&UserActivityManager::HandleSmartDimDecision, base::BindOnce(&UserActivityManager::HandleSmartDimDecision,
weak_ptr_factory_.GetWeakPtr(), std::move(callback)); weak_ptr_factory_.GetWeakPtr(), std::move(callback));
if (base::FeatureList::IsEnabled(features::kSmartDimNewMlAgent))
SmartDimMlAgent::GetInstance()->RequestDimDecision( SmartDimMlAgent::GetInstance()->RequestDimDecision(
features_, std::move(request_callback)); features_, std::move(request_callback));
else
smart_dim_model_->RequestDimDecision(features_,
std::move(request_callback));
} }
waiting_for_final_action_ = true; waiting_for_final_action_ = true;
} }
...@@ -560,8 +553,7 @@ void UserActivityManager::PopulatePreviousEventData( ...@@ -560,8 +553,7 @@ void UserActivityManager::PopulatePreviousEventData(
const base::TimeDelta& now) { const base::TimeDelta& now) {
PreviousEventLoggingResult result = PreviousEventLoggingResult::kSuccess; PreviousEventLoggingResult result = PreviousEventLoggingResult::kSuccess;
if (!model_prediction_) { if (!model_prediction_) {
result = base::FeatureList::IsEnabled(features::kUserActivityPrediction) && result = base::FeatureList::IsEnabled(features::kUserActivityPrediction)
SmartDimModelReady()
? PreviousEventLoggingResult::kErrorModelPredictionMissing ? PreviousEventLoggingResult::kErrorModelPredictionMissing
: PreviousEventLoggingResult::kErrorModelDisabled; : PreviousEventLoggingResult::kErrorModelDisabled;
LogPowerMLPreviousEventLoggingResult(result); LogPowerMLPreviousEventLoggingResult(result);
...@@ -605,10 +597,7 @@ void UserActivityManager::ResetAfterLogging() { ...@@ -605,10 +597,7 @@ void UserActivityManager::ResetAfterLogging() {
void UserActivityManager::CancelDimDecisionRequest() { void UserActivityManager::CancelDimDecisionRequest() {
LOG(WARNING) << "Cancelling pending Smart Dim decision request."; LOG(WARNING) << "Cancelling pending Smart Dim decision request.";
if (base::FeatureList::IsEnabled(features::kSmartDimNewMlAgent))
SmartDimMlAgent::GetInstance()->CancelPreviousRequest(); SmartDimMlAgent::GetInstance()->CancelPreviousRequest();
else
smart_dim_model_->CancelPreviousRequest();
waiting_for_model_decision_ = false; waiting_for_model_decision_ = false;
const base::TimeDelta wait_time = const base::TimeDelta wait_time =
...@@ -617,13 +606,6 @@ void UserActivityManager::CancelDimDecisionRequest() { ...@@ -617,13 +606,6 @@ void UserActivityManager::CancelDimDecisionRequest() {
time_dim_decision_requested_ = base::TimeTicks(); time_dim_decision_requested_ = base::TimeTicks();
} }
bool UserActivityManager::SmartDimModelReady() {
// We assume that BuiltinWorker of SmartDimMlAgent can always load model and
// preprocessor config from rootfs, therefore SmartDimMlAgent is always ready.
return base::FeatureList::IsEnabled(features::kSmartDimNewMlAgent) ||
smart_dim_model_;
}
} // namespace ml } // namespace ml
} // namespace power } // namespace power
} // namespace chromeos } // namespace chromeos
...@@ -13,7 +13,6 @@ ...@@ -13,7 +13,6 @@
#include "chrome/browser/chromeos/login/users/chrome_user_manager.h" #include "chrome/browser/chromeos/login/users/chrome_user_manager.h"
#include "chrome/browser/chromeos/power/ml/boot_clock.h" #include "chrome/browser/chromeos/power/ml/boot_clock.h"
#include "chrome/browser/chromeos/power/ml/idle_event_notifier.h" #include "chrome/browser/chromeos/power/ml/idle_event_notifier.h"
#include "chrome/browser/chromeos/power/ml/smart_dim/model.h"
#include "chrome/browser/chromeos/power/ml/user_activity_event.pb.h" #include "chrome/browser/chromeos/power/ml/user_activity_event.pb.h"
#include "chrome/browser/chromeos/power/ml/user_activity_ukm_logger.h" #include "chrome/browser/chromeos/power/ml/user_activity_ukm_logger.h"
#include "chrome/browser/resource_coordinator/tab_metrics_event.pb.h" #include "chrome/browser/resource_coordinator/tab_metrics_event.pb.h"
...@@ -85,8 +84,7 @@ class UserActivityManager : public ui::UserActivityObserver, ...@@ -85,8 +84,7 @@ class UserActivityManager : public ui::UserActivityObserver,
chromeos::PowerManagerClient* power_manager_client, chromeos::PowerManagerClient* power_manager_client,
session_manager::SessionManager* session_manager, session_manager::SessionManager* session_manager,
mojo::PendingReceiver<viz::mojom::VideoDetectorObserver> receiver, mojo::PendingReceiver<viz::mojom::VideoDetectorObserver> receiver,
const chromeos::ChromeUserManager* user_manager, const chromeos::ChromeUserManager* user_manager);
SmartDimModel* smart_dim_model);
~UserActivityManager() override; ~UserActivityManager() override;
// ui::UserActivityObserver overrides. // ui::UserActivityObserver overrides.
...@@ -155,12 +153,9 @@ class UserActivityManager : public ui::UserActivityObserver, ...@@ -155,12 +153,9 @@ class UserActivityManager : public ui::UserActivityObserver,
void ResetAfterLogging(); void ResetAfterLogging();
// Cancel any pending request to |smart_dim_model_| to get a dim decision. // Cancel any pending request to `SmartDimMlAgent` to get a dim decision.
void CancelDimDecisionRequest(); void CancelDimDecisionRequest();
// If old smart dim model or new SmartDimMlAgent is ready, based on Finch.
bool SmartDimModelReady();
// Time when an idle event is received and we start logging. Null if an idle // Time when an idle event is received and we start logging. Null if an idle
// event hasn't been observed. // event hasn't been observed.
base::Optional<base::TimeDelta> idle_event_start_since_boot_; base::Optional<base::TimeDelta> idle_event_start_since_boot_;
...@@ -190,8 +185,6 @@ class UserActivityManager : public ui::UserActivityObserver, ...@@ -190,8 +185,6 @@ class UserActivityManager : public ui::UserActivityObserver,
UserActivityUkmLogger* const ukm_logger_; UserActivityUkmLogger* const ukm_logger_;
SmartDimModel* const smart_dim_model_;
ScopedObserver<ui::UserActivityDetector, ui::UserActivityObserver> ScopedObserver<ui::UserActivityDetector, ui::UserActivityObserver>
user_activity_observer_; user_activity_observer_;
ScopedObserver<chromeos::PowerManagerClient, ScopedObserver<chromeos::PowerManagerClient,
...@@ -235,7 +228,7 @@ class UserActivityManager : public ui::UserActivityObserver, ...@@ -235,7 +228,7 @@ class UserActivityManager : public ui::UserActivityObserver,
// set to true after we've received an idle event, but haven't received final // set to true after we've received an idle event, but haven't received final
// action to log the event. // action to log the event.
bool waiting_for_final_action_ = false; bool waiting_for_final_action_ = false;
// Whether we are waiting for a decision from the |smart_dim_model_| // Whether we are waiting for a decision from the `SmartDimMlAgent`
// regarding whether to proceed with a dim or not. It is only set // regarding whether to proceed with a dim or not. It is only set
// to true in OnIdleEventObserved() when we request a dim decision. // to true in OnIdleEventObserved() when we request a dim decision.
bool waiting_for_model_decision_ = false; bool waiting_for_model_decision_ = false;
......
...@@ -19,7 +19,6 @@ ...@@ -19,7 +19,6 @@
#include "chrome/browser/chromeos/login/users/fake_chrome_user_manager.h" #include "chrome/browser/chromeos/login/users/fake_chrome_user_manager.h"
#include "chrome/browser/chromeos/power/ml/idle_event_notifier.h" #include "chrome/browser/chromeos/power/ml/idle_event_notifier.h"
#include "chrome/browser/chromeos/power/ml/smart_dim/ml_agent.h" #include "chrome/browser/chromeos/power/ml/smart_dim/ml_agent.h"
#include "chrome/browser/chromeos/power/ml/smart_dim/model.h"
#include "chrome/browser/chromeos/power/ml/user_activity_event.pb.h" #include "chrome/browser/chromeos/power/ml/user_activity_event.pb.h"
#include "chrome/browser/chromeos/power/ml/user_activity_ukm_logger.h" #include "chrome/browser/chromeos/power/ml/user_activity_ukm_logger.h"
#include "chrome/browser/engagement/site_engagement_service.h" #include "chrome/browser/engagement/site_engagement_service.h"
...@@ -104,80 +103,13 @@ class TestingUserActivityUkmLogger : public UserActivityUkmLogger { ...@@ -104,80 +103,13 @@ class TestingUserActivityUkmLogger : public UserActivityUkmLogger {
DISALLOW_COPY_AND_ASSIGN(TestingUserActivityUkmLogger); DISALLOW_COPY_AND_ASSIGN(TestingUserActivityUkmLogger);
}; };
// Testing smart dim model. class UserActivityManagerTest : public ChromeRenderViewHostTestHarness {
class FakeSmartDimModel : public SmartDimModel {
public:
explicit FakeSmartDimModel(
const scoped_refptr<base::SequencedTaskRunner> runner)
: task_runner_(runner) {}
~FakeSmartDimModel() override = default;
void set_inactivity_score(const int inactivity_score) {
inactivity_score_ = inactivity_score;
}
void set_decision_threshold(const int decision_threshold) {
decision_threshold_ = decision_threshold;
}
UserActivityEvent::ModelPrediction ShouldDim(
const UserActivityEvent::Features& input_features) {
UserActivityEvent::ModelPrediction model_prediction;
// If either of these two values are set outside of the legal range [0,100],
// return an error code.
// The |model_applied| field is not filled by the model but by
// UserActivityManager.
if (inactivity_score_ < 0 || inactivity_score_ > 100 ||
decision_threshold_ < 0 || decision_threshold_ > 100) {
model_prediction.set_response(
UserActivityEvent::ModelPrediction::MODEL_ERROR);
} else {
model_prediction.set_decision_threshold(decision_threshold_);
model_prediction.set_inactivity_score(inactivity_score_);
if (inactivity_score_ < decision_threshold_) {
model_prediction.set_response(
UserActivityEvent::ModelPrediction::NO_DIM);
} else {
model_prediction.set_response(UserActivityEvent::ModelPrediction::DIM);
}
}
return model_prediction;
}
// SmartDimModel overrides:
void RequestDimDecision(const UserActivityEvent::Features& features,
DimDecisionCallback dim_callback) override {
// Cancel previously assigned callbacks and set it to the new callback.
cancelable_callback_.Reset(std::move(dim_callback));
base::PostTaskAndReplyWithResult(
task_runner_.get(), FROM_HERE,
base::BindOnce(&FakeSmartDimModel::ShouldDim, base::Unretained(this),
features),
base::BindOnce(cancelable_callback_.callback()));
}
// TODO(crbug.com/893425): Add unit tests that test this API.
void CancelPreviousRequest() override { cancelable_callback_.Cancel(); }
private:
int inactivity_score_ = -1;
int decision_threshold_ = -1;
const scoped_refptr<base::SequencedTaskRunner> task_runner_;
base::CancelableOnceCallback<void(UserActivityEvent::ModelPrediction)>
cancelable_callback_;
DISALLOW_COPY_AND_ASSIGN(FakeSmartDimModel);
};
class UserActivityManagerTest : public ChromeRenderViewHostTestHarness,
public ::testing::WithParamInterface<bool> {
public: public:
UserActivityManagerTest() UserActivityManagerTest()
: ChromeRenderViewHostTestHarness( : ChromeRenderViewHostTestHarness(
base::test::TaskEnvironment::MainThreadType::UI, base::test::TaskEnvironment::MainThreadType::UI,
base::test::TaskEnvironment::TimeSource::MOCK_TIME, base::test::TaskEnvironment::TimeSource::MOCK_TIME,
base::test::TaskEnvironment::ThreadPoolExecutionMode::QUEUED), base::test::TaskEnvironment::ThreadPoolExecutionMode::QUEUED) {}
model_(task_environment()->GetMainThreadTaskRunner()) {}
~UserActivityManagerTest() override = default; ~UserActivityManagerTest() override = default;
...@@ -190,11 +122,10 @@ class UserActivityManagerTest : public ChromeRenderViewHostTestHarness, ...@@ -190,11 +122,10 @@ class UserActivityManagerTest : public ChromeRenderViewHostTestHarness,
activity_logger_ = std::make_unique<UserActivityManager>( activity_logger_ = std::make_unique<UserActivityManager>(
&delegate_, &user_activity_detector_, PowerManagerClient::Get(), &delegate_, &user_activity_detector_, PowerManagerClient::Get(),
&session_manager_, observer.InitWithNewPipeAndPassReceiver(), &session_manager_, observer.InitWithNewPipeAndPassReceiver(),
&fake_user_manager_, &model_); &fake_user_manager_);
machine_learning::ServiceConnection::UseFakeServiceConnectionForTesting( machine_learning::ServiceConnection::UseFakeServiceConnectionForTesting(
&fake_service_connection_); &fake_service_connection_);
use_new_ml_agent_ = GetParam();
} }
void TearDown() override { void TearDown() override {
...@@ -328,13 +259,11 @@ class UserActivityManagerTest : public ChromeRenderViewHostTestHarness, ...@@ -328,13 +259,11 @@ class UserActivityManagerTest : public ChromeRenderViewHostTestHarness,
} }
TestingUserActivityUkmLogger delegate_; TestingUserActivityUkmLogger delegate_;
FakeSmartDimModel model_;
chromeos::FakeChromeUserManager fake_user_manager_; chromeos::FakeChromeUserManager fake_user_manager_;
// Only used to get SourceIds for URLs. // Only used to get SourceIds for URLs.
ukm::TestAutoSetUkmRecorder ukm_recorder_; ukm::TestAutoSetUkmRecorder ukm_recorder_;
TabActivitySimulator tab_activity_simulator_; TabActivitySimulator tab_activity_simulator_;
machine_learning::FakeServiceConnectionImpl fake_service_connection_; machine_learning::FakeServiceConnectionImpl fake_service_connection_;
bool use_new_ml_agent_;
const GURL url1_ = GURL("https://example1.com/"); const GURL url1_ = GURL("https://example1.com/");
const GURL url2_ = GURL("https://example2.com/"); const GURL url2_ = GURL("https://example2.com/");
...@@ -352,7 +281,7 @@ class UserActivityManagerTest : public ChromeRenderViewHostTestHarness, ...@@ -352,7 +281,7 @@ class UserActivityManagerTest : public ChromeRenderViewHostTestHarness,
// After an idle event, we have a ui::Event, we should expect one // After an idle event, we have a ui::Event, we should expect one
// UserActivityEvent. // UserActivityEvent.
TEST_P(UserActivityManagerTest, LogAfterIdleEvent) { TEST_F(UserActivityManagerTest, LogAfterIdleEvent) {
base::test::ScopedFeatureList scoped_feature_list; base::test::ScopedFeatureList scoped_feature_list;
scoped_feature_list.InitAndDisableFeature(features::kUserActivityPrediction); scoped_feature_list.InitAndDisableFeature(features::kUserActivityPrediction);
...@@ -379,7 +308,7 @@ TEST_P(UserActivityManagerTest, LogAfterIdleEvent) { ...@@ -379,7 +308,7 @@ TEST_P(UserActivityManagerTest, LogAfterIdleEvent) {
} }
// Get a user event before an idle event, we should not log it. // Get a user event before an idle event, we should not log it.
TEST_P(UserActivityManagerTest, LogBeforeIdleEvent) { TEST_F(UserActivityManagerTest, LogBeforeIdleEvent) {
base::test::ScopedFeatureList scoped_feature_list; base::test::ScopedFeatureList scoped_feature_list;
scoped_feature_list.InitAndDisableFeature(features::kUserActivityPrediction); scoped_feature_list.InitAndDisableFeature(features::kUserActivityPrediction);
...@@ -393,7 +322,7 @@ TEST_P(UserActivityManagerTest, LogBeforeIdleEvent) { ...@@ -393,7 +322,7 @@ TEST_P(UserActivityManagerTest, LogBeforeIdleEvent) {
// Get a user event, then an idle event, then another user event, // Get a user event, then an idle event, then another user event,
// we should log the last one. // we should log the last one.
TEST_P(UserActivityManagerTest, LogSecondEvent) { TEST_F(UserActivityManagerTest, LogSecondEvent) {
base::test::ScopedFeatureList scoped_feature_list; base::test::ScopedFeatureList scoped_feature_list;
scoped_feature_list.InitAndDisableFeature(features::kUserActivityPrediction); scoped_feature_list.InitAndDisableFeature(features::kUserActivityPrediction);
...@@ -421,7 +350,7 @@ TEST_P(UserActivityManagerTest, LogSecondEvent) { ...@@ -421,7 +350,7 @@ TEST_P(UserActivityManagerTest, LogSecondEvent) {
} }
// Log multiple events. // Log multiple events.
TEST_P(UserActivityManagerTest, LogMultipleEvents) { TEST_F(UserActivityManagerTest, LogMultipleEvents) {
base::test::ScopedFeatureList scoped_feature_list; base::test::ScopedFeatureList scoped_feature_list;
scoped_feature_list.InitAndDisableFeature(features::kUserActivityPrediction); scoped_feature_list.InitAndDisableFeature(features::kUserActivityPrediction);
...@@ -506,7 +435,7 @@ TEST_P(UserActivityManagerTest, LogMultipleEvents) { ...@@ -506,7 +435,7 @@ TEST_P(UserActivityManagerTest, LogMultipleEvents) {
EXPECT_EQ(2, events[3].features().previous_negative_actions_count()); EXPECT_EQ(2, events[3].features().previous_negative_actions_count());
} }
TEST_P(UserActivityManagerTest, UserCloseLid) { TEST_F(UserActivityManagerTest, UserCloseLid) {
base::test::ScopedFeatureList scoped_feature_list; base::test::ScopedFeatureList scoped_feature_list;
scoped_feature_list.InitAndDisableFeature(features::kUserActivityPrediction); scoped_feature_list.InitAndDisableFeature(features::kUserActivityPrediction);
...@@ -521,7 +450,7 @@ TEST_P(UserActivityManagerTest, UserCloseLid) { ...@@ -521,7 +450,7 @@ TEST_P(UserActivityManagerTest, UserCloseLid) {
EXPECT_TRUE(events.empty()); EXPECT_TRUE(events.empty());
} }
TEST_P(UserActivityManagerTest, PowerChangeActivity) { TEST_F(UserActivityManagerTest, PowerChangeActivity) {
base::test::ScopedFeatureList scoped_feature_list; base::test::ScopedFeatureList scoped_feature_list;
scoped_feature_list.InitAndDisableFeature(features::kUserActivityPrediction); scoped_feature_list.InitAndDisableFeature(features::kUserActivityPrediction);
...@@ -547,7 +476,7 @@ TEST_P(UserActivityManagerTest, PowerChangeActivity) { ...@@ -547,7 +476,7 @@ TEST_P(UserActivityManagerTest, PowerChangeActivity) {
EqualEvent(expected_event, events[0].event()); EqualEvent(expected_event, events[0].event());
} }
TEST_P(UserActivityManagerTest, VideoActivity) { TEST_F(UserActivityManagerTest, VideoActivity) {
base::test::ScopedFeatureList scoped_feature_list; base::test::ScopedFeatureList scoped_feature_list;
scoped_feature_list.InitAndDisableFeature(features::kUserActivityPrediction); scoped_feature_list.InitAndDisableFeature(features::kUserActivityPrediction);
...@@ -571,7 +500,7 @@ TEST_P(UserActivityManagerTest, VideoActivity) { ...@@ -571,7 +500,7 @@ TEST_P(UserActivityManagerTest, VideoActivity) {
// System remains idle, screen is dimmed then turned off, and system is finally // System remains idle, screen is dimmed then turned off, and system is finally
// suspended. // suspended.
TEST_P(UserActivityManagerTest, SystemIdleSuspend) { TEST_F(UserActivityManagerTest, SystemIdleSuspend) {
base::test::ScopedFeatureList scoped_feature_list; base::test::ScopedFeatureList scoped_feature_list;
scoped_feature_list.InitAndDisableFeature(features::kUserActivityPrediction); scoped_feature_list.InitAndDisableFeature(features::kUserActivityPrediction);
...@@ -600,7 +529,7 @@ TEST_P(UserActivityManagerTest, SystemIdleSuspend) { ...@@ -600,7 +529,7 @@ TEST_P(UserActivityManagerTest, SystemIdleSuspend) {
// System remains idle, screen is dimmed then turned off, but system is not // System remains idle, screen is dimmed then turned off, but system is not
// suspended. // suspended.
TEST_P(UserActivityManagerTest, SystemIdleNotSuspend) { TEST_F(UserActivityManagerTest, SystemIdleNotSuspend) {
base::test::ScopedFeatureList scoped_feature_list; base::test::ScopedFeatureList scoped_feature_list;
scoped_feature_list.InitAndDisableFeature(features::kUserActivityPrediction); scoped_feature_list.InitAndDisableFeature(features::kUserActivityPrediction);
...@@ -619,7 +548,7 @@ TEST_P(UserActivityManagerTest, SystemIdleNotSuspend) { ...@@ -619,7 +548,7 @@ TEST_P(UserActivityManagerTest, SystemIdleNotSuspend) {
// Test system idle interrupt by user activity. // Test system idle interrupt by user activity.
// We should only observe user activity. // We should only observe user activity.
TEST_P(UserActivityManagerTest, SystemIdleInterrupted) { TEST_F(UserActivityManagerTest, SystemIdleInterrupted) {
base::test::ScopedFeatureList scoped_feature_list; base::test::ScopedFeatureList scoped_feature_list;
scoped_feature_list.InitAndDisableFeature(features::kUserActivityPrediction); scoped_feature_list.InitAndDisableFeature(features::kUserActivityPrediction);
...@@ -649,7 +578,7 @@ TEST_P(UserActivityManagerTest, SystemIdleInterrupted) { ...@@ -649,7 +578,7 @@ TEST_P(UserActivityManagerTest, SystemIdleInterrupted) {
EqualEvent(expected_event, events[0].event()); EqualEvent(expected_event, events[0].event());
} }
TEST_P(UserActivityManagerTest, ScreenLockNoSuspend) { TEST_F(UserActivityManagerTest, ScreenLockNoSuspend) {
base::test::ScopedFeatureList scoped_feature_list; base::test::ScopedFeatureList scoped_feature_list;
scoped_feature_list.InitAndDisableFeature(features::kUserActivityPrediction); scoped_feature_list.InitAndDisableFeature(features::kUserActivityPrediction);
...@@ -662,7 +591,7 @@ TEST_P(UserActivityManagerTest, ScreenLockNoSuspend) { ...@@ -662,7 +591,7 @@ TEST_P(UserActivityManagerTest, ScreenLockNoSuspend) {
ASSERT_EQ(0U, events.size()); ASSERT_EQ(0U, events.size());
} }
TEST_P(UserActivityManagerTest, ScreenLockWithSuspend) { TEST_F(UserActivityManagerTest, ScreenLockWithSuspend) {
base::test::ScopedFeatureList scoped_feature_list; base::test::ScopedFeatureList scoped_feature_list;
scoped_feature_list.InitAndDisableFeature(features::kUserActivityPrediction); scoped_feature_list.InitAndDisableFeature(features::kUserActivityPrediction);
...@@ -689,7 +618,7 @@ TEST_P(UserActivityManagerTest, ScreenLockWithSuspend) { ...@@ -689,7 +618,7 @@ TEST_P(UserActivityManagerTest, ScreenLockWithSuspend) {
// As we log when SuspendImminent is received, sleep duration from SuspendDone // As we log when SuspendImminent is received, sleep duration from SuspendDone
// doesn't make any difference. // doesn't make any difference.
TEST_P(UserActivityManagerTest, SuspendIdleShortSleepDuration) { TEST_F(UserActivityManagerTest, SuspendIdleShortSleepDuration) {
base::test::ScopedFeatureList scoped_feature_list; base::test::ScopedFeatureList scoped_feature_list;
scoped_feature_list.InitAndDisableFeature(features::kUserActivityPrediction); scoped_feature_list.InitAndDisableFeature(features::kUserActivityPrediction);
...@@ -713,7 +642,7 @@ TEST_P(UserActivityManagerTest, SuspendIdleShortSleepDuration) { ...@@ -713,7 +642,7 @@ TEST_P(UserActivityManagerTest, SuspendIdleShortSleepDuration) {
EqualEvent(expected_event, events[0].event()); EqualEvent(expected_event, events[0].event());
} }
TEST_P(UserActivityManagerTest, SuspendLidClosed) { TEST_F(UserActivityManagerTest, SuspendLidClosed) {
base::test::ScopedFeatureList scoped_feature_list; base::test::ScopedFeatureList scoped_feature_list;
scoped_feature_list.InitAndDisableFeature(features::kUserActivityPrediction); scoped_feature_list.InitAndDisableFeature(features::kUserActivityPrediction);
...@@ -736,7 +665,7 @@ TEST_P(UserActivityManagerTest, SuspendLidClosed) { ...@@ -736,7 +665,7 @@ TEST_P(UserActivityManagerTest, SuspendLidClosed) {
EqualEvent(expected_event, events[0].event()); EqualEvent(expected_event, events[0].event());
} }
TEST_P(UserActivityManagerTest, SuspendOther) { TEST_F(UserActivityManagerTest, SuspendOther) {
base::test::ScopedFeatureList scoped_feature_list; base::test::ScopedFeatureList scoped_feature_list;
scoped_feature_list.InitAndDisableFeature(features::kUserActivityPrediction); scoped_feature_list.InitAndDisableFeature(features::kUserActivityPrediction);
...@@ -760,7 +689,7 @@ TEST_P(UserActivityManagerTest, SuspendOther) { ...@@ -760,7 +689,7 @@ TEST_P(UserActivityManagerTest, SuspendOther) {
} }
// Test feature extraction. // Test feature extraction.
TEST_P(UserActivityManagerTest, FeatureExtraction) { TEST_F(UserActivityManagerTest, FeatureExtraction) {
base::test::ScopedFeatureList scoped_feature_list; base::test::ScopedFeatureList scoped_feature_list;
scoped_feature_list.InitAndDisableFeature(features::kUserActivityPrediction); scoped_feature_list.InitAndDisableFeature(features::kUserActivityPrediction);
...@@ -810,7 +739,7 @@ TEST_P(UserActivityManagerTest, FeatureExtraction) { ...@@ -810,7 +739,7 @@ TEST_P(UserActivityManagerTest, FeatureExtraction) {
EXPECT_FALSE(features.screen_locked_initially()); EXPECT_FALSE(features.screen_locked_initially());
} }
TEST_P(UserActivityManagerTest, ManagedDevice) { TEST_F(UserActivityManagerTest, ManagedDevice) {
base::test::ScopedFeatureList scoped_feature_list; base::test::ScopedFeatureList scoped_feature_list;
scoped_feature_list.InitAndDisableFeature(features::kUserActivityPrediction); scoped_feature_list.InitAndDisableFeature(features::kUserActivityPrediction);
...@@ -827,7 +756,7 @@ TEST_P(UserActivityManagerTest, ManagedDevice) { ...@@ -827,7 +756,7 @@ TEST_P(UserActivityManagerTest, ManagedDevice) {
EXPECT_EQ(UserActivityEvent::Features::MANAGED, features.device_management()); EXPECT_EQ(UserActivityEvent::Features::MANAGED, features.device_management());
} }
TEST_P(UserActivityManagerTest, DimAndOffDelays) { TEST_F(UserActivityManagerTest, DimAndOffDelays) {
base::test::ScopedFeatureList scoped_feature_list; base::test::ScopedFeatureList scoped_feature_list;
scoped_feature_list.InitAndDisableFeature(features::kUserActivityPrediction); scoped_feature_list.InitAndDisableFeature(features::kUserActivityPrediction);
...@@ -846,7 +775,7 @@ TEST_P(UserActivityManagerTest, DimAndOffDelays) { ...@@ -846,7 +775,7 @@ TEST_P(UserActivityManagerTest, DimAndOffDelays) {
EXPECT_EQ(1, features.dim_to_screen_off_sec()); EXPECT_EQ(1, features.dim_to_screen_off_sec());
} }
TEST_P(UserActivityManagerTest, DimDelays) { TEST_F(UserActivityManagerTest, DimDelays) {
base::test::ScopedFeatureList scoped_feature_list; base::test::ScopedFeatureList scoped_feature_list;
scoped_feature_list.InitAndDisableFeature(features::kUserActivityPrediction); scoped_feature_list.InitAndDisableFeature(features::kUserActivityPrediction);
...@@ -865,7 +794,7 @@ TEST_P(UserActivityManagerTest, DimDelays) { ...@@ -865,7 +794,7 @@ TEST_P(UserActivityManagerTest, DimDelays) {
EXPECT_TRUE(!features.has_dim_to_screen_off_sec()); EXPECT_TRUE(!features.has_dim_to_screen_off_sec());
} }
TEST_P(UserActivityManagerTest, OffDelays) { TEST_F(UserActivityManagerTest, OffDelays) {
base::test::ScopedFeatureList scoped_feature_list; base::test::ScopedFeatureList scoped_feature_list;
scoped_feature_list.InitAndDisableFeature(features::kUserActivityPrediction); scoped_feature_list.InitAndDisableFeature(features::kUserActivityPrediction);
...@@ -886,7 +815,7 @@ TEST_P(UserActivityManagerTest, OffDelays) { ...@@ -886,7 +815,7 @@ TEST_P(UserActivityManagerTest, OffDelays) {
// Screen is off when idle event is reported. No subsequent change in screen // Screen is off when idle event is reported. No subsequent change in screen
// state. // state.
TEST_P(UserActivityManagerTest, InitialScreenOff) { TEST_F(UserActivityManagerTest, InitialScreenOff) {
base::test::ScopedFeatureList scoped_feature_list; base::test::ScopedFeatureList scoped_feature_list;
scoped_feature_list.InitAndDisableFeature(features::kUserActivityPrediction); scoped_feature_list.InitAndDisableFeature(features::kUserActivityPrediction);
...@@ -918,7 +847,7 @@ TEST_P(UserActivityManagerTest, InitialScreenOff) { ...@@ -918,7 +847,7 @@ TEST_P(UserActivityManagerTest, InitialScreenOff) {
// Screen is off when idle event is reported. No subsequent change in screen // Screen is off when idle event is reported. No subsequent change in screen
// state. // state.
TEST_P(UserActivityManagerTest, InitialScreenStateFlipped) { TEST_F(UserActivityManagerTest, InitialScreenStateFlipped) {
base::test::ScopedFeatureList scoped_feature_list; base::test::ScopedFeatureList scoped_feature_list;
scoped_feature_list.InitAndDisableFeature(features::kUserActivityPrediction); scoped_feature_list.InitAndDisableFeature(features::kUserActivityPrediction);
...@@ -951,7 +880,7 @@ TEST_P(UserActivityManagerTest, InitialScreenStateFlipped) { ...@@ -951,7 +880,7 @@ TEST_P(UserActivityManagerTest, InitialScreenStateFlipped) {
// Screen is off when idle event is reported. No subsequent change in screen // Screen is off when idle event is reported. No subsequent change in screen
// state. // state.
TEST_P(UserActivityManagerTest, ScreenOffStateChanged) { TEST_F(UserActivityManagerTest, ScreenOffStateChanged) {
base::test::ScopedFeatureList scoped_feature_list; base::test::ScopedFeatureList scoped_feature_list;
scoped_feature_list.InitAndDisableFeature(features::kUserActivityPrediction); scoped_feature_list.InitAndDisableFeature(features::kUserActivityPrediction);
...@@ -980,26 +909,18 @@ TEST_P(UserActivityManagerTest, ScreenOffStateChanged) { ...@@ -980,26 +909,18 @@ TEST_P(UserActivityManagerTest, ScreenOffStateChanged) {
EqualEvent(expected_event, events[0].event()); EqualEvent(expected_event, events[0].event());
} }
TEST_P(UserActivityManagerTest, ScreenDimDeferredWithFinalEvent) { TEST_F(UserActivityManagerTest, ScreenDimDeferredWithFinalEvent) {
base::HistogramTester histogram_tester; base::HistogramTester histogram_tester;
const std::map<std::string, std::string> params = { const std::map<std::string, std::string> params = {
{"dim_threshold", "0.651"}}; {"dim_threshold", "0.651"}};
base::test::ScopedFeatureList scoped_feature_list; base::test::ScopedFeatureList scoped_feature_list;
if (use_new_ml_agent_) {
SmartDimMlAgent::GetInstance()->ResetForTesting(); SmartDimMlAgent::GetInstance()->ResetForTesting();
scoped_feature_list.InitAndEnableFeatureWithParameters( scoped_feature_list.InitAndEnableFeatureWithParameters(
features::kUserActivityPrediction, params); features::kUserActivityPrediction, params);
} else {
scoped_feature_list.InitWithFeaturesAndParameters(
{{features::kUserActivityPrediction, params}},
{features::kSmartDimNewMlAgent});
}
// sigmoid(0.43) * 100 = 60 // sigmoid(0.43) * 100 = 60
fake_service_connection_.SetOutputValue(std::vector<int64_t>{1L}, fake_service_connection_.SetOutputValue(std::vector<int64_t>{1L},
std::vector<double>{0.43}); std::vector<double>{0.43});
model_.set_inactivity_score(60);
model_.set_decision_threshold(65);
const IdleEventNotifier::ActivityData data; const IdleEventNotifier::ActivityData data;
bool should_defer = false; bool should_defer = false;
...@@ -1010,10 +931,8 @@ TEST_P(UserActivityManagerTest, ScreenDimDeferredWithFinalEvent) { ...@@ -1010,10 +931,8 @@ TEST_P(UserActivityManagerTest, ScreenDimDeferredWithFinalEvent) {
histogram_tester.ExpectTotalCount( histogram_tester.ExpectTotalCount(
"PowerML.SmartDimModel.RequestCompleteDuration", 1); "PowerML.SmartDimModel.RequestCompleteDuration", 1);
histogram_tester.ExpectBucketCount("PowerML.SmartDimComponent.WorkerType", 0,
if (use_new_ml_agent_) 1);
histogram_tester.ExpectBucketCount("PowerML.SmartDimComponent.WorkerType",
0, 1);
const std::vector<UserActivityEvent>& events = delegate_.events(); const std::vector<UserActivityEvent>& events = delegate_.events();
ASSERT_EQ(1U, events.size()); ASSERT_EQ(1U, events.size());
...@@ -1035,26 +954,18 @@ TEST_P(UserActivityManagerTest, ScreenDimDeferredWithFinalEvent) { ...@@ -1035,26 +954,18 @@ TEST_P(UserActivityManagerTest, ScreenDimDeferredWithFinalEvent) {
EqualModelPrediction(expected_prediction, events[0].model_prediction()); EqualModelPrediction(expected_prediction, events[0].model_prediction());
} }
TEST_P(UserActivityManagerTest, ScreenDimDeferredWithoutFinalEvent) { TEST_F(UserActivityManagerTest, ScreenDimDeferredWithoutFinalEvent) {
base::HistogramTester histogram_tester; base::HistogramTester histogram_tester;
const std::map<std::string, std::string> params = { const std::map<std::string, std::string> params = {
{"dim_threshold", "0.651"}}; {"dim_threshold", "0.651"}};
base::test::ScopedFeatureList scoped_feature_list; base::test::ScopedFeatureList scoped_feature_list;
if (use_new_ml_agent_) {
SmartDimMlAgent::GetInstance()->ResetForTesting(); SmartDimMlAgent::GetInstance()->ResetForTesting();
scoped_feature_list.InitAndEnableFeatureWithParameters( scoped_feature_list.InitAndEnableFeatureWithParameters(
features::kUserActivityPrediction, params); features::kUserActivityPrediction, params);
} else {
scoped_feature_list.InitWithFeaturesAndParameters(
{{features::kUserActivityPrediction, params}},
{features::kSmartDimNewMlAgent});
}
// sigmoid(0.43) * 100 = 60 // sigmoid(0.43) * 100 = 60
fake_service_connection_.SetOutputValue(std::vector<int64_t>{1L}, fake_service_connection_.SetOutputValue(std::vector<int64_t>{1L},
std::vector<double>{0.43}); std::vector<double>{0.43});
model_.set_inactivity_score(60);
model_.set_decision_threshold(65);
const IdleEventNotifier::ActivityData data; const IdleEventNotifier::ActivityData data;
bool should_defer = false; bool should_defer = false;
...@@ -1064,10 +975,8 @@ TEST_P(UserActivityManagerTest, ScreenDimDeferredWithoutFinalEvent) { ...@@ -1064,10 +975,8 @@ TEST_P(UserActivityManagerTest, ScreenDimDeferredWithoutFinalEvent) {
histogram_tester.ExpectTotalCount( histogram_tester.ExpectTotalCount(
"PowerML.SmartDimModel.RequestCompleteDuration", 1); "PowerML.SmartDimModel.RequestCompleteDuration", 1);
histogram_tester.ExpectBucketCount("PowerML.SmartDimComponent.WorkerType", 0,
if (use_new_ml_agent_) 1);
histogram_tester.ExpectBucketCount("PowerML.SmartDimComponent.WorkerType",
0, 1);
const std::vector<UserActivityEvent>& events = delegate_.events(); const std::vector<UserActivityEvent>& events = delegate_.events();
EXPECT_TRUE(events.empty()); EXPECT_TRUE(events.empty());
...@@ -1075,26 +984,18 @@ TEST_P(UserActivityManagerTest, ScreenDimDeferredWithoutFinalEvent) { ...@@ -1075,26 +984,18 @@ TEST_P(UserActivityManagerTest, ScreenDimDeferredWithoutFinalEvent) {
// Tests the cancellation of a Smart Dim decision request, immediately after it // Tests the cancellation of a Smart Dim decision request, immediately after it
// has been requested. // has been requested.
TEST_P(UserActivityManagerTest, ScreenDimRequestCanceled) { TEST_F(UserActivityManagerTest, ScreenDimRequestCanceled) {
base::HistogramTester histogram_tester; base::HistogramTester histogram_tester;
const std::map<std::string, std::string> params = { const std::map<std::string, std::string> params = {
{"dim_threshold", "0.651"}}; {"dim_threshold", "0.651"}};
base::test::ScopedFeatureList scoped_feature_list; base::test::ScopedFeatureList scoped_feature_list;
if (use_new_ml_agent_) {
SmartDimMlAgent::GetInstance()->ResetForTesting(); SmartDimMlAgent::GetInstance()->ResetForTesting();
scoped_feature_list.InitAndEnableFeatureWithParameters( scoped_feature_list.InitAndEnableFeatureWithParameters(
features::kUserActivityPrediction, params); features::kUserActivityPrediction, params);
} else {
scoped_feature_list.InitWithFeaturesAndParameters(
{{features::kUserActivityPrediction, params}},
{features::kSmartDimNewMlAgent});
}
// sigmoid(0.43) * 100 = 60 // sigmoid(0.43) * 100 = 60
fake_service_connection_.SetOutputValue(std::vector<int64_t>{1L}, fake_service_connection_.SetOutputValue(std::vector<int64_t>{1L},
std::vector<double>{0.43}); std::vector<double>{0.43});
model_.set_inactivity_score(60);
model_.set_decision_threshold(65);
const IdleEventNotifier::ActivityData data; const IdleEventNotifier::ActivityData data;
bool should_defer = false; bool should_defer = false;
...@@ -1109,10 +1010,8 @@ TEST_P(UserActivityManagerTest, ScreenDimRequestCanceled) { ...@@ -1109,10 +1010,8 @@ TEST_P(UserActivityManagerTest, ScreenDimRequestCanceled) {
"PowerML.SmartDimModel.RequestCompleteDuration", 0); "PowerML.SmartDimModel.RequestCompleteDuration", 0);
histogram_tester.ExpectTotalCount( histogram_tester.ExpectTotalCount(
"PowerML.SmartDimModel.RequestCanceledDuration", 1); "PowerML.SmartDimModel.RequestCanceledDuration", 1);
histogram_tester.ExpectBucketCount("PowerML.SmartDimComponent.WorkerType", 0,
if (use_new_ml_agent_) 1);
histogram_tester.ExpectBucketCount("PowerML.SmartDimComponent.WorkerType",
0, 1);
// Since the pending SmartDim decision request was canceled, we shouldn't // Since the pending SmartDim decision request was canceled, we shouldn't
// have any UserActivityEvent generated. // have any UserActivityEvent generated.
...@@ -1122,26 +1021,18 @@ TEST_P(UserActivityManagerTest, ScreenDimRequestCanceled) { ...@@ -1122,26 +1021,18 @@ TEST_P(UserActivityManagerTest, ScreenDimRequestCanceled) {
// Tests the cancellation of a Smart Dim decision request, when two idle events // Tests the cancellation of a Smart Dim decision request, when two idle events
// occur in quick succession. This verifies that only one request is serviced. // occur in quick succession. This verifies that only one request is serviced.
TEST_P(UserActivityManagerTest, ScreenDimConsecutiveRequests) { TEST_F(UserActivityManagerTest, ScreenDimConsecutiveRequests) {
base::HistogramTester histogram_tester; base::HistogramTester histogram_tester;
const std::map<std::string, std::string> params = { const std::map<std::string, std::string> params = {
{"dim_threshold", "0.651"}}; {"dim_threshold", "0.651"}};
base::test::ScopedFeatureList scoped_feature_list; base::test::ScopedFeatureList scoped_feature_list;
if (use_new_ml_agent_) {
SmartDimMlAgent::GetInstance()->ResetForTesting(); SmartDimMlAgent::GetInstance()->ResetForTesting();
scoped_feature_list.InitAndEnableFeatureWithParameters( scoped_feature_list.InitAndEnableFeatureWithParameters(
features::kUserActivityPrediction, params); features::kUserActivityPrediction, params);
} else {
scoped_feature_list.InitWithFeaturesAndParameters(
{{features::kUserActivityPrediction, params}},
{features::kSmartDimNewMlAgent});
}
// sigmoid(0.43) * 100 = 60 // sigmoid(0.43) * 100 = 60
fake_service_connection_.SetOutputValue(std::vector<int64_t>{1L}, fake_service_connection_.SetOutputValue(std::vector<int64_t>{1L},
std::vector<double>{0.43}); std::vector<double>{0.43});
model_.set_inactivity_score(60);
model_.set_decision_threshold(65);
const IdleEventNotifier::ActivityData data; const IdleEventNotifier::ActivityData data;
bool should_defer_1 = false; bool should_defer_1 = false;
...@@ -1156,10 +1047,8 @@ TEST_P(UserActivityManagerTest, ScreenDimConsecutiveRequests) { ...@@ -1156,10 +1047,8 @@ TEST_P(UserActivityManagerTest, ScreenDimConsecutiveRequests) {
"PowerML.SmartDimModel.RequestCompleteDuration", 1); "PowerML.SmartDimModel.RequestCompleteDuration", 1);
histogram_tester.ExpectTotalCount( histogram_tester.ExpectTotalCount(
"PowerML.SmartDimModel.RequestCanceledDuration", 1); "PowerML.SmartDimModel.RequestCanceledDuration", 1);
histogram_tester.ExpectBucketCount("PowerML.SmartDimComponent.WorkerType", 0,
if (use_new_ml_agent_) 2);
histogram_tester.ExpectBucketCount("PowerML.SmartDimComponent.WorkerType",
0, 2);
const std::vector<UserActivityEvent>& events = delegate_.events(); const std::vector<UserActivityEvent>& events = delegate_.events();
ASSERT_EQ(1U, events.size()); ASSERT_EQ(1U, events.size());
...@@ -1181,26 +1070,18 @@ TEST_P(UserActivityManagerTest, ScreenDimConsecutiveRequests) { ...@@ -1181,26 +1070,18 @@ TEST_P(UserActivityManagerTest, ScreenDimConsecutiveRequests) {
EqualModelPrediction(expected_prediction, events[0].model_prediction()); EqualModelPrediction(expected_prediction, events[0].model_prediction());
} }
TEST_P(UserActivityManagerTest, ScreenDimNotDeferred) { TEST_F(UserActivityManagerTest, ScreenDimNotDeferred) {
base::HistogramTester histogram_tester; base::HistogramTester histogram_tester;
const std::map<std::string, std::string> params = { const std::map<std::string, std::string> params = {
{"dim_threshold", base::NumberToString(0.0)}}; {"dim_threshold", base::NumberToString(0.0)}};
base::test::ScopedFeatureList scoped_feature_list; base::test::ScopedFeatureList scoped_feature_list;
if (use_new_ml_agent_) {
SmartDimMlAgent::GetInstance()->ResetForTesting(); SmartDimMlAgent::GetInstance()->ResetForTesting();
scoped_feature_list.InitAndEnableFeatureWithParameters( scoped_feature_list.InitAndEnableFeatureWithParameters(
features::kUserActivityPrediction, params); features::kUserActivityPrediction, params);
} else {
scoped_feature_list.InitWithFeaturesAndParameters(
{{features::kUserActivityPrediction, params}},
{features::kSmartDimNewMlAgent});
}
// sigmoid(0.43) * 100 = 60 // sigmoid(0.43) * 100 = 60
fake_service_connection_.SetOutputValue(std::vector<int64_t>{1L}, fake_service_connection_.SetOutputValue(std::vector<int64_t>{1L},
std::vector<double>{0.43}); std::vector<double>{0.43});
model_.set_inactivity_score(60);
model_.set_decision_threshold(50);
const IdleEventNotifier::ActivityData data; const IdleEventNotifier::ActivityData data;
bool should_defer = false; bool should_defer = false;
...@@ -1211,10 +1092,8 @@ TEST_P(UserActivityManagerTest, ScreenDimNotDeferred) { ...@@ -1211,10 +1092,8 @@ TEST_P(UserActivityManagerTest, ScreenDimNotDeferred) {
histogram_tester.ExpectTotalCount( histogram_tester.ExpectTotalCount(
"PowerML.SmartDimModel.RequestCompleteDuration", 1); "PowerML.SmartDimModel.RequestCompleteDuration", 1);
histogram_tester.ExpectBucketCount("PowerML.SmartDimComponent.WorkerType", 0,
if (use_new_ml_agent_) 1);
histogram_tester.ExpectBucketCount("PowerML.SmartDimComponent.WorkerType",
0, 1);
const std::vector<UserActivityEvent>& events = delegate_.events(); const std::vector<UserActivityEvent>& events = delegate_.events();
ASSERT_EQ(1U, events.size()); ASSERT_EQ(1U, events.size());
...@@ -1228,27 +1107,19 @@ TEST_P(UserActivityManagerTest, ScreenDimNotDeferred) { ...@@ -1228,27 +1107,19 @@ TEST_P(UserActivityManagerTest, ScreenDimNotDeferred) {
EqualModelPrediction(expected_prediction, events[0].model_prediction()); EqualModelPrediction(expected_prediction, events[0].model_prediction());
} }
TEST_P(UserActivityManagerTest, TwoScreenDimImminentWithEventInBetween) { TEST_F(UserActivityManagerTest, TwoScreenDimImminentWithEventInBetween) {
base::HistogramTester histogram_tester; base::HistogramTester histogram_tester;
const std::map<std::string, std::string> params = { const std::map<std::string, std::string> params = {
{"dim_threshold", base::NumberToString(0.0)}}; {"dim_threshold", base::NumberToString(0.0)}};
base::test::ScopedFeatureList scoped_feature_list; base::test::ScopedFeatureList scoped_feature_list;
if (use_new_ml_agent_) {
SmartDimMlAgent::GetInstance()->ResetForTesting(); SmartDimMlAgent::GetInstance()->ResetForTesting();
scoped_feature_list.InitAndEnableFeatureWithParameters( scoped_feature_list.InitAndEnableFeatureWithParameters(
features::kUserActivityPrediction, params); features::kUserActivityPrediction, params);
} else {
scoped_feature_list.InitWithFeaturesAndParameters(
{{features::kUserActivityPrediction, params}},
{features::kSmartDimNewMlAgent});
}
model_.set_decision_threshold(50);
// 1st ScreenDimImminent gets deferred // 1st ScreenDimImminent gets deferred
// sigmoid(-0.4) * 100 = 40 // sigmoid(-0.4) * 100 = 40
fake_service_connection_.SetOutputValue(std::vector<int64_t>{1L}, fake_service_connection_.SetOutputValue(std::vector<int64_t>{1L},
std::vector<double>{-0.4}); std::vector<double>{-0.4});
model_.set_inactivity_score(40);
const IdleEventNotifier::ActivityData data; const IdleEventNotifier::ActivityData data;
bool should_defer = false; bool should_defer = false;
...@@ -1264,7 +1135,6 @@ TEST_P(UserActivityManagerTest, TwoScreenDimImminentWithEventInBetween) { ...@@ -1264,7 +1135,6 @@ TEST_P(UserActivityManagerTest, TwoScreenDimImminentWithEventInBetween) {
// sigmoid(-1.35) * 100 = 20 // sigmoid(-1.35) * 100 = 20
fake_service_connection_.SetOutputValue(std::vector<int64_t>{1L}, fake_service_connection_.SetOutputValue(std::vector<int64_t>{1L},
std::vector<double>{-1.35}); std::vector<double>{-1.35});
model_.set_inactivity_score(20);
task_environment()->FastForwardBy(base::TimeDelta::FromSeconds(10)); task_environment()->FastForwardBy(base::TimeDelta::FromSeconds(10));
ReportIdleEvent(data, &should_defer); ReportIdleEvent(data, &should_defer);
task_environment()->RunUntilIdle(); task_environment()->RunUntilIdle();
...@@ -1272,10 +1142,8 @@ TEST_P(UserActivityManagerTest, TwoScreenDimImminentWithEventInBetween) { ...@@ -1272,10 +1142,8 @@ TEST_P(UserActivityManagerTest, TwoScreenDimImminentWithEventInBetween) {
histogram_tester.ExpectTotalCount( histogram_tester.ExpectTotalCount(
"PowerML.SmartDimModel.RequestCompleteDuration", 2); "PowerML.SmartDimModel.RequestCompleteDuration", 2);
histogram_tester.ExpectBucketCount("PowerML.SmartDimComponent.WorkerType", 0,
if (use_new_ml_agent_) 2);
histogram_tester.ExpectBucketCount("PowerML.SmartDimComponent.WorkerType",
0, 2);
// Log when a SuspendImminent is received // Log when a SuspendImminent is received
task_environment()->FastForwardBy(base::TimeDelta::FromSeconds(20)); task_environment()->FastForwardBy(base::TimeDelta::FromSeconds(20));
...@@ -1321,27 +1189,19 @@ TEST_P(UserActivityManagerTest, TwoScreenDimImminentWithEventInBetween) { ...@@ -1321,27 +1189,19 @@ TEST_P(UserActivityManagerTest, TwoScreenDimImminentWithEventInBetween) {
EqualModelPrediction(expected_prediction2, events[1].model_prediction()); EqualModelPrediction(expected_prediction2, events[1].model_prediction());
} }
TEST_P(UserActivityManagerTest, TwoScreenDimImminentWithoutEventInBetween) { TEST_F(UserActivityManagerTest, TwoScreenDimImminentWithoutEventInBetween) {
base::HistogramTester histogram_tester; base::HistogramTester histogram_tester;
const std::map<std::string, std::string> params = { const std::map<std::string, std::string> params = {
{"dim_threshold", base::NumberToString(0.0)}}; {"dim_threshold", base::NumberToString(0.0)}};
base::test::ScopedFeatureList scoped_feature_list; base::test::ScopedFeatureList scoped_feature_list;
if (use_new_ml_agent_) {
SmartDimMlAgent::GetInstance()->ResetForTesting(); SmartDimMlAgent::GetInstance()->ResetForTesting();
scoped_feature_list.InitAndEnableFeatureWithParameters( scoped_feature_list.InitAndEnableFeatureWithParameters(
features::kUserActivityPrediction, params); features::kUserActivityPrediction, params);
} else {
scoped_feature_list.InitWithFeaturesAndParameters(
{{features::kUserActivityPrediction, params}},
{features::kSmartDimNewMlAgent});
}
model_.set_decision_threshold(50);
// 1st ScreenDimImminent gets deferred // 1st ScreenDimImminent gets deferred
// sigmoid(-0.4) * 100 = 40 // sigmoid(-0.4) * 100 = 40
fake_service_connection_.SetOutputValue(std::vector<int64_t>{1L}, fake_service_connection_.SetOutputValue(std::vector<int64_t>{1L},
std::vector<double>{-0.4}); std::vector<double>{-0.4});
model_.set_inactivity_score(40);
const IdleEventNotifier::ActivityData data; const IdleEventNotifier::ActivityData data;
bool should_defer = false; bool should_defer = false;
ReportIdleEvent(data, &should_defer); ReportIdleEvent(data, &should_defer);
...@@ -1352,7 +1212,6 @@ TEST_P(UserActivityManagerTest, TwoScreenDimImminentWithoutEventInBetween) { ...@@ -1352,7 +1212,6 @@ TEST_P(UserActivityManagerTest, TwoScreenDimImminentWithoutEventInBetween) {
// sigmoid(-1.35) * 100 = 20 // sigmoid(-1.35) * 100 = 20
fake_service_connection_.SetOutputValue(std::vector<int64_t>{1L}, fake_service_connection_.SetOutputValue(std::vector<int64_t>{1L},
std::vector<double>{-1.35}); std::vector<double>{-1.35});
model_.set_inactivity_score(20);
task_environment()->FastForwardBy(base::TimeDelta::FromSeconds(10)); task_environment()->FastForwardBy(base::TimeDelta::FromSeconds(10));
ReportIdleEvent(data, &should_defer); ReportIdleEvent(data, &should_defer);
task_environment()->RunUntilIdle(); task_environment()->RunUntilIdle();
...@@ -1360,10 +1219,8 @@ TEST_P(UserActivityManagerTest, TwoScreenDimImminentWithoutEventInBetween) { ...@@ -1360,10 +1219,8 @@ TEST_P(UserActivityManagerTest, TwoScreenDimImminentWithoutEventInBetween) {
histogram_tester.ExpectTotalCount( histogram_tester.ExpectTotalCount(
"PowerML.SmartDimModel.RequestCompleteDuration", 2); "PowerML.SmartDimModel.RequestCompleteDuration", 2);
histogram_tester.ExpectBucketCount("PowerML.SmartDimComponent.WorkerType", 0,
if (use_new_ml_agent_) 2);
histogram_tester.ExpectBucketCount("PowerML.SmartDimComponent.WorkerType",
0, 2);
// Log when a SuspendImminent is received // Log when a SuspendImminent is received
task_environment()->FastForwardBy(base::TimeDelta::FromSeconds(20)); task_environment()->FastForwardBy(base::TimeDelta::FromSeconds(20));
...@@ -1404,53 +1261,8 @@ TEST_P(UserActivityManagerTest, TwoScreenDimImminentWithoutEventInBetween) { ...@@ -1404,53 +1261,8 @@ TEST_P(UserActivityManagerTest, TwoScreenDimImminentWithoutEventInBetween) {
EqualModelPrediction(expected_prediction2, events[0].model_prediction()); EqualModelPrediction(expected_prediction2, events[0].model_prediction());
} }
TEST_P(UserActivityManagerTest, ModelError) {
base::HistogramTester histogram_tester;
const std::map<std::string, std::string> params = {
{"dim_threshold", "0.651"}};
base::test::ScopedFeatureList scoped_feature_list;
// This ModelError only happens in old SmartDimModel, not for new ml_agent.
// Now that the new ml_agent is the default behavior, we need to disable
// kSmartDimNewMlAgent for this test.
scoped_feature_list.InitWithFeaturesAndParameters(
{{features::kUserActivityPrediction, params}},
{features::kSmartDimNewMlAgent});
// This value will trigger a model error.
model_.set_inactivity_score(160);
model_.set_decision_threshold(65);
const IdleEventNotifier::ActivityData data;
bool should_defer = false;
ReportIdleEvent(data, &should_defer);
task_environment()->RunUntilIdle();
ReportUserActivity(nullptr);
EXPECT_FALSE(should_defer);
std::string histogram("PowerML.SmartDimModel.RequestCompleteDuration");
histogram_tester.ExpectTotalCount(histogram, 1);
const std::vector<UserActivityEvent>& events = delegate_.events();
ASSERT_EQ(1U, events.size());
UserActivityEvent::Event expected_event;
expected_event.set_type(UserActivityEvent::Event::REACTIVATE);
expected_event.set_reason(UserActivityEvent::Event::USER_ACTIVITY);
expected_event.set_log_duration_sec(0);
expected_event.set_screen_dim_occurred(false);
expected_event.set_screen_off_occurred(false);
expected_event.set_screen_lock_occurred(false);
EqualEvent(expected_event, events[0].event());
UserActivityEvent::ModelPrediction expected_prediction;
expected_prediction.set_model_applied(false);
expected_prediction.set_response(
UserActivityEvent::ModelPrediction::MODEL_ERROR);
EqualModelPrediction(expected_prediction, events[0].model_prediction());
}
// Test is flaky. See https://crbug.com/938055. // Test is flaky. See https://crbug.com/938055.
TEST_P(UserActivityManagerTest, DISABLED_BasicTabs) { TEST_F(UserActivityManagerTest, DISABLED_BasicTabs) {
base::test::ScopedFeatureList scoped_feature_list; base::test::ScopedFeatureList scoped_feature_list;
scoped_feature_list.InitAndDisableFeature(features::kUserActivityPrediction); scoped_feature_list.InitAndDisableFeature(features::kUserActivityPrediction);
...@@ -1482,7 +1294,7 @@ TEST_P(UserActivityManagerTest, DISABLED_BasicTabs) { ...@@ -1482,7 +1294,7 @@ TEST_P(UserActivityManagerTest, DISABLED_BasicTabs) {
} }
// Test is flaky. See https://crbug.com/938141. // Test is flaky. See https://crbug.com/938141.
TEST_P(UserActivityManagerTest, DISABLED_MultiBrowsersAndTabs) { TEST_F(UserActivityManagerTest, DISABLED_MultiBrowsersAndTabs) {
base::test::ScopedFeatureList scoped_feature_list; base::test::ScopedFeatureList scoped_feature_list;
scoped_feature_list.InitAndDisableFeature(features::kUserActivityPrediction); scoped_feature_list.InitAndDisableFeature(features::kUserActivityPrediction);
...@@ -1529,7 +1341,7 @@ TEST_P(UserActivityManagerTest, DISABLED_MultiBrowsersAndTabs) { ...@@ -1529,7 +1341,7 @@ TEST_P(UserActivityManagerTest, DISABLED_MultiBrowsersAndTabs) {
tab_strip_model3->CloseAllTabs(); tab_strip_model3->CloseAllTabs();
} }
TEST_P(UserActivityManagerTest, Incognito) { TEST_F(UserActivityManagerTest, Incognito) {
base::test::ScopedFeatureList scoped_feature_list; base::test::ScopedFeatureList scoped_feature_list;
scoped_feature_list.InitAndDisableFeature(features::kUserActivityPrediction); scoped_feature_list.InitAndDisableFeature(features::kUserActivityPrediction);
...@@ -1557,7 +1369,7 @@ TEST_P(UserActivityManagerTest, Incognito) { ...@@ -1557,7 +1369,7 @@ TEST_P(UserActivityManagerTest, Incognito) {
tab_strip_model->CloseAllTabs(); tab_strip_model->CloseAllTabs();
} }
TEST_P(UserActivityManagerTest, NoOpenTabs) { TEST_F(UserActivityManagerTest, NoOpenTabs) {
base::test::ScopedFeatureList scoped_feature_list; base::test::ScopedFeatureList scoped_feature_list;
scoped_feature_list.InitAndDisableFeature(features::kUserActivityPrediction); scoped_feature_list.InitAndDisableFeature(features::kUserActivityPrediction);
...@@ -1578,10 +1390,6 @@ TEST_P(UserActivityManagerTest, NoOpenTabs) { ...@@ -1578,10 +1390,6 @@ TEST_P(UserActivityManagerTest, NoOpenTabs) {
EXPECT_FALSE(features.has_has_form_entry()); EXPECT_FALSE(features.has_has_form_entry());
} }
INSTANTIATE_TEST_SUITE_P(UserActivityManagerTestInstantiation,
UserActivityManagerTest,
testing::Values(false, true));
} // namespace ml } // namespace ml
} // namespace power } // namespace power
} // namespace chromeos } // namespace chromeos
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment