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.
4#include "stdafx.h"
5
6#include "winrt_utils.h"
7
8#include "base/logging.h"
9#include "testing/gtest/include/gtest/gtest.h"
10
11namespace {
12
13template <typename Type>
14static HRESULT CreateProperty(Type value, winfoundtn::IPropertyValue** prop) {
15  return E_NOTIMPL;
16}
17
18template <>
19static HRESULT CreateProperty<const wchar_t*>(
20    const wchar_t* value, winfoundtn::IPropertyValue** prop) {
21  mswrw::HString string_value;
22  string_value.Attach(MakeHString(value));
23  return winrt_utils::CreateStringProperty(string_value.Get(), prop);
24}
25
26template <>
27static HRESULT CreateProperty<INT16>(INT16 value,
28                                     winfoundtn::IPropertyValue** prop) {
29  return winrt_utils::CreateInt16Property(value, prop);
30}
31
32template <>
33static HRESULT CreateProperty<INT32>(INT32 value,
34                                     winfoundtn::IPropertyValue** prop) {
35  return winrt_utils::CreateInt32Property(value, prop);
36}
37
38template <>
39static HRESULT CreateProperty<INT64>(INT64 value,
40                                     winfoundtn::IPropertyValue** prop) {
41  return winrt_utils::CreateInt64Property(value, prop);
42}
43
44template <>
45static HRESULT CreateProperty<UINT8>(UINT8 value,
46                                     winfoundtn::IPropertyValue** prop) {
47  return winrt_utils::CreateUInt8Property(value, prop);
48}
49
50template <>
51static HRESULT CreateProperty<UINT16>(UINT16 value,
52                                      winfoundtn::IPropertyValue** prop) {
53  return winrt_utils::CreateUInt16Property(value, prop);
54}
55
56template <>
57static HRESULT CreateProperty<UINT32>(UINT32 value,
58                                      winfoundtn::IPropertyValue** prop) {
59  return winrt_utils::CreateUInt32Property(value, prop);
60}
61
62template <>
63static HRESULT CreateProperty<UINT64>(UINT64 value,
64                                      winfoundtn::IPropertyValue** prop) {
65  return winrt_utils::CreateUInt64Property(value, prop);
66}
67
68template<typename Type>
69void TestCompareProperties(Type value1, Type value2) {
70  mswr::ComPtr<winfoundtn::IPropertyValue> property_1;
71  HRESULT hr = CreateProperty<Type>(value1, property_1.GetAddressOf());
72  ASSERT_TRUE(SUCCEEDED(hr)) << "Can't create Property value 1";
73
74  mswr::ComPtr<winfoundtn::IPropertyValue> other_property_1;
75  hr = CreateProperty<Type>(value1, other_property_1.GetAddressOf());
76  ASSERT_TRUE(SUCCEEDED(hr)) << "Can't create another Property value 1";
77
78  mswr::ComPtr<winfoundtn::IPropertyValue> property_2;
79  hr = CreateProperty<Type>(value2, property_2.GetAddressOf());
80  ASSERT_TRUE(SUCCEEDED(hr)) << "Can't create Property value 2";
81
82  INT32 result = 42;
83  hr = winrt_utils::CompareProperties(
84      property_1.Get(), property_1.Get(), &result);
85  ASSERT_TRUE(SUCCEEDED(hr)) << "Can't compare property_1 to itself";
86  EXPECT_EQ(0, result) << "Bad result value while comparing same property";
87
88  hr = winrt_utils::CompareProperties(
89      property_1.Get(), other_property_1.Get(), &result);
90  ASSERT_TRUE(SUCCEEDED(hr)) << "Can't compare property_1 to other_property_1";
91  EXPECT_EQ(0, result) << "Bad result while comparing equal values";
92
93  hr = winrt_utils::CompareProperties(
94      property_1.Get(), property_2.Get(), &result);
95  ASSERT_TRUE(SUCCEEDED(hr)) << "Can't compare property_1 to property_2";
96  EXPECT_EQ(-1, result) << "Bad result while comparing values for less than";
97
98  hr = winrt_utils::CompareProperties(
99      property_2.Get(), property_1.Get(), &result);
100  ASSERT_TRUE(SUCCEEDED(hr)) << "Can't compare property_1 to property_2";
101  EXPECT_EQ(1, result) << "Bad result value while comparing for greater than";
102}
103
104TEST(PropertyValueCompareTest, CompareProperties) {
105  TestCompareProperties<INT16>(42, 43);
106  TestCompareProperties<INT32>(42, 43);
107  TestCompareProperties<INT64>(42, 43);
108  TestCompareProperties<UINT8>(42, 43);
109  TestCompareProperties<UINT16>(42, 43);
110  TestCompareProperties<UINT32>(42, 43);
111  TestCompareProperties<UINT64>(42, 43);
112  TestCompareProperties<const wchar_t*>(L"abc", L"bcd");
113}
114
115}  // namespace
116