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#include <utility>
10
11#include "chrome/browser/extensions/extension_service_test_with_install.h"
12#include "chrome/common/pref_names.h"
13#include "components/sync_preferences/testing_pref_service_syncable.h"
14#include "extensions/browser/api_test_utils.h"
15#include "extensions/common/extension_builder.h"
16#include "testing/gtest/include/gtest/gtest.h"
17
18namespace extensions {
19
20class EnterpriseHardwarePlatformAPITest
21 : public ExtensionServiceTestWithInstall {
22 public:
23 EnterpriseHardwarePlatformAPITest() = default;
24 ~EnterpriseHardwarePlatformAPITest() override = default;
25
26 protected:
27 EnterpriseHardwarePlatformGetHardwarePlatformInfoFunction* function() {
28 return function_.get();
29 }
30
31 private:
32 void SetUp() override {
33 ExtensionServiceTestWithInstall::SetUp();
34 InitializeExtensionService(ExtensionServiceInitParams());
35
36 extension_ = ExtensionBuilder("Test").Build();
37 function_ = new EnterpriseHardwarePlatformGetHardwarePlatformInfoFunction();
38 function_->set_extension(extension_.get());
39 function_->set_has_callback(true);
40 }
41
42 void TearDown() override {
43 function_.reset();
44 extension_.reset();
45 ExtensionServiceTestWithInstall::TearDown();
46 }
47
48 scoped_refptr<const Extension> extension_;
49 scoped_refptr<EnterpriseHardwarePlatformGetHardwarePlatformInfoFunction>
50 function_;
51
52 DISALLOW_COPY_AND_ASSIGN(EnterpriseHardwarePlatformAPITest);
53};
54
55TEST_F(EnterpriseHardwarePlatformAPITest, GetHardwarePlatformInfoAllowed) {
56 testing_pref_service()->SetManagedPref(
57 prefs::kEnterpriseHardwarePlatformAPIEnabled,
58 std::make_unique<base::Value>(true));
59
60 std::unique_ptr<base::Value> result =
61 api_test_utils::RunFunctionAndReturnSingleResult(function(), "[]",
62 browser_context());
63 ASSERT_TRUE(result);
64 ASSERT_TRUE(result->is_dict());
65 ASSERT_EQ(result->DictSize(), 2u);
66
67 const base::Value* val =
68 result->FindKeyOfType("manufacturer", base::Value::Type::STRING);
69 ASSERT_TRUE(val);
70 const std::string& manufacturer = val->GetString();
71
72 val = result->FindKeyOfType("model", base::Value::Type::STRING);
73 ASSERT_TRUE(val);
74 const std::string& model = val->GetString();
75
76 EXPECT_FALSE(manufacturer.empty());
77 EXPECT_FALSE(model.empty());
78}
79
80TEST_F(EnterpriseHardwarePlatformAPITest,
81 GetHardwarePlatformInfoNotAllowedExplicit) {
82 testing_pref_service()->SetDefaultPrefValue(
83 prefs::kEnterpriseHardwarePlatformAPIEnabled, base::Value(false));
84 std::string error = api_test_utils::RunFunctionAndReturnError(
85 function(), "[]", browser_context());
86 EXPECT_FALSE(error.empty());
87}
88
89TEST_F(EnterpriseHardwarePlatformAPITest,
90 GetHardwarePlatformInfoNotAllowedImplicit) {
91 std::string error = api_test_utils::RunFunctionAndReturnError(
92 function(), "[]", browser_context());
93 EXPECT_FALSE(error.empty());
94}
95
96} // namespace extensions
97