Search in sources :

Example 6 with BlockPath

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

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

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

the class CourseOutlineFragment method onActivityResult.

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    switch(requestCode) {
        // the activity stack to point to it.
        case REQUEST_SHOW_COURSE_UNIT_DETAIL:
            {
                switch(resultCode) {
                    case Activity.RESULT_OK:
                        {
                            CourseComponent outlineComp = courseManager.getComponentById(courseData.getCourse().getId(), courseComponentId);
                            String leafCompId = (String) data.getSerializableExtra(Router.EXTRA_COURSE_COMPONENT_ID);
                            CourseComponent leafComp = courseManager.getComponentById(courseData.getCourse().getId(), leafCompId);
                            BlockPath outlinePath = outlineComp.getPath();
                            BlockPath leafPath = leafComp.getPath();
                            int outlinePathSize = outlinePath.getPath().size();
                            if (!outlineComp.equals(leafPath.get(outlinePathSize - 1))) {
                                getActivity().setResult(Activity.RESULT_OK, data);
                                getActivity().finish();
                            } else {
                                int leafPathSize = leafPath.getPath().size();
                                if (outlinePathSize == leafPathSize - 2) {
                                    updateRowSelection(leafCompId);
                                } else {
                                    for (int i = outlinePathSize + 1; i < leafPathSize - 1; i += 2) {
                                        CourseComponent nextComp = leafPath.get(i);
                                        environment.getRouter().showCourseContainerOutline(CourseOutlineFragment.this, REQUEST_SHOW_COURSE_UNIT_DETAIL, courseData, nextComp.getId(), leafCompId, isVideoMode);
                                    }
                                }
                            }
                        }
                }
                break;
            }
    }
}
Also used : BlockPath(org.edx.mobile.model.course.BlockPath) CourseComponent(org.edx.mobile.model.course.CourseComponent)

Example 9 with BlockPath

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

the class CourseOutlineAdapter method getRowViewForContainer.

private void getRowViewForContainer(ViewHolder holder, final SectionRow row) {
    final CourseComponent component = row.component;
    String courseId = component.getCourseId();
    BlockPath path = component.getPath();
    // FIXME - we should add a new column in database - pathinfo.
    // then do the string match to get the record
    String chapterId = path.get(1) == null ? "" : path.get(1).getDisplayName();
    String sequentialId = path.get(2) == null ? "" : path.get(2).getDisplayName();
    holder.rowTitle.setText(component.getDisplayName());
    holder.numOfVideoAndDownloadArea.setVisibility(View.VISIBLE);
    if (component.isGraded()) {
        holder.bulkDownload.setVisibility(View.INVISIBLE);
        holder.rowSubtitlePanel.setVisibility(View.VISIBLE);
        holder.rowSubtitleIcon.setVisibility(View.VISIBLE);
        holder.rowSubtitle.setVisibility(View.VISIBLE);
        holder.rowSubtitle.setText(component.getFormat());
        holder.rowSubtitle.setTypeface(holder.rowSubtitle.getTypeface(), Typeface.BOLD);
        holder.rowSubtitle.setTextColor(ContextCompat.getColor(context, R.color.edx_brand_gray_dark));
        if (!TextUtils.isEmpty(component.getDueDate())) {
            try {
                holder.rowSubtitleDueDate.setText(getFormattedDueDate(component.getDueDate()));
                holder.rowSubtitleDueDate.setVisibility(View.VISIBLE);
            } catch (IllegalArgumentException e) {
                logger.error(e);
            }
        }
    }
    final int totalDownloadableVideos = component.getDownloadableVideosCount();
    // support video download for video type excluding the ones only viewable on web
    if (totalDownloadableVideos == 0) {
        holder.numOfVideoAndDownloadArea.setVisibility(View.GONE);
    } else {
        holder.bulkDownload.setVisibility(View.VISIBLE);
        holder.noOfVideos.setVisibility(View.VISIBLE);
        holder.noOfVideos.setText("" + totalDownloadableVideos);
        Integer downloadedCount = dbStore.getDownloadedVideosCountForSection(courseId, chapterId, sequentialId, null);
        if (downloadedCount == totalDownloadableVideos) {
            holder.noOfVideos.setVisibility(View.VISIBLE);
            setRowStateOnDownload(holder, DownloadEntry.DownloadedState.DOWNLOADED, null);
        } else if (dbStore.getDownloadingVideosCountForSection(courseId, chapterId, sequentialId, null) + downloadedCount == totalDownloadableVideos) {
            holder.noOfVideos.setVisibility(View.GONE);
            setRowStateOnDownload(holder, DownloadEntry.DownloadedState.DOWNLOADING, new View.OnClickListener() {

                @Override
                public void onClick(View downloadView) {
                    mDownloadListener.viewDownloadsStatus();
                }
            });
        } else {
            holder.noOfVideos.setVisibility(View.VISIBLE);
            setRowStateOnDownload(holder, DownloadEntry.DownloadedState.ONLINE, new View.OnClickListener() {

                @Override
                public void onClick(View downloadView) {
                    mDownloadListener.download(component.getVideos());
                }
            });
        }
    }
}
Also used : BlockPath(org.edx.mobile.model.course.BlockPath) View(android.view.View) IconImageView(com.joanzapata.iconify.widget.IconImageView) TextView(android.widget.TextView) CourseComponent(org.edx.mobile.model.course.CourseComponent)

Aggregations

BlockPath (org.edx.mobile.model.course.BlockPath)9 CourseComponent (org.edx.mobile.model.course.CourseComponent)8 ArrayList (java.util.ArrayList)3 IBlock (org.edx.mobile.model.course.IBlock)3 View (android.view.View)2 TextView (android.widget.TextView)2 IconImageView (com.joanzapata.iconify.widget.IconImageView)2 List (java.util.List)2 EnrolledCoursesResponse (org.edx.mobile.model.api.EnrolledCoursesResponse)2 CourseStructureV1Model (org.edx.mobile.model.course.CourseStructureV1Model)2 VideoBlockModel (org.edx.mobile.model.course.VideoBlockModel)2 Test (org.junit.Test)2 Intent (android.content.Intent)1 Bundle (android.os.Bundle)1 Fragment (android.support.v4.app.Fragment)1 ImageView (android.widget.ImageView)1 Filter (org.edx.mobile.model.Filter)1 BlockType (org.edx.mobile.model.course.BlockType)1 HasDownloadEntry (org.edx.mobile.model.course.HasDownloadEntry)1 VideoInfo (org.edx.mobile.model.course.VideoInfo)1