Skip to content

Commit 5ede3ad

Browse files
committed
Restructured the project, added unit tests, etc.
1 parent 679daed commit 5ede3ad

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+884
-196
lines changed

.gitignore

+3-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ TestResults
1212
*.suo
1313
*.user
1414
*.sln.docstates
15+
*.userprefs
1516

1617
# Build results
1718
[Dd]ebug/
@@ -110,4 +111,5 @@ UpgradeLog*.XML
110111
ComponentStore/
111112
lib/
112113

113-
*.DS_Store
114+
*.DS_Store
115+
*.droidres.db
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
using System;
2+
using AltBeaconOrg.BoundBeacon;
3+
using AltBeaconOrg.BoundBeacon.Logging;
4+
using NUnit.Framework;
5+
6+
namespace AndroidAltBeaconLibrary.UnitTests
7+
{
8+
public class AltBeaconParserTest : TestBase
9+
{
10+
[Test]
11+
public void TestRecognizeBeacon()
12+
{
13+
var beaconManager = BeaconManager.GetInstanceForApplication(Android.App.Application.Context);
14+
var bytes = HexStringToByteArray("02011a1bff1801beac2f234454cf6d4a0fadf2f4911ba9ffa600010002c50900");
15+
AltBeaconParser parser = new AltBeaconParser();
16+
Beacon beacon = parser.FromScanData(bytes, -55, null);
17+
Assert.AreEqual(1, beacon.DataFields.Count, "Beacon should have one data field");
18+
Assert.AreEqual(9, ((AltBeacon) beacon).MfgReserved, "manData should be parsed");
19+
}
20+
21+
[Test]
22+
public void TestDetectsDaveMHardwareBeacon()
23+
{
24+
var bytes = HexStringToByteArray("02011a1bff1801beac2f234454cf6d4a0fadf2f4911ba9ffa600050003be020e09526164426561636f6e20555342020a0300000000000000000000000000");
25+
var parser = new AltBeaconParser();
26+
var beacon = parser.FromScanData(bytes, -55, null);
27+
Assert.NotNull(beacon, "Beacon should be not null if parsed successfully");
28+
}
29+
30+
[Test]
31+
public void TestDetectsAlternateBeconType()
32+
{
33+
var bytes = HexStringToByteArray("02011a1bff1801aabb2f234454cf6d4a0fadf2f4911ba9ffa600010002c50900");
34+
var parser = new AltBeaconParser();
35+
parser.SetMatchingBeaconTypeCode(new Java.Lang.Long(0xaabbL));
36+
var beacon = parser.FromScanData(bytes, -55, null);
37+
Assert.NotNull(beacon, "Beacon should be not null if parsed successfully");
38+
}
39+
40+
[Test]
41+
public void TestParseWrongFormatReturnsNothing()
42+
{
43+
LogManager.D("XXX", "testParseWrongFormatReturnsNothing start");
44+
var bytes = HexStringToByteArray("02011a1aff1801ffff2f234454cf6d4a0fadf2f4911ba9ffa600010002c509");
45+
var parser = new AltBeaconParser();
46+
var beacon = parser.FromScanData(bytes, -55, null);
47+
LogManager.D("XXX", "testParseWrongFormatReturnsNothing end");
48+
Assert.Null(beacon, "Beacon should be null if not parsed successfully");
49+
}
50+
51+
[Test]
52+
public void TestParsesBeaconMissingDataField()
53+
{
54+
var bytes = HexStringToByteArray("02011a1aff1801beac2f234454cf6d4a0fadf2f4911ba9ffa600010002c5000000");
55+
var parser = new AltBeaconParser();
56+
var beacon = parser.FromScanData(bytes, -55, null);
57+
Assert.AreEqual(-55, beacon.Rssi, "mRssi should be as passed in");
58+
Assert.AreEqual("2f234454-cf6d-4a0f-adf2-f4911ba9ffa6", beacon.GetIdentifier(0).ToString(), "uuid should be parsed");
59+
Assert.AreEqual("1", beacon.GetIdentifier(1).ToString(), "id2 should be parsed");
60+
Assert.AreEqual("2", beacon.GetIdentifier(2).ToString(), "id3 should be parsed");
61+
Assert.AreEqual(-59, beacon.TxPower, "txPower should be parsed");
62+
Assert.AreEqual(0x118 ,beacon.Manufacturer, "manufacturer should be parsed");
63+
Assert.AreEqual(Convert.ToInt64(new Java.Lang.Long(0)), Convert.ToInt64(beacon.DataFields[0]), "missing data field zero should be zero");
64+
}
65+
}
66+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using System;
2+
using AltBeaconOrg.BoundBeacon;
3+
using Android.OS;
4+
using NUnit.Framework;
5+
6+
namespace AndroidAltBeaconLibrary.UnitTests
7+
{
8+
[TestFixture]
9+
public class AltBeaconTest : TestBase
10+
{
11+
[Test]
12+
public void TestRecognizeBeacon()
13+
{
14+
var bytes = HexStringToByteArray("02011a1bff1801beac2f234454cf6d4a0fadf2f4911ba9ffa600010002c509");
15+
var parser = new AltBeaconParser();
16+
var beacon = parser.FromScanData(bytes, -55, null);
17+
Assert.AreEqual(9, ((AltBeacon) beacon).MfgReserved, "manData should be parsed");
18+
}
19+
20+
[Test]
21+
public void TestCanSerializeParcelable()
22+
{
23+
var parcel = Parcel.Obtain();
24+
var beacon = new AltBeacon.Builder().SetMfgReserved(7).SetId1("1").SetId2("2").SetId3("3").SetRssi(4)
25+
.SetBeaconTypeCode(5).SetTxPower(6)
26+
.SetBluetoothAddress("1:2:3:4:5:6").Build();
27+
beacon.WriteToParcel(parcel, 0);
28+
parcel.SetDataPosition(0);
29+
var beacon2 = new AltBeacon(parcel);
30+
Assert.AreEqual(((AltBeacon)beacon).MfgReserved, ((AltBeacon)beacon2).MfgReserved, "beaconMfgReserved is same after deserialization");
31+
}
32+
}
33+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<PropertyGroup>
4+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
5+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
6+
<ProjectGuid>{E25C7961-349C-4464-A3D4-4AB8170A9FC1}</ProjectGuid>
7+
<ProjectTypeGuids>{EFBA0AD7-5A72-4C68-AF49-83D382785DCF};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
8+
<OutputType>Library</OutputType>
9+
<RootNamespace>AndroidAltBeaconLibrary.UnitTests</RootNamespace>
10+
<AssemblyName>AndroidAltBeaconLibrary.UnitTests</AssemblyName>
11+
<TargetFrameworkVersion>v7.1</TargetFrameworkVersion>
12+
<AndroidApplication>True</AndroidApplication>
13+
<AndroidResgenFile>Resources\Resource.designer.cs</AndroidResgenFile>
14+
<AndroidResgenClass>Resource</AndroidResgenClass>
15+
<MonoAndroidResourcePrefix>Resources</MonoAndroidResourcePrefix>
16+
<MonoAndroidAssetsPrefix>Assets</MonoAndroidAssetsPrefix>
17+
<AndroidUseLatestPlatformSdk>true</AndroidUseLatestPlatformSdk>
18+
<AndroidManifest>Properties\AndroidManifest.xml</AndroidManifest>
19+
</PropertyGroup>
20+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
21+
<DebugSymbols>true</DebugSymbols>
22+
<DebugType>full</DebugType>
23+
<Optimize>false</Optimize>
24+
<OutputPath>bin\Debug</OutputPath>
25+
<DefineConstants>DEBUG;</DefineConstants>
26+
<ErrorReport>prompt</ErrorReport>
27+
<WarningLevel>4</WarningLevel>
28+
<AndroidLinkMode>None</AndroidLinkMode>
29+
<AndroidSupportedAbis>arm64-v8a;armeabi;armeabi-v7a;x86</AndroidSupportedAbis>
30+
</PropertyGroup>
31+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
32+
<DebugSymbols>true</DebugSymbols>
33+
<DebugType>pdbonly</DebugType>
34+
<Optimize>true</Optimize>
35+
<OutputPath>bin\Release</OutputPath>
36+
<ErrorReport>prompt</ErrorReport>
37+
<WarningLevel>4</WarningLevel>
38+
<AndroidManagedSymbols>true</AndroidManagedSymbols>
39+
<AndroidUseSharedRuntime>false</AndroidUseSharedRuntime>
40+
</PropertyGroup>
41+
<ItemGroup>
42+
<Reference Include="System" />
43+
<Reference Include="System.Xml" />
44+
<Reference Include="System.Core" />
45+
<Reference Include="Mono.Android" />
46+
<Reference Include="Xamarin.Android.NUnitLite" />
47+
</ItemGroup>
48+
<ItemGroup>
49+
<Compile Include="MainActivity.cs" />
50+
<Compile Include="TestsSample.cs" />
51+
<Compile Include="Resources\Resource.designer.cs" />
52+
<Compile Include="Properties\AssemblyInfo.cs" />
53+
<Compile Include="AltBeaconParserTest.cs" />
54+
<Compile Include="AltBeaconTest.cs" />
55+
<Compile Include="TestBase.cs" />
56+
<Compile Include="BeaconParserTest.cs" />
57+
<Compile Include="AssertEx.cs" />
58+
</ItemGroup>
59+
<ItemGroup>
60+
<None Include="Resources\AboutResources.txt" />
61+
<None Include="Assets\AboutAssets.txt" />
62+
<None Include="Properties\AndroidManifest.xml" />
63+
</ItemGroup>
64+
<ItemGroup>
65+
<AndroidResource Include="Resources\mipmap-hdpi\Icon.png" />
66+
<AndroidResource Include="Resources\mipmap-mdpi\Icon.png" />
67+
<AndroidResource Include="Resources\mipmap-xhdpi\Icon.png" />
68+
<AndroidResource Include="Resources\mipmap-xxhdpi\Icon.png" />
69+
<AndroidResource Include="Resources\mipmap-xxxhdpi\Icon.png" />
70+
</ItemGroup>
71+
<ItemGroup>
72+
<ProjectReference Include="..\AndroidAltBeaconLibrary\AndroidAltBeaconLibrary.csproj">
73+
<Project>{4D26EC32-DC3D-4046-A845-306A40014ADF}</Project>
74+
<Name>AndroidAltBeaconLibrary</Name>
75+
<Private>False</Private>
76+
</ProjectReference>
77+
</ItemGroup>
78+
<Import Project="$(MSBuildExtensionsPath)\Xamarin\Android\Xamarin.Android.CSharp.targets" />
79+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using NUnit.Framework;
2+
3+
namespace AndroidAltBeaconLibrary.UnitTests
4+
{
5+
public static class AssertEx
6+
{
7+
public static void AreEqual(string message, int expected, int actual)
8+
{
9+
Assert.AreEqual(expected, actual, message);
10+
}
11+
12+
public static void AreEqual(string message, long expected, long actual)
13+
{
14+
Assert.AreEqual(expected, actual, message);
15+
}
16+
17+
public static void AreEqual(string message, object expected, object actual)
18+
{
19+
Assert.AreEqual(expected, actual, message);
20+
}
21+
}
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Any raw assets you want to be deployed with your application can be placed in
2+
this directory (and child directories) and given a Build Action of "AndroidAsset".
3+
4+
These files will be deployed with your package and will be accessible using Android's
5+
AssetManager, like this:
6+
7+
public class ReadAsset : Activity
8+
{
9+
protected override void OnCreate (Bundle bundle)
10+
{
11+
base.OnCreate (bundle);
12+
13+
InputStream input = Assets.Open ("my_asset.txt");
14+
}
15+
}
16+
17+
Additionally, some Android functions will automatically load asset files:
18+
19+
Typeface tf = Typeface.CreateFromAsset (Context.Assets, "fonts/samplefont.ttf");
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
using System;
2+
using AltBeaconOrg.BoundBeacon;
3+
using NUnit.Framework;
4+
5+
namespace AndroidAltBeaconLibrary.UnitTests
6+
{
7+
[TestFixture]
8+
public class BeaconParserTest : TestBase
9+
{
10+
[Test]
11+
public void testSetBeaconLayout()
12+
{
13+
var bytes = HexStringToByteArray("02011a1bffbeac2f234454cf6d4a0fadf2f4911ba9ffa600010002c509000000");
14+
var parser = new BeaconParser();
15+
parser.SetBeaconLayout("m:2-3=beac,i:4-19,i:20-21,i:22-23,p:24-24,d:25-25");
16+
17+
Assert.AreEqual(2, parser.MatchingBeaconTypeCodeStartOffset, "parser should get beacon type code start offset");
18+
Assert.AreEqual(3, parser.MatchingBeaconTypeCodeEndOffset, "parser should get beacon type code end offset");
19+
Assert.AreEqual(Convert.ToInt64(0xbeacL), Convert.ToInt64(parser.MatchingBeaconTypeCode), "parser should get beacon type code");
20+
Assert.AreEqual(4, parser.IdentifierStartOffsets[0], "parser should get identifier start offset");
21+
AssertEx.AreEqual("parser should get identifier end offset", 19, parser.IdentifierEndOffsets[0]);
22+
AssertEx.AreEqual("parser should get identifier start offset", 20, parser.IdentifierStartOffsets[1]);
23+
AssertEx.AreEqual("parser should get identifier end offset", 21, parser.IdentifierEndOffsets[1]);
24+
AssertEx.AreEqual("parser should get identifier start offset", 22, parser.IdentifierStartOffsets[2]);
25+
AssertEx.AreEqual("parser should get identifier end offset", 23, parser.IdentifierEndOffsets[2]);
26+
AssertEx.AreEqual("parser should get power start offset", 24, Convert.ToInt32(parser.PowerStartOffset));
27+
AssertEx.AreEqual("parser should get power end offset", 24, Convert.ToInt32(parser.PowerEndOffset));
28+
AssertEx.AreEqual("parser should get data start offset", 25, parser.DataStartOffsets[0]);
29+
AssertEx.AreEqual("parser should get data end offset", 25, parser.DataEndOffsets[0]);
30+
}
31+
}
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
using System.Reflection;
2+
3+
using Android.App;
4+
using Android.OS;
5+
using Xamarin.Android.NUnitLite;
6+
7+
namespace AndroidAltBeaconLibrary.UnitTests
8+
{
9+
[Activity(Label = "AndroidAltBeaconLibrary.UnitTests", MainLauncher = true)]
10+
public class MainActivity : TestSuiteActivity
11+
{
12+
protected override void OnCreate(Bundle bundle)
13+
{
14+
// tests can be inside the main assembly
15+
AddTest(Assembly.GetExecutingAssembly());
16+
// or in any reference assemblies
17+
// AddTest (typeof (Your.Library.TestClass).Assembly);
18+
19+
// Once you called base.OnCreate(), you cannot add more assemblies.
20+
base.OnCreate(bundle);
21+
}
22+
}
23+
24+
public static class ObjectTypeHelper
25+
{
26+
public static T Cast<T>(this Java.Lang.Object obj) where T : class
27+
{
28+
var propertyInfo = obj.GetType().GetProperty("Instance");
29+
return propertyInfo == null ? null : propertyInfo.GetValue(obj, null) as T;
30+
}
31+
}
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="AndroidAltBeaconLibrary.UnitTests">
3+
<uses-sdk android:minSdkVersion="18" />
4+
<uses-permission android:name="android.permission.BLUETOOTH" />
5+
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
6+
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
7+
<application android:label="AndroidAltBeaconLibrary.UnitTests"></application>
8+
</manifest>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using System.Reflection;
2+
using System.Runtime.CompilerServices;
3+
using Android.App;
4+
5+
// Information about this assembly is defined by the following attributes.
6+
// Change them to the values specific to your project.
7+
8+
[assembly: AssemblyTitle("AndroidAltBeaconLibrary.UnitTests")]
9+
[assembly: AssemblyDescription("")]
10+
[assembly: AssemblyConfiguration("")]
11+
[assembly: AssemblyCompany("")]
12+
[assembly: AssemblyProduct("")]
13+
[assembly: AssemblyCopyright("${AuthorCopyright}")]
14+
[assembly: AssemblyTrademark("")]
15+
[assembly: AssemblyCulture("")]
16+
17+
// The assembly version has the format "{Major}.{Minor}.{Build}.{Revision}".
18+
// The form "{Major}.{Minor}.*" will automatically update the build and revision,
19+
// and "{Major}.{Minor}.{Build}.*" will update just the revision.
20+
21+
[assembly: AssemblyVersion("1.0.0")]
22+
23+
// The following attributes are used to specify the signing key for the assembly,
24+
// if desired. See the Mono documentation for more information about signing.
25+
26+
//[assembly: AssemblyDelaySign(false)]
27+
//[assembly: AssemblyKeyFile("")]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
Images, layout descriptions, binary blobs and string dictionaries can be included
2+
in your application as resource files. Various Android APIs are designed to
3+
operate on the resource IDs instead of dealing with images, strings or binary blobs
4+
directly.
5+
6+
For example, a sample Android app that contains a user interface layout (main.axml),
7+
an internationalization string table (strings.xml) and some icons (drawable-XXX/icon.png)
8+
would keep its resources in the "Resources" directory of the application:
9+
10+
Resources/
11+
drawable/
12+
icon.png
13+
14+
layout/
15+
main.axml
16+
17+
values/
18+
strings.xml
19+
20+
In order to get the build system to recognize Android resources, set the build action to
21+
"AndroidResource". The native Android APIs do not operate directly with filenames, but
22+
instead operate on resource IDs. When you compile an Android application that uses resources,
23+
the build system will package the resources for distribution and generate a class called "R"
24+
(this is an Android convention) that contains the tokens for each one of the resources
25+
included. For example, for the above Resources layout, this is what the R class would expose:
26+
27+
public class R {
28+
public class drawable {
29+
public const int icon = 0x123;
30+
}
31+
32+
public class layout {
33+
public const int main = 0x456;
34+
}
35+
36+
public class strings {
37+
public const int first_string = 0xabc;
38+
public const int second_string = 0xbcd;
39+
}
40+
}
41+
42+
You would then use R.drawable.icon to reference the drawable/icon.png file, or R.layout.main
43+
to reference the layout/main.axml file, or R.strings.first_string to reference the first
44+
string in the dictionary file values/strings.xml.

0 commit comments

Comments
 (0)