Skip to content
This repository was archived by the owner on Apr 27, 2024. It is now read-only.

Commit 6c6ef89

Browse files
author
Dirk Lemstra
committed
Added ImageAspectRatio, ImageSize and ImageBackgroundColor to ButtonsTemplate.
1 parent 41cc65e commit 6c6ef89

File tree

3 files changed

+202
-1
lines changed

3 files changed

+202
-1
lines changed

src/LineBot/Messages/Template/ButtonsTemplate.cs

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ namespace Line
2525
public sealed class ButtonsTemplate : IButtonsTemplate
2626
{
2727
private Uri _thumbnailUrl;
28+
private string _color = "#FFFFFF";
2829
private string _title;
2930
private string _text;
3031
private IEnumerable<ITemplateAction> _actions;
@@ -71,6 +72,46 @@ public Uri ThumbnailUrl
7172
}
7273
}
7374

75+
/// <summary>
76+
/// Gets or sets the aspect ratio of the image.
77+
/// </summary>
78+
[JsonProperty("imageAspectRatio")]
79+
[JsonConverter(typeof(EnumConverter<ImageAspectRatio>))]
80+
public ImageAspectRatio ImageAspectRatio { get; set; } = ImageAspectRatio.Rectangle;
81+
82+
/// <summary>
83+
/// Gets or sets the size of the image.
84+
/// </summary>
85+
[JsonProperty("imageSize")]
86+
[JsonConverter(typeof(EnumConverter<MessageType>))]
87+
public ImageSize ImageSize { get; set; } = ImageSize.Cover;
88+
89+
/// <summary>
90+
/// Gets or sets the background color of the image.
91+
/// </summary>
92+
[JsonProperty("imageBackgroundColor")]
93+
public string ImageBackgroundColor
94+
{
95+
get
96+
{
97+
return _color;
98+
}
99+
100+
set
101+
{
102+
if (value == null || value.Length != 7)
103+
throw new InvalidOperationException("The color should be 7 characters long.");
104+
105+
if (value[0] != '#')
106+
throw new InvalidOperationException("The color should start with #.");
107+
108+
if (!IsValidColor(value))
109+
throw new InvalidOperationException("The color contains invalid characters.");
110+
111+
_color = value.ToUpperInvariant();
112+
}
113+
}
114+
74115
/// <summary>
75116
/// Gets or sets the title.
76117
/// </summary>
@@ -157,5 +198,29 @@ public IEnumerable<ITemplateAction> Actions
157198
_actions = value;
158199
}
159200
}
201+
202+
private static bool IsValidColor(string value)
203+
{
204+
for (int i = 1; i < value.Length; i++)
205+
{
206+
char character = value[i];
207+
208+
// 0-9
209+
if (character >= 48 && character <= 57)
210+
continue;
211+
212+
// A-F
213+
if (character >= 65 && character <= 70)
214+
continue;
215+
216+
// a-f
217+
if (character >= 97 && character <= 102)
218+
continue;
219+
220+
return false;
221+
}
222+
223+
return true;
224+
}
160225
}
161226
}

tests/LineBot.Tests/Messages/Template/ButtonsTemplateTests/TheConstructor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public void ShouldInitializeTheInstance()
3434
};
3535

3636
string serialized = JsonConvert.SerializeObject(template);
37-
Assert.AreEqual(@"{""type"":""buttons"",""thumbnailImageUrl"":""https://foo.bar"",""title"":""Foo"",""text"":""Test"",""actions"":null}", serialized);
37+
Assert.AreEqual(@"{""type"":""buttons"",""thumbnailImageUrl"":""https://foo.bar"",""imageAspectRatio"":""rectangle"",""imageSize"":""cover"",""imageBackgroundColor"":""#FFFFFF"",""title"":""Foo"",""text"":""Test"",""actions"":null}", serialized);
3838
}
3939
}
4040
}
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
// Copyright 2017 Dirk Lemstra (https://github.com/dlemstra/line-bot-sdk-dotnet)
2+
//
3+
// Dirk Lemstra licenses this file to you under the Apache License,
4+
// version 2.0 (the "License"); you may not use this file except in compliance
5+
// with the License. You may obtain a copy of the License at:
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11+
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12+
// License for the specific language governing permissions and limitations
13+
// under the License.
14+
15+
using System;
16+
using Microsoft.VisualStudio.TestTools.UnitTesting;
17+
18+
namespace Line.Tests.Messages.Template
19+
{
20+
public partial class ButtonsTemplateTests
21+
{
22+
[TestClass]
23+
public class TheimageBackgroundColorProperty
24+
{
25+
[TestMethod]
26+
public void ShouldThowExceptionWhenValueIsNull()
27+
{
28+
ButtonsTemplate template = new ButtonsTemplate();
29+
30+
ExceptionAssert.Throws<InvalidOperationException>("The color should be 7 characters long.", () =>
31+
{
32+
template.ImageBackgroundColor = null;
33+
});
34+
}
35+
36+
[TestMethod]
37+
public void ShouldThowExceptionWhenValueIsToLong()
38+
{
39+
ButtonsTemplate template = new ButtonsTemplate();
40+
41+
ExceptionAssert.Throws<InvalidOperationException>("The color should be 7 characters long.", () =>
42+
{
43+
template.ImageBackgroundColor = "#FFFFFFF";
44+
});
45+
}
46+
47+
[TestMethod]
48+
public void ShouldThowExceptionWhenValueNotStartsWithNumberSign()
49+
{
50+
ButtonsTemplate template = new ButtonsTemplate();
51+
52+
ExceptionAssert.Throws<InvalidOperationException>("The color should start with #.", () =>
53+
{
54+
template.ImageBackgroundColor = "FFFFFFF";
55+
});
56+
}
57+
58+
[TestMethod]
59+
public void ShouldThowExceptionWhenValueIsInvalid()
60+
{
61+
ButtonsTemplate template = new ButtonsTemplate();
62+
63+
ExceptionAssert.Throws<InvalidOperationException>("The color contains invalid characters.", () =>
64+
{
65+
template.ImageBackgroundColor = "#@FFFFF";
66+
});
67+
}
68+
69+
[TestMethod]
70+
public void ShouldThowExceptionWhenValueIsInvalid2()
71+
{
72+
ButtonsTemplate template = new ButtonsTemplate();
73+
74+
ExceptionAssert.Throws<InvalidOperationException>("The color contains invalid characters.", () =>
75+
{
76+
template.ImageBackgroundColor = "#GFFFFF";
77+
});
78+
}
79+
80+
[TestMethod]
81+
public void ShouldThowExceptionWhenValueIsInvalid3()
82+
{
83+
ButtonsTemplate template = new ButtonsTemplate();
84+
85+
ExceptionAssert.Throws<InvalidOperationException>("The color contains invalid characters.", () =>
86+
{
87+
template.ImageBackgroundColor = "#`FFFFF";
88+
});
89+
}
90+
91+
[TestMethod]
92+
public void ShouldThowExceptionWhenValueIsInvalid4()
93+
{
94+
ButtonsTemplate template = new ButtonsTemplate();
95+
96+
ExceptionAssert.Throws<InvalidOperationException>("The color contains invalid characters.", () =>
97+
{
98+
template.ImageBackgroundColor = "#gFFFFF";
99+
});
100+
}
101+
102+
[TestMethod]
103+
public void ShouldThowExceptionWhenValueIsInvalid5()
104+
{
105+
ButtonsTemplate template = new ButtonsTemplate();
106+
107+
ExceptionAssert.Throws<InvalidOperationException>("The color contains invalid characters.", () =>
108+
{
109+
template.ImageBackgroundColor = "#/FFFFF";
110+
});
111+
}
112+
113+
[TestMethod]
114+
public void ShouldThowExceptionWhenValueIsInvalid6()
115+
{
116+
ButtonsTemplate template = new ButtonsTemplate();
117+
118+
ExceptionAssert.Throws<InvalidOperationException>("The color contains invalid characters.", () =>
119+
{
120+
template.ImageBackgroundColor = "#:FFFFF";
121+
});
122+
}
123+
124+
[TestMethod]
125+
public void ShouldUppercaseTheValue()
126+
{
127+
ButtonsTemplate template = new ButtonsTemplate
128+
{
129+
ImageBackgroundColor = "#ff00FF"
130+
};
131+
132+
Assert.AreEqual("#FF00FF", template.ImageBackgroundColor);
133+
}
134+
}
135+
}
136+
}

0 commit comments

Comments
 (0)