Search in sources :

Example 1 with Filter

use of org.edx.mobile.model.Filter in project edx-app-android by edx.

the class CourseComponentTest method testFindWithFilter.

@Test
public void testFindWithFilter() throws Exception {
    CourseComponent found = course.find(new Filter<CourseComponent>() {

        @Override
        public boolean apply(CourseComponent component) {
            return false;
        }
    });
    assertTrue("find with filter failed", found == null);
    found = course.find(new Filter<CourseComponent>() {

        @Override
        public boolean apply(CourseComponent component) {
            return component == unit1;
        }
    });
    assertTrue("find with filter failed", found == unit1);
    found = course.find(new Filter<CourseComponent>() {

        @Override
        public boolean apply(CourseComponent component) {
            return component.getType() == BlockType.VIDEO;
        }
    });
    assertTrue("find with filter failed", found == unit1);
}
Also used : Filter(org.edx.mobile.model.Filter) CourseComponent(org.edx.mobile.model.course.CourseComponent) Test(org.junit.Test)

Example 2 with Filter

use of org.edx.mobile.model.Filter in project edx-app-android by edx.

the class ApiTests method testGetCourseStructure.

@Test
public void testGetCourseStructure() throws Exception {
    login();
    // General overall testing of CourseComponent API without recursion
    EnrolledCoursesResponse e = executeStrict(courseAPI.getEnrolledCourses()).get(0);
    final String courseId = e.getCourse().getId();
    final CourseStructureV1Model model = executeStrict(courseAPI.getCourseStructure(courseId));
    final CourseComponent courseComponent = (CourseComponent) CourseAPI.normalizeCourseStructure(model, courseId);
    assertNotNull(courseComponent);
    assertNotNull(courseComponent.getRoot());
    assertEquals(courseId, courseComponent.getCourseId());
    List<IBlock> children = courseComponent.getChildren();
    assertNotNull(children);
    List<CourseComponent> childContainers = new ArrayList<>();
    List<CourseComponent> childLeafs = new ArrayList<>();
    for (IBlock c : children) {
        assertTrue(c instanceof CourseComponent);
        final CourseComponent child = (CourseComponent) c;
        assertEquals(child, courseComponent.find(new Filter<CourseComponent>() {

            @Override
            public boolean apply(CourseComponent component) {
                return child.getId().equals(component.getId());
            }
        }));
        List<IBlock> grandchildren = child.getChildren();
        for (IBlock gc : grandchildren) {
            assertTrue(gc instanceof CourseComponent);
            final CourseComponent grandchild = (CourseComponent) c;
            assertEquals(grandchild, courseComponent.find(new Filter<CourseComponent>() {

                @Override
                public boolean apply(CourseComponent component) {
                    return grandchild.getId().equals(component.getId());
                }
            }));
        }
        assertNull(child.find(new Filter<CourseComponent>() {

            @Override
            public boolean apply(CourseComponent component) {
                return courseComponent.getId().equals(component.getId());
            }
        }));
        if (child.isContainer()) {
            childContainers.add(child);
        } else {
            childLeafs.add(child);
        }
    }
    assertEquals(childContainers, courseComponent.getChildContainers());
    assertEquals(childLeafs, courseComponent.getChildLeafs());
    assertTrue(courseComponent.isLastChild());
    int childrenSize = children.size();
    assertTrue(childrenSize > 0);
    assertTrue(((CourseComponent) children.get(childrenSize - 1)).isLastChild());
    BlockType blockType = courseComponent.getType();
    assertSame(courseComponent, courseComponent.getAncestor(Integer.MAX_VALUE));
    assertSame(courseComponent, courseComponent.getAncestor(EnumSet.of(blockType)));
    List<VideoBlockModel> videos = courseComponent.getVideos();
    assertNotNull(videos);
    for (HasDownloadEntry video : videos) {
        assertNotNull(video);
        assertTrue(video instanceof CourseComponent);
        CourseComponent videoComponent = (CourseComponent) video;
        assertFalse(videoComponent.isContainer());
        assertEquals(BlockType.VIDEO, videoComponent.getType());
    }
    for (BlockType type : BlockType.values()) {
        EnumSet<BlockType> typeSet = EnumSet.of(type);
        List<CourseComponent> typeComponents = new ArrayList<>();
        courseComponent.fetchAllLeafComponents(typeComponents, typeSet);
        for (CourseComponent typeComponent : typeComponents) {
            assertEquals(type, typeComponent.getType());
            verifyModelParsing(typeComponent);
        }
        if (type != blockType) {
            assertNotSame(courseComponent, courseComponent.getAncestor(EnumSet.of(type)));
        }
    }
    BlockPath path = courseComponent.getPath();
    assertNotNull(path);
    assertEquals(1, path.getPath().size());
    assertSame(courseComponent, path.get(0));
    List<CourseComponent> leafComponents = new ArrayList<>();
    courseComponent.fetchAllLeafComponents(leafComponents, EnumSet.allOf(BlockType.class));
    for (CourseComponent leafComponent : leafComponents) {
        BlockPath leafPath = leafComponent.getPath();
        assertNotNull(leafPath);
        int pathSize = leafPath.getPath().size();
        assertTrue(pathSize > 1);
        CourseComponent component = leafComponent;
        for (int i = pathSize - 1; i >= 0; i--) {
            assertSame(component, leafPath.get(i));
            component = component.getParent();
        }
    }
}
Also used : IBlock(org.edx.mobile.model.course.IBlock) BlockPath(org.edx.mobile.model.course.BlockPath) ArrayList(java.util.ArrayList) VideoBlockModel(org.edx.mobile.model.course.VideoBlockModel) CourseComponent(org.edx.mobile.model.course.CourseComponent) HasDownloadEntry(org.edx.mobile.model.course.HasDownloadEntry) Filter(org.edx.mobile.model.Filter) BlockType(org.edx.mobile.model.course.BlockType) EnrolledCoursesResponse(org.edx.mobile.model.api.EnrolledCoursesResponse) CourseStructureV1Model(org.edx.mobile.model.course.CourseStructureV1Model) Test(org.junit.Test)

Aggregations

Filter (org.edx.mobile.model.Filter)2 CourseComponent (org.edx.mobile.model.course.CourseComponent)2 Test (org.junit.Test)2 ArrayList (java.util.ArrayList)1 EnrolledCoursesResponse (org.edx.mobile.model.api.EnrolledCoursesResponse)1 BlockPath (org.edx.mobile.model.course.BlockPath)1 BlockType (org.edx.mobile.model.course.BlockType)1 CourseStructureV1Model (org.edx.mobile.model.course.CourseStructureV1Model)1 HasDownloadEntry (org.edx.mobile.model.course.HasDownloadEntry)1 IBlock (org.edx.mobile.model.course.IBlock)1 VideoBlockModel (org.edx.mobile.model.course.VideoBlockModel)1