[go: nahoru, domu]

[Lacros] Implement Lacros-compatible GetDeviceType

Currently, Lacros is not aware of the type of device ("Chromebook",
"Chromebox" etc.) it is running on. However, this information is needed
for some UI elements.

This CL uses the Lacros init_params to tell Lacros which type of device
it is running on. It implements a GetDeviceType() which can be used by
both Ash and Lacros - this is based on the existing GetDeviceType() in
ash/constants/devicetype.cc.
Current uses of the ash-exclusive GetDeviceType() will be migrated to
use the new one in follow-up CLs.

Bug: 1284585
Change-Id: Iceb0b2e10744657e18a2555383a0fc57d81e65f0
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3477020
Reviewed-by: Denis Kuznetsov <antrim@chromium.org>
Reviewed-by: Hidehiko Abe <hidehiko@chromium.org>
Commit-Queue: Fabian Sommer <fabiansommer@chromium.org>
Cr-Commit-Position: refs/heads/main@{#978212}
diff --git a/chromeos/constants/devicetype_unittest.cc b/chromeos/constants/devicetype_unittest.cc
new file mode 100644
index 0000000..e3b314e
--- /dev/null
+++ b/chromeos/constants/devicetype_unittest.cc
@@ -0,0 +1,96 @@
+// Copyright 2022 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "chromeos/constants/devicetype.h"
+
+#include "base/command_line.h"
+#include "base/test/task_environment.h"
+#include "build/build_config.h"
+#include "build/chromeos_buildflags.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+#if BUILDFLAG(IS_CHROMEOS_LACROS)
+#include "chromeos/crosapi/mojom/crosapi.mojom.h"
+#include "chromeos/lacros/lacros_service.h"
+#include "chromeos/lacros/lacros_test_helper.h"
+#endif
+
+namespace chromeos {
+
+#if BUILDFLAG(IS_CHROMEOS_ASH)
+TEST(DeviceTypeTest, GetDeviceTypeAsh) {
+  auto* command_line = base::CommandLine::ForCurrentProcess();
+  {
+    command_line->InitFromArgv({"", "--form-factor=CHROMEBOOK"});
+    EXPECT_EQ(chromeos::GetDeviceType(), chromeos::DeviceType::kChromebook);
+  }
+  {
+    command_line->InitFromArgv({"", "--form-factor=CHROMEBASE"});
+    EXPECT_EQ(chromeos::GetDeviceType(), chromeos::DeviceType::kChromebase);
+  }
+  {
+    command_line->InitFromArgv({"", "--form-factor=CHROMEBIT"});
+    EXPECT_EQ(chromeos::GetDeviceType(), chromeos::DeviceType::kChromebit);
+  }
+  {
+    command_line->InitFromArgv({"", "--form-factor=CHROMEBOX"});
+    EXPECT_EQ(chromeos::GetDeviceType(), chromeos::DeviceType::kChromebox);
+  }
+  {
+    command_line->InitFromArgv({"", ""});
+    EXPECT_EQ(chromeos::GetDeviceType(), chromeos::DeviceType::kUnknown);
+  }
+}
+#endif
+
+#if BUILDFLAG(IS_CHROMEOS_LACROS)
+TEST(DeviceTypeTest, GetDeviceTypeLacros) {
+  base::test::TaskEnvironment task_environment;
+  chromeos::ScopedDisableCrosapiForTesting disable_crosapi;
+  chromeos::LacrosService lacros_service;
+  {
+    auto params = crosapi::mojom::BrowserInitParams::New();
+    params->device_type =
+        crosapi::mojom::BrowserInitParams::DeviceType::kChromebook;
+    lacros_service.SetInitParamsForTests(std::move(params));
+    EXPECT_EQ(GetDeviceType(), chromeos::DeviceType::kChromebook);
+  }
+  {
+    auto params = crosapi::mojom::BrowserInitParams::New();
+    params->device_type =
+        crosapi::mojom::BrowserInitParams::DeviceType::kChromebase;
+    lacros_service.SetInitParamsForTests(std::move(params));
+    EXPECT_EQ(GetDeviceType(), chromeos::DeviceType::kChromebase);
+  }
+  {
+    auto params = crosapi::mojom::BrowserInitParams::New();
+    params->device_type =
+        crosapi::mojom::BrowserInitParams::DeviceType::kChromebit;
+    lacros_service.SetInitParamsForTests(std::move(params));
+    EXPECT_EQ(GetDeviceType(), chromeos::DeviceType::kChromebit);
+  }
+  {
+    auto params = crosapi::mojom::BrowserInitParams::New();
+    params->device_type =
+        crosapi::mojom::BrowserInitParams::DeviceType::kChromebox;
+    lacros_service.SetInitParamsForTests(std::move(params));
+    EXPECT_EQ(GetDeviceType(), chromeos::DeviceType::kChromebox);
+  }
+  {
+    auto params = crosapi::mojom::BrowserInitParams::New();
+    params->device_type =
+        crosapi::mojom::BrowserInitParams::DeviceType::kUnknown;
+    lacros_service.SetInitParamsForTests(std::move(params));
+    EXPECT_EQ(GetDeviceType(), chromeos::DeviceType::kUnknown);
+  }
+  {
+    // When device_type is not set, GetDeviceType() should return kUnknown.
+    auto params = crosapi::mojom::BrowserInitParams::New();
+    lacros_service.SetInitParamsForTests(std::move(params));
+    EXPECT_EQ(GetDeviceType(), chromeos::DeviceType::kUnknown);
+  }
+}
+#endif
+
+}  // namespace chromeos