1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "base/environment.h"
6 #include "base/files/file_util.h"
7 #include "base/sys_info.h"
8 #include "base/threading/platform_thread.h"
9 #include "base/time/time.h"
10 #include "testing/gtest/include/gtest/gtest.h"
11 #include "testing/platform_test.h"
13 typedef PlatformTest SysInfoTest
;
16 #if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_ANDROID)
17 TEST_F(SysInfoTest
, MaxSharedMemorySize
) {
18 // We aren't actually testing that it's correct, just that it's sane.
19 EXPECT_GT(base::SysInfo::MaxSharedMemorySize(), 0u);
23 TEST_F(SysInfoTest
, NumProcs
) {
24 // We aren't actually testing that it's correct, just that it's sane.
25 EXPECT_GE(base::SysInfo::NumberOfProcessors(), 1);
28 TEST_F(SysInfoTest
, AmountOfMem
) {
29 // We aren't actually testing that it's correct, just that it's sane.
30 EXPECT_GT(base::SysInfo::AmountOfPhysicalMemory(), 0);
31 EXPECT_GT(base::SysInfo::AmountOfPhysicalMemoryMB(), 0);
32 // The maxmimal amount of virtual memory can be zero which means unlimited.
33 EXPECT_GE(base::SysInfo::AmountOfVirtualMemory(), 0);
36 TEST_F(SysInfoTest
, AmountOfFreeDiskSpace
) {
37 // We aren't actually testing that it's correct, just that it's sane.
39 ASSERT_TRUE(base::GetTempDir(&tmp_path
));
40 EXPECT_GT(base::SysInfo::AmountOfFreeDiskSpace(tmp_path
), 0)
44 TEST_F(SysInfoTest
, HasSeekPenalty
) {
46 ASSERT_TRUE(base::GetTempDir(&tmp_path
));
48 base::SysInfo::HasSeekPenalty(tmp_path
, &unused
);
51 #if defined(OS_WIN) || defined(OS_MACOSX)
52 TEST_F(SysInfoTest
, OperatingSystemVersionNumbers
) {
53 int32 os_major_version
= -1;
54 int32 os_minor_version
= -1;
55 int32 os_bugfix_version
= -1;
56 base::SysInfo::OperatingSystemVersionNumbers(&os_major_version
,
59 EXPECT_GT(os_major_version
, -1);
60 EXPECT_GT(os_minor_version
, -1);
61 EXPECT_GT(os_bugfix_version
, -1);
65 TEST_F(SysInfoTest
, Uptime
) {
66 int64 up_time_1
= base::SysInfo::Uptime();
67 // UpTime() is implemented internally using TimeTicks::Now(), which documents
68 // system resolution as being 1-15ms. Sleep a little longer than that.
69 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(20));
70 int64 up_time_2
= base::SysInfo::Uptime();
71 EXPECT_GT(up_time_1
, 0);
72 EXPECT_GT(up_time_2
, up_time_1
);
75 #if defined(OS_MACOSX) && !defined(OS_IOS)
76 TEST_F(SysInfoTest
, HardwareModelName
) {
77 std::string hardware_model
= base::SysInfo::HardwareModelName();
78 EXPECT_FALSE(hardware_model
.empty());
82 #if defined(OS_CHROMEOS)
84 TEST_F(SysInfoTest
, GoogleChromeOSVersionNumbers
) {
85 int32 os_major_version
= -1;
86 int32 os_minor_version
= -1;
87 int32 os_bugfix_version
= -1;
88 const char kLsbRelease
[] =
90 "CHROMEOS_RELEASE_VERSION=1.2.3.4\n";
91 base::SysInfo::SetChromeOSVersionInfoForTest(kLsbRelease
, base::Time());
92 base::SysInfo::OperatingSystemVersionNumbers(&os_major_version
,
95 EXPECT_EQ(1, os_major_version
);
96 EXPECT_EQ(2, os_minor_version
);
97 EXPECT_EQ(3, os_bugfix_version
);
100 TEST_F(SysInfoTest
, GoogleChromeOSVersionNumbersFirst
) {
101 int32 os_major_version
= -1;
102 int32 os_minor_version
= -1;
103 int32 os_bugfix_version
= -1;
104 const char kLsbRelease
[] =
105 "CHROMEOS_RELEASE_VERSION=1.2.3.4\n"
106 "FOO=1234123.34.5\n";
107 base::SysInfo::SetChromeOSVersionInfoForTest(kLsbRelease
, base::Time());
108 base::SysInfo::OperatingSystemVersionNumbers(&os_major_version
,
111 EXPECT_EQ(1, os_major_version
);
112 EXPECT_EQ(2, os_minor_version
);
113 EXPECT_EQ(3, os_bugfix_version
);
116 TEST_F(SysInfoTest
, GoogleChromeOSNoVersionNumbers
) {
117 int32 os_major_version
= -1;
118 int32 os_minor_version
= -1;
119 int32 os_bugfix_version
= -1;
120 const char kLsbRelease
[] = "FOO=1234123.34.5\n";
121 base::SysInfo::SetChromeOSVersionInfoForTest(kLsbRelease
, base::Time());
122 base::SysInfo::OperatingSystemVersionNumbers(&os_major_version
,
125 EXPECT_EQ(0, os_major_version
);
126 EXPECT_EQ(0, os_minor_version
);
127 EXPECT_EQ(0, os_bugfix_version
);
130 TEST_F(SysInfoTest
, GoogleChromeOSLsbReleaseTime
) {
131 const char kLsbRelease
[] = "CHROMEOS_RELEASE_VERSION=1.2.3.4";
132 // Use a fake time that can be safely displayed as a string.
133 const base::Time
lsb_release_time(base::Time::FromDoubleT(12345.6));
134 base::SysInfo::SetChromeOSVersionInfoForTest(kLsbRelease
, lsb_release_time
);
135 base::Time parsed_lsb_release_time
= base::SysInfo::GetLsbReleaseTime();
136 EXPECT_DOUBLE_EQ(lsb_release_time
.ToDoubleT(),
137 parsed_lsb_release_time
.ToDoubleT());
140 TEST_F(SysInfoTest
, IsRunningOnChromeOS
) {
141 base::SysInfo::SetChromeOSVersionInfoForTest("", base::Time());
142 EXPECT_FALSE(base::SysInfo::IsRunningOnChromeOS());
144 const char kLsbRelease1
[] =
145 "CHROMEOS_RELEASE_NAME=Non Chrome OS\n"
146 "CHROMEOS_RELEASE_VERSION=1.2.3.4\n";
147 base::SysInfo::SetChromeOSVersionInfoForTest(kLsbRelease1
, base::Time());
148 EXPECT_FALSE(base::SysInfo::IsRunningOnChromeOS());
150 const char kLsbRelease2
[] =
151 "CHROMEOS_RELEASE_NAME=Chrome OS\n"
152 "CHROMEOS_RELEASE_VERSION=1.2.3.4\n";
153 base::SysInfo::SetChromeOSVersionInfoForTest(kLsbRelease2
, base::Time());
154 EXPECT_TRUE(base::SysInfo::IsRunningOnChromeOS());
156 const char kLsbRelease3
[] =
157 "CHROMEOS_RELEASE_NAME=Chromium OS\n";
158 base::SysInfo::SetChromeOSVersionInfoForTest(kLsbRelease3
, base::Time());
159 EXPECT_TRUE(base::SysInfo::IsRunningOnChromeOS());
162 #endif // OS_CHROMEOS