# Copyright (c) 2021-2023 Huawei Device Co., Ltd.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import("//build/test.gni")
import("../../powermgr.gni")

module_output_path = "power_manager/power_manager"

###############################################################################
config("module_private_config") {
  visibility = [ ":*" ]

  include_dirs = [
    "include",
    "mock",
    "${powermgr_service_path}/native/src/hibernate",
    "${powermgr_service_path}/native/src/ulsr",
    "${powermgr_service_path}/native/src/proximity_sensor_controller",
    "${powermgr_service_path}/native/src/runninglock",
    "${powermgr_service_path}/native/src/shutdown",
    "${powermgr_service_path}/native/src/suspend",
    "${powermgr_service_path}/native/src/wakeup",
    "${powermgr_service_path}/native/src/screenoffpre",
    "${powermgr_service_path}/native/src/setting",
  ]

  if (power_manager_feature_wakeup_action) {
    include_dirs += [ "${powermgr_service_path}/native/src/wakeup_action" ]
  }

  if (power_manager_feature_screen_saver) {
    include_dirs += [ "${powermgr_service_path}/native/src/power_screen_saver" ]
  }
}

deps_ex = [
  "ability_base:base",
  "ability_base:want",
  "ability_runtime:ability_manager",
  "bundle_framework:appexecfwk_base",
  "bundle_framework:appexecfwk_core",
  "cJSON:cjson",
  "c_utils:utils",
  "common_event_service:cesfwk_innerkits",
  "ffrt:libffrt",
  "googletest:gmock_main",
  "googletest:gtest_main",
  "hilog:libhilog",
  "input:libmmi-client",
  "ipc:ipc_core",
  "libxml2:libxml2",
  "safwk:system_ability_fwk",
  "samgr:samgr_proxy",
  "data_share:datashare_common",
  "data_share:datashare_consumer",
]

if (has_sensors_sensor_part) {
  deps_ex += [ "sensor:sensor_interface_native" ]
}
if (has_hiviewdfx_hisysevent_part) {
  deps_ex += [ "hisysevent:libhisysevent" ]
}

if (power_manager_feature_report_screenoff_invalid) {
  deps_ex += [ "window_manager:libwm_lite" ]
  defines += [ "POWER_MANAGER_REPORT_SCREENOFF_INVALID" ]
}

##############################systemtest##########################################

ohos_systemtest("ces_system") {
  sanitize = {
    cfi = true
    cfi_cross_dso = true
    debug = false
    blocklist = "../cfi_blocklist.txt"
  }

  module_out_path = module_output_path

  sources = [ "src/ces_system_test.cpp" ]

  configs = [
    "${powermgr_utils_path}:utils_config",
    ":module_private_config",
    "${powermgr_utils_path}:coverage_flags",
  ]

  deps = [
    "${powermgr_inner_api}:powermgr_client",
    "${powermgr_inner_api}:powermgr_stub",
    "${powermgr_utils_path}/ffrt:power_ffrt",
    "${powermgr_service_path}:powermgrservice",
    "${powermgr_service_path}/native/src/actions:powermgr_actions",
  ]

  external_deps = deps_ex
}

##############################mocksystemtest##########################################

ohos_systemtest("test_power_mgr_mock_system") {
  module_out_path = module_output_path

  sanitize = {
    cfi = true
    cfi_cross_dso = true
    debug = false
    blocklist = "../cfi_blocklist.txt"
  }

  sources = [ "src/power_mgr_mock_system_test.cpp" ]

  configs = [
    "${powermgr_utils_path}:utils_config",
    ":module_private_config",
    "${powermgr_utils_path}:coverage_flags",
  ]

  deps = [
    "${powermgr_inner_api}:powermgr_client",
    "${powermgr_inner_api}:powermgr_stub",
    "${powermgr_utils_path}/ffrt:power_ffrt",
    "${powermgr_service_path}:powermgrservice",
    "${powermgr_service_path}/native/src/actions:powermgr_actions",
  ]

  external_deps = deps_ex
}

################################powersavemode################################

ohos_systemtest("test_power_mgr_powersavemode") {
  module_out_path = module_output_path

  sources = [ "src/power_mgr_powersavemode_test.cpp" ]

  sanitize = {
    cfi = true
    cfi_cross_dso = true
    debug = false
    blocklist = "../cfi_blocklist.txt"
  }

  configs = [
    "${powermgr_utils_path}:utils_config",
    ":module_private_config",
    "${powermgr_utils_path}:coverage_flags",
  ]

  deps = [
    "${powermgr_inner_api}:powermgr_client",
    "${powermgr_inner_api}:powermgr_stub",
    "${powermgr_utils_path}/ffrt:power_ffrt",
    "${powermgr_service_path}:powermgrservice",
    "${powermgr_service_path}/native/src/actions:powermgr_actions",
  ]

  external_deps = deps_ex
}

################################powersetdevicemode################################

ohos_systemtest("test_power_mgr_powersetdevicemode") {
  module_out_path = module_output_path

  sources = [ "src/power_mgr_setdevicemode_test.cpp" ]

  sanitize = {
    cfi = true
    cfi_cross_dso = true
    debug = false
    blocklist = "../cfi_blocklist.txt"
  }

  configs = [
    "${powermgr_utils_path}:utils_config",
    ":module_private_config",
    "${powermgr_utils_path}:coverage_flags",
  ]

  deps = [
    "${powermgr_inner_api}:powermgr_client",
    "${powermgr_inner_api}:powermgr_stub",
    "${powermgr_utils_path}/ffrt:power_ffrt",
    "${powermgr_service_path}:powermgrservice",
    "${powermgr_service_path}/native/src/actions:powermgr_actions",
  ]

  external_deps = deps_ex
}

################################powermgrsystem################################

ohos_systemtest("test_power_mgr_system") {
  sanitize = {
    cfi = true
    cfi_cross_dso = true
    debug = false
    blocklist = "../cfi_blocklist.txt"
  }

  module_out_path = module_output_path

  resource_config_file =
      "${powermgr_root_path}/test/systemtest/resources/ohos_test.xml"

  sources = [ "src/power_mgr_system_test.cpp" ]

  configs = [
    "${powermgr_utils_path}:utils_config",
    ":module_private_config",
    "${powermgr_utils_path}:coverage_flags",
  ]

  deps = [
    "${powermgr_inner_api}:powermgr_client",
    "${powermgr_inner_api}:powermgr_stub",
    "${powermgr_utils_path}/ffrt:power_ffrt",
    "${powermgr_service_path}:powermgrservice",
    "${powermgr_service_path}/native/src/actions:powermgr_actions",
  ]

  external_deps = deps_ex
}

################################fastshutdown################################

ohos_systemtest("test_power_mgr_shutdown_fast") {
  module_out_path = module_output_path

  sources = [ "src/power_mgr_shutdown_fast_test.cpp" ]

  sanitize = {
    cfi = true
    cfi_cross_dso = true
    debug = false
    blocklist = "../cfi_blocklist.txt"
  }

  configs = [
    "${powermgr_utils_path}:utils_config",
    ":module_private_config",
    "${powermgr_utils_path}:coverage_flags",
  ]

  deps = [
    "${powermgr_inner_api}:powermgr_client",
    "${powermgr_inner_api}:powermgr_stub",
    "${powermgr_utils_path}/ffrt:power_ffrt",
    "${powermgr_service_path}:powermgrservice",
    "${powermgr_service_path}/native/src/actions:powermgr_actions",
  ]

  external_deps = deps_ex
}

################################powerevent################################

ohos_systemtest("test_power_level_event_system_test_off") {
  module_out_path = module_output_path

  sources = [ "src/power_level_event_system_test_off.cpp" ]

  sanitize = {
    cfi = true
    cfi_cross_dso = true
    debug = false
    blocklist = "../cfi_blocklist.txt"
  }

  configs = [
    "${powermgr_utils_path}:utils_config",
    ":module_private_config",
    "${powermgr_utils_path}:coverage_flags",
  ]

  deps = [
    "${powermgr_inner_api}:powermgr_client",
    "${powermgr_inner_api}:powermgr_stub",
    "${powermgr_utils_path}/ffrt:power_ffrt",
    "${powermgr_service_path}:powermgrservice",
    "${powermgr_service_path}/native/src/actions:powermgr_actions",
  ]

  external_deps = deps_ex
}

################################powerevent################################

ohos_systemtest("test_power_level_event_system_test_on") {
  module_out_path = module_output_path

  sources = [ "src/power_level_event_system_test_on.cpp" ]

  sanitize = {
    cfi = true
    cfi_cross_dso = true
    debug = false
    blocklist = "../cfi_blocklist.txt"
  }

  configs = [
    "${powermgr_utils_path}:utils_config",
    ":module_private_config",
    "${powermgr_utils_path}:coverage_flags",
  ]

  deps = [
    "${powermgr_inner_api}:powermgr_client",
    "${powermgr_inner_api}:powermgr_stub",
    "${powermgr_utils_path}/ffrt:power_ffrt",
    "${powermgr_service_path}:powermgrservice",
    "${powermgr_service_path}/native/src/actions:powermgr_actions",
  ]

  external_deps = deps_ex
}

##############################sttest##########################################
ohos_systemtest("test_power_st_mgr_mock") {
  module_out_path = module_output_path

  sanitize = {
    cfi = true
    cfi_cross_dso = true
    debug = false
    blocklist = "../cfi_blocklist.txt"
  }

  sources = [ "src/power_mgr_st_mock_test.cpp" ]

  configs = [
    "${powermgr_utils_path}:utils_config",
    ":module_private_config",
    "${powermgr_utils_path}:coverage_flags",
  ]

  deps = [
    "${powermgr_inner_api}:powermgr_client",
    "${powermgr_inner_api}:powermgr_stub",
    "${powermgr_utils_path}/ffrt:power_ffrt",
    "${powermgr_service_path}:powermgrservice",
    "${powermgr_service_path}/native/src/actions:powermgr_actions",
  ]

  external_deps = deps_ex
}

##############################suspendsttest##########################################
ohos_systemtest("test_power_st_mgr_mock_suspend") {
  module_out_path = module_output_path

  sanitize = {
    cfi = true
    cfi_cross_dso = true
    debug = false
    blocklist = "../cfi_blocklist.txt"
  }

  sources = [ "src/power_mgr_st_suspend_test.cpp" ]

  configs = [
    "${powermgr_utils_path}:utils_config",
    ":module_private_config",
    "${powermgr_utils_path}:coverage_flags",
  ]

  deps = [
    "${powermgr_inner_api}:powermgr_client",
    "${powermgr_inner_api}:powermgr_stub",
    "${powermgr_utils_path}/ffrt:power_ffrt",
    "${powermgr_service_path}:powermgrservice",
    "${powermgr_service_path}/native/src/actions:powermgr_actions",
  ]

  external_deps = deps_ex
}

##############################settinghelp##########################################
ohos_systemtest("test_power_setting_help") {
  module_out_path = module_output_path

  sanitize = {
    cfi = true
    cfi_cross_dso = true
    debug = false
    blocklist = "../cfi_blocklist.txt"
  }

  sources = [ "src/power_setting_help_test.cpp" ]

  configs = [
    "${powermgr_utils_path}:utils_config",
    ":module_private_config",
    "${powermgr_utils_path}:coverage_flags",
  ]

  include_dirs = [ "${powermgr_service_path}/native/src/setting/" ]
  
  deps = [
    "${powermgr_inner_api}:powermgr_client",
    "${powermgr_inner_api}:powermgr_stub",
    "${powermgr_service_path}:powermgrservice",
    "${powermgr_service_path}/native/src/actions:powermgr_actions",
  ]

  external_deps = [ "data_share:datashare_consumer" ]
  external_deps += deps_ex

  if (power_manager_feature_charging_type_setting &&
      defined(global_parts_info) &&
      defined(global_parts_info.powermgr_battery_manager)) {
    defines += [ "POWER_MANAGER_ENABLE_CHARGING_TYPE_SETTING" ]
    external_deps += [ "battery_manager:batterysrv_client" ]
  }

  if (power_manager_feature_disable_auto_displayoff) {
    defines += [ "POWER_MANAGER_DISABLE_AUTO_DISPLAYOFF" ]
  }
}

##############################settinghelper##########################################
ohos_systemtest("test_power_setting_helper") {
  module_out_path = module_output_path

  sanitize = {
    cfi = true
    cfi_cross_dso = true
    debug = false
    blocklist = "../cfi_blocklist.txt"
  }

  sources = [ "src/power_setting_helper_test.cpp" ]

  configs = [
    "${powermgr_utils_path}:utils_config",
    ":module_private_config",
    "${powermgr_utils_path}:coverage_flags",
  ]

  include_dirs = [ "${powermgr_service_path}/native/src/setting/" ]

  deps = [
    "${powermgr_inner_api}:powermgr_client",
    "${powermgr_inner_api}:powermgr_stub",
    "${powermgr_service_path}:powermgrservice",
    "${powermgr_service_path}/native/src/actions:powermgr_actions",
  ]

  external_deps = [ "data_share:datashare_consumer" ]
  external_deps += deps_ex

  if (power_manager_feature_charging_type_setting &&
      defined(global_parts_info) &&
      defined(global_parts_info.powermgr_battery_manager)) {
    defines += [ "POWER_MANAGER_ENABLE_CHARGING_TYPE_SETTING" ]
    external_deps += [ "battery_manager:batterysrv_client" ]
  }

  if (power_manager_feature_disable_auto_displayoff) {
    defines += [ "POWER_MANAGER_DISABLE_AUTO_DISPLAYOFF" ]
  }
}

group("systemtest_powermgr") {
  testonly = true
  deps = [
    ":test_power_level_event_system_test_off",
    ":test_power_level_event_system_test_on",
    ":test_power_mgr_mock_system",
    ":test_power_mgr_powersavemode",
    ":test_power_mgr_powersetdevicemode",
    ":test_power_mgr_shutdown_fast",
    ":test_power_mgr_system",
    ":test_power_st_mgr_mock",
    ":test_power_st_mgr_mock_suspend",
    ":test_power_setting_help",
    ":test_power_setting_helper",
  ]
}