import { assert , expect } from "chai" ; import { Id64 } from "@itwin/core-bentley" ; import { BackgroundMapProps , BackgroundMapSettings , ColorDef } from "@itwin/core-common" ; import { IModelConnection , Pixel , SnapshotConnection } from "@itwin/core-frontend" ; import { TestUtility } from "../TestUtility" ; import { testOnScreenViewport , TestViewport } from "../TestViewport" ; describe ( "Background map (#integration)" , ( ) => { let imodel ; before ( async ( ) => { assert . isDefined ( process . env . TEST_BING_MAPS_KEY , "The test requires that a Bing Maps key is configured." ) ; assert . isDefined ( process . env . TEST_MAPBOX_KEY , "The test requires that a MapBox key is configured." ) ; await TestUtility . startFrontend ( { ... TestUtility . iModelAppOptions , renderSys : { dpiAwareViewports : false } , mapLayerOptions : { BingMaps : { key : "key" , value : process . env . TEST_BING_MAPS_KEY ! } , MapBoxImagery : { key : "access_token" , value : process . env . TEST_MAPBOX_KEY ! } } } ) ; imodel = await SnapshotConnection . openFile ( "mirukuru.ibim" ) ; } ) ; after ( async ( ) => { if ( imodel ) await imodel . close ( ) ; await TestUtility . shutdownFrontend ( ) ; } ) ; it ( "obscures model based on settings" , async ( ) => { type PixelType = "model" | "bg" | "map" ; async function expectPixelTypes ( vp , mapProps , expectedCenterColor , expectedCornerColor , expectedCenterFeature , expectedCornerFeature ) { if ( mapProps ) { vp . viewFlags = vp . viewFlags . with ( "backgroundMap" , true ) ; vp . backgroundMapSettings = BackgroundMapSettings . fromJSON ( mapProps ) ; } await vp . waitForAllTilesToRender ( ) ; vp . invalidateRenderPlan ( ) ; await vp . waitForAllTilesToRender ( ) ; const cx = Math . floor ( vp . viewRect . width / 2 ) ; const cy = Math . floor ( vp . viewRect . height / 2 ) ; const expectColor = ( x , y , expectedColor ) => { const actualColor = vp . readColor ( x , y ) ; expect ( actualColor . equalsColorDef ( ColorDef . white ) ) . to . equal ( "model" === expectedColor ) ; expect ( actualColor . equalsColorDef ( vp . view . backgroundColor ) ) . to . equal ( "bg" === expectedColor ) ; } ; expectColor ( cx , cy , expectedCenterColor ) ; expectColor ( 1 , 1 , expectedCornerColor ) ; const expectPixel = ( x , y , expectedPixel ) => { const actualPixel = vp . readPixel ( x , y , true ) ; if ( "bg" === expectedPixel ) { expect ( actualPixel . type ) . to . equal ( Pixel . GeometryType . None ) ; return ; } expect ( actualPixel . type ) . to . equal ( Pixel . GeometryType . Surface ) ; expect ( actualPixel . featureTable ) . not . to . be . undefined ; expect ( Id64 . isTransient ( actualPixel . featureTable ! . modelId ) ) . to . equal ( "map" === expectedPixel ) ; } ; expectPixel ( cx , cy , expectedCenterFeature ) ; expectPixel ( 1 , 1 , expectedCornerFeature ) ; } type Test = [ BackgroundMapProps | undefined , PixelType , PixelType , PixelType , PixelType ] ; const tests = [ [ undefined , "model" , "bg" , "model" , "bg" ] , [ { groundBias : 10 , nonLocatable : true } , "model" , "map" , "model" , "bg" ] , [ { groundBias : - 10 , nonLocatable : true } , "model" , "map" , "model" , "bg" ] , [ { useDepthBuffer : true , groundBias : 10 , nonLocatable : true } , "map" , "map" , "model" , "bg" ] , [ { useDepthBuffer : true , groundBias : - 10 , nonLocatable : true } , "model" , "map" , "model" , "bg" ] , [ { nonLocatable : true } , "model" , "map" , "model" , "bg" ] , [ { } , "model" , "map" , "model" , "map" ] , [ { groundBias : 10 } , "model" , "map" , "model" , "map" ] , [ { groundBias : - 10 } , "model" , "map" , "model" , "map" ] , [ { useDepthBuffer : true , groundBias : 10 } , "map" , "map" , "map" , "map" ] , [ { useDepthBuffer : true , groundBias : - 10 } , "model" , "map" , "model" , "map" ] , ] ; await testOnScreenViewport ( "0x24" , imodel , 100 , 100 , async ( vp ) => { vp . viewFlags = vp . viewFlags . with ( "lighting" , false ) ; for ( const test of tests ) await expectPixelTypes ( vp , test [ 0 ] , test [ 1 ] , test [ 2 ] , test [ 3 ] , test [ 4 ] ) ; } ) ; } ) ; } ) ;