1 //
2 // Copyright (C) 2014 The Android Open Source Project
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16
17 #include "update_engine/update_manager/real_time_provider.h"
18
19 #include <memory>
20
21 #include <base/logging.h>
22 #include <base/time/time.h>
23 #include <gtest/gtest.h>
24
25 #include "update_engine/common/fake_clock.h"
26 #include "update_engine/update_manager/umtest_utils.h"
27
28 using base::Time;
29 using chromeos_update_engine::FakeClock;
30 using std::unique_ptr;
31
32 namespace chromeos_update_manager {
33
34 class UmRealTimeProviderTest : public ::testing::Test {
35 protected:
SetUp()36 void SetUp() override {
37 // The provider initializes correctly.
38 provider_.reset(new RealTimeProvider(&fake_clock_));
39 ASSERT_NE(nullptr, provider_.get());
40 ASSERT_TRUE(provider_->Init());
41 }
42
43 // Generates a fixed timestamp for use in faking the current time.
CurrTime()44 Time CurrTime() {
45 Time::Exploded now_exp;
46 now_exp.year = 2014;
47 now_exp.month = 3;
48 now_exp.day_of_week = 2;
49 now_exp.day_of_month = 18;
50 now_exp.hour = 8;
51 now_exp.minute = 5;
52 now_exp.second = 33;
53 now_exp.millisecond = 675;
54 return Time::FromLocalExploded(now_exp);
55 }
56
57 FakeClock fake_clock_;
58 unique_ptr<RealTimeProvider> provider_;
59 };
60
TEST_F(UmRealTimeProviderTest,CurrDateValid)61 TEST_F(UmRealTimeProviderTest, CurrDateValid) {
62 const Time now = CurrTime();
63 Time::Exploded exploded;
64 now.LocalExplode(&exploded);
65 exploded.hour = 0;
66 exploded.minute = 0;
67 exploded.second = 0;
68 exploded.millisecond = 0;
69 const Time expected = Time::FromLocalExploded(exploded);
70
71 fake_clock_.SetWallclockTime(now);
72 UmTestUtils::ExpectVariableHasValue(expected, provider_->var_curr_date());
73 }
74
TEST_F(UmRealTimeProviderTest,CurrHourValid)75 TEST_F(UmRealTimeProviderTest, CurrHourValid) {
76 const Time now = CurrTime();
77 Time::Exploded expected;
78 now.LocalExplode(&expected);
79 fake_clock_.SetWallclockTime(now);
80 UmTestUtils::ExpectVariableHasValue(expected.hour,
81 provider_->var_curr_hour());
82 }
83
84 } // namespace chromeos_update_manager
85