Esempio n. 1
0
  private void updateCardInList(Card card) {
    Note note = card.note();
    for (Card c : note.cards()) {
      int aPos = getPosition(mAllCards, c.getId());
      int pos = getPosition(mCards, c.getId());

      String sfld = note.getSFld();
      mAllCards.get(aPos).put("sfld", sfld);
      mCards.get(pos).put("sfld", sfld);

      if (mWholeCollection) {
        String deckName;
        try {
          deckName = mCol.getDecks().get(card.getDid()).getString("name");
        } catch (JSONException e) {
          throw new RuntimeException(e);
        }
        mAllCards.get(aPos).put("deck", deckName);
        mCards.get(pos).put("deck", deckName);
      }

      String flags =
          Integer.toString((c.getQueue() == -1 ? 1 : 0) + (note.hasTag("marked") ? 2 : 0));
      mAllCards.get(aPos).put("flags", flags);
      mCards.get(pos).put("flags", flags);
    }
    updateList();
  }
 /** Check that inserting and removing a note into default deck works as expected */
 public void testInsertAndRemoveNote() throws Exception {
   // Get required objects for test
   final ContentResolver cr = getContext().getContentResolver();
   final Collection col = CollectionHelper.getInstance().getCol(getContext());
   final AddContentApi api = new AddContentApi(getContext());
   // Add the note
   Uri newNoteUri = api.addNewNote(mModelId, 1, TEST_NOTE_FIELDS, TEST_TAG);
   assertNotNull("Check that URI returned from addNewNote is not null", newNoteUri);
   // Check that it looks as expected
   Note addedNote = new Note(col, Long.parseLong(newNoteUri.getLastPathSegment()));
   addedNote.load();
   assertTrue(
       "Check that fields were set correctly",
       Arrays.equals(addedNote.getFields(), TEST_NOTE_FIELDS));
   assertEquals("Check that tag was set correctly", TEST_TAG, addedNote.getTags().get(0));
   int expectedNumCards = col.getModels().get(mModelId).getJSONArray("tmpls").length();
   assertEquals(
       "Check that correct number of cards generated", expectedNumCards, addedNote.cards().size());
   // Now delete the note
   cr.delete(newNoteUri, null, null);
   try {
     addedNote.load();
     fail("Expected RuntimeException to be thrown when deleting note");
   } catch (RuntimeException e) {
     // Expect RuntimeException to be thrown when loading deleted note
   }
 }
 /** Initially create one note for each model. */
 @Override
 protected void setUp() throws Exception {
   super.setUp();
   Log.i(AnkiDroidApp.TAG, "setUp()");
   mCreatedNotes = new ArrayList<>();
   final Collection col = CollectionHelper.getInstance().getCol(getContext());
   // Add a new basic model that we use for testing purposes (existing models could potentially be
   // corrupted)
   JSONObject model = Models.addBasicModel(col, BASIC_MODEL_NAME);
   mModelId = model.getLong("id");
   ArrayList<String> flds = col.getModels().fieldNames(model);
   // Use the names of the fields as test values for the notes which will be added
   mDummyFields = flds.toArray(new String[flds.size()]);
   // create test decks and add one note for every deck
   final AddContentApi api = new AddContentApi(getContext());
   HashMap<Long, String> deckList = api.getDeckList();
   mNumDecksBeforeTest = deckList.size();
   // TODO: add the notes directly with libanki
   for (int i = 0; i < TEST_DECKS.length; i++) {
     mTestDeckIds[i] = api.addNewDeck(TEST_DECKS[i]);
     Uri newNoteUri = api.addNewNote(mModelId, mTestDeckIds[i], mDummyFields, TEST_TAG);
     assertNotNull(newNoteUri);
     mCreatedNotes.add(newNoteUri);
     // Check that the flds data was set correctly
     long nid = Long.parseLong(newNoteUri.getLastPathSegment());
     Note addedNote = col.getNote(nid);
     assertTrue(
         "Check that the flds data was set correctly",
         Arrays.equals(addedNote.getFields(), mDummyFields));
     assertTrue("Check that there was at least one card generated", addedNote.cards().size() > 0);
   }
   // Add a note to the default deck as well so that testQueryNextCard() works
   Uri newNoteUri = api.addNewNote(mModelId, 1, mDummyFields, TEST_TAG);
   assertNotNull(newNoteUri);
   mCreatedNotes.add(newNoteUri);
 }