Ejemplo n.º 1
0
 internal static BaseMock GetILocalRegistryInstance()
 {
     GenericMockFactory factory = new GenericMockFactory("ILocalRegistry", new Type[] { typeof(ILocalRegistry) });
     BaseMock mockObj = factory.GetInstance();
     string name = string.Format("{0}.{1}", typeof(ILocalRegistry).FullName, "CreateInstance");
     mockObj.AddMethodCallback(name, new EventHandler<CallbackArgs>(CreateInstanceCallBack));
     return mockObj;
 }
        internal static BaseMock GetInstance()
        {
            //Create a base mock
            GenericMockFactory factory = new GenericMockFactory("ILocalRegistry3", new Type[] { typeof(ILocalRegistry3) });
            BaseMock mockObj = factory.GetInstance();

            //Add method call back for GetLocalRegistryRoot
            string methodName = string.Format("{0}.{1}", typeof(ILocalRegistry3).FullName, "GetLocalRegistryRoot");
            mockObj.AddMethodCallback(methodName, new EventHandler<CallbackArgs>(GetLocalRegistryRootCallBack));

            return mockObj;
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Use to create an IOleServiceProvider with the basic services required by
        /// MS.VS.Shell.Package.SetSite() base implementation
        /// </summary>
        /// <returns></returns>
        public static OleServiceProvider CreateOleServiceProviderWithBasicServices()
        {
            // Create the service provider
            OleServiceProvider serviceProvider = new OleServiceProvider();

            // Add IProfferService
            // Create the type only once, then create as many instances as required.
            if (profferServiceFactory == null)
            {
                profferServiceFactory = new GenericMockFactory("MockProfferService", new Type[] { typeof(IProfferService) });
            }
            BaseMock mockObject = profferServiceFactory.GetInstance();

            mockObject.AddMethodCallback(string.Format(CultureInfo.InvariantCulture, "{0}.{1}", typeof(IProfferService).FullName, "ProfferService"),
                                         new EventHandler <CallbackArgs>(ProfferServiceCallback));
            serviceProvider.AddService(typeof(SProfferService), mockObject, false);

            // Add IUIHostLocale
            if (hostLocaleFactory == null)
            {
                hostLocaleFactory = new GenericMockFactory("MockUiHostLocale", new Type[] { typeof(IUIHostLocale), typeof(IUIHostLocale2) });
            }
            mockObject = hostLocaleFactory.GetInstance();
            // Set the return value to 0 (S_OK) and the out parameter to 1033 (enu).
            mockObject.AddMethodReturnValues(string.Format(CultureInfo.InvariantCulture,
                                                           "{0}.{1}",
                                                           typeof(IUIHostLocale).FullName,
                                                           "GetUILocale"),
                                             new object[] { 0, (uint)1033 });
            serviceProvider.AddService(typeof(SUIHostLocale), mockObject, false);

            // Add IVsResourceManager
            if (resourceManagerFactory == null)
            {
                resourceManagerFactory = new GenericMockFactory("MockResourceManager", new Type[] { typeof(IVsResourceManager) });
            }
            mockObject = resourceManagerFactory.GetInstance();
            mockObject.AddMethodReturnValues(string.Format(CultureInfo.InvariantCulture,
                                                           "{0}.{1}",
                                                           typeof(IVsResourceManager).FullName,
                                                           "LoadResourceString"),
                                             new object[] { 0, Guid.Empty, 0, null, "Mock Localized String" });
            serviceProvider.AddService(typeof(SVsResourceManager), mockObject, false);

            return(serviceProvider);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Given a mock object, this function will add to it a callback function to handle
        /// IConnectionPointContainer.FindConnectionPoint for all the event interfaces contained
        /// in the array passed as parameter.
        /// </summary>
        public static void AddConnectionPointsToContainer(BaseMock mockContainer, Type[] eventInterfaces)
        {
            // Check that the mock object implements IConnectionPointContainer.
            if (null == (mockContainer as IConnectionPointContainer))
            {
                throw new InvalidCastException("Parameter mockContainer does not implement IConnectionPointContainer.");
            }
            // Check if there is any interface in the array.
            if ((null == eventInterfaces) || (eventInterfaces.Length == 0))
            {
                throw new ArgumentNullException("eventIterfaces");
            }
            // Create the Dictionary that will store the connection points.
            Dictionary <Guid, IConnectionPoint> connectionPoints = new Dictionary <Guid, IConnectionPoint>();

            // Get the factory for the connection points.
            if (null == connectionPointFactory)
            {
                connectionPointFactory = new GenericMockFactory("MockLibraryConnectionPoint", new Type[] { typeof(IConnectionPoint) });
            }

            // Create a connection point for every type in the array.
            foreach (Type eventInterface in eventInterfaces)
            {
                BaseMock connectionMock = connectionPointFactory.GetInstance();
                // Set a return value for the Advise method so that the cookie will be not zero.
                connectionMock.AddMethodReturnValues(
                    string.Format(CultureInfo.InvariantCulture, "{0}.{1}", typeof(IConnectionPoint).FullName, "Advise"),
                    new object[] { null, (uint)1 });
                // Add this connection point to the dictionary.
                connectionPoints.Add(eventInterface.GUID, connectionMock as IConnectionPoint);
            }

            // Set the dictionary as member data for the container mock.
            mockContainer[connectionPointsCollection] = connectionPoints;

            // Set the callback for the FindConnectionPoint method.
            mockContainer.AddMethodCallback(
                string.Format(CultureInfo.InvariantCulture, "{0}.{1}", typeof(IConnectionPointContainer).FullName, "FindConnectionPoint"),
                new EventHandler <CallbackArgs>(FindConnectionPointCallback));
        }
Ejemplo n.º 5
0
        public void TestOutput() {
            callbackExecuted = false;
            // As first create a service provider.
            using(OleServiceProvider serviceProvider = OleServiceProvider.CreateOleServiceProviderWithBasicServices()) {
                // Create a mock object for the output window pane.
                GenericMockFactory mockWindowPaneFactory = new GenericMockFactory("MockOutputWindowPane", new Type[] { typeof(IVsOutputWindowPane) });
                BaseMock mockWindowPane = mockWindowPaneFactory.GetInstance();
                mockWindowPane.AddMethodCallback(string.Format("{0}.{1}", typeof(IVsOutputWindowPane).FullName, "OutputString"),
                                                 new EventHandler<CallbackArgs>(OutputWindowPaneCallback));

                // Now create the mock object for the output window.
                if(null == mockOutputWindowFactory) {
                    mockOutputWindowFactory = new GenericMockFactory("MockOutputWindow1", new Type[] { typeof(IVsOutputWindow) });
                }
                BaseMock mockOutputWindow = mockOutputWindowFactory.GetInstance();
                mockOutputWindow.AddMethodReturnValues(
                        string.Format("{0}.{1}", typeof(IVsOutputWindow).FullName, "GetPane"),
                        new object[] { 0, Guid.Empty, (IVsOutputWindowPane)mockWindowPane });

                // Add the output window to the services provided by the service provider.
                serviceProvider.AddService(typeof(SVsOutputWindow), mockOutputWindow, false);

                // Create an instance of the package and initialize it calling SetSite.
                SrcMLServicePackage package = new SrcMLServicePackage();
                int result = ((IVsPackage)package).SetSite(serviceProvider);
                Assert.IsTrue(Microsoft.VisualStudio.ErrorHandler.Succeeded(result), "SetSite failed.");

                // Now we can create an instance of the service
                SrcMLGlobalService service = new SrcMLGlobalService(package, extensionDirectory);

                service.GlobalServiceFunction();
                
                Assert.IsTrue(callbackExecuted, "OutputText not called.");
                ((IVsPackage)package).SetSite(null);
                ((IVsPackage)package).Close();
            }
        }
Ejemplo n.º 6
0
 private static BaseMock GetIVsTextLinesInstance()
 {
     GenericMockFactory factory = new GenericMockFactory("IVsTextLines", new Type[] { typeof(IVsTextLines), typeof(IObjectWithSite) });
     BaseMock mockObj = factory.GetInstance();
     return mockObj;
 }