Skip to content

Commit

Permalink
Add navigator.hardwareConcurrency support for linux.
Browse files Browse the repository at this point in the history
b/341774149
  • Loading branch information
aee-google committed Aug 13, 2024
1 parent 9545024 commit a2dbcac
Show file tree
Hide file tree
Showing 9 changed files with 115 additions and 0 deletions.
1 change: 1 addition & 0 deletions cobalt/black_box_tests/black_box_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
# 'h5vcc_watchdog_api_test',
'http_cache',
'javascript_profiler',
'navigator_test',
'performance_resource_timing_test',
'persistent_cookie',
'pointer_event_on_fixed_element_test',
Expand Down
29 changes: 29 additions & 0 deletions cobalt/black_box_tests/testdata/navigator_test.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<!DOCTYPE html>
<!--
Copyright 2024 The Cobalt Authors. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<html>
<head>
<script src="black_box_js_test_utils.js"></script>
</head>
<body>
<script>
setupFinished();
assertTrue(typeof(navigator.hardwareConcurrency) === 'number');
assertTrue(navigator.hardwareConcurrency >= 1);
onEndTest();
</script>
</body>
</html>
47 changes: 47 additions & 0 deletions cobalt/black_box_tests/tests/navigator_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# Copyright 2024 The Cobalt Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Test navigator API."""

from cobalt.black_box_tests import black_box_tests
from cobalt.black_box_tests.threaded_web_server import ThreadedWebServer
import logging

PLATFORMS_SUPPORTED = [
'linux-x64x11',
'linux-x64x11-egl',
'linux-x64x11-gcc-6-3',
'linux-x64x11-skia',
'android-arm',
'android-arm64',
'android-arm64-vulkan',
'android-x86',
'raspi-2',
'raspi-2-skia',
'linux-x64x11-clang-crosstool',
]


class NavigatorTest(black_box_tests.BlackBoxTestCase):

def test_navigator(self):
if self.launcher_params.platform not in PLATFORMS_SUPPORTED:
logging.warning('Blackbox tests disabled for platform:%s',
self.launcher_params.platform)
return

with ThreadedWebServer(binding_address=self.GetBindingAddress()) as server:
url = server.GetURL(file_name='testdata/navigator_test.html')
with self.CreateCobaltRunner(url=url) as runner:
runner.WaitForJSTestsSetup()
self.assertTrue(runner.JSTestsSucceeded())
1 change: 1 addition & 0 deletions cobalt/browser/idl_files.gni
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,7 @@ dependency_idl_files = [
"//cobalt/dom/global_event_handlers.idl",
"//cobalt/dom/html_element_cssom_view.idl",
"//cobalt/dom/mouse_event_cssom_view.idl",
"//cobalt/dom/navigator_concurrent_hardware.idl",
"//cobalt/dom/navigator_licenses.idl",
"//cobalt/dom/navigator_plugins.idl",
"//cobalt/dom/navigator_storage_utils.idl",
Expand Down
5 changes: 5 additions & 0 deletions cobalt/dom/navigator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include <vector>

#include "base/optional.h"
#include "base/system/sys_info.h"
#include "base/trace_event/trace_event.h"
#include "cobalt/dom/captions/system_caption_settings.h"
#include "cobalt/dom/dom_settings.h"
Expand Down Expand Up @@ -154,6 +155,10 @@ Navigator::Navigator(script::EnvironmentSettings* settings,
new media_capture::MediaDevices(settings, script_value_factory())),
system_caption_settings_(captions) {}

uint64_t Navigator::hardware_concurrency() const {
return static_cast<uint64_t>(base::SysInfo::NumberOfProcessors());
}

const std::string Navigator::licenses() const {
GeneratedResourceMap resource_map;
DOMEmbeddedResources::GenerateMap(resource_map);
Expand Down
2 changes: 2 additions & 0 deletions cobalt/dom/navigator.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ class Navigator : public web::NavigatorBase {
Navigator(const Navigator&) = delete;
Navigator& operator=(const Navigator&) = delete;

uint64_t hardware_concurrency() const;

// Web API: NavigatorLicenses
const std::string licenses() const;

Expand Down
1 change: 1 addition & 0 deletions cobalt/dom/navigator.idl
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

interface Navigator {};

Navigator implements NavigatorConcurrentHardware;
Navigator implements NavigatorID;
Navigator implements NavigatorLanguage;
Navigator implements NavigatorPlugins;
Expand Down
18 changes: 18 additions & 0 deletions cobalt/dom/navigator_concurrent_hardware.idl
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright 2024 The Cobalt Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

[NoInterfaceObject]
interface NavigatorConcurrentHardware {
readonly attribute unsigned long long hardwareConcurrency;
};
11 changes: 11 additions & 0 deletions cobalt/dom/navigator_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include <string>

#include "base/logging.h"
#include "base/strings/string_number_conversions.h"
#include "cobalt/bindings/testing/utils.h"
#include "cobalt/dom/testing/test_with_javascript.h"
#include "cobalt/web/testing/gtest_workarounds.h"
Expand Down Expand Up @@ -160,5 +161,15 @@ TEST_F(NavigatorTest, NavigatorOnline) {
EXPECT_EQ("true", result);
}

TEST_F(NavigatorTest, NavigatorConcurrentHardware) {
std::string result;
EXPECT_TRUE(EvaluateScript("typeof navigator.hardwareConcurrency", &result));
EXPECT_EQ("number", result);
EXPECT_TRUE(EvaluateScript("navigator.hardwareConcurrency", &result));
int count = -1;
EXPECT_TRUE(base::StringToInt(result, &count));
EXPECT_GE(count, 0);
}

} // namespace dom
} // namespace cobalt

0 comments on commit a2dbcac

Please sign in to comment.