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:
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.
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     Time time;
55     ignore_result(Time::FromLocalExploded(now_exp, &time));
56     return time;
57   }
58 
59   FakeClock fake_clock_;
60   unique_ptr<RealTimeProvider> provider_;
61 };
62 
63 TEST_F(UmRealTimeProviderTest, CurrDateValid) {
64   const Time now = CurrTime();
65   Time::Exploded exploded;
66   now.LocalExplode(&exploded);
67   exploded.hour = 0;
68   exploded.minute = 0;
69   exploded.second = 0;
70   exploded.millisecond = 0;
71   Time expected;
72   ignore_result(Time::FromLocalExploded(exploded, &expected));
73 
74   fake_clock_.SetWallclockTime(now);
75   UmTestUtils::ExpectVariableHasValue(expected, provider_->var_curr_date());
76 }
77 
78 TEST_F(UmRealTimeProviderTest, CurrHourValid) {
79   const Time now = CurrTime();
80   Time::Exploded expected;
81   now.LocalExplode(&expected);
82   fake_clock_.SetWallclockTime(now);
83   UmTestUtils::ExpectVariableHasValue(expected.hour,
84                                       provider_->var_curr_hour());
85 }
86 
87 TEST_F(UmRealTimeProviderTest, CurrMinuteValid) {
88   const Time now = CurrTime();
89   Time::Exploded expected;
90   now.LocalExplode(&expected);
91   fake_clock_.SetWallclockTime(now);
92   UmTestUtils::ExpectVariableHasValue(expected.minute,
93                                       provider_->var_curr_minute());
94 }
95 
96 }  // namespace chromeos_update_manager
97