Files
acceptance-tests
data
debian
docs
external
Newtonsoft.Json
api-doc-tools
api-snapshot
aspnetwebstack
packages
src
test
Microsoft.TestCommon
Microsoft.Web.Helpers.Test
Properties
AnalyticsTest.cs
BingTest.cs
FacebookTest.cs
FileUploadTest.cs
GamerCardTest.cs
GravatarTest.cs
LinkShareTest.cs
MapsTest.cs
Microsoft.Web.Helpers.Test.csproj
PreAppStartCodeTest.cs
ReCaptchaTest.cs
ThemesTest.cs
TwitterTest.cs
UrlBuilderTest.cs
VideoTest.cs
packages.config
Microsoft.Web.Http.Data.Test
Microsoft.Web.Mvc.Test
Microsoft.Web.WebPages.OAuth.Test
SPA.Test
System.Json.Test.Integration
System.Json.Test.Unit
System.Net.Http.Formatting.Test.Integration
System.Net.Http.Formatting.Test.Unit
System.Web.Helpers.Test
System.Web.Http.Integration.Test
System.Web.Http.SelfHost.Test
System.Web.Http.Test
System.Web.Http.WebHost.Test
System.Web.Mvc.Test
System.Web.Razor.Test
System.Web.WebPages.Administration.Test
System.Web.WebPages.Deployment.Test
System.Web.WebPages.Razor.Test
System.Web.WebPages.Test
WebMatrix.Data.Test
WebMatrix.WebData.Test
Settings.StyleCop
tools
.gitattributes
.gitignore
License.txt
README.md
Runtime.msbuild
Runtime.sln
Runtime.xunit
Settings.StyleCop
build.cmd
bdwgc
binary-reference-assemblies
bockbuild
boringssl
cecil
cecil-legacy
corefx
corert
helix-binaries
ikdasm
ikvm
illinker-test-assets
linker
llvm-project
nuget-buildtasks
nunit-lite
roslyn-binaries
rx
xunit-binaries
how-to-bump-roslyn-binaries.md
ikvm-native
llvm
m4
man
mcs
mk
mono
msvc
netcore
po
runtime
samples
scripts
support
tools
COPYING.LIB
LICENSE
Makefile.am
Makefile.in
NEWS
README.md
acinclude.m4
aclocal.m4
autogen.sh
code_of_conduct.md
compile
config.guess
config.h.in
config.rpath
config.sub
configure.REMOVED.git-id
configure.ac.REMOVED.git-id
depcomp
install-sh
ltmain.sh.REMOVED.git-id
missing
mkinstalldirs
mono-uninstalled.pc.in
test-driver
winconfig.h

190 lines
7.9 KiB
C#
Raw Normal View History

// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.
using System;
using System.Linq;
using System.Web;
using System.Web.Helpers.Test;
using System.Web.TestUtil;
using System.Web.WebPages.Scope;
using Xunit;
using Assert = Microsoft.TestCommon.AssertEx;
namespace Microsoft.Web.Helpers.Test
{
public class LinkShareTest
{
private static LinkShareSite[] _allLinkShareSites = new[]
{
LinkShareSite.Delicious, LinkShareSite.Digg, LinkShareSite.GoogleBuzz,
LinkShareSite.Facebook, LinkShareSite.Reddit, LinkShareSite.StumbleUpon, LinkShareSite.Twitter
};
[Fact]
public void RenderWithFacebookFirst_ReturnsHtmlWithFacebookAndThenOthersTest()
{
string pageTitle = "page1";
string pageLinkBack = "page link back";
string twitterUserName = String.Empty;
string twitterTag = String.Empty;
string actual;
actual = LinkShare.GetHtml(pageTitle, pageLinkBack, twitterUserName, twitterTag, LinkShareSite.Facebook, LinkShareSite.All).ToString();
Assert.True(actual.Contains("twitter.com"));
int pos = actual.IndexOf("facebook.com");
Assert.True(pos > 0);
int pos2 = actual.IndexOf("reddit.com");
Assert.True(pos2 > pos);
pos2 = actual.IndexOf("digg.com");
Assert.True(pos2 > pos);
}
[Fact]
public void BitlyApiKeyThrowsWhenSetToNull()
{
Assert.ThrowsArgumentNull(() => LinkShare.BitlyApiKey = null, "value");
}
[Fact]
public void BitlyApiKeyUsesScopeStorage()
{
// Arrange
var value = "value";
// Act
LinkShare.BitlyApiKey = value;
// Assert
Assert.Equal(LinkShare.BitlyApiKey, value);
Assert.Equal(ScopeStorage.CurrentScope[LinkShare._bitlyApiKey], value);
}
[Fact]
public void BitlyLoginThrowsWhenSetToNull()
{
Assert.ThrowsArgumentNull(() => LinkShare.BitlyLogin = null, "value");
}
[Fact]
public void BitlyLoginUsesScopeStorage()
{
// Arrange
var value = "value";
// Act
LinkShare.BitlyLogin = value;
// Assert
Assert.Equal(LinkShare.BitlyLogin, value);
Assert.Equal(ScopeStorage.CurrentScope[LinkShare._bitlyLogin], value);
}
[Fact]
public void RenderWithNullPageTitle_ThrowsException()
{
Assert.ThrowsArgumentNullOrEmptyString(
() => LinkShare.GetHtml(null).ToString(),
"pageTitle");
}
[Fact]
public void Render_WithFacebook_Works()
{
string actualHTML = LinkShare.GetHtml("page-title", "www.foo.com", linkSites: LinkShareSite.Facebook).ToString();
string expectedHTML =
"<a href=\"http://www.facebook.com/sharer.php?u=www.foo.com&amp;t=page-title\" target=\"_blank\" title=\"Share on Facebook\"><img alt=\"Share on Facebook\" src=\"http://www.facebook.com/favicon.ico\" style=\"border:0; height:16px; width:16px; margin:0 1px;\" title=\"Share on Facebook\" /></a>";
UnitTestHelper.AssertEqualsIgnoreWhitespace(actualHTML, expectedHTML);
}
[Fact]
public void Render_WithFacebookAndDigg_Works()
{
string actualHTML = LinkShare.GetHtml("page-title", "www.foo.com", linkSites: new[] { LinkShareSite.Facebook, LinkShareSite.Digg }).ToString();
string expectedHTML =
"<a href=\"http://www.facebook.com/sharer.php?u=www.foo.com&amp;t=page-title\" target=\"_blank\" title=\"Share on Facebook\"><img alt=\"Share on Facebook\" src=\"http://www.facebook.com/favicon.ico\" style=\"border:0; height:16px; width:16px; margin:0 1px;\" title=\"Share on Facebook\" /></a><a href=\"http://digg.com/submit?url=www.foo.com&amp;title=page-title\" target=\"_blank\" title=\"Digg!\"><img alt=\"Digg!\" src=\"http://digg.com/img/badges/16x16-digg-guy.gif\" style=\"border:0; height:16px; width:16px; margin:0 1px;\" title=\"Digg!\" /></a>";
UnitTestHelper.AssertEqualsIgnoreWhitespace(actualHTML, expectedHTML);
}
[Fact]
public void Render_WithFacebook_RendersAnchorTitle()
{
string actualHTML = LinkShare.GetHtml("page-title", "www.foo.com", linkSites: LinkShareSite.Facebook).ToString();
string expectedHtml = @"<a href=""http://www.facebook.com/sharer.php?u=www.foo.com&amp;t=page-title"" target=""_blank"" title=""Share on Facebook"">
<img alt=""Share on Facebook"" src=""http://www.facebook.com/favicon.ico"" style=""border:0; height:16px; width:16px; margin:0 1px;"" title=""Share on Facebook"" />
</a>";
UnitTestHelper.AssertEqualsIgnoreWhitespace(expectedHtml, actualHTML);
}
[Fact]
public void LinkShare_GetSitesInOrderReturnsAllSitesWhenArgumentIsNull()
{
// Act and Assert
var result = LinkShare.GetSitesInOrder(linkSites: null);
Assert.Equal(_allLinkShareSites, result.ToArray());
}
[Fact]
public void LinkShare_GetSitesInOrderReturnsAllSitesWhenArgumentIEmpty()
{
// Act
var result = LinkShare.GetSitesInOrder(linkSites: new LinkShareSite[] { });
// Assert
Assert.Equal(_allLinkShareSites, result.ToArray());
}
[Fact]
public void LinkShare_GetSitesInOrderReturnsAllSitesWhenAllIsFirstItem()
{
// Act
var result = LinkShare.GetSitesInOrder(linkSites: new[] { LinkShareSite.All, LinkShareSite.Reddit });
// Assert
Assert.Equal(_allLinkShareSites, result.ToArray());
}
[Fact]
public void LinkShare_GetSitesInOrderReturnsSitesInOrderWhenAllIsNotFirstItem()
{
// Act
var result = LinkShare.GetSitesInOrder(linkSites: new[] { LinkShareSite.Reddit, LinkShareSite.Facebook, LinkShareSite.All });
// Assert
Assert.Equal(new[]
{
LinkShareSite.Reddit, LinkShareSite.Facebook, LinkShareSite.Delicious, LinkShareSite.Digg,
LinkShareSite.GoogleBuzz, LinkShareSite.StumbleUpon, LinkShareSite.Twitter
}, result.ToArray());
}
[Fact]
public void LinkShare_EncodesParameters()
{
// Arrange
var expectedHtml =
@"<a href=""http://reddit.com/submit?url=www.foo.com&amp;title=%26%26"" target=""_blank"" title=""Reddit!"">
<img alt=""Reddit!"" src=""http://www.Reddit.com/favicon.ico"" style=""border:0; height:16px; width:16px; margin:0 1px;"" title=""Reddit!"" />
</a>
<a href=""http://twitter.com/home/?status=%26%26%3a+www.foo.com%2c+(via+%40%40%3cTweeter+Bot%3e)+I+%3c3+Tweets"" target=""_blank"" title=""Share on Twitter"">
<img alt=""Share on Twitter"" src=""http://twitter.com/favicon.ico"" style=""border:0; height:16px; width:16px; margin:0 1px;"" title=""Share on Twitter"" />
</a>";
// Act
var actualHtml = LinkShare.GetHtml("&&", "www.foo.com", "<Tweeter Bot>", "I <3 Tweets", LinkShareSite.Reddit, LinkShareSite.Twitter).ToString();
// Assert
UnitTestHelper.AssertEqualsIgnoreWhitespace(expectedHtml, actualHtml);
}
[Fact]
public void LinkshareRendersValidXhtml()
{
string result = "<html> <head> \n <title> </title> \n </head> \n <body> <div> \n" +
LinkShare.GetHtml("any<>title", "my test page <>") +
"\n </div> </body> \n </html>";
HtmlString htmlResult = new HtmlString(result);
XhtmlAssert.Validate1_0(htmlResult);
}
}
}