Search in sources :

Example 6 with IBlock

use of org.edx.mobile.model.course.IBlock 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)

Example 7 with IBlock

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

the class CourseOutlineActivityTest method sectionNavigationTest.

// Since Robolectric doesn't simulate actual Activity navigation, we
// can only test forward navigation, and only up to one level. This
// blocks us from testing the back stack restructuring upon switching
// to a different section from CourseUnitNavigationActivityTest.
/**
 * Testing navigation to a section
 */
@Test
public void sectionNavigationTest() {
    Intent intent = getIntent();
    Bundle extras = intent.getBundleExtra(Router.EXTRA_BUNDLE);
    EnrolledCoursesResponse courseData = (EnrolledCoursesResponse) extras.getSerializable(Router.EXTRA_COURSE_DATA);
    assertNotNull(courseData);
    String courseId = courseData.getCourse().getId();
    CourseStructureV1Model model;
    CourseComponent courseComponent;
    try {
        model = executeStrict(courseAPI.getCourseStructure(courseId));
        courseComponent = (CourseComponent) CourseAPI.normalizeCourseStructure(model, courseId);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    int subsectionRowIndex = -1;
    String subsectionId = null;
    CourseComponent subsectionUnit = null;
    List<IBlock> sections = courseComponent.getChildren();
    sectionIteration: for (@SuppressWarnings("unused") IBlock section : sections) {
        subsectionRowIndex++;
        for (IBlock subsection : section.getChildren()) {
            subsectionRowIndex++;
            if (((CourseComponent) subsection).isContainer()) {
                subsectionId = subsection.getId();
                List<CourseComponent> leafComponents = new ArrayList<>();
                courseComponent.fetchAllLeafComponents(leafComponents, EnumSet.allOf(BlockType.class));
                subsectionUnit = leafComponents.get(0);
                break sectionIteration;
            }
        }
    }
    assertNotNull(subsectionId);
    extras.putString(Router.EXTRA_COURSE_COMPONENT_ID, courseComponent.getId());
    ActivityController<? extends CourseOutlineActivity> controller = initialize(intent);
    CourseOutlineActivity activity = controller.get();
    Fragment fragment = activity.getSupportFragmentManager().findFragmentByTag(CourseOutlineFragment.TAG);
    assertThat(fragment).isInstanceOf(CourseOutlineFragment.class);
    CourseOutlineFragment courseOutlineFragment = (CourseOutlineFragment) fragment;
    clickRow(controller, courseOutlineFragment, subsectionRowIndex);
    Intent newIntent = assertNextStartedActivity(activity, CourseOutlineActivity.class);
    Bundle newData = newIntent.getBundleExtra(Router.EXTRA_BUNDLE);
    assertNotNull(newData);
    assertEquals(courseData, newData.getSerializable(Router.EXTRA_COURSE_DATA));
    assertEquals(subsectionId, newData.getString(Router.EXTRA_COURSE_COMPONENT_ID));
    // Back stack reconstruction upon receiving a specific path
    Intent resultData = new Intent();
    resultData.putExtra(Router.EXTRA_COURSE_COMPONENT_ID, subsectionUnit.getId());
    courseOutlineFragment.onActivityResult(CourseOutlineFragment.REQUEST_SHOW_COURSE_UNIT_DETAIL, Activity.RESULT_OK, resultData);
    ShadowActivity shadowActivity = Shadows.shadowOf(activity);
    BlockPath outlinePath = courseComponent.getPath();
    BlockPath leafPath = subsectionUnit.getPath();
    int outlinePathSize = outlinePath.getPath().size();
    for (int i = outlinePathSize + 1; ; i += 2) {
        newIntent = shadowActivity.getNextStartedActivity();
        CourseComponent nextComp = leafPath.get(i);
        if (nextComp == null || !nextComp.isContainer()) {
            assertNull(newIntent);
            break;
        }
        assertNotNull(newIntent);
        assertThat(newIntent).hasComponent(activity, CourseOutlineActivity.class);
        newData = newIntent.getBundleExtra(Router.EXTRA_BUNDLE);
        assertNotNull(newData);
        assertEquals(courseData, newData.getSerializable(Router.EXTRA_COURSE_DATA));
        assertEquals(nextComp.getId(), newData.getString(Router.EXTRA_COURSE_COMPONENT_ID));
    }
}
Also used : IBlock(org.edx.mobile.model.course.IBlock) BlockPath(org.edx.mobile.model.course.BlockPath) Bundle(android.os.Bundle) Intent(android.content.Intent) Fragment(android.support.v4.app.Fragment) CourseComponent(org.edx.mobile.model.course.CourseComponent) EnrolledCoursesResponse(org.edx.mobile.model.api.EnrolledCoursesResponse) ShadowActivity(org.robolectric.shadows.ShadowActivity) ArrayList(java.util.ArrayList) List(java.util.List) CourseStructureV1Model(org.edx.mobile.model.course.CourseStructureV1Model) Test(org.junit.Test)

Example 8 with IBlock

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

the class CourseOutlineActivityTest method unitNavigationTest.

/**
 * Testing navigation to a unit
 */
@Test
public void unitNavigationTest() {
    Intent intent = getIntent();
    Bundle extras = intent.getBundleExtra(Router.EXTRA_BUNDLE);
    EnrolledCoursesResponse courseData = (EnrolledCoursesResponse) extras.getSerializable(Router.EXTRA_COURSE_DATA);
    assertNotNull(courseData);
    String courseId = courseData.getCourse().getId();
    CourseStructureV1Model model;
    CourseComponent courseComponent;
    try {
        model = executeStrict(courseAPI.getCourseStructure(courseId));
        courseComponent = (CourseComponent) CourseAPI.normalizeCourseStructure(model, courseId);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    List<CourseComponent> leafComponents = new ArrayList<>();
    courseComponent.fetchAllLeafComponents(leafComponents, EnumSet.allOf(BlockType.class));
    CourseComponent courseUnit = leafComponents.get(0);
    CourseComponent lastUnit = leafComponents.get(leafComponents.size() - 1);
    assertNotEquals(lastUnit, courseUnit);
    courseComponent = courseUnit.getParent();
    if (courseUnit.getPath().getPath().size() % 2 > 0) {
        courseComponent = courseComponent.getParent();
    }
    int subsectionRowIndex = -1;
    List<IBlock> sections = courseComponent.getChildren();
    sectionIteration: for (@SuppressWarnings("unused") IBlock section : sections) {
        subsectionRowIndex++;
        if (courseUnit.equals(section)) {
            break;
        }
        for (@SuppressWarnings("unused") IBlock subsection : section.getChildren()) {
            subsectionRowIndex++;
            if (courseUnit.equals(subsection)) {
                break sectionIteration;
            }
        }
    }
    extras.putString(Router.EXTRA_COURSE_COMPONENT_ID, courseComponent.getId());
    ActivityController<? extends CourseOutlineActivity> controller = initialize(intent);
    CourseOutlineActivity activity = controller.get();
    Fragment fragment = activity.getSupportFragmentManager().findFragmentByTag(CourseOutlineFragment.TAG);
    assertThat(fragment).isInstanceOf(CourseOutlineFragment.class);
    CourseOutlineFragment courseOutlineFragment = (CourseOutlineFragment) fragment;
    clickRow(controller, courseOutlineFragment, subsectionRowIndex);
    Intent newIntent = assertNextStartedActivity(activity, CourseUnitNavigationActivity.class);
    Bundle newData = newIntent.getBundleExtra(Router.EXTRA_BUNDLE);
    assertNotNull(newData);
    assertEquals(courseData, newData.getSerializable(Router.EXTRA_COURSE_DATA));
    assertEquals(courseUnit.getId(), newData.getSerializable(Router.EXTRA_COURSE_COMPONENT_ID));
    // Test the back stack reconstruction upon receiving a specific path
    // Should not perform any action if it receives a unit selection from itself
    Intent resultData = new Intent();
    resultData.putExtra(Router.EXTRA_COURSE_COMPONENT_ID, courseUnit.getId());
    courseOutlineFragment.onActivityResult(CourseOutlineFragment.REQUEST_SHOW_COURSE_UNIT_DETAIL, Activity.RESULT_OK, resultData);
    ShadowActivity shadowActivity = Shadows.shadowOf(activity);
    assertNull(shadowActivity.getNextStartedActivity());
    assertFalse(shadowActivity.isFinishing());
    // Should finish itself to start the new navigation back stack if it receives
    // a unit selection from another section
    resultData.putExtra(Router.EXTRA_COURSE_COMPONENT_ID, lastUnit.getId());
    courseOutlineFragment.onActivityResult(CourseOutlineFragment.REQUEST_SHOW_COURSE_UNIT_DETAIL, Activity.RESULT_OK, resultData);
    assertNull(shadowActivity.getNextStartedActivity());
    assertTrue(shadowActivity.isFinishing());
}
Also used : IBlock(org.edx.mobile.model.course.IBlock) Bundle(android.os.Bundle) ArrayList(java.util.ArrayList) Intent(android.content.Intent) Fragment(android.support.v4.app.Fragment) CourseComponent(org.edx.mobile.model.course.CourseComponent) BlockType(org.edx.mobile.model.course.BlockType) EnrolledCoursesResponse(org.edx.mobile.model.api.EnrolledCoursesResponse) ShadowActivity(org.robolectric.shadows.ShadowActivity) CourseStructureV1Model(org.edx.mobile.model.course.CourseStructureV1Model) Test(org.junit.Test)

Aggregations

IBlock (org.edx.mobile.model.course.IBlock)8 CourseComponent (org.edx.mobile.model.course.CourseComponent)6 ArrayList (java.util.ArrayList)3 EnrolledCoursesResponse (org.edx.mobile.model.api.EnrolledCoursesResponse)3 BlockPath (org.edx.mobile.model.course.BlockPath)3 CourseStructureV1Model (org.edx.mobile.model.course.CourseStructureV1Model)3 Test (org.junit.Test)3 Intent (android.content.Intent)2 Bundle (android.os.Bundle)2 Fragment (android.support.v4.app.Fragment)2 BlockType (org.edx.mobile.model.course.BlockType)2 ShadowActivity (org.robolectric.shadows.ShadowActivity)2 NonNull (android.support.annotation.NonNull)1 Nullable (android.support.annotation.Nullable)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1 Filter (org.edx.mobile.model.Filter)1 LectureModel (org.edx.mobile.model.api.LectureModel)1 SectionEntry (org.edx.mobile.model.api.SectionEntry)1