Add autotrimming in engine and tests it