--HG--
branch : dev
This commit is contained in:
Nathan Heskew
2010-10-07 23:17:29 -07:00
19 changed files with 347 additions and 56 deletions

View File

@@ -17,7 +17,10 @@ namespace Orchard.Tests.DisplayManagement {
WorkContext _workContext;
protected override void Register(Autofac.ContainerBuilder builder) {
_defaultShapeTable = new ShapeTable { Descriptors = new Dictionary<string, ShapeDescriptor>() };
_defaultShapeTable = new ShapeTable {
Descriptors = new Dictionary<string, ShapeDescriptor>(),
Bindings = new Dictionary<string, ShapeBinding>()
};
_workContext = new TestWorkContext {
CurrentTheme = new Theme { ThemeName = "Hello" }
};
@@ -26,10 +29,22 @@ namespace Orchard.Tests.DisplayManagement {
builder.RegisterType<DefaultDisplayManager>().As<IDisplayManager>();
builder.RegisterType<TestShapeTableManager>().As<IShapeTableManager>();
builder.RegisterType<TestWorkContextAccessor>().As<IWorkContextAccessor>();
builder.RegisterType<TestDisplayEvents>().As<IShapeDisplayEvents>()
.As<TestDisplayEvents>()
.InstancePerLifetimeScope();
builder.Register(ctx => _defaultShapeTable);
builder.Register(ctx => _workContext);
}
class TestDisplayEvents : IShapeDisplayEvents {
public Action<ShapeDisplayingContext> Displaying = ctx => { };
public Action<ShapeDisplayedContext> Displayed = ctx => { };
void IShapeDisplayEvents.Displaying(ShapeDisplayingContext context) { Displaying(context); }
void IShapeDisplayEvents.Displayed(ShapeDisplayedContext context) { Displayed(context); }
}
public class Theme : ITheme {
public string ThemeName { get; set; }
public string DisplayName { get; set; }
@@ -83,11 +98,10 @@ namespace Orchard.Tests.DisplayManagement {
public IContainerProvider ContainerProvider { get; set; }
public override T Resolve<T>() {
if (typeof(T) == typeof(ILifetimeScope))
{
return (T) ContainerProvider.RequestLifetime;
if (typeof(T) == typeof(ILifetimeScope)) {
return (T)ContainerProvider.RequestLifetime;
}
throw new NotImplementedException();
}
@@ -102,8 +116,10 @@ namespace Orchard.Tests.DisplayManagement {
}
void AddShapeDescriptor(ShapeDescriptor shapeDescriptor) {
_defaultShapeTable.Descriptors[shapeDescriptor.ShapeType] = shapeDescriptor;
foreach (var binding in shapeDescriptor.Bindings) {
_defaultShapeTable.Descriptors[binding.Key] = shapeDescriptor;
binding.Value.ShapeDescriptor = shapeDescriptor;
_defaultShapeTable.Bindings[binding.Key] = binding.Value;
}
}
@@ -161,7 +177,7 @@ namespace Orchard.Tests.DisplayManagement {
}
[Test]
public void RenderAlternateShape() {
public void RenderAlternateShapeExplicitly() {
var displayManager = _container.Resolve<IDisplayManager>();
var shape = new Shape {
@@ -186,5 +202,125 @@ namespace Orchard.Tests.DisplayManagement {
var result = displayManager.Execute(CreateDisplayContext(shape));
Assert.That(result.ToString(), Is.EqualTo("Hello again!"));
}
[Test]
public void RenderAlternateShapeByMostRecentlyAddedMatchingAlternate() {
var displayManager = _container.Resolve<IDisplayManager>();
var shape = new Shape {
Metadata = new ShapeMetadata {
Type = "Foo"
}
};
shape.Metadata.Alternates.Add("Foo__1");
shape.Metadata.Alternates.Add("Foo__2");
shape.Metadata.Alternates.Add("Foo__3");
var descriptor = new ShapeDescriptor {
ShapeType = "Foo",
};
AddBinding(descriptor, "Foo", ctx => new HtmlString("Hi there!"));
AddBinding(descriptor, "Foo__1", ctx => new HtmlString("Hello (1)!"));
AddBinding(descriptor, "Foo__2", ctx => new HtmlString("Hello (2)!"));
AddShapeDescriptor(descriptor);
var result = displayManager.Execute(CreateDisplayContext(shape));
Assert.That(result.ToString(), Is.EqualTo("Hello (2)!"));
}
private static void AddBinding(ShapeDescriptor descriptor, string bindingName, Func<DisplayContext, IHtmlString> binding) {
descriptor.Bindings[bindingName] = new ShapeBinding {
BindingName = bindingName,
Binding = binding,
};
}
[Test]
public void IShapeDisplayEventsIsCalled() {
var displayManager = _container.Resolve<IDisplayManager>();
var shape = new Shape {
Metadata = new ShapeMetadata {
Type = "Foo"
}
};
var descriptor = new ShapeDescriptor {
ShapeType = "Foo",
};
AddBinding(descriptor, "Foo", ctx => new HtmlString("yarg"));
AddShapeDescriptor(descriptor);
var displayingEventCount = 0;
var displayedEventCount = 0;
_container.Resolve<TestDisplayEvents>().Displaying = ctx => { ++displayingEventCount; };
_container.Resolve<TestDisplayEvents>().Displayed = ctx => { ++displayedEventCount; ctx.ChildContent = new HtmlString("[" + ctx.ChildContent.ToHtmlString() + "]"); };
var result = displayManager.Execute(CreateDisplayContext(shape));
Assert.That(displayingEventCount, Is.EqualTo(1));
Assert.That(displayedEventCount, Is.EqualTo(1));
Assert.That(result.ToString(), Is.EqualTo("[yarg]"));
}
[Test]
public void ShapeDescriptorDisplayingAndDisplayedAreCalled() {
var displayManager = _container.Resolve<IDisplayManager>();
var shape = new Shape {
Metadata = new ShapeMetadata {
Type = "Foo"
}
};
var descriptor = new ShapeDescriptor {
ShapeType = "Foo",
};
AddBinding(descriptor, "Foo", ctx => new HtmlString("yarg"));
AddShapeDescriptor(descriptor);
var displayingEventCount = 0;
var displayedEventCount = 0;
descriptor.Displaying = new Action<ShapeDisplayingContext>[] { ctx => { ++displayingEventCount; } };
descriptor.Displayed = new Action<ShapeDisplayedContext>[] { ctx => { ++displayedEventCount; ctx.ChildContent = new HtmlString("[" + ctx.ChildContent.ToHtmlString() + "]"); } };
var result = displayManager.Execute(CreateDisplayContext(shape));
Assert.That(displayingEventCount, Is.EqualTo(1));
Assert.That(displayedEventCount, Is.EqualTo(1));
Assert.That(result.ToString(), Is.EqualTo("[yarg]"));
}
[Test]
public void DisplayingEventFiresEarlyEnoughToAddAlternateShapeBindingNames() {
var displayManager = _container.Resolve<IDisplayManager>();
var shapeFoo = new Shape {
Metadata = new ShapeMetadata {
Type = "Foo"
}
};
var descriptorFoo = new ShapeDescriptor {
ShapeType = "Foo",
};
AddBinding(descriptorFoo, "Foo", ctx => new HtmlString("alpha"));
AddShapeDescriptor(descriptorFoo);
var descriptorBar = new ShapeDescriptor {
ShapeType = "Bar",
};
AddBinding(descriptorBar, "Bar", ctx => new HtmlString("beta"));
AddShapeDescriptor(descriptorBar);
var resultNormally = displayManager.Execute(CreateDisplayContext(shapeFoo));
descriptorFoo.Displaying = new Action<ShapeDisplayingContext>[] { ctx => ctx.ShapeMetadata.Alternates.Add("Bar") };
var resultWithOverride = displayManager.Execute(CreateDisplayContext(shapeFoo));
Assert.That(resultNormally.ToString(), Is.EqualTo("alpha"));
Assert.That(resultWithOverride.ToString(), Is.EqualTo("beta"));
}
}
}

View File

@@ -3,6 +3,7 @@ using System.Linq;
using Autofac;
using NUnit.Framework;
using Orchard.DisplayManagement.Descriptors;
using Orchard.DisplayManagement.Implementation;
using Orchard.Environment.Extensions.Models;
namespace Orchard.Tests.DisplayManagement.Descriptors {
@@ -12,7 +13,9 @@ namespace Orchard.Tests.DisplayManagement.Descriptors {
builder.RegisterType<DefaultShapeTableManager>().As<IShapeTableManager>();
builder.RegisterType<TestShapeProvider>().As<IShapeTableProvider>()
.WithMetadata("Feature", TestFeature());
.WithMetadata("Feature", TestFeature())
.As<TestShapeProvider>()
.InstancePerLifetimeScope();
}
static Feature TestFeature() {
@@ -29,8 +32,12 @@ namespace Orchard.Tests.DisplayManagement.Descriptors {
}
public class TestShapeProvider : IShapeTableProvider {
public void Discover(ShapeTableBuilder builder) {
public Action<ShapeTableBuilder> Discover = x => { };
void IShapeTableProvider.Discover(ShapeTableBuilder builder) {
builder.Describe("Hello");
Discover(builder);
}
}
@@ -48,5 +55,29 @@ namespace Orchard.Tests.DisplayManagement.Descriptors {
Assert.That(shapeTable1.Descriptors["Hello"], Is.Not.Null);
Assert.That(shapeTable2.Descriptors["Hello"], Is.Not.Null);
}
[Test]
public void CallbackAlterationsContributeToDescriptor() {
Action<ShapeCreatingContext> cb1 = x => { };
Action<ShapeCreatedContext> cb2 = x => { };
Action<ShapeDisplayingContext> cb3 = x => { };
Action<ShapeDisplayedContext> cb4 = x => { };
_container.Resolve<TestShapeProvider>().Discover =
builder => builder.Describe("Foo")
.OnCreating(cb1)
.OnCreated(cb2)
.OnDisplaying(cb3)
.OnDisplayed(cb4);
var manager = _container.Resolve<IShapeTableManager>();
var foo = manager.GetShapeTable(null).Descriptors["Foo"];
Assert.That(foo.Creating.Single(), Is.SameAs(cb1));
Assert.That(foo.Created.Single(), Is.SameAs(cb2));
Assert.That(foo.Displaying.Single(), Is.SameAs(cb3));
Assert.That(foo.Displayed.Single(), Is.SameAs(cb4));
}
}
}