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, reproduced please specify the author and source.
Tips:https://code.csdn.net/titer1/pat_aha/blob/master/markdown/android/
Summary
- Fully demonstrate the process of content provider additions
- The process of dynamically displaying Android unit tests
Moving Pictures Haunt Oh! O (^▽^) O, motion diagram at the end
OVERVIW Chart
Or the old picture, (Create/insert/query)
Below we start the content provider study
复习四大 组件content
Chapter Overview
4.07 init code for content provider
-The test piling function takes up half of the
-then the traditional function inside modifies the Weathercontrace
-Added the Weatherprovider
-Modified the Fetchweathertask function, the source of the code is
Disassembled from the Forececastfragment code.
-In addition to modifying Weathercontrace, other files are newly added
Keyword URI
The weathercontract adds up to 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 note 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 well-written test code framework, just open the comments here
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 enable (open comment) 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
Increase the provider cloth to gather
-Estimate to compare from 1-generation code to 2-generation code
Smart Compiler
Smart Tips
Android in debug mode, there are interesting topics inside, such as the hint of variable values
When searching for characters, actively filter the characters inside comments
Test OK Unit testing methods && Testpractise
And I did not choose the right button trigger, but the direct run (shift F10)
(4a-28) The method of unit test inside, can reproduce completely
-Show the running configuration
-Show how errors are located in error situations
-Show normal situation
使用的是 sunshine version2 的4.02的代码,code: testPractise
todo 实践后思考空白的android test工程是怎样的?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 passes completely.
- This function completely calls the operation function of the DB.
And it's interesting to call the Testutilities.validatecurrentrecord.
(There are also 2 assert, the assertion is finally triggered)
This is my observation.
The first invocation of a function in the Testutilities class,
In the previous round, I set breakpoints in 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
And 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 is to recognize unit tests
The magic of unit testing is huge.
Subsequent
From 4.06-4.10 (GitHub)
The 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
Almost every kind of testprovider function (Query/gettype/insert/updat)
Contains a filter for URI Matcher
Recent discoveries can 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
At this point, 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 pause update, if you are interested can SMS contact
Udacity Android Practice Note: Lesson 4 Part B