Search in sources :

Example 1 with CourseStructureV1Model

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

the class CourseUnitWebViewFragmentTest method getHtmlUnit.

/**
 * Method for iterating through the mock course response data, and
 * returning the first video block leaf.
 *
 * @return The first {@link HtmlBlockModel} leaf in the mock course data
 */
private HtmlBlockModel getHtmlUnit() throws CourseContentNotValidException {
    EnrolledCoursesResponse courseData;
    try {
        courseData = executeStrict(courseAPI.getEnrolledCourses()).get(0);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    String courseId = courseData.getCourse().getId();
    CourseStructureV1Model model;
    try {
        model = executeStrict(courseAPI.getCourseStructure(courseId));
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    CourseComponent courseComponent = (CourseComponent) CourseAPI.normalizeCourseStructure(model, courseId);
    List<CourseComponent> htmlBlockUnits = new ArrayList<>();
    courseComponent.fetchAllLeafComponents(htmlBlockUnits, EnumSet.of(BlockType.HTML));
    return (HtmlBlockModel) htmlBlockUnits.get(0);
}
Also used : EnrolledCoursesResponse(org.edx.mobile.model.api.EnrolledCoursesResponse) ArrayList(java.util.ArrayList) HtmlBlockModel(org.edx.mobile.model.course.HtmlBlockModel) CourseStructureV1Model(org.edx.mobile.model.course.CourseStructureV1Model) CourseContentNotValidException(org.edx.mobile.exception.CourseContentNotValidException) CourseComponent(org.edx.mobile.model.course.CourseComponent)

Example 2 with CourseStructureV1Model

use of org.edx.mobile.model.course.CourseStructureV1Model 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 3 with CourseStructureV1Model

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

the class CourseUnitNavigationActivityTest method getIntent.

/**
 * {@inheritDoc}
 */
@Override
protected Intent getIntent() {
    EnrolledCoursesResponse courseData;
    try {
        courseData = executeStrict(courseAPI.getEnrolledCourses()).get(0);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    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);
    Intent intent = super.getIntent();
    Bundle extras = new Bundle();
    extras.putSerializable(Router.EXTRA_COURSE_DATA, courseData);
    extras.putString(Router.EXTRA_COURSE_COMPONENT_ID, courseUnit.getId());
    intent.putExtra(Router.EXTRA_BUNDLE, extras);
    return intent;
}
Also used : BlockType(org.edx.mobile.model.course.BlockType) Bundle(android.os.Bundle) EnrolledCoursesResponse(org.edx.mobile.model.api.EnrolledCoursesResponse) ArrayList(java.util.ArrayList) Intent(android.content.Intent) CourseStructureV1Model(org.edx.mobile.model.course.CourseStructureV1Model) IOException(java.io.IOException) CourseComponent(org.edx.mobile.model.course.CourseComponent)

Example 4 with CourseStructureV1Model

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

the class CourseUnitNavigationActivityTest method initializeTest.

/**
 * {@inheritDoc}
 */
@Test
@Override
public void initializeTest() {
    super.initializeTest();
    Intent intent = getIntent();
    ActivityController<? extends CourseUnitNavigationActivity> controller = Robolectric.buildActivity(getActivityClass()).withIntent(intent);
    CourseUnitNavigationActivity activity = controller.get();
    controller.create();
    assertNotNull(activity.findViewById(R.id.course_unit_nav_bar));
    View prev = activity.findViewById(R.id.goto_prev);
    assertNotNull(prev);
    assertThat(prev).isInstanceOf(TextView.class);
    TextView prevButton = (TextView) prev;
    View next = activity.findViewById(R.id.goto_next);
    assertNotNull(next);
    assertThat(next).isInstanceOf(TextView.class);
    TextView nextButton = (TextView) next;
    View prevUnitTitle = activity.findViewById(R.id.prev_unit_title);
    assertNotNull(prevUnitTitle);
    assertThat(prevUnitTitle).isInstanceOf(TextView.class);
    TextView prevUnitLabel = (TextView) prevUnitTitle;
    View nextUnitTitle = activity.findViewById(R.id.next_unit_title);
    assertNotNull(nextUnitTitle);
    assertThat(nextUnitTitle).isInstanceOf(TextView.class);
    TextView nextUnitLabel = (TextView) nextUnitTitle;
    View pager = activity.findViewById(R.id.pager);
    assertNotNull(pager);
    assertThat(pager).isInstanceOf(ViewPager.class);
    ViewPager viewPager = (ViewPager) pager;
    PagerAdapter pagerAdapter = viewPager.getAdapter();
    assertNotNull(pagerAdapter);
    // Text navigation through units
    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);
    }
    assertNotNull(courseComponent);
    final String unitId = extras.getString(Router.EXTRA_COURSE_COMPONENT_ID);
    assertNotNull(unitId);
    CourseComponent currentUnit = courseComponent.find(new Filter<CourseComponent>() {

        @Override
        public boolean apply(CourseComponent courseComponent) {
            return unitId.equals(courseComponent.getId());
        }
    });
    assertNotNull(currentUnit);
// Since Robolectric current does not call the scroll callbacks due
// to not supporting view drawing (see
// https://github.com/robolectric/robolectric/issues/2007), we can't
// test the ViewPager navigation at the moment.
// TODO: Uncomment the following code when this issue is fixed
/*List<CourseComponent> units = new ArrayList<>();
        courseComponent.fetchAllLeafComponents(units,
                EnumSet.allOf(BlockType.class));
        assertThat(units).isNotEmpty();
        controller.start().postCreate((Bundle)null).resume().visible();
        ListIterator<CourseComponent> unitIterator = units.listIterator(1);
        for (CourseComponent prevUnit = null;;) {
            int unitIndex = unitIterator.previousIndex();
            CourseComponent nextUnit = unitIterator.hasNext() ?
                    unitIterator.next() : null;
            verifyState(activity, unitIndex, currentUnit, prevUnit, nextUnit,
                    viewPager, pagerAdapter, prevButton, nextButton,
                    prevUnitLabel, nextUnitLabel);
            if (nextUnit == null) break;
            // The Scheduler needs to be paused while clicking the next button
            // to enable the FragmentStatePagerAdapter to clear it's transaction
            // state, and thus avoid the commit being called recursively
            Scheduler foregroundScheduler = ShadowApplication.getInstance()
                    .getForegroundThreadScheduler();
            foregroundScheduler.pause();
            assertTrue(nextButton.performClick());
            foregroundScheduler.unPause();
            prevUnit = currentUnit;
            currentUnit = nextUnit;
        }
        // Now iterate back in reverse order to test the previous button
        unitIterator = units.listIterator(units.size() - 1);
        for (CourseComponent nextUnit = null;;) {
            int unitIndex = unitIterator.nextIndex();
            CourseComponent prevUnit = unitIterator.hasPrevious() ?
                    unitIterator.previous() : null;
            verifyState(activity, unitIndex, currentUnit, prevUnit, nextUnit,
                    viewPager, pagerAdapter, prevButton, nextButton,
                    prevUnitLabel, nextUnitLabel);
            if (prevUnit == null) break;
            Scheduler foregroundScheduler = ShadowApplication.getInstance()
                    .getForegroundThreadScheduler();
            foregroundScheduler.pause();
            assertTrue(prevButton.performClick());
            foregroundScheduler.unPause();
            nextUnit = currentUnit;
            currentUnit = prevUnit;
        }*/
}
Also used : Bundle(android.os.Bundle) Intent(android.content.Intent) View(android.view.View) TextView(android.widget.TextView) DisableableViewPager(org.edx.mobile.view.custom.DisableableViewPager) ViewPager(android.support.v4.view.ViewPager) PagerAdapter(android.support.v4.view.PagerAdapter) CourseUnitPagerAdapter(org.edx.mobile.view.adapters.CourseUnitPagerAdapter) CourseComponent(org.edx.mobile.model.course.CourseComponent) IOException(java.io.IOException) EnrolledCoursesResponse(org.edx.mobile.model.api.EnrolledCoursesResponse) TextView(android.widget.TextView) CourseStructureV1Model(org.edx.mobile.model.course.CourseStructureV1Model) Test(org.junit.Test)

Example 5 with CourseStructureV1Model

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

the class CourseUnitVideoFragmentTest method getVideoUnit.

/**
 * Method for iterating through the mock course response data, and
 * returning the first video block leaf.
 *
 * @return The first {@link VideoBlockModel} leaf in the mock course data
 */
private VideoBlockModel getVideoUnit() {
    EnrolledCoursesResponse courseData;
    try {
        courseData = executeStrict(courseAPI.getEnrolledCourses()).get(0);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    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);
    }
    return (VideoBlockModel) courseComponent.getVideos().get(0);
}
Also used : EnrolledCoursesResponse(org.edx.mobile.model.api.EnrolledCoursesResponse) CourseStructureV1Model(org.edx.mobile.model.course.CourseStructureV1Model) VideoBlockModel(org.edx.mobile.model.course.VideoBlockModel) CourseComponent(org.edx.mobile.model.course.CourseComponent)

Aggregations

EnrolledCoursesResponse (org.edx.mobile.model.api.EnrolledCoursesResponse)8 CourseComponent (org.edx.mobile.model.course.CourseComponent)8 CourseStructureV1Model (org.edx.mobile.model.course.CourseStructureV1Model)8 Intent (android.content.Intent)5 Bundle (android.os.Bundle)5 ArrayList (java.util.ArrayList)5 Test (org.junit.Test)4 BlockType (org.edx.mobile.model.course.BlockType)3 IBlock (org.edx.mobile.model.course.IBlock)3 Fragment (android.support.v4.app.Fragment)2 IOException (java.io.IOException)2 BlockPath (org.edx.mobile.model.course.BlockPath)2 VideoBlockModel (org.edx.mobile.model.course.VideoBlockModel)2 ShadowActivity (org.robolectric.shadows.ShadowActivity)2 PagerAdapter (android.support.v4.view.PagerAdapter)1 ViewPager (android.support.v4.view.ViewPager)1 View (android.view.View)1 TextView (android.widget.TextView)1 List (java.util.List)1 CourseContentNotValidException (org.edx.mobile.exception.CourseContentNotValidException)1