This source file includes following definitions.
- NowTicks
- network_time_notifier_
- TearDown
- Now
- TicksNow
- AddToTicksNow
- StartTracker
- StopTracker
- UpdateNetworkTime
- ValidateExpectedTime
- network_time_tracker
- TEST_F
- TEST_F
- TEST_F
- TEST_F
#include "chrome/browser/network_time/network_time_tracker.h"
#include <math.h>
#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/compiler_specific.h"
#include "base/message_loop/message_loop.h"
#include "content/public/test/test_browser_thread.h"
#include "net/base/network_time_notifier.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace {
const int64 kLatency1 = 50;
const int64 kLatency2 = 500;
const int64 kResolution1 = 17;
const int64 kResolution2 = 177;
const int64 kPseudoSleepTime1 = 500000001;
const int64 kPseudoSleepTime2 = 1888;
class TestTickClock : public base::TickClock {
public:
explicit TestTickClock(base::TimeTicks* ticks_now) : ticks_now_(ticks_now) {}
virtual ~TestTickClock() {}
virtual base::TimeTicks NowTicks() OVERRIDE {
return *ticks_now_;
}
private:
base::TimeTicks* ticks_now_;
};
}
class NetworkTimeTrackerTest : public testing::Test {
public:
NetworkTimeTrackerTest()
: ui_thread(content::BrowserThread::UI, &message_loop_),
io_thread(content::BrowserThread::IO, &message_loop_),
now_(base::Time::NowFromSystemTime()),
tick_clock_(new TestTickClock(&ticks_now_)),
network_time_notifier_(
new net::NetworkTimeNotifier(
tick_clock_.PassAs<base::TickClock>())) {}
virtual ~NetworkTimeTrackerTest() {}
virtual void TearDown() OVERRIDE {
message_loop_.RunUntilIdle();
}
base::Time Now() const {
return now_ + (ticks_now_ - base::TimeTicks());
}
base::TimeTicks TicksNow() const {
return ticks_now_;
}
void AddToTicksNow(int64 ms) {
ticks_now_ += base::TimeDelta::FromMilliseconds(ms);
}
void StartTracker() {
network_time_tracker_.reset(new NetworkTimeTracker());
network_time_notifier_->AddObserver(
network_time_tracker_->BuildObserverCallback());
message_loop_.RunUntilIdle();
}
void StopTracker() {
network_time_tracker_.reset();
}
void UpdateNetworkTime(const base::Time& network_time,
const base::TimeDelta& resolution,
const base::TimeDelta& latency,
const base::TimeTicks& post_time) {
message_loop_.PostTask(
FROM_HERE,
base::Bind(&net::NetworkTimeNotifier::UpdateNetworkTime,
base::Unretained(network_time_notifier_.get()),
network_time,
resolution,
latency,
post_time));
message_loop_.RunUntilIdle();
}
testing::AssertionResult ValidateExpectedTime() const {
base::Time network_time;
base::TimeDelta uncertainty;
if (!network_time_tracker_->GetNetworkTime(TicksNow(),
&network_time,
&uncertainty))
return testing::AssertionFailure() << "Failed to get network time.";
if (fabs(static_cast<double>(Now().ToInternalValue() -
network_time.ToInternalValue())) >
static_cast<double>(uncertainty.ToInternalValue())) {
return testing::AssertionFailure()
<< "Expected network time not within uncertainty.";
}
return testing::AssertionSuccess();
}
NetworkTimeTracker* network_time_tracker() {
return network_time_tracker_.get();
}
private:
base::MessageLoop message_loop_;
content::TestBrowserThread ui_thread;
content::TestBrowserThread io_thread;
base::Time now_;
base::TimeTicks ticks_now_;
scoped_ptr<TestTickClock> tick_clock_;
scoped_ptr<net::NetworkTimeNotifier> network_time_notifier_;
scoped_ptr<NetworkTimeTracker> network_time_tracker_;
};
TEST_F(NetworkTimeTrackerTest, Uninitialized) {
base::Time network_time;
base::TimeDelta uncertainty;
StartTracker();
EXPECT_FALSE(network_time_tracker()->GetNetworkTime(base::TimeTicks(),
&network_time,
&uncertainty));
}
TEST_F(NetworkTimeTrackerTest, NetworkTimeUpdates) {
StartTracker();
UpdateNetworkTime(
Now(),
base::TimeDelta::FromMilliseconds(kResolution1),
base::TimeDelta::FromMilliseconds(kLatency1),
TicksNow());
EXPECT_TRUE(ValidateExpectedTime());
AddToTicksNow(kPseudoSleepTime1);
EXPECT_TRUE(ValidateExpectedTime());
UpdateNetworkTime(
Now(),
base::TimeDelta::FromMilliseconds(kResolution2),
base::TimeDelta::FromMilliseconds(kLatency2),
TicksNow());
AddToTicksNow(kPseudoSleepTime2);
EXPECT_TRUE(ValidateExpectedTime());
base::Time old_now = Now();
base::TimeTicks old_ticks = TicksNow();
AddToTicksNow(kPseudoSleepTime2);
UpdateNetworkTime(
old_now,
base::TimeDelta::FromMilliseconds(kResolution2),
base::TimeDelta::FromMilliseconds(kLatency2),
old_ticks);
EXPECT_TRUE(ValidateExpectedTime());
}
TEST_F(NetworkTimeTrackerTest, UpdateThenStartTracker) {
UpdateNetworkTime(
Now(),
base::TimeDelta::FromMilliseconds(kResolution1),
base::TimeDelta::FromMilliseconds(kLatency1),
TicksNow());
StartTracker();
EXPECT_TRUE(ValidateExpectedTime());
}
TEST_F(NetworkTimeTrackerTest, UpdateAfterTrackerDestroyed) {
StartTracker();
StopTracker();
UpdateNetworkTime(
Now(),
base::TimeDelta::FromMilliseconds(kResolution1),
base::TimeDelta::FromMilliseconds(kLatency1),
TicksNow());
}