diff options
author | Kohei Yoshida <kohei.yoshida@collabora.com> | 2017-05-15 22:36:08 -0400 |
---|---|---|
committer | Kohei Yoshida <libreoffice@kohei.us> | 2017-05-17 05:17:31 +0200 |
commit | 7ae50f522f94987afe85674f6691fc37c55bd3d1 (patch) | |
tree | a5108a5a5cf9fd94d2c54ebca0a7bc37a66af895 | |
parent | 034be10413ed4915090678ad4f1d48596cf5e206 (diff) |
tdf#43535: add unit test for this.
Change-Id: I4e6b27f82d1344ac77b02867e1e717c510a133b6
Reviewed-on: https://gerrit.libreoffice.org/37696
Tested-by: Jenkins <ci@libreoffice.org>
Reviewed-by: Kohei Yoshida <libreoffice@kohei.us>
-rw-r--r-- | sc/qa/unit/ucalc.cxx | 153 | ||||
-rw-r--r-- | sc/qa/unit/ucalc.hxx | 4 |
2 files changed, 157 insertions, 0 deletions
diff --git a/sc/qa/unit/ucalc.cxx b/sc/qa/unit/ucalc.cxx index f573cc8d8d86..3f6a38820abc 100644 --- a/sc/qa/unit/ucalc.cxx +++ b/sc/qa/unit/ucalc.cxx @@ -68,6 +68,7 @@ #include <editable.hxx> #include <bcaslot.hxx> #include <sharedformula.hxx> +#include <tabprotection.hxx> #include <formula/IFunctionDescription.hxx> @@ -6949,6 +6950,158 @@ void Test::testPrecisionAsShown() m_pDoc->DeleteTab(0); } +void Test::testProtectedSheetEditByRow() +{ + ScDocFunc& rDocFunc = getDocShell().GetDocFunc(); + m_pDoc->InsertTab(0, "Protected"); + + { + // Remove protected flags from rows 2-5. + ScPatternAttr aAttr(m_pDoc->GetPool()); + aAttr.GetItemSet().Put(ScProtectionAttr(false)); + m_pDoc->ApplyPatternAreaTab(0, 1, MAXCOL, 4, 0, aAttr); + + // Protect the sheet without any options. + ScTableProtection aProtect; + aProtect.setProtected(true); + m_pDoc->SetTabProtection(0, &aProtect); + + // Try to delete row 3. It should fail. + ScRange aRow3(0,2,0,MAXCOL,2,0); + ScMarkData aMark; + aMark.SelectOneTable(0); + bool bDeleted = rDocFunc.DeleteCells(aRow3, &aMark, DEL_DELROWS, true); + CPPUNIT_ASSERT_MESSAGE("deletion of row 3 should fail.", !bDeleted); + + // Protect the sheet but allow row deletion. + aProtect.setOption(ScTableProtection::DELETE_ROWS, true); + m_pDoc->SetTabProtection(0, &aProtect); + + // Now we should be able to delete row 3. + bDeleted = rDocFunc.DeleteCells(aRow3, &aMark, DEL_DELROWS, true); + CPPUNIT_ASSERT_MESSAGE("deletion of row 3 should succeed.", bDeleted); + + // But, row deletion should still fail on a protected row. + ScRange aRow10(0,9,0,MAXCOL,9,0); + bDeleted = rDocFunc.DeleteCells(aRow10, &aMark, DEL_DELROWS, true); + CPPUNIT_ASSERT_MESSAGE("deletion of row 10 should not be allowed.", !bDeleted); + + // Try inserting a new row. It should fail. + bool bInserted = rDocFunc.InsertCells(aRow3, &aMark, INS_INSROWS_AFTER, true, true); + CPPUNIT_ASSERT_MESSAGE("row insertion at row 3 should fail.", !bInserted); + + // Allow row insertions. + aProtect.setOption(ScTableProtection::INSERT_ROWS, true); + m_pDoc->SetTabProtection(0, &aProtect); + + bInserted = rDocFunc.InsertCells(aRow3, &aMark, INS_INSROWS_AFTER, true, true); + CPPUNIT_ASSERT_MESSAGE("row insertion at row 3 should succeed.", bInserted); + + // Row insertion is allowed even when the rows above and below have protected flags set. + bInserted = rDocFunc.InsertCells(aRow10, &aMark, INS_INSROWS_AFTER, true, true); + CPPUNIT_ASSERT_MESSAGE("row insertion at row 10 should succeed.", bInserted); + } + + m_pDoc->InsertTab(1, "Matrix"); // This sheet is unprotected. + + { + // Insert matrix into B2:C3. + ScMarkData aMark; + aMark.SelectOneTable(1); + m_pDoc->InsertMatrixFormula(1, 1, 2, 2, aMark, "={1;2|3;4}"); + + CPPUNIT_ASSERT_EQUAL(1.0, m_pDoc->GetValue(ScAddress(1,1,1))); + CPPUNIT_ASSERT_EQUAL(2.0, m_pDoc->GetValue(ScAddress(2,1,1))); + CPPUNIT_ASSERT_EQUAL(3.0, m_pDoc->GetValue(ScAddress(1,2,1))); + CPPUNIT_ASSERT_EQUAL(4.0, m_pDoc->GetValue(ScAddress(2,2,1))); + + // Try to insert a row at row 3. It should fail because of matrix's presence. + + ScRange aRow3(0,2,1,MAXCOL,2,1); + bool bInserted = rDocFunc.InsertCells(aRow3, &aMark, INS_INSROWS_BEFORE, true, true); + CPPUNIT_ASSERT_MESSAGE("row insertion at row 3 should fail.", !bInserted); + } + + m_pDoc->DeleteTab(1); + m_pDoc->DeleteTab(0); +} + +void Test::testProtectedSheetEditByColumn() +{ + ScDocFunc& rDocFunc = getDocShell().GetDocFunc(); + m_pDoc->InsertTab(0, "Protected"); + + { + // Remove protected flags from columns B to E. + ScPatternAttr aAttr(m_pDoc->GetPool()); + aAttr.GetItemSet().Put(ScProtectionAttr(false)); + m_pDoc->ApplyPatternAreaTab(1, 0, 4, MAXROW, 0, aAttr); + + // Protect the sheet without any options. + ScTableProtection aProtect; + aProtect.setProtected(true); + m_pDoc->SetTabProtection(0, &aProtect); + + // Try to delete column C. It should fail. + ScRange aCol3(2,0,0,2,MAXROW,0); + ScMarkData aMark; + aMark.SelectOneTable(0); + bool bDeleted = rDocFunc.DeleteCells(aCol3, &aMark, DEL_DELCOLS, true); + CPPUNIT_ASSERT_MESSAGE("deletion of column 3 should fail.", !bDeleted); + + // Protect the sheet but allow column deletion. + aProtect.setOption(ScTableProtection::DELETE_COLUMNS, true); + m_pDoc->SetTabProtection(0, &aProtect); + + // Now we should be able to delete column C. + bDeleted = rDocFunc.DeleteCells(aCol3, &aMark, DEL_DELCOLS, true); + CPPUNIT_ASSERT_MESSAGE("deletion of column 3 should succeed.", bDeleted); + + // But, column deletion should still fail on a protected column. + ScRange aCol10(9,0,0,9,MAXROW,0); + bDeleted = rDocFunc.DeleteCells(aCol10, &aMark, DEL_DELCOLS, true); + CPPUNIT_ASSERT_MESSAGE("deletion of column 10 should not be allowed.", !bDeleted); + + // Try inserting a new column. It should fail. + bool bInserted = rDocFunc.InsertCells(aCol3, &aMark, INS_INSCOLS_AFTER, true, true); + CPPUNIT_ASSERT_MESSAGE("column insertion at column 3 should fail.", !bInserted); + + // Allow column insertions. + aProtect.setOption(ScTableProtection::INSERT_COLUMNS, true); + m_pDoc->SetTabProtection(0, &aProtect); + + bInserted = rDocFunc.InsertCells(aCol3, &aMark, INS_INSCOLS_AFTER, true, true); + CPPUNIT_ASSERT_MESSAGE("column insertion at column 3 should succeed.", bInserted); + + // Column insertion is allowed even when the columns above and below have protected flags set. + bInserted = rDocFunc.InsertCells(aCol10, &aMark, INS_INSCOLS_AFTER, true, true); + CPPUNIT_ASSERT_MESSAGE("column insertion at column 10 should succeed.", bInserted); + } + + m_pDoc->InsertTab(1, "Matrix"); // This sheet is unprotected. + + { + // Insert matrix into B2:C3. + ScMarkData aMark; + aMark.SelectOneTable(1); + m_pDoc->InsertMatrixFormula(1, 1, 2, 2, aMark, "={1;2|3;4}"); + + CPPUNIT_ASSERT_EQUAL(1.0, m_pDoc->GetValue(ScAddress(1,1,1))); + CPPUNIT_ASSERT_EQUAL(2.0, m_pDoc->GetValue(ScAddress(2,1,1))); + CPPUNIT_ASSERT_EQUAL(3.0, m_pDoc->GetValue(ScAddress(1,2,1))); + CPPUNIT_ASSERT_EQUAL(4.0, m_pDoc->GetValue(ScAddress(2,2,1))); + + // Try to insert a column at column C. It should fail because of matrix's presence. + + ScRange aCol3(2,0,1,2,MAXROW,1); + bool bInserted = rDocFunc.InsertCells(aCol3, &aMark, INS_INSCOLS_BEFORE, true, true); + CPPUNIT_ASSERT_MESSAGE("column insertion at column C should fail.", !bInserted); + } + + m_pDoc->DeleteTab(1); + m_pDoc->DeleteTab(0); +} + CPPUNIT_TEST_SUITE_REGISTRATION(Test); CPPUNIT_PLUGIN_IMPLEMENT(); diff --git a/sc/qa/unit/ucalc.hxx b/sc/qa/unit/ucalc.hxx index acb918642fe4..9028928f3136 100644 --- a/sc/qa/unit/ucalc.hxx +++ b/sc/qa/unit/ucalc.hxx @@ -527,6 +527,8 @@ public: void testEmptyCalcDocDefaults(); void testPrecisionAsShown(); + void testProtectedSheetEditByRow(); + void testProtectedSheetEditByColumn(); CPPUNIT_TEST_SUITE(Test); CPPUNIT_TEST(testCollator); @@ -794,6 +796,8 @@ public: CPPUNIT_TEST(testTdf97587); CPPUNIT_TEST(testEmptyCalcDocDefaults); CPPUNIT_TEST(testPrecisionAsShown); + CPPUNIT_TEST(testProtectedSheetEditByRow); + CPPUNIT_TEST(testProtectedSheetEditByColumn); CPPUNIT_TEST_SUITE_END(); private: |