Udacity Android Practice Note: Lesson 4 Part B

Source: Internet
Author: User

Udacity Android Practice Note: Lesson 4 Part B

/titer1/archimedes of dry Goods shop chores
Source: Https://code.csdn.net/titer1
Contact: 13,073,161,968 (SMS Best)
Disclaimer: This document is licensed under the following protocols: Free reprint-Non-commercial-non-derivative-retention Attribution | Creative Commons by-nc-nd 3.0. Reprint please indicate the author and source.
Tips:https://code.csdn.net/titer1/pat_aha/blob/master/markdown/android/

Summary

    • Completely demonstrate the process of content provider joining
    • Dynamic display of the Android unit test process
      Moving Pictures Haunt Oh! O (^▽^) O. Moving diagram at the end
OVERVIW Chart

Or the old picture, (Create/insert/query)

Below we start to learn about content provider

复习四大 组件content
Chapter Overview

4.07 init code for content provider


-The test piling function accounted for more than half
-then the traditional function inside changed the Weathercontrace
-Added the Weatherprovider
-Changed the Fetchweathertask function. The source of the code is
Disassembled from the Forececastfragment code.
-In addition to changing the Weathercontrace, the other files are newly added

Keyword URI

The most weathercontract added is the function that generates the URI.

    /* Inner class that defines the table contents of the location table */    publicstaticfinalclass LocationEntry implements BaseColumns {    ...        publicstaticbuildLocationUri(long id) {            return ContentUris.withAppendedId(CONTENT_URI, id);        }    }    ===
    / * Inner class that defines the table contents of the Weather table * /     Public Static Final  class weatherentry implements basecolumns {... Public StaticUribuildweatherlocation(String locationsetting) {return NULL; } Public StaticUribuildweatherlocationwithstartdate(String locationsetting,LongStartDate) {LongNormalizeddate = Normalizedate (startdate);returnContent_uri.buildupon (). Appendpath (locationsetting). Appendqueryparameter (Column_date, Long.toString (No        rmalizeddate)). Build (); } Public StaticUribuildweatherlocationwithdate(String locationsetting,LongDate) {returnContent_uri.buildupon (). Appendpath (locationsetting). Appendpath (Long.tostring (normalizedate (date)). Bui        LD (); } Public StaticStringGetlocationsettingfromuri(URI Uri) {returnUri.getpathsegments (). Get (1); } Public Static Long Getdatefromuri(URI Uri) {returnLong.parselong (Uri.getpathsegments (). Get (2)); } Public Static Long Getstartdatefromuri(URI Uri) {String datestring = Uri.getqueryparameter (column_date);if(NULL! = datestring && datestring.length () >0)returnLong.parselong (datestring);Else                return 0; }    }}
4.08 URI location with weather

Only 2 Updates

Weathercontract
        /*            Student: This is the buildWeatherLocation function you filled in.         */        publicstaticbuildWeatherLocation(String locationSetting) {            return CONTENT_URI.buildUpon().appendPath(locationSetting).build();//上一版本号这里返回空值        }

# #2/2 Test

Just open the gaze here
"' Java
/*
Students:this is isn't a complete test for the weathercontract-just for the functions
That we expect your to write.
*/
public class Testweathercontract extends Androidtestcase {

 //intentionally includes a slash to make sure Uri is getting quoted correctlyprivate static final String test_  Weather_location = "/north Pole";p rivate static final long test_weather_date = 1419033600L; December 20th, 2014/* Students:uncomment this off to test your weather the location function. */public void Testbuildweatherlocation () {Uri Locationuri = WeatherContract.WeatherEntry.buildWeatherLocation (test_    Weather_location);  Assertnotnull ("Error:null Uri returned.    You must fill-in buildweatherlocation in "+" weathercontract. ", Locationuri); Assertequals ("Error:weather location isn't properly appended to the end of the Uri", Test_weather_location, Loca    Tionuri.getlastpathsegment ());            Assertequals ("Error:weather location Uri doesn ' t match our expected result", locationuri.tostring (), "Content://com.example.android.sunshine.app/weather/%2fnorth%20pole");}  

}
"'

4.09 write URI matcher1/2 Weatherprovider

This is the code inside the frame, update one place

    / * Students:here is where you need to create the urimatcher. This urimatcher would match each URI to the WEATHER, Weather_with_location, Weather_with_location_and_date, a  ND location integer constants defined above.     You can test the uncommenting the Testurimatcher test within Testurimatcher. */    StaticUrimatcher Buildurimatcher () {//I know what do you ' re thinking. Why create a urimatcher when can use regular        //expressions instead? Because you ' re isn't crazy, that 's why.        //All paths added to the Urimatcher has a corresponding code to return when a match is        //found. The code passed into the constructor represents the "code to return" for the root        //URI. It ' s common to use No_match as the code for this case.        FinalUrimatcher Matcher =NewUrimatcher (Urimatcher.no_match);FinalString authority = weathercontract.content_authority;//For each of the type of URI want to add, create a corresponding code.Matcher.adduri (authority, Weathercontract.path_weather, WEATHER); Matcher.adduri (authority, Weathercontract.path_weather +"/*", weather_with_location); Matcher.adduri (authority, Weathercontract.path_weather +"/*/#", weather_with_location_and_date); Matcher.adduri (authority, weathercontract.path_location, location);returnMatcher }
2/2 Test

is still a tested code frame that has been written, here only to open the gaze

 Public  class testurimatcher extends androidtestcase {.../ * Students:this function tests that your urimatcher returns the correct integer value for each of the  Uri types that our contentprovider can handle.     Uncomment this is ready to test your urimatcher. */     Public void Testurimatcher() {Urimatcher testmatcher = Weatherprovider.buildurimatcher (); Assertequals ("Error:the WEATHER URI was matched incorrectly.", Testmatcher.match (Test_weather_dir), weatherprovider.weather); Assertequals ("Error:the WEATHER with the location URI is matched incorrectly.", Testmatcher.match (Test_weather_with_location_dir), weatherprovider.weather_with_location); Assertequals ("Error:the WEATHER with location and DATE URI is matched incorrectly.", Testmatcher.match (Test_weather_with_location_and_date_dir), Weatherprovider.weather_with_location_and_dat        E); Assertequals ("Error:the location URI is matched incorrectly.", Testmatcher.match (Test_location_dir), weatherprovider.location); }

···

4.10 Register Content PROVIDER1/2

Here only to enable (open gaze) testproviderregistry

 Public  class testprovider extends androidtestcase {.../ * This test checks to make sure the content provider is registered correctly.     Students:uncomment this test to make sure you ' ve correctly registered the Weatherprovider. */     Public void Testproviderregistry() {Packagemanager pm = Mcontext.getpackagemanager ();//We Define the component name based on the package name from the context and the        //Weatherprovider class.ComponentName componentname =NewComponentName (Mcontext.getpackagename (), WeatherProvider.class.getName ());Try{//Fetch the provider info using the component name from the Packagemanager            //This throws an exception if the provider isn ' t registered.Providerinfo providerinfo = Pm.getproviderinfo (ComponentName,0);//Make sure, the registered authority matches the authority from the contract.Assertequals ("Error:weatherprovider registered with authority:"+ providerinfo.authority +"Instead of authority:"+ weathercontract.content_authority, providerinfo.authority, weathercontract.content_authority); }Catch(Packagemanager.namenotfoundexception e) {//I guess the provider isn ' t registered correctly.Asserttrue ("Error:weatherprovider not registered at"+ Mcontext.getpackagename (),false); }    }    }
Androidmanifest Update
        <provider            android:authorities="com.example.android.sunshine.app"            android:name=".data.WeatherProvider" />
Nap

Add provider Cloth
-expected to be compared from the 1-generation version code to the 2-generation code

Smart Compiler

Smart Tips
Android in debug mode, there are very interesting topics inside, such as the hint of variable values
And when searching for characters, actively filter the characters within the gaze

Test OK unit test method && Testpractise


And I did not choose the right button trigger, but the direct run (shift F10)
(4a-28) Inside the unit test method. can be reproduced completely


-Show the configuration of execution
-Show how errors are located in error situations
-Show normal situation

使用的是 sunshine version2 的4.02的代码。code: testPractise
todo 实践后思考空白的android testproject是怎样的?4.02章节里面。命名唯独有效的3个testcase,怎么显示有6个testcase ?

The Testcreatedb of TestDB

4.04 (git version) delete the testpractise,

实际执行记录:我在全部的 assertTrue,assertFalse,assertEqual位置都下了断点,可是唯独testDb中的testCreateDb 被触发。数字上。run窗体显示的“6of6”ok,但6个中的4个被触发,我怀疑这个6是不是显示过时
The testlocationtable of TestDB

The test was completely passed.

    • This function completely calls the operation function of the DB.
      • Insert
      • Query

And it's interesting to call the Testutilities.validatecurrentrecord.
(There are also 2 assert, the assertion is eventually triggered)

This is my observation.
The first invocation of a function in the Testutilities class.
The previous round. I set a breakpoint inside the static function of this class.
Have not been called.

And this class Testutilities provides sample data for database insertions.

todo实践后思考:既然单元測试都是利用AndroidTestCase那么为什么每次都是testDb被调用,而其它AndroidTestCase的继承类testUtilities 没有被直接调用.
Testweathertable and Refactoring testlocationtable

4.06 Lesson,
And the interesting thing is that the former internal call called the
Locationtable's Content

All the tests passed.

todo 内容深入分析        // Fifth Step: Validate the location Query        TestUtilities.validateCurrentRecord("testInsertReadDb weatherEntry failed to validate",                weatherCursor, weatherValues);
Nap

The first 6 subsections of GitHub are the recognition unit test

The Magic of unit test is very big. Over here

Maybe

From 4.06-4.10 (GitHub)
Test functions are:
-Test Weathercontract
-Test URI Matcher
-Test Provider
-Test Providerregistery

Constructs a content provider, each module can not be sloppy

Good night, nap.

At least the direction is bright.

Review next day

Explain why not all URIs are hit before

Look at weather provider and Testprovider's demo.
The author is a one-off function of the enable, each test. Gradually deepen

Nearly every function of Testprovider (Query/gettype/insert/updat)
Contains a filter for URI Matcher

Recently found to be able to view the contents of each test:

Video 4b-24 is a phase summary

bulkinsert 事实上利用了事务,唯独endtrancsation 才执行操作

Section

Subsection: The entire chapter is URI use
-Define URI
-Update URI (Feflect to number)
-URI Matcher (I think, here Addmatch)
-Implement (I think, Surimatcher.match (URI);)

说到。开源非常多 封装了 content provider ?

toco
怎样理解 notification oserver ..here

Integration

So far. Our content provider has not been linked with database.
Operation is not Test*.java, and is not mainly weatherprovider inside

We'll focus on fetchweathertask.
-Addlocation
-Getweatherdatafromjson Insert + query part
(Processing routines after network closing)

See where the use of Getcontentresolver, is the use of ContentProvider place,

At last

For smoother UI Theread, loader
至此,我们的video 来到4b的借书,代码
The code scope is:

Todo with code section

Android Hello
Android goodbye
Oh. This blog Android paused for updates. If you are interested in being able to text contact

Udacity Android Practice Note: Lesson 4 Part B

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.