Skip to content

Commit

Permalink
Adding tests for IBindable and IDictionary
Browse files Browse the repository at this point in the history
  • Loading branch information
ujjwalchadha committed Oct 28, 2020
1 parent 892817e commit a36c61e
Show file tree
Hide file tree
Showing 5 changed files with 207 additions and 0 deletions.
92 changes: 92 additions & 0 deletions TestComponentCSharp/ManualProjectionTestClasses.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
#include "pch.h"
#include "ManualProjectionTestClasses.h"
#include "IBindableIteratorTest.g.cpp"
#include "IDictionaryTest.g.cpp"

namespace winrt
{
template <typename T, typename Allocator = std::allocator<T>>
Windows::Foundation::Collections::IVectorView<T> single_threaded_vector_view(std::vector<T, Allocator>&& values = {})
{
return make<impl::input_vector_view<T, std::vector<T, Allocator>>>(std::move(values));
}

template <typename K, typename V, typename Allocator = std::allocator<K>>
Windows::Foundation::Collections::IMapView<K, V> single_threaded_map_view(std::map<K, V, Allocator>&& values = {})
{
return make<impl::input_map_view<K, V, std::map<K, V, Allocator>>>(std::move(values));
}
}

namespace winrt::TestComponentCSharp::implementation
{
IBindableIteratorTest::IBindableIteratorTest()
{

}

bool IBindableIteratorTest::MoveNext()
{
return true;
}

Windows::Foundation::IInspectable IBindableIteratorTest::Current()
{
return Windows::Foundation::PropertyValue::CreateInt32(27861);
}

bool IBindableIteratorTest::HasCurrent()
{
return true;
}
IDictionaryTest::IDictionaryTest()
{
}
hstring IDictionaryTest::Lookup(hstring key)
{
return _map.Lookup(key);
}
bool IDictionaryTest::HasKey(hstring key)
{
return _map.HasKey(key);
}
Windows::Foundation::Collections::IMapView<hstring, hstring> IDictionaryTest::GetView()
{
return _map.GetView();
}
bool IDictionaryTest::Insert(hstring key, hstring value)
{
return _map.Insert(key, value);
}
void IDictionaryTest::Remove(hstring key)
{
_map.Remove(key);
}
void IDictionaryTest::Clear()
{
_map.Clear();
}
int IDictionaryTest::Size()
{
return _map.Size();
}

Windows::Foundation::Collections::IIterator<Windows::Foundation::Collections::IKeyValuePair<hstring, hstring>> IDictionaryTest::First()
{
return _map.First();
}

bool IDictionaryTest::Consume(Windows::Foundation::Collections::IMap<hstring, hstring> map)
{
map.Clear();

map.Insert(L"key.cpp", L"value.cpp");
if (map.Size() != 1)
{
return false;
}

return true;
}

}
53 changes: 53 additions & 0 deletions TestComponentCSharp/ManualProjectionTestClasses.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#pragma once
#include "IBindableIteratorTest.g.h"
#include "IDictionaryTest.g.h"

namespace winrt::TestComponentCSharp::implementation
{
struct IBindableIteratorTest : IBindableIteratorTestT<IBindableIteratorTest>
{
IBindableIteratorTest();
bool MoveNext();
Windows::Foundation::IInspectable Current();
bool HasCurrent();
};

struct IDictionaryTest : IDictionaryTestT<IDictionaryTest>
{
private:
Windows::Foundation::Collections::IMap<hstring, hstring> _map{
winrt::single_threaded_map<hstring, hstring>()
};
public:
IDictionaryTest();
hstring Lookup(hstring key);
bool HasKey(hstring key);
Windows::Foundation::Collections::IMapView<hstring, hstring> GetView();
bool Insert(hstring key, hstring value);
void Remove(hstring key);
void Clear();
int Size();
Windows::Foundation::Collections::IIterator<Windows::Foundation::Collections::IKeyValuePair<hstring, hstring>> First();

static bool Consume(Windows::Foundation::Collections::IMap<hstring, hstring> map);
};
}

namespace winrt::TestComponentCSharp::factory_implementation
{
struct IBindableIteratorTest : IBindableIteratorTestT<IBindableIteratorTest, implementation::IBindableIteratorTest, Windows::Foundation::IStringable>
{
hstring ToString()
{
return L"IBindableIteratorTest";
}
};

struct IDictionaryTest : IDictionaryTestT<IDictionaryTest, implementation::IDictionaryTest, Windows::Foundation::IStringable>
{
hstring ToString()
{
return L"IDictionaryTest";
}
};
}
19 changes: 19 additions & 0 deletions TestComponentCSharp/TestComponentCSharp.idl
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,25 @@ namespace TestComponentCSharp
void Observe(Microsoft.UI.Xaml.Interop.IBindableObservableVector vector);
}

//[default_interface]
//runtimeclass IBindableVectorTest : Microsoft.UI.Xaml.Interop.IBindableVector
//{
// //IBindableVectorTest();
//}

[default_interface]
runtimeclass IBindableIteratorTest : Microsoft.UI.Xaml.Interop.IBindableIterator
{
IBindableIteratorTest();
}

[default_interface]
runtimeclass IDictionaryTest : Windows.Foundation.Collections.IMap<String, String>
{
IDictionaryTest();
static Boolean Consume(Windows.Foundation.Collections.IMap<String, String> map);
}

// IInspectable-based interop interface
[uuid(39E050C3-4E74-441A-8DC0-B81104DF949C)]
interface IUserConsentVerifierInterop
Expand Down
1 change: 1 addition & 0 deletions TestComponentCSharp/TestComponentCSharp.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@
</ClCompile>
<ClCompile Include="NonAgileClass.cpp" />
<ClCompile Include="ComImports.cpp" />
<ClCompile Include="ManualProjectionTestClasses.cpp" />
</ItemGroup>
<ItemGroup>
<Midl Include="TestComponentCSharp.idl" />
Expand Down
42 changes: 42 additions & 0 deletions UnitTest/TestComponentCSharp_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2152,5 +2152,47 @@ public void TestNonProjectedRuntimeClass()
CryptographicKey cryptoKey = mac.CreateKey(keyMaterial);
Assert.NotNull(cryptoKey);
}

[Fact]
public void TestIBindableIterator()
{
IBindableIteratorTest bindableIterator = new IBindableIteratorTest();
Assert.True(bindableIterator.MoveNext());
Assert.True(bindableIterator.HasCurrent);
Assert.Equal(27861, bindableIterator.Current);
}

[Fact]
public void TestIDictionary()
{
IDictionaryTest dictionary = new IDictionaryTest();

dictionary["key"] = "value";
Assert.Equal("value", dictionary["key"]);

dictionary.Add("key2", "value2");
Assert.Equal(2, dictionary.Count);

Assert.True(dictionary.ContainsKey("key2"));

KeyValuePair<string, string> k = new KeyValuePair<string, string>("key2", "value2");
Assert.True(dictionary.Contains(k));

KeyValuePair<string, string>[] pairs = new KeyValuePair<string, string>[2];
dictionary.CopyTo(pairs, 0);
Assert.Equal(2, pairs.Length);

dictionary.Remove("key2");
Assert.ThrowsAny<Exception>(() => dictionary["key2"]);
Assert.False(dictionary.TryGetValue("key2", out var key2Val));

Assert.True(dictionary.TryGetValue("key", out var keyVal));
Assert.Equal("value", keyVal);

dictionary.Clear();
Assert.Empty(dictionary);

Assert.True(IDictionaryTest.Consume(dictionary));
}
}
}

0 comments on commit a36c61e

Please sign in to comment.