|
| 1 | +// Copyright 2018 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. |
| 4 | + |
| 5 | +#include "chrome/browser/extensions/api/enterprise_hardware_platform/enterprise_hardware_platform_api.h" |
| 6 | + |
| 7 | +#include <memory> |
| 8 | +#include <string> |
| 9 | + |
| 10 | +#include "base/json/json_writer.h" |
| 11 | +#include "chrome/browser/extensions/extension_api_unittest.h" |
| 12 | +#include "chrome/browser/extensions/extension_function_test_utils.h" |
| 13 | +#include "chrome/browser/extensions/extension_service.h" |
| 14 | +#include "chrome/browser/extensions/extension_service_test_with_install.h" |
| 15 | +#include "components/crx_file/id_util.h" |
| 16 | +#include "extensions/common/extension_builder.h" |
| 17 | +#include "testing/gtest/include/gtest/gtest.h" |
| 18 | + |
| 19 | +namespace extensions { |
| 20 | + |
| 21 | +class EnterpriseHardwarePlatformAPITest |
| 22 | + : public ExtensionServiceTestWithInstall { |
| 23 | + public: |
| 24 | + EnterpriseHardwarePlatformAPITest() = default; |
| 25 | + ~EnterpriseHardwarePlatformAPITest() override = default; |
| 26 | + Browser* browser() { return browser_.get(); } |
| 27 | + |
| 28 | + private: |
| 29 | + void SetUp() override { |
| 30 | + ExtensionServiceTestWithInstall::SetUp(); |
| 31 | + InitializeEmptyExtensionService(); |
| 32 | + browser_window_ = std::make_unique<TestBrowserWindow>(); |
| 33 | + Browser::CreateParams params(profile(), true); |
| 34 | + params.type = Browser::TYPE_TABBED; |
| 35 | + params.window = browser_window_.get(); |
| 36 | + browser_ = std::make_unique<Browser>(params); |
| 37 | + } |
| 38 | + |
| 39 | + void TearDown() override { |
| 40 | + browser_.reset(); |
| 41 | + browser_window_.reset(); |
| 42 | + ExtensionServiceTestWithInstall::TearDown(); |
| 43 | + } |
| 44 | + |
| 45 | + std::unique_ptr<TestBrowserWindow> browser_window_; |
| 46 | + std::unique_ptr<Browser> browser_; |
| 47 | + |
| 48 | + DISALLOW_COPY_AND_ASSIGN(EnterpriseHardwarePlatformAPITest); |
| 49 | +}; |
| 50 | + |
| 51 | +TEST_F(EnterpriseHardwarePlatformAPITest, GetHardwarePlatformInfo) { |
| 52 | + scoped_refptr<const Extension> extension = ExtensionBuilder("Test").Build(); |
| 53 | + scoped_refptr<EnterpriseHardwarePlatformGetHardwarePlatformInfoFunction> |
| 54 | + function = |
| 55 | + new EnterpriseHardwarePlatformGetHardwarePlatformInfoFunction(); |
| 56 | + function->set_extension(extension.get()); |
| 57 | + function->set_has_callback(true); |
| 58 | + |
| 59 | + std::string args; |
| 60 | + base::JSONWriter::Write(base::ListValue(), &args); |
| 61 | + |
| 62 | + std::unique_ptr<base::Value> result( |
| 63 | + extension_function_test_utils::RunFunctionAndReturnSingleResult( |
| 64 | + function.get(), args, browser())); |
| 65 | + base::RunLoop().RunUntilIdle(); |
| 66 | + |
| 67 | + ASSERT_TRUE(result); |
| 68 | + ASSERT_TRUE(result->is_dict()); |
| 69 | + ASSERT_EQ(result->DictSize(), 2u); |
| 70 | + |
| 71 | + const base::Value* val = |
| 72 | + result->FindKeyOfType("manufacturer", base::Value::Type::STRING); |
| 73 | + ASSERT_TRUE(val); |
| 74 | + const std::string& manufacturer = val->GetString(); |
| 75 | + |
| 76 | + val = result->FindKeyOfType("model", base::Value::Type::STRING); |
| 77 | + ASSERT_TRUE(val); |
| 78 | + const std::string& model = val->GetString(); |
| 79 | + |
| 80 | + EXPECT_FALSE(manufacturer.empty()); |
| 81 | + EXPECT_FALSE(model.empty()); |
| 82 | +} |
| 83 | + |
| 84 | +} // namespace extensions |
0 commit comments