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

ash / public / cpp / power_utils_unittest.cc [blame]

// Copyright 2018 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/public/cpp/power_utils.h"

#include "base/time/time.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace ash {

namespace power_utils {

using PowerUtilsTest = testing::Test;

TEST_F(PowerUtilsTest, SplitTimeIntoHoursAndMinutes) {
  int hours = 0, minutes = 0;
  SplitTimeIntoHoursAndMinutes(base::Seconds(0), &hours, &minutes);
  EXPECT_EQ(0, hours);
  EXPECT_EQ(0, minutes);

  SplitTimeIntoHoursAndMinutes(base::Seconds(60), &hours, &minutes);
  EXPECT_EQ(0, hours);
  EXPECT_EQ(1, minutes);

  SplitTimeIntoHoursAndMinutes(base::Seconds(3600), &hours, &minutes);
  EXPECT_EQ(1, hours);
  EXPECT_EQ(0, minutes);

  SplitTimeIntoHoursAndMinutes(base::Seconds(3600 + 60), &hours, &minutes);
  EXPECT_EQ(1, hours);
  EXPECT_EQ(1, minutes);

  SplitTimeIntoHoursAndMinutes(base::Seconds(7 * 3600 + 23 * 60), &hours,
                               &minutes);
  EXPECT_EQ(7, hours);
  EXPECT_EQ(23, minutes);

  // Check that minutes are rounded.
  SplitTimeIntoHoursAndMinutes(base::Seconds(2 * 3600 + 3 * 60 + 30), &hours,
                               &minutes);
  EXPECT_EQ(2, hours);
  EXPECT_EQ(4, minutes);

  SplitTimeIntoHoursAndMinutes(base::Seconds(2 * 3600 + 3 * 60 + 29), &hours,
                               &minutes);
  EXPECT_EQ(2, hours);
  EXPECT_EQ(3, minutes);

  // Check that times close to hour boundaries aren't incorrectly rounded such
  // that they display 60 minutes: http://crbug.com/368261
  SplitTimeIntoHoursAndMinutes(base::Seconds(3599.9), &hours, &minutes);
  EXPECT_EQ(1, hours);
  EXPECT_EQ(0, minutes);

  SplitTimeIntoHoursAndMinutes(base::Seconds(3600.1), &hours, &minutes);
  EXPECT_EQ(1, hours);
  EXPECT_EQ(0, minutes);
}

TEST_F(PowerUtilsTest, ShouldDisplayBatteryTime) {
  EXPECT_FALSE(ShouldDisplayBatteryTime(base::Seconds(-1)));
  EXPECT_FALSE(ShouldDisplayBatteryTime(base::Seconds(0)));
  EXPECT_FALSE(ShouldDisplayBatteryTime(base::Seconds(59)));
  EXPECT_TRUE(ShouldDisplayBatteryTime(base::Seconds(60)));
  EXPECT_TRUE(ShouldDisplayBatteryTime(base::Seconds(600)));
  EXPECT_TRUE(ShouldDisplayBatteryTime(base::Seconds(3600)));

  // Matches the constant in power_utils.cc.
  base::TimeDelta max_displayed_battery_time = base::Days(1);
  EXPECT_TRUE(ShouldDisplayBatteryTime(max_displayed_battery_time));
  EXPECT_FALSE(
      ShouldDisplayBatteryTime(max_displayed_battery_time + base::Seconds(1)));
}

}  // namespace power_utils

}  // namespace ash