public async Task GetContent(Type type, bool found)
        {
            var attr1 = new ExportDocumentControllerExtensionAttribute {
                FileExtension = ".test"
            };

            try {
                ProducerExtension.ResetCounters();
                IdeServices.DocumentControllerService.RegisterControllerExtension(attr1, typeof(ProducerExtension));

                using (var controller = new TestControllerWithExtension()) {
                    await controller.Initialize(new FileDescriptor ("foo.test", null, null));

                    if (found)
                    {
                        Assert.IsNotNull(controller.GetContent(type));
                    }
                    else
                    {
                        Assert.IsNull(controller.GetContent(type));
                    }
                }
            } finally {
                IdeServices.DocumentControllerService.UnregisterControllerExtension(attr1);
            }
        }
        public async Task OwnerChangedNotification()
        {
            var attr1 = new ExportDocumentControllerExtensionAttribute {
                FileExtension = ".test"
            };

            try {
                ProducerExtension.ResetCounters();
                IdeServices.DocumentControllerService.RegisterControllerExtension(attr1, typeof(ProducerExtension));

                using (var controller = new TestControllerWithExtension()) {
                    var owner = new MyWorkspaceObject();
                    await controller.Initialize(new FileDescriptor ("foo.test", null, owner));

                    var ext = controller.GetContent <ProducerExtension> ();
                    Assert.IsNotNull(ext);
                    Assert.AreSame(owner, ext.KnownOwner);

                    var owner2 = new MyWorkspaceObject();
                    controller.Owner = owner2;
                    Assert.AreSame(owner2, ext.KnownOwner);
                }
            } finally {
                IdeServices.DocumentControllerService.UnregisterControllerExtension(attr1);
            }
        }
        public async Task OwnerActivationDeactivation()
        {
            var attr1 = new ExportDocumentControllerExtensionAttribute {
                FileExtension = ".test"
            };

            try {
                ProducerExtension.ResetCounters();
                IdeServices.DocumentControllerService.RegisterControllerExtension(attr1, typeof(OwnerConditionedExtension));

                using (var controller = new TestControllerWithExtension()) {
                    await controller.Initialize(new FileDescriptor ("foo.test", null, new MyWorkspaceObject()));

                    Assert.IsNotNull(controller.GetContent <OwnerConditionedExtension> ());
                    Assert.AreEqual(1, OwnerConditionedExtension.LiveExtensions.Count);

                    controller.Owner = null;

                    Assert.IsNull(controller.GetContent <OwnerConditionedExtension> ());
                    Assert.AreEqual(0, OwnerConditionedExtension.LiveExtensions.Count);

                    controller.Owner = new MyWorkspaceObject();

                    Assert.IsNotNull(controller.GetContent <OwnerConditionedExtension> ());
                    Assert.AreEqual(1, OwnerConditionedExtension.LiveExtensions.Count);
                }
            } finally {
                IdeServices.DocumentControllerService.UnregisterControllerExtension(attr1);
            }
        }
        public async Task FileExtensionActivationDeactivation()
        {
            var attr1 = new ExportDocumentControllerExtensionAttribute {
                FileExtension = ".test"
            };

            try {
                ProducerExtension.ResetCounters();
                IdeServices.DocumentControllerService.RegisterControllerExtension(attr1, typeof(ProducerExtension));

                using (var controller = new TestControllerWithExtension()) {
                    await controller.Initialize(new FileDescriptor ("foo.bar", null, null));

                    Assert.AreEqual(0, ProducerExtension.InstancesCreated);

                    controller.FilePath = "foo.test";
                    Assert.AreEqual(1, ProducerExtension.InstancesCreated);
                    Assert.AreEqual(0, ProducerExtension.InstancesDisposed);
                    Assert.IsNotNull(controller.GetContent <ProducerExtension> ());

                    controller.FilePath = "foo.var";
                    Assert.AreEqual(1, ProducerExtension.InstancesCreated);
                    Assert.AreEqual(1, ProducerExtension.InstancesDisposed);
                    Assert.IsNull(controller.GetContent <ProducerExtension> ());
                }
            } finally {
                IdeServices.DocumentControllerService.UnregisterControllerExtension(attr1);
            }
        }
        public async Task ExtensionDependingOnExtension()
        {
            var attr1 = new ExportDocumentControllerExtensionAttribute {
                FileExtension = ".test"
            };
            var attr2 = new ExportDocumentControllerExtensionAttribute {
                FileExtension = "*"
            };

            try {
                ProducerExtension.ResetCounters();
                IdeServices.DocumentControllerService.RegisterControllerExtension(attr1, typeof(ProducerExtension));
                IdeServices.DocumentControllerService.RegisterControllerExtension(attr2, typeof(ConsumerExtension));

                using (var controller = new TestControllerWithExtension()) {
                    await controller.Initialize(new FileDescriptor ("foo.test", null, null));

                    Assert.AreEqual(1, ProducerExtension.InstancesCreated);
                    Assert.AreEqual(0, ProducerExtension.InstancesDisposed);

                    Assert.AreEqual(2, ConsumerExtension.InstancesCreated);
                    Assert.AreEqual(1, ConsumerExtension.InstancesDisposed);

                    Assert.IsNotNull(controller.GetContent <ProducerExtension> ());
                    Assert.IsNotNull(controller.GetContent <ConsumerExtension> ());

                    // If Producer extension is removed, consumer should also be removed

                    controller.FilePath = "foo.txt";

                    Assert.IsNull(controller.GetContent <ProducerExtension> ());
                    Assert.IsNull(controller.GetContent <ConsumerExtension> ());
                    Assert.AreEqual(1, ProducerExtension.InstancesDisposed);
                    Assert.AreEqual(0, ConsumerExtension.LiveExtensions.Count);
                }

                Assert.AreEqual(0, ProducerExtension.LiveExtensions.Count);
                Assert.AreEqual(0, ConsumerExtension.LiveExtensions.Count);
            } finally {
                IdeServices.DocumentControllerService.UnregisterControllerExtension(attr1);
                IdeServices.DocumentControllerService.UnregisterControllerExtension(attr2);
            }
        }
        public async Task FileAttributeFilters(ExportDocumentControllerExtensionAttribute attribute, bool matches)
        {
            try {
                ProducerExtension.ResetCounters();
                IdeServices.DocumentControllerService.RegisterControllerExtension(attribute, typeof(ProducerExtension));

                using (var controller = new TestControllerWithExtension()) {
                    await controller.Initialize(new FileDescriptor ("foo.test", "application/test", null));

                    var ext = controller.GetContent <ProducerExtension> ();
                    if (matches)
                    {
                        Assert.IsNotNull(ext);
                    }
                    else
                    {
                        Assert.IsNull(ext);
                    }
                }
            } finally {
                IdeServices.DocumentControllerService.UnregisterControllerExtension(attribute);
            }
        }
        public async Task GetContents(Type type, int count)
        {
            var attr1 = new ExportDocumentControllerExtensionAttribute {
                FileExtension = ".test"
            };
            var attr2 = new ExportDocumentControllerExtensionAttribute {
                FileExtension = "*"
            };

            try {
                ProducerExtension.ResetCounters();
                IdeServices.DocumentControllerService.RegisterControllerExtension(attr1, typeof(ProducerExtension));
                IdeServices.DocumentControllerService.RegisterControllerExtension(attr2, typeof(ProducerExtension));

                using (var controller = new TestControllerWithExtension()) {
                    await controller.Initialize(new FileDescriptor ("foo.test", null, null));

                    Assert.AreEqual(count, controller.GetContents(type).Count());
                }
            } finally {
                IdeServices.DocumentControllerService.UnregisterControllerExtension(attr1);
                IdeServices.DocumentControllerService.UnregisterControllerExtension(attr2);
            }
        }
        public async Task ProjectReloadCapabilityOverride()
        {
            var attr1 = new ExportDocumentControllerExtensionAttribute {
                FileExtension = ".full, .full-unsaved-default, .full-unsaved-default-none"
            };
            var attr2 = new ExportDocumentControllerExtensionAttribute {
                FileExtension = ".unsaved, .full-unsaved-default, .full-unsaved-default-none"
            };
            var attr3 = new ExportDocumentControllerExtensionAttribute {
                FileExtension = ".none, .full-unsaved-default-none"
            };
            var attr4 = new ExportDocumentControllerExtensionAttribute {
                FileExtension = ".default, .full-unsaved-default"
            };

            try {
                ProducerExtension.ResetCounters();
                IdeServices.DocumentControllerService.RegisterControllerExtension(attr1, typeof(FullReloadExtension));
                IdeServices.DocumentControllerService.RegisterControllerExtension(attr2, typeof(UnsavedDataReloadExtension));
                IdeServices.DocumentControllerService.RegisterControllerExtension(attr3, typeof(NoReloadExtension));
                IdeServices.DocumentControllerService.RegisterControllerExtension(attr4, typeof(ProducerExtension));

                using (var controller = new TestControllerWithExtension()) {
                    await controller.Initialize(new FileDescriptor ("foo.noext", null, null));

                    // No extensions

                    controller.SetReloadCapability(ProjectReloadCapability.None);
                    Assert.AreEqual(ProjectReloadCapability.None, controller.ProjectReloadCapability);

                    controller.SetReloadCapability(ProjectReloadCapability.Full);
                    Assert.AreEqual(ProjectReloadCapability.Full, controller.ProjectReloadCapability);

                    controller.SetReloadCapability(ProjectReloadCapability.UnsavedData);
                    Assert.AreEqual(ProjectReloadCapability.UnsavedData, controller.ProjectReloadCapability);

                    // Extension with ProjectReloadCapability.Full

                    controller.FilePath = "foo.full";

                    controller.SetReloadCapability(ProjectReloadCapability.None);
                    Assert.AreEqual(ProjectReloadCapability.None, controller.ProjectReloadCapability);

                    controller.SetReloadCapability(ProjectReloadCapability.Full);
                    Assert.AreEqual(ProjectReloadCapability.Full, controller.ProjectReloadCapability);

                    controller.SetReloadCapability(ProjectReloadCapability.UnsavedData);
                    Assert.AreEqual(ProjectReloadCapability.UnsavedData, controller.ProjectReloadCapability);

                    // Extension with ProjectReloadCapability.UnsavedData

                    controller.FilePath = "foo.unsaved";

                    controller.SetReloadCapability(ProjectReloadCapability.None);
                    Assert.AreEqual(ProjectReloadCapability.None, controller.ProjectReloadCapability);

                    controller.SetReloadCapability(ProjectReloadCapability.Full);
                    Assert.AreEqual(ProjectReloadCapability.UnsavedData, controller.ProjectReloadCapability);

                    controller.SetReloadCapability(ProjectReloadCapability.UnsavedData);
                    Assert.AreEqual(ProjectReloadCapability.UnsavedData, controller.ProjectReloadCapability);

                    // Extension with ProjectReloadCapability.None

                    controller.FilePath = "foo.none";

                    controller.SetReloadCapability(ProjectReloadCapability.None);
                    Assert.AreEqual(ProjectReloadCapability.None, controller.ProjectReloadCapability);

                    controller.SetReloadCapability(ProjectReloadCapability.Full);
                    Assert.AreEqual(ProjectReloadCapability.None, controller.ProjectReloadCapability);

                    controller.SetReloadCapability(ProjectReloadCapability.UnsavedData);
                    Assert.AreEqual(ProjectReloadCapability.None, controller.ProjectReloadCapability);

                    // Extension with default

                    controller.FilePath = "foo.default";

                    controller.SetReloadCapability(ProjectReloadCapability.None);
                    Assert.AreEqual(ProjectReloadCapability.None, controller.ProjectReloadCapability);

                    controller.SetReloadCapability(ProjectReloadCapability.Full);
                    Assert.AreEqual(ProjectReloadCapability.Full, controller.ProjectReloadCapability);

                    controller.SetReloadCapability(ProjectReloadCapability.UnsavedData);
                    Assert.AreEqual(ProjectReloadCapability.UnsavedData, controller.ProjectReloadCapability);

                    // Extensions with Full, UnsavedData and default

                    controller.FilePath = "foo.full-unsaved-default";

                    controller.SetReloadCapability(ProjectReloadCapability.None);
                    Assert.AreEqual(ProjectReloadCapability.None, controller.ProjectReloadCapability);

                    controller.SetReloadCapability(ProjectReloadCapability.Full);
                    Assert.AreEqual(ProjectReloadCapability.UnsavedData, controller.ProjectReloadCapability);

                    controller.SetReloadCapability(ProjectReloadCapability.UnsavedData);
                    Assert.AreEqual(ProjectReloadCapability.UnsavedData, controller.ProjectReloadCapability);

                    // Extensions with Full, UnsavedData, Default and none

                    controller.FilePath = "foo.full-unsaved-default-none";

                    controller.SetReloadCapability(ProjectReloadCapability.None);
                    Assert.AreEqual(ProjectReloadCapability.None, controller.ProjectReloadCapability);

                    controller.SetReloadCapability(ProjectReloadCapability.Full);
                    Assert.AreEqual(ProjectReloadCapability.None, controller.ProjectReloadCapability);

                    controller.SetReloadCapability(ProjectReloadCapability.UnsavedData);
                    Assert.AreEqual(ProjectReloadCapability.None, controller.ProjectReloadCapability);
                }
            } finally {
                IdeServices.DocumentControllerService.UnregisterControllerExtension(attr1);
                IdeServices.DocumentControllerService.UnregisterControllerExtension(attr2);
                IdeServices.DocumentControllerService.UnregisterControllerExtension(attr3);
                IdeServices.DocumentControllerService.UnregisterControllerExtension(attr4);
            }
        }