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/cros/fake_system_state.h"
26 #include "update_engine/update_manager/umtest_utils.h"
27 
28 using base::Time;
29 using chromeos_update_engine::FakeSystemState;
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     FakeSystemState::CreateInstance();
38     // The provider initializes correctly.
39     provider_.reset(new RealTimeProvider());
40     ASSERT_NE(nullptr, provider_.get());
41     ASSERT_TRUE(provider_->Init());
42   }
43 
44   // Generates a fixed timestamp for use in faking the current time.
CurrTime()45   Time CurrTime() {
46     Time::Exploded now_exp;
47     now_exp.year = 2014;
48     now_exp.month = 3;
49     now_exp.day_of_week = 2;
50     now_exp.day_of_month = 18;
51     now_exp.hour = 8;
52     now_exp.minute = 5;
53     now_exp.second = 33;
54     now_exp.millisecond = 675;
55     Time time;
56     ignore_result(Time::FromLocalExploded(now_exp, &time));
57     return time;
58   }
59 
60   unique_ptr<RealTimeProvider> provider_;
61 };
62 
TEST_F(UmRealTimeProviderTest,CurrDateValid)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   FakeSystemState::Get()->fake_clock()->SetWallclockTime(now);
75   UmTestUtils::ExpectVariableHasValue(expected, provider_->var_curr_date());
76 }
77 
TEST_F(UmRealTimeProviderTest,CurrHourValid)78 TEST_F(UmRealTimeProviderTest, CurrHourValid) {
79   const Time now = CurrTime();
80   Time::Exploded expected;
81   now.LocalExplode(&expected);
82   FakeSystemState::Get()->fake_clock()->SetWallclockTime(now);
83   UmTestUtils::ExpectVariableHasValue(expected.hour,
84                                       provider_->var_curr_hour());
85 }
86 
TEST_F(UmRealTimeProviderTest,CurrMinuteValid)87 TEST_F(UmRealTimeProviderTest, CurrMinuteValid) {
88   const Time now = CurrTime();
89   Time::Exploded expected;
90   now.LocalExplode(&expected);
91   FakeSystemState::Get()->fake_clock()->SetWallclockTime(now);
92   UmTestUtils::ExpectVariableHasValue(expected.minute,
93                                       provider_->var_curr_minute());
94 }
95 
96 }  // namespace chromeos_update_manager
97