Helper for unit tests requiring a mocked graphics device
This doesn't actually mock the graphics device, but creates a real graphics device on an invisible window. Tests have shown this method to be fast enough for usage in a unit test.
Наследование: IGraphicsDeviceService
    public void TestContentAccess() {
      using(XnaGame game = new XnaGame()) {
        MockedGraphicsDeviceService service = new MockedGraphicsDeviceService();
        using(service.CreateDevice()) {
          game.Services.AddService(typeof(IGraphicsDeviceService), service);

          string assetPath = writeBytesToTempFile(
            Resources.UnitTestResources.UnitTestEffect
          );
          try {
            game.Content.RootDirectory = Path.GetDirectoryName(assetPath);
            string assetName = Path.GetFileNameWithoutExtension(assetPath);
  
            SharedGameContentManager adapter = new SharedGameContentManager(game);
            
            Effect loadedFromGame = game.Content.Load<Effect>(assetName);
            Effect loadedFromShared = adapter.Load<Effect>(assetName);
            
            Assert.AreSame(loadedFromGame, loadedFromShared);
          }
          finally {
            File.Delete(assetPath);
          }
        } // using graphics device
      } // using game
    }
        public void TestGraphicsDeviceServiceEvents()
        {
            MockedGraphicsDeviceService             mock             = new MockedGraphicsDeviceService();
            Mock <IGraphicsDeviceServiceSubscriber> mockedSubscriber = mockSubscriber(mock);

            mockedSubscriber.Expects.One.Method(
                m => m.DeviceCreated(null, null)
                ).WithAnyArguments();

            using (IDisposable keeper = mock.CreateDevice()) {
                this.mockery.VerifyAllExpectationsHaveBeenMet();

                mockedSubscriber.Expects.One.Method(
                    m => m.DeviceResetting(null, null)
                    ).WithAnyArguments();
                mockedSubscriber.Expects.One.Method(
                    m => m.DeviceReset(null, null)
                    ).WithAnyArguments();

                mock.ResetDevice();
                this.mockery.VerifyAllExpectationsHaveBeenMet();

                mockedSubscriber.Expects.One.Method(
                    m => m.DeviceDisposing(null, null)
                    ).WithAnyArguments();
            }
            this.mockery.VerifyAllExpectationsHaveBeenMet();
        }
        public void TestNotSupportedExceptionForReferenceRasterizer()
        {
            MockedGraphicsDeviceService mock = new MockedGraphicsDeviceService(
                DeviceType.Reference
                );

            mock.DeviceCreated += delegate(object sender, EventArgs arguments) {
#if XNA_4
                throw new InvalidOperationException("Simulated error for unit testing");
#else
                throw new NotSupportedException("Simulated error for unit testing");
#endif
            };

            Console.Error.WriteLine(
                "The next line should contain an error message indicating that the reference " +
                "rasterizer could not be created"
                );
#if XNA_4
            Assert.Throws <InvalidOperationException>(
#else
            Assert.Throws <NotSupportedException>(
#endif
                delegate() {
                mock.CreateDevice();
                mock.DestroyDevice();
            }
                );
        }
Пример #4
0
    public void TestRenderSkybox() {
      MockedGraphicsDeviceService mockGraphicsDeviceService =
        new MockedGraphicsDeviceService(DeviceType.Reference);

      using(IDisposable keeper = mockGraphicsDeviceService.CreateDevice()) {
        using(
          BasicEffect effect = new BasicEffect(
#if XNA_4
            mockGraphicsDeviceService.GraphicsDevice
#else
            mockGraphicsDeviceService.GraphicsDevice, new EffectPool()
#endif
          )
        ) {
          using(
            SkyboxCube skyboxCube = new SkyboxCube(
              mockGraphicsDeviceService.GraphicsDevice
            )
          ) {
            skyboxCube.AssignVertexBuffer();
#if XNA_4
            EffectTechnique technique = effect.CurrentTechnique;
            for(int pass = 0; pass < technique.Passes.Count; ++pass) {
              technique.Passes[pass].Apply();

              skyboxCube.DrawNorthernFace();
              skyboxCube.DrawEasternFace();
              skyboxCube.DrawSouthernFace();
              skyboxCube.DrawWesternFace();
              skyboxCube.DrawUpperFace();
              skyboxCube.DrawLowerFace();
            }
#else
            effect.Begin();
            try {
              EffectTechnique technique = effect.CurrentTechnique;
              for(int pass = 0; pass < technique.Passes.Count; ++pass) {
                technique.Passes[pass].Begin();
                try {
                  skyboxCube.DrawNorthernFace();
                  skyboxCube.DrawEasternFace();
                  skyboxCube.DrawSouthernFace();
                  skyboxCube.DrawWesternFace();
                  skyboxCube.DrawUpperFace();
                  skyboxCube.DrawLowerFace();
                }
                finally {
                  technique.Passes[pass].End();
                }
              }
            }
            finally {
              effect.End();
            }
#endif
          }
        }
      }
    }
 public void TestConstructor() {
   MockedGraphicsDeviceService service = new MockedGraphicsDeviceService();
   using(IDisposable keeper = service.CreateDevice()) {
     using(IDisposable context = new BasicEffectDrawContext(service.GraphicsDevice)) {
       Assert.IsNotNull(context);
     }
   }
 }
        public void TestGraphicsDeviceCreation()
        {
            MockedGraphicsDeviceService mock = new MockedGraphicsDeviceService();

            using (IDisposable keeper = mock.CreateDevice()) {
                Assert.IsNotNull(mock.GraphicsDevice);
            }
        }
        public void TestRedundantDestroyInvocation()
        {
            MockedGraphicsDeviceService mock = new MockedGraphicsDeviceService();

            using (IDisposable keeper = mock.CreateDevice()) {
                mock.DestroyDevice();
            } // should not cause an exception
        }
Пример #8
0
    public void TestConstructor() {
      MockedGraphicsDeviceService mockGraphicsDeviceService =
        new MockedGraphicsDeviceService();

      using(IDisposable keeper = mockGraphicsDeviceService.CreateDevice()) {
        SkyboxCube theSkybox = new SkyboxCube(mockGraphicsDeviceService.GraphicsDevice);
        theSkybox.Dispose();
      }
    }
    public void TestServiceProvider() {
      MockedGraphicsDeviceService mock = new MockedGraphicsDeviceService();

      IServiceProvider serviceProvider = mock.ServiceProvider;
      Assert.IsNotNull(serviceProvider);

      IGraphicsDeviceService service = (IGraphicsDeviceService)serviceProvider.GetService(
        typeof(IGraphicsDeviceService)
      );
      Assert.AreSame(mock, service);
    }
Пример #10
0
    public void TestSimpleConstructor() {
      MockedGraphicsDeviceService mockGraphicsDeviceService =
        new MockedGraphicsDeviceService();

      using(IDisposable keeper = mockGraphicsDeviceService.CreateDevice()) {
        WaterGrid theGrid = new WaterGrid(
          mockGraphicsDeviceService.GraphicsDevice,
          new Vector2(-10.0f, -10.0f), new Vector2(10.0f, 10.0f)
        );
        theGrid.Dispose();
      }
    }
 public void TestEqualsWithDifferentObject() {
   MockedGraphicsDeviceService service = new MockedGraphicsDeviceService();
   using(IDisposable keeper = service.CreateDevice()) {
     using(
       BasicEffectDrawContext test1 = new BasicEffectDrawContext(service.GraphicsDevice)
     ) {
       using(
         BasicEffectDrawContext test2 = new BasicEffectDrawContext(service.GraphicsDevice)
       ) {
         Assert.IsTrue(test1.Equals((object)test2));
       }
     }
   }
 }
Пример #12
0
    public void TestThrowOnInvalidSegmentCountX() {
      MockedGraphicsDeviceService mockGraphicsDeviceService =
        new MockedGraphicsDeviceService();

      using(IDisposable keeper = mockGraphicsDeviceService.CreateDevice()) {
        Assert.Throws<ArgumentException>(
          delegate() {
            new WaterGrid(
              mockGraphicsDeviceService.GraphicsDevice, Vector2.Zero, Vector2.Zero, 0, 20
            );
          }
        );
      }
    }
        public void TestServiceProvider()
        {
            MockedGraphicsDeviceService mock = new MockedGraphicsDeviceService();

            IServiceProvider serviceProvider = mock.ServiceProvider;

            Assert.IsNotNull(serviceProvider);

            IGraphicsDeviceService service = (IGraphicsDeviceService)serviceProvider.GetService(
                typeof(IGraphicsDeviceService)
                );

            Assert.AreSame(mock, service);
        }
        public void TestAutomaticGraphicsDeviceDestruction()
        {
            MockedGraphicsDeviceService mock = new MockedGraphicsDeviceService();

            try {
                using (IDisposable keeper = mock.CreateDevice()) {
                    Assert.IsNotNull(mock.GraphicsDevice);
                    throw new ArithmeticException("Test exception");
                }
            }
            catch (ArithmeticException) {
                // Munch
            }

            Assert.IsNull(mock.GraphicsDevice);
        }
        public void TestGraphicsDeviceServiceEvents()
        {
            MockedGraphicsDeviceService      mock             = new MockedGraphicsDeviceService();
            IGraphicsDeviceServiceSubscriber mockedSubscriber = mockSubscriber(mock);

            Expect.Once.On(mockedSubscriber).Method("DeviceCreated").WithAnyArguments();
            using (IDisposable keeper = mock.CreateDevice()) {
                this.mockery.VerifyAllExpectationsHaveBeenMet();

                Expect.Once.On(mockedSubscriber).Method("DeviceResetting").WithAnyArguments();
                Expect.Once.On(mockedSubscriber).Method("DeviceReset").WithAnyArguments();
                mock.ResetDevice();
                this.mockery.VerifyAllExpectationsHaveBeenMet();

                Expect.Once.On(mockedSubscriber).Method("DeviceDisposing").WithAnyArguments();
            }
            this.mockery.VerifyAllExpectationsHaveBeenMet();
        }
        public void TestExceptionDuringDeviceCreation()
        {
            MockedGraphicsDeviceService mock = new MockedGraphicsDeviceService();

            IGraphicsDeviceServiceSubscriber mockedSubscriber = mockSubscriber(mock);

            Expect.Once.On(mockedSubscriber).Method("DeviceCreated").WithAnyArguments();

            mock.DeviceCreated += (DeviceEventHandler) delegate(object sender, EventArgs arguments) {
                Assert.IsNotNull(mock.GraphicsDevice);
                throw new ArithmeticException("Test exception");
            };
            try {
                mock.CreateDevice();
            }
            catch (ArithmeticException) {
                // Munch
            }

            Assert.IsNull(mock.GraphicsDevice);
        }
    public void TestBeginEnd() {
      MockedGraphicsDeviceService service = new MockedGraphicsDeviceService();
      using(IDisposable keeper = service.CreateDevice()) {
#if XNA_4
        using(BasicEffect effect = new BasicEffect(service.GraphicsDevice)) {
          TestEffectDrawContext test = new TestEffectDrawContext(effect);

          for(int pass = 0; pass < test.Passes; ++pass) {
            test.Apply(pass);
          }
        }
#else
        using(EffectPool pool = new EffectPool()) {
          using(BasicEffect effect = new BasicEffect(service.GraphicsDevice, pool)) {
            TestEffectDrawContext test = new TestEffectDrawContext(effect);

            test.Begin();
            try {
              for(int pass = 0; pass < test.Passes; ++pass) {
                test.BeginPass(pass);
                test.EndPass();
              }
            }
            finally {
              test.End();
            }
          }
        }
#endif
      }
    }
Пример #18
0
 /// <summary>Initializes a new graphics device keeper</summary>
 /// <param name="dummyService">
 ///   Dummy graphics device service for whose graphics device the keeper
 ///   will be responsible
 /// </param>
 public GraphicsDeviceKeeper(MockedGraphicsDeviceService dummyService)
 {
     this.dummyService = dummyService;
 }
    public void TestDummyGraphicsDeviceServiceEvents() {
      MockedGraphicsDeviceService originalService = new MockedGraphicsDeviceService();
      originalService.CreateDevice();

      bool deviceExists = true;
      try {
        IGraphicsDeviceService dummyService;
        dummyService = GraphicsDeviceServiceHelper.MakeDummyGraphicsDeviceService(
          originalService.GraphicsDevice
        );
        IGraphicsDeviceServiceSubscriber mockedSubscriber = mockSubscriber(dummyService);
        try {
          Expect.Once.On(mockedSubscriber).Method("DeviceResetting").WithAnyArguments();
          Expect.Once.On(mockedSubscriber).Method("DeviceReset").WithAnyArguments();
          originalService.ResetDevice();
          this.mockery.VerifyAllExpectationsHaveBeenMet();

          Expect.Once.On(mockedSubscriber).Method("DeviceDisposing").WithAnyArguments();
          deviceExists = false;
          originalService.DestroyDevice();
          this.mockery.VerifyAllExpectationsHaveBeenMet();
        }
        finally {
          unmockSubscriber(dummyService, mockedSubscriber);
        }
      }
      finally {
        if(deviceExists) {
          originalService.DestroyDevice();
        }
      }
    }
    public void TestPrivateServiceProvider() {
      MockedGraphicsDeviceService originalService = new MockedGraphicsDeviceService();
      IServiceProvider provider = GraphicsDeviceServiceHelper.MakePrivateServiceProvider(
        originalService
      );

      IGraphicsDeviceService service = (IGraphicsDeviceService)provider.GetService(
        typeof(IGraphicsDeviceService)
      );

      Assert.AreSame(originalService, service);
    }
Пример #21
0
    public void TestRenderingProperties() {
      MockedGraphicsDeviceService mockGraphicsDeviceService =
        new MockedGraphicsDeviceService();

      using(IDisposable keeper = mockGraphicsDeviceService.CreateDevice()) {
        using(
          WaterGrid theGrid = new WaterGrid(
            mockGraphicsDeviceService.GraphicsDevice,
            new Vector2(-10.0f, -10.0f), new Vector2(10.0f, 10.0f),
            20, 20
          )
        ) {
#if !XNA_4
          Assert.IsNotNull(theGrid.VertexDeclaration);
#endif
          Assert.IsNotNull(theGrid.VertexBuffer);
          Assert.IsNotNull(theGrid.IndexBuffer);
        }
      }
    }
        public void TestExceptionDuringDeviceCreation()
        {
            MockedGraphicsDeviceService mock = new MockedGraphicsDeviceService();

              Mock<IGraphicsDeviceServiceSubscriber> mockedSubscriber = mockSubscriber(mock);

              mockedSubscriber.Expects.One.Method(
            m => m.DeviceCreated(null, null)
              ).WithAnyArguments();

              mock.DeviceCreated += (DeviceEventHandler)delegate(object sender, EventArgs arguments) {
            Assert.IsNotNull(mock.GraphicsDevice);
            throw new ArithmeticException("Test exception");
              };
              try {
            mock.CreateDevice();
              }
              catch(ArithmeticException) {
            // Munch
              }

              Assert.IsNull(mock.GraphicsDevice);
        }
    public void TestEqualsWithIncpmpatibleDrawContext() {
      MockedGraphicsDeviceService service = new MockedGraphicsDeviceService();
      using(IDisposable keeper = service.CreateDevice()) {
#if XNA_4
        using(BasicEffect effect = new BasicEffect(service.GraphicsDevice)) {
          TestEffectDrawContext test1 = new TestEffectDrawContext(effect);
          TestDrawContext test2 = new TestDrawContext();
          Assert.IsFalse(test1.Equals((object)test2));
        }
#else
        using(EffectPool pool = new EffectPool()) {
          using(BasicEffect effect = new BasicEffect(service.GraphicsDevice, pool)) {
            TestEffectDrawContext test1 = new TestEffectDrawContext(effect);
            TestDrawContext test2 = new TestDrawContext();
            Assert.IsFalse(test1.Equals((object)test2));
          }
        }
#endif
      }
    }
    public void TestGraphicsDeviceServiceEvents() {
      MockedGraphicsDeviceService mock = new MockedGraphicsDeviceService();
      IGraphicsDeviceServiceSubscriber mockedSubscriber = mockSubscriber(mock);

      Expect.Once.On(mockedSubscriber).Method("DeviceCreated").WithAnyArguments();
      using(IDisposable keeper = mock.CreateDevice()) {
        this.mockery.VerifyAllExpectationsHaveBeenMet();

        Expect.Once.On(mockedSubscriber).Method("DeviceResetting").WithAnyArguments();
        Expect.Once.On(mockedSubscriber).Method("DeviceReset").WithAnyArguments();
        mock.ResetDevice();
        this.mockery.VerifyAllExpectationsHaveBeenMet();

        Expect.Once.On(mockedSubscriber).Method("DeviceDisposing").WithAnyArguments();
      }
      this.mockery.VerifyAllExpectationsHaveBeenMet();
    }
        public void TestRedundantDestroyInvocation()
        {
            MockedGraphicsDeviceService mock = new MockedGraphicsDeviceService();

              using(IDisposable keeper = mock.CreateDevice()) {
            mock.DestroyDevice();
              } // should not cause an exception
        }
 public void TestEffectRetrieval() {
   MockedGraphicsDeviceService service = new MockedGraphicsDeviceService();
   using(IDisposable keeper = service.CreateDevice()) {
     using(
       BasicEffectDrawContext test = new BasicEffectDrawContext(service.GraphicsDevice)
     ) {
       Assert.AreSame(test.Effect, test.BasicEffect);
     }
   }
 }
        public void TestNotSupportedExceptionForReferenceRasterizer()
        {
            MockedGraphicsDeviceService mock = new MockedGraphicsDeviceService(
            DeviceType.Reference
              );
              mock.DeviceCreated += delegate(object sender, EventArgs arguments) {
            throw new InvalidOperationException("Simulated error for unit testing");
              };

              Console.Error.WriteLine(
            "The next line should contain an error message indicating that the reference " +
            "rasterizer could not be created"
              );
              Assert.Throws<InvalidOperationException>(
            delegate() {
              mock.CreateDevice();
              mock.DestroyDevice();
            }
              );
        }
Пример #28
0
    public void TestGraphicsDeviceReset() {
      MockedGraphicsDeviceService service = new MockedGraphicsDeviceService();
      using(IDisposable keeper = service.CreateDevice()) {
        using(TestDrawable drawable = new TestDrawable(service)) {
          Assert.AreEqual(0, drawable.LoadContentFalseCount);
          Assert.AreEqual(0, drawable.UnloadContentFalseCount);

          service.ResetDevice();

          Assert.AreEqual(1, drawable.LoadContentFalseCount);
          Assert.AreEqual(1, drawable.UnloadContentFalseCount);
        }
      }
    }
    public void TestEqualsWithIncompatibleEffect() {
      MockedGraphicsDeviceService service = new MockedGraphicsDeviceService();
      using(IDisposable keeper = service.CreateDevice()) {
        using(
          BasicEffectDrawContext test1 = new BasicEffectDrawContext(service.GraphicsDevice)
        ) {
          TestEffectDrawContext test2 = new TestEffectDrawContext(null);

          Assert.IsFalse(test1.Equals((object)test2));
        }
      }
    }
Пример #30
0
 /// <summary>Immediately releases all resources owned by the instancer</summary>
 public void Dispose()
 {
     if(this.dummyService != null) {
       this.dummyService.DestroyDevice();
       this.dummyService = null;
     }
 }
    public void TestEffectRetrieval() {
      MockedGraphicsDeviceService service = new MockedGraphicsDeviceService();
      using(IDisposable keeper = service.CreateDevice()) {
#if XNA_4
        using(BasicEffect effect = new BasicEffect(service.GraphicsDevice)) {
          TestEffectDrawContext test = new TestEffectDrawContext(effect);

          Assert.AreSame(effect, test.Effect);
        }
#else
        using(EffectPool pool = new EffectPool()) {
          using(BasicEffect effect = new BasicEffect(service.GraphicsDevice, pool)) {
            TestEffectDrawContext test = new TestEffectDrawContext(effect);

            Assert.AreSame(effect, test.Effect);
          }
        }
#endif
      }
    }
    public void TestConstructor() {
      MockedGraphicsDeviceService service = new MockedGraphicsDeviceService();
      using(IDisposable keeper = service.CreateDevice()) {
#if XNA_4
        using(BasicEffect effect = new BasicEffect(service.GraphicsDevice)) {
          TestEffectDrawContext test = new TestEffectDrawContext(effect);
          Assert.GreaterOrEqual(test.Passes, 1);
        }
#else
        using(EffectPool pool = new EffectPool()) {
          using(BasicEffect effect = new BasicEffect(service.GraphicsDevice, pool)) {
            TestEffectDrawContext test = new TestEffectDrawContext(effect);
            Assert.GreaterOrEqual(test.Passes, 1);
          }
        }
#endif
      }
    }
Пример #33
0
 /// <summary>Initializes a new graphics device keeper</summary>
 /// <param name="dummyService">
 ///   Dummy graphics device service for whose graphics device the keeper
 ///   will be responsible
 /// </param>
 public GraphicsDeviceKeeper(MockedGraphicsDeviceService dummyService)
 {
     this.dummyService = dummyService;
 }
Пример #34
0
    public void TestStatisticalProperties() {
      MockedGraphicsDeviceService mockGraphicsDeviceService =
        new MockedGraphicsDeviceService();

      using(IDisposable keeper = mockGraphicsDeviceService.CreateDevice()) {
        using(
          WaterGrid theGrid = new WaterGrid(
            mockGraphicsDeviceService.GraphicsDevice,
            new Vector2(-10.0f, -10.0f), new Vector2(10.0f, 10.0f),
            4, 4
          )
        ) {
          Assert.AreEqual(PrimitiveType.TriangleStrip, theGrid.PrimitiveType);
          Assert.AreEqual(25, theGrid.VertexCount); // 4x4 segments = 5x5 vertices
          Assert.AreEqual(37, theGrid.IndexCount); // pick a pen & paper and check it...
          Assert.AreEqual(35, theGrid.PrimitiveCount); // 8 per row, 3 degenerate polys
        }
      }
    }
 public void TestDummyGraphicsDeviceService() {
   MockedGraphicsDeviceService originalService = new MockedGraphicsDeviceService();
   using(IDisposable keeper = originalService.CreateDevice()) {
     IGraphicsDeviceService dummyService;
     dummyService = GraphicsDeviceServiceHelper.MakeDummyGraphicsDeviceService(
       originalService.GraphicsDevice
     );
     try {
       Assert.AreSame(originalService.GraphicsDevice, dummyService.GraphicsDevice);
     }
     finally {
       IDisposable disposable = dummyService as IDisposable;
       if(disposable != null) {
         disposable.Dispose();
       }
     }
   }
 }
        public void TestGraphicsDeviceCreation()
        {
            MockedGraphicsDeviceService mock = new MockedGraphicsDeviceService();

              using(IDisposable keeper = mock.CreateDevice()) {
            Assert.IsNotNull(mock.GraphicsDevice);
              }
        }
        public void TestAutomaticGraphicsDeviceDestruction()
        {
            MockedGraphicsDeviceService mock = new MockedGraphicsDeviceService();

              try {
            using(IDisposable keeper = mock.CreateDevice()) {
              Assert.IsNotNull(mock.GraphicsDevice);
              throw new ArithmeticException("Test exception");
            }
              }
              catch(ArithmeticException) {
            // Munch
              }

              Assert.IsNull(mock.GraphicsDevice);
        }
        public void TestGraphicsDeviceServiceEvents()
        {
            MockedGraphicsDeviceService mock = new MockedGraphicsDeviceService();
              Mock<IGraphicsDeviceServiceSubscriber> mockedSubscriber = mockSubscriber(mock);

              mockedSubscriber.Expects.One.Method(
            m => m.DeviceCreated(null, null)
              ).WithAnyArguments();

              using(IDisposable keeper = mock.CreateDevice()) {
            this.mockery.VerifyAllExpectationsHaveBeenMet();

            mockedSubscriber.Expects.One.Method(
              m => m.DeviceResetting(null, null)
            ).WithAnyArguments();
            mockedSubscriber.Expects.One.Method(
              m => m.DeviceReset(null, null)
            ).WithAnyArguments();

            mock.ResetDevice();
            this.mockery.VerifyAllExpectationsHaveBeenMet();

            mockedSubscriber.Expects.One.Method(
              m => m.DeviceDisposing(null, null)
            ).WithAnyArguments();
              }
              this.mockery.VerifyAllExpectationsHaveBeenMet();
        }
Пример #39
0
 public void TestDraw() {
   MockedGraphicsDeviceService service = new MockedGraphicsDeviceService();
   using(Drawable drawable = new TestDrawable(service)) {
     drawable.Draw(new GameTime());
   }
 }