1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65
   66
   67
   68
   69
   70
   71
   72
   73
   74
   75
   76
   77
   78
   79
   80
   81
   82
   83
   84
   85
   86
   87
   88
   89
   90
   91
   92
   93
   94
   95
   96
   97
   98
   99
  100
  101
  102
  103
  104

ash / ambient / util / time_of_day_utils_unittest.cc [blame]

// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ash/ambient/util/time_of_day_utils.h"

#include <tuple>
#include <utility>

#include "ash/ambient/metrics/ambient_metrics.h"
#include "ash/constants/ash_features.h"
#include "ash/public/cpp/personalization_app/time_of_day_test_utils.h"
#include "base/files/file_path.h"
#include "base/strings/string_util.h"
#include "base/strings/stringprintf.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/test/scoped_feature_list.h"
#include "base/test/task_environment.h"
#include "base/test/test_future.h"
#include "chromeos/ash/components/dbus/dlcservice/fake_dlcservice_client.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace ash::personalization_app {
namespace {

constexpr base::FilePath::CharType kTestDlcRootPath[] =
    FILE_PATH_LITERAL("/test/time_of_day");

class TimeOfDayUtilsTest : public testing::Test {
 protected:
  TimeOfDayUtilsTest() {
    feature_list_.InitWithFeatures(personalization_app::GetTimeOfDayFeatures(),
                                   {});
    dlcservice_client_.set_install_root_path(kTestDlcRootPath);
  }

  base::test::TaskEnvironment task_environment_;
  FakeDlcserviceClient dlcservice_client_;
  base::test::ScopedFeatureList feature_list_;
};

class TimeOfDayUtilsMetricsTest
    : public TimeOfDayUtilsTest,
      public testing::WithParamInterface<
          std::tuple<const char*, std::pair<const char*, DlcError>>> {};

TEST_F(TimeOfDayUtilsTest, GetAmbientVideoHtmlPathDlcEnabled) {
  base::test::TestFuture<base::FilePath> future;
  GetAmbientVideoHtmlPath(ambient::kAmbientVideoDlcForegroundLabel,
                          future.GetCallback());
  EXPECT_EQ(
      future.Get(),
      base::FilePath(kTestDlcRootPath).Append(kTimeOfDayVideoHtmlSubPath));
}

TEST_F(TimeOfDayUtilsTest, GetAmbientVideoHtmlPathDlcInstallError) {
  dlcservice_client_.set_install_error(dlcservice::kErrorInternal);
  base::test::TestFuture<base::FilePath> future;
  GetAmbientVideoHtmlPath(ambient::kAmbientVideoDlcForegroundLabel,
                          future.GetCallback());
  EXPECT_TRUE(future.Get().empty());
}

TEST_P(TimeOfDayUtilsMetricsTest, GetAmbientVideoHtmlPathDlcMetrics) {
  dlcservice_client_.set_install_error(std::get<1>(GetParam()).first);
  base::test::TestFuture<base::FilePath> future;
  base::HistogramTester histogram_tester;
  GetAmbientVideoHtmlPath(std::get<0>(GetParam()), future.GetCallback());
  ASSERT_TRUE(future.Wait());
  histogram_tester.ExpectUniqueSample(
      base::StringPrintf("Ash.AmbientMode.VideoDlcInstall.%s.Error",
                         std::get<0>(GetParam())),
      static_cast<int>(std::get<1>(GetParam()).second),
      /*expected_bucket_count=*/1);
}

INSTANTIATE_TEST_SUITE_P(
    AllDlcErrorsAndLabels,
    TimeOfDayUtilsMetricsTest,
    testing::Combine(
        testing::Values(ambient::kAmbientVideoDlcForegroundLabel,
                        ambient::kAmbientVideoDlcBackgroundLabel),
        testing::Values(
            std::make_pair(dlcservice::kErrorNone, DlcError::kNone),
            std::make_pair(dlcservice::kErrorInternal, DlcError::kInternal),
            std::make_pair(dlcservice::kErrorBusy, DlcError::kBusy),
            std::make_pair(dlcservice::kErrorNeedReboot, DlcError::kNeedReboot),
            std::make_pair(dlcservice::kErrorInvalidDlc, DlcError::kInvalidDlc),
            std::make_pair(dlcservice::kErrorAllocation, DlcError::kAllocation),
            std::make_pair(dlcservice::kErrorNoImageFound,
                           DlcError::kNoImageFound),
            std::make_pair("InvalidDlcErrorCode", DlcError::kUnknown))),
    [](const testing::TestParamInfo<TimeOfDayUtilsMetricsTest::ParamType>&
           info) {
      // gtest does not allow periods in the test case name.
      std::string dlc_error_code;
      base::RemoveChars(std::get<1>(info.param).first, ".", &dlc_error_code);
      return base::StringPrintf("%s_%s", std::get<0>(info.param),
                                dlc_error_code.c_str());
    });

}  // namespace
}  // namespace ash::personalization_app